Add clipboard.writeBuffer

This commit is contained in:
Ryohei Ikegami 2017-04-21 14:47:04 +09:00 committed by Kevin Sawicki
parent 6bcf3597c2
commit 66cc07d8e9
3 changed files with 24 additions and 0 deletions

View file

@ -53,6 +53,15 @@ v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
args->isolate(), data.data(), data.length()).ToLocalChecked(); args->isolate(), data.data(), data.length()).ToLocalChecked();
} }
void Clipboard::WriteBuffer(const std::string& format_string,
const v8::Local<v8::Value> buffer,
mate::Arguments* args) {
auto format = ui::Clipboard::GetFormatType(format_string);
ui::ScopedClipboardWriter writer(GetClipboardType(args));
writer.WriteData(node::Buffer::Data(buffer), node::Buffer::Length(buffer),
format);
}
void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) { void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardType(args)); ui::ScopedClipboardWriter writer(GetClipboardType(args));
base::string16 text, html, bookmark; base::string16 text, html, bookmark;
@ -191,6 +200,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("readFindText", &atom::api::Clipboard::ReadFindText); dict.SetMethod("readFindText", &atom::api::Clipboard::ReadFindText);
dict.SetMethod("writeFindText", &atom::api::Clipboard::WriteFindText); dict.SetMethod("writeFindText", &atom::api::Clipboard::WriteFindText);
dict.SetMethod("readBuffer", &atom::api::Clipboard::ReadBuffer); dict.SetMethod("readBuffer", &atom::api::Clipboard::ReadBuffer);
dict.SetMethod("writeBuffer", &atom::api::Clipboard::WriteBuffer);
dict.SetMethod("clear", &atom::api::Clipboard::Clear); dict.SetMethod("clear", &atom::api::Clipboard::Clear);
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings // TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings

View file

@ -49,6 +49,9 @@ class Clipboard {
static v8::Local<v8::Value> ReadBuffer(const std::string& format_string, static v8::Local<v8::Value> ReadBuffer(const std::string& format_string,
mate::Arguments* args); mate::Arguments* args);
static void WriteBuffer(const std::string& format_string,
const v8::Local<v8::Value> buffer,
mate::Arguments* args);
private: private:
DISALLOW_COPY_AND_ASSIGN(Clipboard); DISALLOW_COPY_AND_ASSIGN(Clipboard);

View file

@ -94,6 +94,17 @@ describe('clipboard module', function () {
}) })
}) })
describe('clipboard.writeBuffer(format, buffer)', () => {
it('writes a Buffer for the specified format', () => {
if (process.platform !== 'darwin') return
const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public.utf8-plain-text', buffer)
console.log(clipboard.readText())
assert.equal(clipboard.readText(), 'writeBuffer')
})
})
describe('clipboard.readBuffer(format)', function () { describe('clipboard.readBuffer(format)', function () {
it('returns a Buffer of the content for the specified format', function () { it('returns a Buffer of the content for the specified format', function () {
if (process.platform !== 'darwin') return if (process.platform !== 'darwin') return