Add buffer support in clipboard.write()

This commit is contained in:
Ryohei Ikegami 2017-04-21 15:15:29 +09:00 committed by Kevin Sawicki
parent 66cc07d8e9
commit 03fdc7aa28
2 changed files with 17 additions and 1 deletions

View file

@ -66,6 +66,7 @@ void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardType(args));
base::string16 text, html, bookmark;
gfx::Image image;
std::map<std::string, v8::Local<v8::Value>> customBuffers;
if (data.Get("text", &text)) {
writer.WriteText(text);
@ -84,6 +85,14 @@ void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
if (data.Get("image", &image))
writer.WriteImage(image.AsBitmap());
if (data.Get("buffer", &customBuffers)) {
for (auto i = customBuffers.begin(); i != customBuffers.end(); ++i) {
writer.WriteData(node::Buffer::Data(i->second),
node::Buffer::Length(i->second),
ui::Clipboard::GetFormatType(i->first));
}
}
}
base::string16 Clipboard::ReadText(mate::Arguments* args) {

View file

@ -1,5 +1,6 @@
const assert = require('assert')
const path = require('path')
const fs = require('fs')
const {Buffer} = require('buffer')
const {clipboard, nativeImage} = require('electron')
@ -67,17 +68,23 @@ describe('clipboard module', function () {
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'}
const pdfType = process.platform === 'darwin' ? 'com.adobe.pdf' : 'application/pdf'
const pdf = fs.readFileSync(path.join(fixtures, 'assets', 'cat.pdf'))
clipboard.write({
text: 'test',
html: '<b>Hi</b>',
rtf: '{\\rtf1\\utf8 text}',
bookmark: 'a title',
image: p
image: p,
buffer: {
[pdfType]: pdf
}
})
assert.equal(clipboard.readText(), text)
assert.equal(clipboard.readHTML(), markup)
assert.equal(clipboard.readRTF(), rtf)
assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
assert.deepEqual(clipboard.readBuffer(pdfType), pdf)
if (process.platform !== 'linux') {
assert.deepEqual(clipboard.readBookmark(), bookmark)