Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: clipboard.getNativeFormat() and clipboard.writeBuffers() #229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

coreh
Copy link

@coreh coreh commented Jan 29, 2020

Hey folks 👋

Here's an initial version of the specs for the proposed clipboard.getNativeFormat() and clipboard.writeBuffers() API methods that we started working on during the contributor training

cc @itsananderson @loc

@coreh coreh requested a review from a team as a code owner January 29, 2020 20:21

* `portableFormat` String - One of the supported native formats: `text`, `bookmark`, `html`, `rtf`, `image`

Returns `String` - An opaque, platform-dependent value representing a clipboard format (e.g. `text/plain`, `public.text` or `13`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably the only useful application of this value would be to pass it into the new clipboard.writeBuffers (or existing clipboard.writeBuffer) function, yes?

So this would be used like:

clipboard.writeBuffers({
  [clipboard.getNativeFormat('text')]: Buffer.from(...),
  [myCustomFormat[os.platform()]]: Buffer.from(...),
})

That seems a little awkward to me, though I'm not sure I have a better suggestion. Here are some random thoughts, maybe useful, maybe not.

  1. Given that custom formats will also necessarily be platform-specific, how do we expect users of the API to handle those? Should we have some examples to guide best practices?
  2. These values are effectively constants. Should we expose them as such? e.g. clipboard.format.TEXT
  3. Could we use JS Symbols effectively here...? e.g. if clipboard.format.TEXT was a special symbol, that could distinguish it from custom formats while avoiding the collision problem.

Copy link
Member

@jkleinsc jkleinsc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromium is implementing raw clipboard access which should accomplish the same goal as this API.
Details here: https://bugs.chromium.org/p/chromium/issues/detail?id=897289. I've tested this functionality in Electron and what has been implemented in Chromium is available in Electron in the renderer (raw write is implemented; raw read is in process and not yet available). If we need this functionality in the main process we should use the same api shapes used by Chromium.

The explainer here has a pretty good summary of the API:
https://github.com/WICG/raw-clipboard-access/blob/master/explainer.md

@itsananderson
Copy link
Member

I wrote up a really basic Fiddle for testing the Chromium feature, in case it's a helpful jumpstart for anyone: https://gist.github.com/2802c6a3744a31d3a81961bc118632d2 - I didn't do anything fancy to write custom binary formats, but it should be simple to add.

Exposing a compatible API in the main process does seem useful, especially since renderer processes are restricted to reading/writing the clipboard when they have user focus. We may need to make small adjustments to the API, like swapping Buffers for Blobs for binary clipboard values.

@henrit
Copy link

henrit commented Sep 17, 2021

Chromium's raw clipboard API was abandoned last June. Its implementation has been scrubbed from the chromium codebase.

There is now instead a proposal for standardizing Pickling for Async Clipboard API, but it is explicitly designed to prevent setting arbitrary formats on the clipboard in the ways a native app would.

This leaves Electron users in the position of continuing to put together one-off node.js native clipboard modules, or waiting for this proposed change to land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants