diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index 71cf7616d8..1a673260bd 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -4,18 +4,12 @@ Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer-process) -The following example shows how to write a string to the clipboard: - -```javascript -const { clipboard } = require('electron') -clipboard.writeText('Example String') -``` - On Linux, there is also a `selection` clipboard. To manipulate it you need to pass `selection` to each method: ```javascript const { clipboard } = require('electron') + clipboard.writeText('Example String', 'selection') console.log(clipboard.readText('selection')) ``` @@ -28,56 +22,106 @@ The `clipboard` module has the following methods: ### `clipboard.readText([type])` -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `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 plain text. +```js +const { clipboard } = require('electron') + +clipboard.writeText('hello i am a bit of text!') + +const text = clipboard.readText() +console.log(text) +// hello i am a bit of text!' +``` + ### `clipboard.writeText(text[, type])` * `text` String -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux. Writes the `text` into the clipboard as plain text. +```js +const { clipboard } = require('electron') + +const text = 'hello i am a bit of text!' +clipboard.writeText(text) +``` + ### `clipboard.readHTML([type])` -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `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 markup. +```js +const { clipboard } = require('electron') + +clipboard.writeHTML('Hi') +const html = clipboard.readHTML() + +console.log(html) +// Hi +``` + ### `clipboard.writeHTML(markup[, type])` * `markup` String -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux. Writes `markup` to the clipboard. +```js +const { clipboard } = require('electron') + +clipboard.writeHTML('Hiselection
')) + +const hasFormat = clipboard.has('selection
') +console.log(hasFormat) +// 'true' or 'false ``` ### `clipboard.read(format)` _Experimental_ @@ -157,14 +214,33 @@ Returns `String` - Reads `format` type from the clipboard. Returns `Buffer` - Reads `format` type from the clipboard. +```js +const { clipboard } = require('electron') + +const buffer = Buffer.from('this is binary', 'utf8') +clipboard.writeBuffer('public.utf8-plain-text', buffer) + +const ret = clipboard.readBuffer('public.utf8-plain-text') + +console.log(buffer.equals(out)) +// true +``` + ### `clipboard.writeBuffer(format, buffer[, type])` _Experimental_ * `format` String * `buffer` Buffer -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux. Writes the `buffer` into the clipboard as `format`. +```js +const { clipboard } = require('electron') + +const buffer = Buffer.from('writeBuffer', 'utf8') +clipboard.writeBuffer('public.utf8-plain-text', buffer) +``` + ### `clipboard.write(data[, type])` * `data` Object @@ -173,10 +249,29 @@ Writes the `buffer` into the clipboard as `format`. * `image` [NativeImage](native-image.md) (optional) * `rtf` String (optional) * `bookmark` String (optional) - The title of the URL at `text`. -* `type` String (optional) - Can be `selection` or `clipboard`. `selection` is only available on Linux. +* `type` String (optional) - Can be `selection` or `clipboard`; default is 'clipboard'. `selection` is only available on Linux. -```javascript -const { clipboard } = require('electron') -clipboard.write({ text: 'test', html: 'test' }) -``` Writes `data` to the clipboard. + +```js +const { clipboard } = require('electron') + +clipboard.write({ + text: 'test', + html: 'Hi', + rtf: '{\\rtf1\\utf8 text}', + bookmark: 'a title' +}) + +console.log(clipboard.readText()) +// 'test' + +console.log(clipboard.readHTML()) +// Hi + +console.log(clipboard.readRTF()) +// '{\\rtf1\\utf8 text}' + +console.log(clipboard.readBookmark()) +// { title: 'a title', url: 'test' } +```