clipboard: api to write multiple formats to same writer
This commit is contained in:
parent
d661099322
commit
2d3e938a7f
3 changed files with 61 additions and 1 deletions
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/image_converter.h"
|
#include "atom/common/native_mate_converters/image_converter.h"
|
||||||
#include "atom/common/native_mate_converters/string16_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/arguments.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "third_party/skia/include/core/SkBitmap.h"
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
|
@ -50,9 +51,35 @@ std::string Read(const std::string& format_string,
|
||||||
return data;
|
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 ReadText(mate::Arguments* args) {
|
||||||
base::string16 data;
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +126,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
dict.SetMethod("availableFormats", &AvailableFormats);
|
dict.SetMethod("availableFormats", &AvailableFormats);
|
||||||
dict.SetMethod("has", &Has);
|
dict.SetMethod("has", &Has);
|
||||||
dict.SetMethod("read", &Read);
|
dict.SetMethod("read", &Read);
|
||||||
|
dict.SetMethod("write", &Write);
|
||||||
dict.SetMethod("readText", &ReadText);
|
dict.SetMethod("readText", &ReadText);
|
||||||
dict.SetMethod("writeText", &WriteText);
|
dict.SetMethod("writeText", &WriteText);
|
||||||
dict.SetMethod("readHtml", &ReadHtml);
|
dict.SetMethod("readHtml", &ReadHtml);
|
||||||
|
|
|
@ -88,3 +88,17 @@ console.log(clipboard.has('<p>selection</p>'));
|
||||||
Reads the `data` in clipboard.
|
Reads the `data` in clipboard.
|
||||||
|
|
||||||
**Note:** This API is experimental and could be removed in future.
|
**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: "<b>test</b>"});
|
||||||
|
```
|
||||||
|
Writes the `data` iinto clipboard.
|
||||||
|
|
|
@ -32,3 +32,21 @@ describe 'clipboard module', ->
|
||||||
'<string>Hi</string>'
|
'<string>Hi</string>'
|
||||||
clipboard.writeHtml text
|
clipboard.writeHtml text
|
||||||
assert.equal clipboard.readHtml(), markup
|
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'
|
||||||
|
'<meta charset=\'utf-8\'><b>Hi</b>'
|
||||||
|
else if process.platform is 'linux'
|
||||||
|
'<meta http-equiv="content-type" ' +
|
||||||
|
'content="text/html; charset=utf-8"><b>Hi</b>'
|
||||||
|
else
|
||||||
|
'<b>Hi</b>'
|
||||||
|
clipboard.write {text: "test", html: '<b>Hi</b>', image: p}
|
||||||
|
assert.equal clipboard.readText(), text
|
||||||
|
assert.equal clipboard.readHtml(), markup
|
||||||
|
assert.equal clipboard.readImage().toDataUrl(), i.toDataUrl()
|
||||||
|
|
Loading…
Reference in a new issue