diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index cb413800be77..01e670be9261 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -54,12 +54,16 @@ std::string Read(const std::string& format_string, void Write(const mate::Dictionary& data, mate::Arguments* args) { ui::ScopedClipboardWriter writer(GetClipboardType(args)); - base::string16 text, html; + base::string16 text, html, bookmark; gfx::Image image; - if (data.Get("text", &text)) + if (data.Get("text", &text)) { writer.WriteText(text); + if (data.Get("bookmark", &bookmark)) + writer.WriteBookmark(bookmark, base::UTF16ToUTF8(text)); + } + if (data.Get("rtf", &text)) { std::string rtf = base::UTF16ToUTF8(text); writer.WriteRTF(rtf); @@ -122,6 +126,23 @@ void WriteHtml(const base::string16& html, mate::Arguments* args) { writer.WriteHTML(html, std::string()); } +v8::Local ReadBookmark(mate::Arguments* args) { + base::string16 title; + std::string url; + mate::Dictionary dict = mate::Dictionary::CreateEmpty(args->isolate()); + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + clipboard->ReadBookmark(&title, &url); + dict.Set("title", title); + dict.Set("url", url); + return dict.GetHandle(); +} + +void WriteBookmark(const base::string16& title, const std::string& url, + mate::Arguments* args) { + ui::ScopedClipboardWriter writer(GetClipboardType(args)); + writer.WriteBookmark(title, url); +} + gfx::Image ReadImage(mate::Arguments* args) { ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); SkBitmap bitmap = clipboard->ReadImage(GetClipboardType(args)); @@ -150,6 +171,8 @@ void Initialize(v8::Local exports, v8::Local unused, dict.SetMethod("writeRTF", &WriteRtf); dict.SetMethod("readHTML", &ReadHtml); dict.SetMethod("writeHTML", &WriteHtml); + dict.SetMethod("readBookmark", &ReadBookmark); + dict.SetMethod("writeBookmark", &WriteBookmark); dict.SetMethod("readImage", &ReadImage); dict.SetMethod("writeImage", &WriteImage); dict.SetMethod("clear", &Clear); diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index c2a16a79b9fb..85712bc651d9 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -75,6 +75,20 @@ Returns the content in the clipboard as RTF. Writes the `text` into the clipboard in RTF. +### `clipboard.readBookmark()` _macOS_ _Windows_ + +Returns an Object containing `title` and `url` keys representing the bookmark in +the clipboard. The `title` and `url` values will be empty strings when the +bookmark is unavailable. + +### `clipboard.writeBookmark(title, url[, type])` _macOS_ _Windows_ + +* `title` String +* `url` String +* `type` String (optional) + +Writes the `title` and `url` into the clipboard as a bookmark. + ### `clipboard.clear([type])` * `type` String (optional) @@ -111,6 +125,8 @@ Reads `data` from the clipboard. * `text` String * `html` String * `image` [NativeImage](native-image.md) + * `rtf` String + * `bookmark` String - The title of the url at `text`. * `type` String (optional) ```javascript diff --git a/spec/api-clipboard-spec.js b/spec/api-clipboard-spec.js index 344e01452fba..25b65b4784d0 100644 --- a/spec/api-clipboard-spec.js +++ b/spec/api-clipboard-spec.js @@ -41,6 +41,24 @@ describe('clipboard module', function () { }) }) + describe('clipboard.readBookmark', function () { + it('returns title and url', function () { + if (process.platform === 'linux') return + + clipboard.writeBookmark('a title', 'http://electron.atom.io') + assert.deepEqual(clipboard.readBookmark(), { + title: 'a title', + url: 'http://electron.atom.io' + }) + + clipboard.writeText('no bookmark') + assert.deepEqual(clipboard.readBookmark(), { + title: '', + url: '' + }) + }) + }) + describe('clipboard.write()', function () { it('returns data correctly', function () { var text = 'test' @@ -48,16 +66,22 @@ describe('clipboard module', function () { var p = path.join(fixtures, 'assets', 'logo.png') var i = nativeImage.createFromPath(p) var markup = process.platform === 'darwin' ? "Hi" : process.platform === 'linux' ? 'Hi' : 'Hi' + var bookmark = {title: 'a title', url: 'test'} clipboard.write({ text: 'test', html: 'Hi', rtf: '{\\rtf1\\utf8 text}', + bookmark: 'a title', image: p }) assert.equal(clipboard.readText(), text) assert.equal(clipboard.readHTML(), markup) assert.equal(clipboard.readRTF(), rtf) assert.equal(clipboard.readImage().toDataURL(), i.toDataURL()) + + if (process.platform !== 'linux') { + assert.deepEqual(clipboard.readBookmark(), bookmark) + } }) }) })