diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index 645e6a68b2..349b35ce58 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -69,6 +69,11 @@ gfx::Image ReadImage(ui::ClipboardType type) { return gfx::Image::CreateFrom1xBitmap(bitmap); } +void WriteImage(const gfx::Image& image, ui::ClipboardType type) { + ui::ScopedClipboardWriter writer(type); + writer.WriteImage(image.AsBitmap()); +} + void Clear(ui::ClipboardType type) { ui::Clipboard::GetForCurrentThread()->Clear(type); } @@ -81,6 +86,7 @@ void Initialize(v8::Handle exports, v8::Handle unused, dict.SetMethod("_readText", &ReadText); dict.SetMethod("_writeText", &WriteText); dict.SetMethod("_readImage", &ReadImage); + dict.SetMethod("_writeImage", &WriteImage); dict.SetMethod("_clear", &Clear); } diff --git a/atom/common/api/lib/clipboard.coffee b/atom/common/api/lib/clipboard.coffee index 17b98e4aa1..582feba4b1 100644 --- a/atom/common/api/lib/clipboard.coffee +++ b/atom/common/api/lib/clipboard.coffee @@ -1,13 +1,9 @@ -if process.platform is 'linux' and process.type is 'renderer' - # On Linux we could not access clipboard in renderer process. - module.exports = require('remote').require 'clipboard' -else - binding = process.atomBinding 'clipboard' - - module.exports = - has: (format, type='standard') -> binding._has format, type - read: (format, type='standard') -> binding._read format, type - readText: (type='standard') -> binding._readText type - writeText: (text, type='standard') -> binding._writeText text, type - readImage: (type='standard') -> binding._readImage type - clear: (type='standard') -> binding._clear type +binding = process.atomBinding 'clipboard' +module.exports = + has: (format, type='standard') -> binding._has format, type + read: (format, type='standard') -> binding._read format, type + readText: (type='standard') -> binding._readText type + writeText: (text, type='standard') -> binding._writeText text, type + readImage: (type='standard') -> binding._readImage type + writeImage: (image, type='standard') -> binding._writeImage image, type + clear: (type='standard') -> binding._clear type diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index 33fb1207f0..c0f830491c 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -30,6 +30,19 @@ Returns the content in clipboard as plain text. Writes the `text` into clipboard as plain text. +## clipboard.readImage([type]) + +* `type` String + +Returns the content in clipboard as [NativeImage](native-image.md). + +## clipboard.writeImage(image[, type]) + +* `image` [NativeImage](native-image.md) +* `type` String + +Writes the `image` into clipboard. + ## clipboard.clear([type]) * `type` String diff --git a/spec/api-clipboard-spec.coffee b/spec/api-clipboard-spec.coffee index eecf61ca45..feadd15b0a 100644 --- a/spec/api-clipboard-spec.coffee +++ b/spec/api-clipboard-spec.coffee @@ -1,7 +1,18 @@ assert = require 'assert' clipboard = require 'clipboard' +nativeImage = require 'native-image' +path = require 'path' describe 'clipboard module', -> + fixtures = path.resolve __dirname, 'fixtures' + + describe 'clipboard.readImage()', -> + it 'returns NativeImage intance', -> + p = path.join fixtures, 'assets', 'logo.png' + i = nativeImage.createFromPath p + clipboard.writeImage p + assert.equal clipboard.readImage().toDataUrl(), i.toDataUrl() + describe 'clipboard.readText()', -> it 'returns unicode string correctly', -> text = '千江有水千江月,万里无云万里天'