Merge pull request #6233 from electron/clipboard-bookmark-support

Add support for reading/writing bookmarks from the clipboard
This commit is contained in:
Cheng Zhao 2016-06-25 05:23:32 +00:00 committed by GitHub
commit 04b30afbed
3 changed files with 65 additions and 2 deletions

View file

@ -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<v8::Value> 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<v8::Object> exports, v8::Local<v8::Value> 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);

View file

@ -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

View file

@ -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' ? "<meta charset='utf-8'><b>Hi</b>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><b>Hi</b>' : '<b>Hi</b>'
var bookmark = {title: 'a title', url: 'test'}
clipboard.write({
text: 'test',
html: '<b>Hi</b>',
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)
}
})
})
})