Re: [w3c/editing] Seeking feedback on delayed clipboard rendering proposal (Issue #417)

We did some investigations into whether the existing read/write APIs are enough to implement delayed generation of clipboard data. Here are our findings:

**Problems with the existing API**
Current `ClipboardItemData` type that is a  [`Promise<Blob or DOMString>`](https://w3c.github.io/clipboard-apis/#ref-for-typedefdef-clipboarditemdata) doesn't provide the web authors an option to only generate the Blobs for specific formats when it's actually requested by the target app where the paste operation is performed. The executor function in the `Promise` runs immediately, so it defeats the purpose of delayed rendering by not allowing the web authors to only generate the expensive formats when it's requested by the system clipboard. 

e.g.
`function generateExpensiveHTML(resolve, reject) {
const blobInput = new Blob([ '<p>Some HTML</p>'], {type: 'text/html'});
resolve(blobInput);
}`

`const promiseBlob = new Promise(generateExpensiveHTML);
const clipboardItem = new ClipboardItem({'text/html': promiseBlob});
navigator.clipboard.write([clipboardItem]);`

Here the executor function `generateExpensiveHTML` runs immediately, so the web authors don't have a way to delay the execution of this function until the system clipboard asks for the data for this particular format.

There are couple of solutions that solves the issues mentioned above:
1. [Map of MIME type to promise in ClipboardItem constructor](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/DelayedClipboard/DelayedClipboardRenderingExplainer.md#considered-alternative-1-map-of-mime-type-to-promise-in-clipboarditem-constructor)
Here the web authors can provide a map of callbacks to formats that are delay generated. That way browsers know which formats to write immediately to the clipboard and which ones to mark as delay generated in the system clipboard.
**Cons**: Redundant format info need to be provided in the callback map that confuses the browser if a format is present in both the callback map and in the ClipboardItem constructor. This also affects ergonomics of the API usage.

2. Provide a callback argument in the ClipboardItem constructor that returns a Blob when executed.
`callback ClipboardDelayedCallbackBlob = Blob();`
`callback ClipboardDelayedCallbackString = DOMString();`
`constructor(record<DOMString, Promise<Blob> or ClipboardDelayedCallbackBlob or ClipboardDelayedCallbackString> items,`

In this case the web author can decide which formats to delay generate and which ones to resolve immediately without having to provide the format and callback info in a separate map. This provides better developer ergonomics and easier to reason about as to which formats should be written immediately to the system clipboard and which ones should be marked as delayed generate.
**We want to pursue option 2, so please let us know if you have any feedback on the API design.**
_Note: We tried option 2 in Chromium, but we are suspecting that the IDL compiler has a bug as it is not able to compile this syntax for some reason._
@sanketj opened https://github.com/whatwg/webidl/issues/1278 to seek clarification.
 @anaskim @inexorabletash @annevk @whsieh @a-sully @evanstade

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/editing/issues/417#issuecomment-1461652377
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/editing/issues/417/1461652377@github.com>

Received on Thursday, 9 March 2023 09:30:41 UTC