diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index 5ae318510e0..430d8629379 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -53,6 +53,19 @@ v8::Local Clipboard::ReadBuffer(const std::string& format_string, args->isolate(), data.data(), data.length()).ToLocalChecked(); } +void Clipboard::WriteBuffer(const std::string& format, + const v8::Local 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 exports, v8::Local 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 diff --git a/atom/common/api/atom_api_clipboard.h b/atom/common/api/atom_api_clipboard.h index d7cbe7a42a6..92bc7b04a45 100644 --- a/atom/common/api/atom_api_clipboard.h +++ b/atom/common/api/atom_api_clipboard.h @@ -49,6 +49,9 @@ class Clipboard { static v8::Local ReadBuffer(const std::string& format_string, mate::Arguments* args); + static void WriteBuffer(const std::string& format_string, + const v8::Local buffer, + mate::Arguments* args); private: DISALLOW_COPY_AND_ASSIGN(Clipboard); diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index 559fc01a90d..ef62acb8bb0 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -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 diff --git a/spec/api-clipboard-spec.js b/spec/api-clipboard-spec.js index a4a0e964304..84a4de742ea 100644 --- a/spec/api-clipboard-spec.js +++ b/spec/api-clipboard-spec.js @@ -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