mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
feat: add clipboard.{read|write}Svg()
This commit is contained in:
@@ -92,6 +92,19 @@ Returns [`NativeImage`](native-image.md) - The image content in the clipboard.
|
||||
|
||||
Writes `image` to the clipboard.
|
||||
|
||||
### `clipboard.readSvg([type])`
|
||||
|
||||
* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux.
|
||||
|
||||
Returns `String` - the content in the clipboard as SVG vector image data.
|
||||
|
||||
### `clipboard.writeSvg(data, [type])`
|
||||
|
||||
* `data` String
|
||||
* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux.
|
||||
|
||||
Write SVG vector `data` to the clipboard.
|
||||
|
||||
### `clipboard.readRTF([type])`
|
||||
|
||||
* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux.
|
||||
@@ -253,6 +266,7 @@ clipboard.writeBuffer('public/utf8-plain-text', buffer)
|
||||
* `image` [NativeImage](native-image.md) (optional)
|
||||
* `rtf` String (optional)
|
||||
* `bookmark` String (optional) - The title of the URL at `text`.
|
||||
* `svg` String (optional)
|
||||
* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux.
|
||||
|
||||
Writes `data` to the clipboard.
|
||||
|
||||
@@ -96,7 +96,7 @@ void Clipboard::WriteBuffer(const std::string& format,
|
||||
void Clipboard::Write(const gin_helper::Dictionary& data,
|
||||
gin_helper::Arguments* args) {
|
||||
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
|
||||
std::u16string text, html, bookmark;
|
||||
std::u16string text, html, bookmark, svg;
|
||||
gfx::Image image;
|
||||
|
||||
if (data.Get("text", &text)) {
|
||||
@@ -106,16 +106,17 @@ void Clipboard::Write(const gin_helper::Dictionary& data,
|
||||
writer.WriteBookmark(bookmark, base::UTF16ToUTF8(text));
|
||||
}
|
||||
|
||||
if (data.Get("rtf", &text)) {
|
||||
std::string rtf = base::UTF16ToUTF8(text);
|
||||
writer.WriteRTF(rtf);
|
||||
}
|
||||
if (data.Get("rtf", &text))
|
||||
writer.WriteRTF(base::UTF16ToUTF8(text));
|
||||
|
||||
if (data.Get("html", &html))
|
||||
writer.WriteHTML(html, std::string());
|
||||
|
||||
if (data.Get("image", &image))
|
||||
writer.WriteImage(image.AsBitmap());
|
||||
|
||||
if (data.Get("svg", &svg))
|
||||
writer.WriteSvg(svg);
|
||||
}
|
||||
|
||||
std::u16string Clipboard::ReadText(gin_helper::Arguments* args) {
|
||||
@@ -222,6 +223,19 @@ void Clipboard::WriteImage(const gfx::Image& image,
|
||||
}
|
||||
}
|
||||
|
||||
std::u16string Clipboard::ReadSvg(gin_helper::Arguments* args) {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
std::u16string data;
|
||||
clipboard->ReadSvg(GetClipboardBuffer(args), nullptr, &data);
|
||||
return data;
|
||||
}
|
||||
|
||||
void Clipboard::WriteSvg(const std::u16string& text,
|
||||
gin_helper::Arguments* args) {
|
||||
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
|
||||
writer.WriteSvg(text);
|
||||
}
|
||||
|
||||
#if !defined(OS_MAC)
|
||||
void Clipboard::WriteFindText(const std::u16string& text) {}
|
||||
std::u16string Clipboard::ReadFindText() {
|
||||
@@ -259,6 +273,8 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||
dict.SetMethod("writeBookmark", &electron::api::Clipboard::WriteBookmark);
|
||||
dict.SetMethod("readImage", &electron::api::Clipboard::ReadImage);
|
||||
dict.SetMethod("writeImage", &electron::api::Clipboard::WriteImage);
|
||||
dict.SetMethod("readSvg", &electron::api::Clipboard::ReadSvg);
|
||||
dict.SetMethod("writeSvg", &electron::api::Clipboard::WriteSvg);
|
||||
dict.SetMethod("readFindText", &electron::api::Clipboard::ReadFindText);
|
||||
dict.SetMethod("writeFindText", &electron::api::Clipboard::WriteFindText);
|
||||
dict.SetMethod("readBuffer", &electron::api::Clipboard::ReadBuffer);
|
||||
|
||||
@@ -53,6 +53,9 @@ class Clipboard {
|
||||
static gfx::Image ReadImage(gin_helper::Arguments* args);
|
||||
static void WriteImage(const gfx::Image& image, gin_helper::Arguments* args);
|
||||
|
||||
static std::u16string ReadSvg(gin_helper::Arguments* args);
|
||||
static void WriteSvg(const std::u16string& text, gin_helper::Arguments* args);
|
||||
|
||||
static std::u16string ReadFindText();
|
||||
static void WriteFindText(const std::u16string& text);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const { expect } = require('chai');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const { Buffer } = require('buffer');
|
||||
const { ifdescribe } = require('./spec-helpers');
|
||||
|
||||
@@ -19,6 +20,16 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
|
||||
});
|
||||
});
|
||||
|
||||
describe('clipboard.readSvg()', () => {
|
||||
it('returns vector image data as string', () => {
|
||||
const p = path.join(fixtures, 'assets', 'rabbit.svg');
|
||||
const data = fs.readFileSync(p).toString();
|
||||
clipboard.writeText(data);
|
||||
const svgData = clipboard.readText();
|
||||
expect(svgData).to.equal(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clipboard.readText()', () => {
|
||||
it('returns unicode string correctly', () => {
|
||||
const text = '千江有水千江月,万里无云万里天';
|
||||
@@ -72,8 +83,10 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
|
||||
it('returns data correctly', () => {
|
||||
const text = 'test';
|
||||
const rtf = '{\\rtf1\\utf8 text}';
|
||||
const p = path.join(fixtures, 'assets', 'logo.png');
|
||||
const i = nativeImage.createFromPath(p);
|
||||
const imgPath = path.join(fixtures, 'assets', 'logo.png');
|
||||
const i = nativeImage.createFromPath(imgPath);
|
||||
const svgPath = path.join(fixtures, 'assets', 'rabbit.svg');
|
||||
const svg = fs.readFileSync(svgPath).toString();
|
||||
const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : '<b>Hi</b>';
|
||||
const bookmark = { title: 'a title', url: 'test' };
|
||||
clipboard.write({
|
||||
@@ -81,7 +94,8 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
|
||||
html: '<b>Hi</b>',
|
||||
rtf: '{\\rtf1\\utf8 text}',
|
||||
bookmark: 'a title',
|
||||
image: p
|
||||
image: imgPath,
|
||||
svg
|
||||
});
|
||||
|
||||
expect(clipboard.readText()).to.equal(text);
|
||||
@@ -89,6 +103,7 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
|
||||
expect(clipboard.readRTF()).to.equal(rtf);
|
||||
const readImage = clipboard.readImage();
|
||||
expect(readImage.toDataURL()).to.equal(i.toDataURL());
|
||||
expect(clipboard.readSvg()).to.equal(svg);
|
||||
|
||||
if (process.platform !== 'linux') {
|
||||
if (process.platform !== 'win32') {
|
||||
|
||||
3676
spec/fixtures/assets/rabbit.svg
vendored
Normal file
3676
spec/fixtures/assets/rabbit.svg
vendored
Normal file
File diff suppressed because it is too large
Load Diff
|
After Width: | Height: | Size: 552 KiB |
Reference in New Issue
Block a user