Skip to content

Commit a924f35

Browse files
committed
rfc: clipboard rearchitecture
1 parent d89000c commit a924f35

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
# RFC
2+
3+
- Start Date: 2025-08-07
4+
- RFC PR: [electron/rfcs#19](https://github.com/electron/rfcs/pull/19)
5+
- Electron Issues:
6+
- [electron/electron#23156](https://github.com/electron/electron/issues/23156)
7+
- [electron/electron#31130](https://github.com/electron/electron/issues/31130)
8+
- [electron/electron#31115](https://github.com/electron/electron/issues/31115)
9+
- [electron/electron#30699](https://github.com/electron/electron/issues/30699)
10+
- [electron/electron#26377](https://github.com/electron/electron/issues/26377)
11+
- [electron/electron#11838](https://github.com/electron/electron/issues/11838)
12+
- [electron/electron#9035](https://github.com/electron/electron/issues/9035)
13+
- [electron/electron#5078](https://github.com/electron/electron/issues/5078)
14+
- [electron/governance#229](https://github.com/electron/governance/pull/229)
15+
- Reference Implementation:
16+
- Status: **Proposed**
17+
18+
# Clipboard Module Rearchitecture
19+
20+
## Summary
21+
22+
This RFC outlines a rearchitecturing of the [`clipboard`](https://www.electronjs.org/docs/latest/api/clipboard) module in Electron to improve alignment with the Clipboard API as specified by the [W3C](https://w3c.github.io/clipboard-apis/#clipboard-interface).
23+
24+
## Motivation
25+
26+
Chromium has made significant changes to their Clipboard API, including the ability to handle [Custom Formats](https://bugs.chromium.org/p/chromium/issues/detail?id=106449). Electron has had to break small aspects of its clipboard API to account for this, and it's unclear what other changes we may be forced to adapt to as they continue this work. Beyond this, users have been asking for an improved Clipboard API as well as surfaced a number of bugs with its current implementation - the most detailed of which is [here](https://github.com/electron/electron/issues/23156).
27+
28+
Electron provided a [`clipboard`](https://www.electronjs.org/docs/latest/api/clipboard) API to its users preceding the specification of the [Clipboard API by the W3C](https://w3c.github.io/clipboard-apis/#clipboard-interface). As a result, we locked into an API surface and created an API contract with our users that became progressively harder to alter without breaking said contract while the web drove a more standardized approach. On top of this confusion, the [`clipboard`](https://www.electronjs.org/docs/latest/api/clipboard) module provided by Electron is a dual-process module, meaning that it exists in parallel to the more robust API provided by Chromium in the renderer process via `navigator.clipboard` global.
29+
30+
At this point, it becomes clear that it is in users' and maintainers' best interest to reconsider how we approach the `clipboard` module - breaking changes might be difficult in the short term but provide more consistency and functionality as we look to the future of the module.
31+
32+
## Guide-level explanation
33+
34+
### API Design
35+
36+
The largest consideration for changes to the `clipboard` module is to what degree we should choose to align with the W3C specification. As we consider this, we should consider what needs or use cases might exist for Electron uses which necessarily wouldn't exist for Chrome users, and how we would plan to account for that in our new design.
37+
38+
#### Preferred Approach/Solution
39+
40+
This design recommends alignment with W3C where possible, with some additional APIs exposed to handle desktop use cases which aren’t considered by the specification. Given this approach, we will preserve some of the existing APIs, remove some of the existing APIs, and, modify the rest of the APIs to be in compliance with the W3C spec as much as possible.
41+
42+
**APIs to Preserve**
43+
* `clipboard.clear([type])`
44+
* Not supported through existing Web APIs or in the specification.
45+
* `clipboard.readText([type])`
46+
* Already implemented per specification.
47+
* `clipboard.writeText(text[, type])`
48+
* Already implemented per specification.
49+
* `clipboard.has(format[, type])`
50+
* Convenience method to quickly check if a particular format is available.
51+
52+
**APIs to Remove**
53+
* `clipboard.availableFormats([type])`
54+
* Superseded by `clipboard.read` [Web API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read)
55+
* `clipboard.readBookmark()`
56+
* Superseded by `clipboard.read` with custom `electron/bookmark` MIME type
57+
* `clipboard.writeBookmark(title, url[, type])`
58+
* Superseded by `clipboard.write` with custom `electron/bookmark` MIME type
59+
* `clipboard.readBuffer(format)`
60+
* Superseded by `clipboard.read`[Web API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read)
61+
* `clipboard.writeBuffer(format, buffer[, type])`
62+
* Superseded by `clipboard.write` [Web API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write)
63+
* `clipboard.readFindText()` _macOS_
64+
* Superseded by `clipboard.read` with custom `electron/findtext` MIME type
65+
* `clipboard.writeFindText(text)`
66+
* Superseded by `clipboard.write` with custom `electron/findtext` MIME type
67+
* `clipboard.readHTML()`
68+
* Superseded by `clipboard.read` with `text/html` MIME type
69+
* `clipboard.writeHTML(markup[, type])`
70+
* Superseded by `clipboard.write` with `text/html` MIME type
71+
* `clipboard.readImage([type])`
72+
* Superseded by `clipboard.read()` with `image/*` (where * is bmp, gif, jpeg, png, svg+xml, etc.)
73+
* `clipboard.writeImage(image[, type])`
74+
* Superseded by `clipboard.write` with `image/*` (where * is bmp, gif, jpeg, png, svg+xml, etc.)
75+
* `clipboard.readRTF([type])`
76+
* Superseded by `clipboard.read` with `application/rtf` MIME type
77+
* `clipboard.writeRTF(text[, type])`
78+
* Superseded by `clipboard.write` with `application/rtf` MIME type
79+
80+
**APIs to Modify**
81+
* `clipboard.read([clipboardType])`
82+
* `clipboardType` string (optional) - Can be `selection` or `clipboard`; default is `clipboard`. `selection` is only available on Linux.
83+
* This API will be modified to bring into spec compliance with the [Web API clipboard.read](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read)
84+
* Returns a Promise that resolves with an array of [ClipboardItem](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem) objects containing the
85+
clipboard's contents. The promise is rejected if permission to access the clipboard is not granted.
86+
* Ensure that raw formats are preserved
87+
* Custom MIME types e.g. `electron application/filepath` will be used to allow support of non-standard clipboard formats. This follows
88+
the W3C proposal for supporting [Web Custom formats for Async Clipboard API](https://github.com/w3c/editing/blob/gh-pages/docs/clipboard-pickling/explainer.md#custom-formats).
89+
The exception here is that instead of using the `web` prefix, we will use the `electron` prefix to prevent possible collisions with custom web formats.
90+
* `clipboard.write(data[, clipboardType]])​`
91+
* `data` an array of [ClipboardItem](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem) objects containing data to be written to the clipboard.
92+
* `clipboardType` string (optional) - Can be `selection` or `clipboard`; default is `clipboard`. `selection` is only available on Linux.
93+
* This API will be modified to bring into spec compliance with the [Web API `clipboard.write`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write).
94+
* Returns a Promise which is resolved when the data has been written to the clipboard. The promise is rejected if the clipboard is unable to complete the clipboard access.
95+
* Ensure that raw formats are preserved
96+
* Custom MIME types e.g. `electron application/filepath` will be used to allow support of non-standard clipboard formats. This follows
97+
the W3C proposal for supporting [Web Custom formats for Async Clipboard API](https://github.com/w3c/editing/blob/gh-pages/docs/clipboard-pickling/explainer.md#custom-formats).
98+
The exception here is that instead of using the `web` prefix, we will use the `electron` prefix to prevent possible collisions with custom web formats.
99+
* clipboardType is only used for Linux to specify if clipboard is regular clipboard or `selection` clipboard.
100+
101+
## Reference-level explanation
102+
103+
### Example migration code
104+
```js
105+
const { serialize, deserialize } = require("v8")
106+
107+
async function readClipboard(format, clipboardType) {
108+
const clipboardItems = await clipboard.read(clipboardType);
109+
const foundItem = clipboardItems.find(clipboardItem => {
110+
return clipboardItem.types.includes(format);
111+
});
112+
if (foundItem) {
113+
const buffer = await findTextItem.getType(format);
114+
return buffer.toString();
115+
}
116+
}
117+
118+
async function writeClipboard(format, text, clipboardType) {
119+
return clipboard.write([
120+
{
121+
[format]: Buffer.from(text)
122+
}
123+
], clipboardType);
124+
}
125+
126+
async function readBuffer(format, clipboardType) {
127+
const clipboardItems = await clipboard.read(clipboardType);
128+
const foundItem = clipboardItems.find(clipboardItem => {
129+
return clipboardItem.types.includes(format);
130+
});
131+
if (foundItem) {
132+
const buffer = foundItem.getType(format);
133+
return buffer;
134+
}
135+
}
136+
137+
async function writeBuffer(format, buffer, clipboardType) {
138+
return clipboard.write([
139+
{
140+
[format]: buffer
141+
}
142+
], clipboardType);
143+
}
144+
145+
async function availableFormats(clipboardType) {
146+
const clipboardItems = await clipboard.read(clipboardType);
147+
const clipboardFormats = [];
148+
for (const clipboardItem of clipboardItems) {
149+
for (const type of clipboardItem.types) {
150+
if (!clipboardFormats.includes(type)) {
151+
clipboardFormats.push(type);
152+
}
153+
}
154+
}
155+
return clipboardFormats;
156+
}
157+
158+
async function has(format, clipboardType) {
159+
const clipboardItems = await clipboard.read(clipboardType);
160+
const matchingClipboardItem = clipboardItems.find(clipboardItem => {
161+
return clipboardItem.types.includes(format);
162+
});
163+
if (matchingClipboardItem) {
164+
return true;
165+
}
166+
}
167+
168+
const BOOKMARK_MIME_TYPE = 'electron application/bookmark';
169+
async function readBookmark() {
170+
const bookmarkItem = await readBuffer(BOOKMARK_MIME_TYPE);
171+
if (bookmarkItem) {
172+
return deserialize(bookmarkItem);
173+
}
174+
}
175+
176+
async function writeBookmark(title, url) {
177+
const buffer = serialize({
178+
text: title,
179+
bookmark: url
180+
});
181+
return writeBuffer(BOOKMARK_MIME_TYPE, buffer);
182+
}
183+
184+
const FILE_PATH_MIME_TYPE = 'electron application/filepath';
185+
async function readFilePaths() {
186+
const filePathsItem = await readBuffer(FILE_PATH_MIME_TYPE);
187+
if (filePathsItem) {
188+
return deserialize(filePathsItem);
189+
}
190+
}
191+
192+
async function writeFilePaths(paths) {
193+
const filePathsBuffer = serialize(paths);
194+
return writeBuffer(BOOKMARK_MIME_TYPE, filePathsBuffer);
195+
}
196+
197+
const FIND_TEXT_MIME_TYPE = 'electron application/findtext';
198+
async function readFindText() {
199+
return readClipboard(FIND_TEXT_MIME_TYPE);
200+
}
201+
202+
async function writeFindText(text) {
203+
return writeClipboard(FIND_TEXT_MIME_TYPE, text);
204+
}
205+
206+
const HTML_MIME_TYPE = 'text/html';
207+
async function readHTML(clipboardType) {
208+
return readClipboard(HTML_MIME_TYPE, clipboardType);
209+
}
210+
211+
async function writeHTML(markup, clipboardType) {
212+
return writeClipboard(HTML_MIME_TYPE, markup, clipboardType);
213+
}
214+
215+
const PNG_MIME_TYPE = 'image/png';
216+
const JPEG_MIME_TYPE = 'image/jpeg';
217+
async function readImage(clipboardType)​ {
218+
const clipboardItems = await clipboard.read(clipboardType);
219+
//Look for PNG first
220+
let foundItem = clipboardItems.find(clipboardItem => {
221+
return clipboardItem.types.includes(PNG_MIME_TYPE);
222+
});
223+
if (!foundItem) {
224+
foundItem = clipboardItems.find(clipboardItem => {
225+
return clipboardItem.types.includes(JPEG_MIME_TYPE);
226+
});
227+
}
228+
if (foundItem) {
229+
let buffer;
230+
if (foundItem.types.includes(PNG_MIME_TYPE)) {
231+
buffer = foundItem.getType(PNG_MIME_TYPE);
232+
} else {
233+
buffer = foundItem.getType(JPEG_MIME_TYPE);
234+
}
235+
return nativeImage.createFromBuffer(buffer);
236+
}
237+
}
238+
239+
async function writeImage(image, clipboardType)​ {
240+
const buffer = image.getBitmap();
241+
const dataUrl = image.toDataURL();
242+
const regex = /^data:(.+\/.+);.*$/;
243+
const matches = dataUrl.match(regex);
244+
return clipboard.write([
245+
{
246+
[matches[1]]: buffer
247+
}
248+
], clipboardType);
249+
}
250+
251+
const RTF_MIME_TYPE = 'application/rtf';
252+
async function readRTF(clipboardType) {
253+
return readClipboard(RTF_MIME_TYPE, clipboardType);
254+
}
255+
256+
async function writeRTF(text, clipboardType)​ {
257+
return writeClipboard(RTF_MIME_TYPE, text, clipboardType);
258+
}
259+
```
260+
261+
## Drawbacks
262+
263+
This is a major refactor of an API with a pretty large surface area; however there are parts
264+
of the current [`clipboard`](https://www.electronjs.org/docs/latest/api/clipboard) module that
265+
are still marked as `Experimental`.
266+
267+
## Rationale and alternatives
268+
269+
1. Strict alignment with W3C specification - no additional APIs exposed.
270+
2. Leave API as-is and continue to adapt it as required by upstream changes by Chromium development.
271+
272+
## Prior art
273+
274+
- [feat(clipboard): support read/write files PR](https://github.com/electron/electron/pull/47975)
275+
- [feat: add clipboard.writeFilePaths/readFilePaths APIs PR](https://github.com/electron/electron/pull/26693)
276+
277+
## Unresolved questions
278+
279+
- What parts of the design do you expect to resolve through the RFC process before this gets merged?
280+
- What parts of the design do you expect to resolve through the implementation of this feature
281+
before stabilization?
282+
- What related issues do you consider out of scope for this RFC that could be addressed in the
283+
future independently of the solution that comes out of this RFC?
284+
285+
## Future possibilities
286+
287+
Think about what the natural extension and evolution of your proposal would be and how it would
288+
affect the project as a whole in a holistic way. Try to use this section as a tool to more fully
289+
consider all possible interactions with the project in your proposal.
290+
291+
This is also a good place to "dump ideas", if they are out of scope for the RFC you are writing but
292+
otherwise related.
293+
294+
If you have tried and cannot think of any future possibilities, you may simply state that you
295+
cannot think of anything.
296+
297+
Note that having something written down in the future possibilities section is not a reason to
298+
accept the current or a future RFC; such notes should be in the section on motivation or
299+
rationale in this or subsequent RFCs. The section merely provides additional information.

0 commit comments

Comments
 (0)