Provide API to access selection clipboard, fixes #377.
This commit is contained in:
parent
7c14c2758b
commit
950704c7e8
3 changed files with 73 additions and 30 deletions
|
@ -11,15 +11,37 @@
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct Converter<ui::Clipboard::Buffer> {
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Handle<v8::Value> val,
|
||||||
|
ui::Clipboard::Buffer* out) {
|
||||||
|
std::string type;
|
||||||
|
if (!Converter<std::string>::FromV8(isolate, val, &type))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (type == "selection")
|
||||||
|
*out = ui::Clipboard::BUFFER_STANDARD;
|
||||||
|
else
|
||||||
|
*out = ui::Clipboard::BUFFER_SELECTION;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
bool Has(const std::string& format_string) {
|
bool Has(const std::string& format_string, ui::Clipboard::Buffer buffer) {
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||||
return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD);
|
return clipboard->IsFormatAvailable(format, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Read(const std::string& format_string) {
|
std::string Read(const std::string& format_string,
|
||||||
|
ui::Clipboard::Buffer buffer) {
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||||
|
|
||||||
|
@ -28,34 +50,32 @@ std::string Read(const std::string& format_string) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
string16 ReadText() {
|
string16 ReadText(ui::Clipboard::Buffer buffer) {
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
||||||
|
|
||||||
string16 data;
|
string16 data;
|
||||||
clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &data);
|
ui::Clipboard::GetForCurrentThread()->ReadText(buffer, &data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteText(const std::string text) {
|
void WriteText(const std::string text, ui::Clipboard::Buffer buffer) {
|
||||||
ui::Clipboard::ObjectMap object_map;
|
ui::Clipboard::ObjectMap object_map;
|
||||||
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
||||||
std::vector<char>(text.begin(), text.end()));
|
std::vector<char>(text.begin(), text.end()));
|
||||||
|
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
clipboard->WriteObjects(buffer, object_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear(ui::Clipboard::Buffer buffer) {
|
||||||
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
ui::Clipboard::GetForCurrentThread()->Clear(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize(v8::Handle<v8::Object> exports) {
|
void Initialize(v8::Handle<v8::Object> exports) {
|
||||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||||
dict.SetMethod("has", &Has);
|
dict.SetMethod("_has", &Has);
|
||||||
dict.SetMethod("read", &Read);
|
dict.SetMethod("_read", &Read);
|
||||||
dict.SetMethod("readText", &ReadText);
|
dict.SetMethod("_readText", &ReadText);
|
||||||
dict.SetMethod("writeText", &WriteText);
|
dict.SetMethod("_writeText", &WriteText);
|
||||||
dict.SetMethod("clear", &Clear);
|
dict.SetMethod("_clear", &Clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
module.exports =
|
if process.platform is 'linux' and process.type is 'renderer'
|
||||||
if process.platform is 'linux' and process.type is 'renderer'
|
# On Linux we could not access clipboard in renderer process.
|
||||||
# On Linux we could not access clipboard in renderer process.
|
module.exports = require('remote').process.atomBinding 'clipboard'
|
||||||
require('remote').process.atomBinding 'clipboard'
|
else
|
||||||
else
|
binding = process.atomBinding 'clipboard'
|
||||||
process.atomBinding 'clipboard'
|
|
||||||
|
module.exports =
|
||||||
|
has: (format, type='standard') -> binding._has format, type
|
||||||
|
read: (format, type='standard') -> binding._read format, type
|
||||||
|
readText: (type='standard') -> binding._readText type
|
||||||
|
writeText: (text, type='standard') -> binding._writeText text, type
|
||||||
|
clear: (type='standard') -> binding._clear type
|
||||||
|
|
|
@ -1,38 +1,55 @@
|
||||||
# clipboard
|
# clipboard
|
||||||
|
|
||||||
An example of writing a string to clipboard:
|
The `clipboard` provides methods to do copy/paste operations. An example of
|
||||||
|
writing a string to clipboard:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var clipboard = require('clipboard');
|
var clipboard = require('clipboard');
|
||||||
clipboard.writeText('Example String');
|
clipboard.writeText('Example String');
|
||||||
```
|
```
|
||||||
|
|
||||||
## clipboard.readText()
|
On X Window systems, there is also a selection clipboard, to manipulate in it
|
||||||
|
you need to pass `selection` to each method:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var clipboard = require('clipboard');
|
||||||
|
clipboard.writeText('Example String', 'selection');
|
||||||
|
console.log(clipboard.readText('selection'));
|
||||||
|
```
|
||||||
|
|
||||||
|
## clipboard.readText([type])
|
||||||
|
|
||||||
|
* `type` String
|
||||||
|
|
||||||
Returns the content in clipboard as plain text.
|
Returns the content in clipboard as plain text.
|
||||||
|
|
||||||
## clipboard.writeText(text)
|
## clipboard.writeText(text[, type])
|
||||||
|
|
||||||
* `text` String
|
* `text` String
|
||||||
|
* `type` String
|
||||||
|
|
||||||
Writes the `text` into clipboard as plain text.
|
Writes the `text` into clipboard as plain text.
|
||||||
|
|
||||||
## clipboard.clear()
|
## clipboard.clear([type])
|
||||||
|
|
||||||
|
* `type` String
|
||||||
|
|
||||||
Clears everything in clipboard.
|
Clears everything in clipboard.
|
||||||
|
|
||||||
## clipboard.has(type)
|
## clipboard.has(format[, type])
|
||||||
|
|
||||||
|
* `format` String
|
||||||
* `type` String
|
* `type` String
|
||||||
|
|
||||||
Returns whether clipboard has data in specified `type`.
|
Returns whether clipboard has data in specified `format`.
|
||||||
|
|
||||||
**Note:** This API is experimental and could be removed in future.
|
**Note:** This API is experimental and could be removed in future.
|
||||||
|
|
||||||
## clipboard.read(type)
|
## clipboard.read(format[, type])
|
||||||
|
|
||||||
|
* `format` String
|
||||||
* `type` String
|
* `type` String
|
||||||
|
|
||||||
Reads the data in clipboard of the `type`.
|
Reads the data in clipboard of the `format`.
|
||||||
|
|
||||||
**Note:** This API is experimental and could be removed in future.
|
**Note:** This API is experimental and could be removed in future.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue