Merge pull request #9243 from seanchas116/custom-clipboard-data

Support writing custom formats into clipboard
This commit is contained in:
Kevin Sawicki 2017-05-22 14:01:05 -07:00 committed by GitHub
commit 337f61af98
4 changed files with 41 additions and 0 deletions

View file

@ -53,6 +53,19 @@ v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
args->isolate(), data.data(), data.length()).ToLocalChecked();
}
void Clipboard::WriteBuffer(const std::string& format,
const v8::Local<v8::Value> buffer,
mate::Arguments* args) {
if (!node::Buffer::HasInstance(buffer)) {
args->ThrowError("buffer must be a node Buffer");
return;
}
ui::ScopedClipboardWriter writer(GetClipboardType(args));
writer.WriteData(node::Buffer::Data(buffer), node::Buffer::Length(buffer),
ui::Clipboard::GetFormatType(format));
}
void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardType(args));
base::string16 text, html, bookmark;
@ -191,6 +204,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("readFindText", &atom::api::Clipboard::ReadFindText);
dict.SetMethod("writeFindText", &atom::api::Clipboard::WriteFindText);
dict.SetMethod("readBuffer", &atom::api::Clipboard::ReadBuffer);
dict.SetMethod("writeBuffer", &atom::api::Clipboard::WriteBuffer);
dict.SetMethod("clear", &atom::api::Clipboard::Clear);
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings

View file

@ -49,6 +49,9 @@ class Clipboard {
static v8::Local<v8::Value> ReadBuffer(const std::string& format_string,
mate::Arguments* args);
static void WriteBuffer(const std::string& format_string,
const v8::Local<v8::Value> buffer,
mate::Arguments* args);
private:
DISALLOW_COPY_AND_ASSIGN(Clipboard);

View file

@ -157,6 +157,14 @@ Returns `String` - Reads `format` type from the clipboard.
Returns `Buffer` - Reads `format` type from the clipboard.
### `clipboard.writeBuffer(format, buffer[, type])` _Experimental_
* `format` String
* `buffer` Buffer
* `type` String (optional)
Writes the `buffer` into the clipboard as `format`.
### `clipboard.write(data[, type])`
* `data` Object

View file

@ -94,6 +94,22 @@ describe('clipboard module', function () {
})
})
describe('clipboard.writeBuffer(format, buffer)', () => {
it('writes a Buffer for the specified format', () => {
if (process.platform !== 'darwin') return
const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public.utf8-plain-text', buffer)
assert.equal(clipboard.readText(), 'writeBuffer')
})
it('throws an error when a non-Buffer is specified', () => {
assert.throws(() => {
clipboard.writeBuffer('public.utf8-plain-text', 'hello')
}, /buffer must be a node Buffer/)
})
})
describe('clipboard.readBuffer(format)', function () {
it('returns a Buffer of the content for the specified format', function () {
if (process.platform !== 'darwin') return