diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index 273d30d96770..0837a3dc93f0 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -7,6 +7,7 @@ #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" +#include "base/strings/utf_string_conversions.h" #include "native_mate/arguments.h" #include "native_mate/dictionary.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -50,9 +51,35 @@ std::string Read(const std::string& format_string, return data; } +void Write(const mate::Dictionary& data, + mate::Arguments* args) { + ui::ScopedClipboardWriter writer(GetClipboardType(args)); + base::string16 text, html; + gfx::Image image; + + if (data.Get("text", &text)) + writer.WriteText(text); + + if (data.Get("html", &html)) + writer.WriteHTML(html, std::string()); + + if (data.Get("image", &image)) + writer.WriteImage(image.AsBitmap()); +} + base::string16 ReadText(mate::Arguments* args) { base::string16 data; - ui::Clipboard::GetForCurrentThread()->ReadText(GetClipboardType(args), &data); + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + auto type = GetClipboardType(args); + if (clipboard->IsFormatAvailable( + ui::Clipboard::GetPlainTextWFormatType(), type)) { + clipboard->ReadText(type, &data); + } else if (clipboard->IsFormatAvailable( + ui::Clipboard::GetPlainTextFormatType(), type)) { + std::string result; + clipboard->ReadAsciiText(type, &result); + data = base::ASCIIToUTF16(result); + } return data; } @@ -99,6 +126,7 @@ void Initialize(v8::Local exports, v8::Local unused, dict.SetMethod("availableFormats", &AvailableFormats); dict.SetMethod("has", &Has); dict.SetMethod("read", &Read); + dict.SetMethod("write", &Write); dict.SetMethod("readText", &ReadText); dict.SetMethod("writeText", &WriteText); dict.SetMethod("readHtml", &ReadHtml); diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index b9ca8b3a9b90..572e91e96637 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -88,3 +88,17 @@ console.log(clipboard.has('

selection

')); Reads the `data` in clipboard. **Note:** This API is experimental and could be removed in future. + +## clipboard.write(data[, type]) + +* `data` Object + * `text` String + * `html` String + * `image` [NativeImage](native-image.md) +* `type` String + +```javascript +var clipboard = require('clipboard'); +clipboard.write({text: 'test', html: "test"}); +``` +Writes the `data` iinto clipboard. diff --git a/spec/api-clipboard-spec.coffee b/spec/api-clipboard-spec.coffee index 135ed6cf1570..ddb38825e35f 100644 --- a/spec/api-clipboard-spec.coffee +++ b/spec/api-clipboard-spec.coffee @@ -32,3 +32,21 @@ describe 'clipboard module', -> 'Hi' clipboard.writeHtml text assert.equal clipboard.readHtml(), markup + + describe 'clipboard.write()', -> + it 'returns data correctly', -> + text = 'test' + p = path.join fixtures, 'assets', 'logo.png' + i = nativeImage.createFromPath p + markup = + if process.platform is 'darwin' + 'Hi' + else if process.platform is 'linux' + 'Hi' + else + 'Hi' + clipboard.write {text: "test", html: 'Hi', image: p} + assert.equal clipboard.readText(), text + assert.equal clipboard.readHtml(), markup + assert.equal clipboard.readImage().toDataUrl(), i.toDataUrl()