Repository navigation
fix(clipboard): copy, cut and paste keep working when Firefox refuses a clipboard call - #13892
yuxi-liu-wired wants to merge 7 commits into
Conversation
| (when-not (or blocks-written? text-written?) | ||
| (throw (ex-info "The clipboard refused the copied blocks" {:block-uuids top-level-block-uuids}))) |
There was a problem hiding this comment.
🔴 Successful quick copy blocks the cut
When later writes fail after copy-cached-selection-text! succeeds, copy-selection-blocks rejects the cut. Its success check ignores the initial write, so copied blocks remain in the graph.
Learn more
A cut first calls copy-cached-selection-text!, which starts a clipboard write synchronously when every selected block snapshot is ready. The later writes occur after asynchronous block export and database loads. On a browser that requires a user gesture, the early write can succeed while both later writes fail. The new guard sees only the later promises, so it aborts the cut despite text already reaching the clipboard.
Example: Select a cached block and invoke Cut. Its title is written during the gesture; both writes after export are refused. The cut reports failure and leaves the block in the graph instead of completing the cut.
Recommended fix: Include the completion of the cached write in the success decision, or replace the early write with a single tracked write that preserves the synchronous user-gesture path. Ensure the accepted result represents the text intended for the cut.
Was this helpful? React with 👍 or 👎 to provide feedback.
| attempts.push(() => navigator.clipboard.writeText(text)) | ||
| return writeClipboardWithFallback(attempts) |
There was a problem hiding this comment.
🔍 Single-block cut remains outside the fix
The separate cut-block! deletes before its clipboard write settles. If every new fallback fails, that path still deletes the block and now leaves a rejected promise unhandled.
Was this helpful? React with 👍 or 👎 to provide feedback.
|
Thanks, both points were right. Two follow-up commits on the branch.
Checked with the three namespaces ( |
ae2f2d6 to
b39eab5
Compare
d0f7077 to
fd7f6b9
Compare
…seq format is refused In Firefox, copying blocks did not put them on the clipboard through the structured write: the ClipboardItem carried the non-standard "web application/logseq" type, which Firefox rejects with NotAllowedError. The rejection was swallowed and logged, so the copy looked like it had worked, the html and plain text of that write were lost, and callers could not tell that the write had failed. `writeClipboard` now tries the richest format first and falls back when a write is refused, either by a rejected promise or by a synchronous throw: the Logseq format with text and html, then text and html only, then `writeText`. It resolves when one of them succeeded and rejects, with the error logged, when all of them failed, so a caller that deletes after copying can tell. Chrome still receives the Logseq format. The new frontend.clipboard-write-test stubs `navigator.clipboard` and `ClipboardItem` to refuse what Firefox refuses, and checks the fallback order, that the text and html survive, and that a total failure rejects. Fixes logseq/db-test#918
Pasting in Firefox did nothing for text copied outside Logseq. When no blocks are cached in memory, `get-copied-blocks` calls `navigator.clipboard.read()` to look for the Logseq format. Firefox rejects that call with NotAllowedError, the rejection reached the catch in `paste-copied-blocks-or-text`, which only logged "Paste failed", and the text from the paste event was never inserted. `get-copied-blocks` now catches a failed read, logs a warning and resolves to nil, which is what it returns when the clipboard holds no copied blocks. The paste then goes on with the text of the paste event. The new tests stub `navigator.clipboard.read` to reject: `get-copied-blocks` resolves to nil, and `editor-on-paste!` inserts the pasted text. Fixes logseq/db-test#919
…ut in Firefox Cutting selected blocks in Firefox could leave the blocks in the graph (the report), and could just as well delete blocks that never reached the clipboard. `cut-selection-blocks` deletes after `copy-selection-blocks`. That copy let a failure of the structured clipboard write abort the cut, even when the plain text was already on the clipboard, and it ignored the result of the plain text write, so a refused clipboard did not stop the delete. `copy-selection-blocks` now waits for both clipboard writes. The copy succeeds when either one succeeded, since the text then reaches the clipboard and can be pasted, and it rejects when the browser refused both. A rejected copy aborts the cut before anything is deleted and does not record a cut. Together with the writeClipboard fallback, Firefox, which refuses only the Logseq format, cuts normally. The new tests in editor-test stub both clipboard writes: a copy that is refused everywhere rejects without recording a cut and a cut then deletes nothing; when only the structured write is refused the copy succeeds and the cut deletes. Fixes logseq/db-test#1468
… copy A cut first writes the cached titles of the selected blocks to the clipboard, synchronously within the user gesture, and only then loads and exports the blocks for the later writes. `copy-selection-blocks` decided whether the copy succeeded from the later writes alone. In a browser that accepts a clipboard write only inside the gesture, the early write succeeded and the later ones were refused, so the copy was reported as failed and the cut was aborted although the text was on the clipboard. `copy-selection-blocks` now tracks the promise of the early write and counts it with the others: the copy fails only when every write was refused. The new test makes only the first plain text write succeed and checks that the copy completes and records the cut. Refs logseq/db-test#1468
…ved the block Cutting one block from its context menu (`cut-block!`) started the clipboard write and deleted the block without waiting for the write. If the browser refused every format, the block was deleted although nothing reached the clipboard, and the rejected write was left unhandled. `cut-block!` now waits for the write. When it succeeded it records the cut and deletes as before. When the browser refused it, nothing is deleted and the refusal is logged instead of being left as an unhandled rejection. This is the same rule the selection cut follows. The new test makes the clipboard write reject and checks that the block and its child stay on the page and that `cut-block!` resolves. Refs logseq/db-test#1468
The text of a paste is inserted only while the editor that was open when the paste started is still the edit input (fix(paste): drop text paste when the editor exits before the clipboard resolves). editor-on-paste-pastes-text-when-clipboard-read-is-rejected stubbed no edit input, so with that change the text is dropped and the test sees nil. The test now stubs the edit input id and state/get-input as the other text-path paste tests do.
editor-on-paste-pastes-text-when-clipboard-read-is-rejected stubbed `state/get-input` to nil. fix(paste): drop text paste when the editor exits before the clipboard resolves (fork branch fix/paste-after-editor-exit) now pastes the text only when `input` is non-nil and identical to `(state/get-input)`. With both changes merged, the test's nil input drops the text and the test sees nil. The ReferenceError `document is not defined` logged next to that failure is the error html-parser/convert raises under node; paste.cljs catches it and falls back to the plain text, so it plays no part in the failure.
The test now stubs `state/get-input` with one input object, `#js {:value ""}`, as the other text-path paste tests do. The paste code is unchanged. The test passes on this branch alone and with the guard from fix/paste-after-editor-exit applied.
Fixes logseq/db-test#919
fd7f6b9 to
0c137ff
Compare
Fixes logseq/db-test#919, fixes logseq/db-test#1468, fixes logseq/db-test#918.
What the user sees
In the web build on Firefox, the clipboard did not work:
NotAllowedError: Clipboard read operation is not allowed.web application/logseqtype, which Firefox rejects. The rejection was only logged, so the copy looked fine to the app.Cause
get-copied-blocks(src/main/frontend/handler/paste.cljs) awaitsnavigator.clipboard.read()to look for the Logseq format when nothing is cached in memory. Firefox rejects it; the rejection reached the catch ofpaste-copied-blocks-or-text, which logs "Paste failed" and never callspaste-copied-text, so the text of the paste event was dropped.writeClipboard(src/main/frontend/utils.js) built oneClipboardItemwith theweb application/logseqkey and swallowed any failure of the write (.catch+console.log), so nobody could react to a refused write and nothing was retried with a format the browser accepts.copy-selection-blocks(src/main/frontend/handler/editor.cljs) did not wait for the plain text write and let a failure of the structured write abort the whole copy, even when the text was already on the clipboard.cut-selection-blocksdeletes after that copy, so it either did not delete (copy aborted) or deleted although the clipboard write was refused (swallowed rejection). I could not run Firefox, so which of the two Firefox hits depends on whether it rejects or throws on the unsupported type; both are covered.cut-block!(the single-block cut from the context menu) started its clipboard write and deleted the block without waiting for the write.The principle behind the change: a rejected clipboard API must fall back to plain text and must never lose data (no delete after a failed copy).
Change
Five commits:
writeClipboardtries the richest format first and falls back on a rejected promise or a synchronous throw: Logseq format + text + html, then text + html, thenwriteText. It resolves when one of them succeeded and rejects (and logs) when all of them failed. Chrome still gets the Logseq format. The first attempt still starts synchronously within the user gesture.get-copied-blockscatches a failedclipboard.read()orgetType, logs a warning and resolves to nil (its existing "no copied blocks" result), so the paste continues with the text of the paste event.copy-selection-blockswaits for the clipboard writes. It succeeds when one of them succeeded, and rejects when the browser refused all of them. A rejected copy abortscut-selection-blocksbefore the delete, and no cut op is recorded.copy-selection-blocksstarts synchronously within the user gesture (the cached titles of the selected blocks) is tracked and counts as a copy too. Before, a browser that accepts only that write and refuses the later ones aborted the cut although the text was on the clipboard. That early write holds the titles of the selected blocks, not their children; when only it succeeds, the cut goes on with a clipboard that lacks the children's text (the cut can be undone).cut-block!waits for its clipboard write and deletes only after it succeeded. When the browser refuses it, nothing is deleted and the refusal is logged instead of left as an unhandled rejection.Tests
All stub the clipboard to reject; each fails without its change and passes with it:
frontend.clipboard-write-test(new): the Logseq format is still written where supported; when the format is rejected, or theClipboardItemconstructor throws, text and html are written without it; when noClipboardItemcan be written,writeTextgets the text; when everything is refused the promise rejects. Without the fix: 4 failures; with: 0.frontend.handler.paste-test:get-copied-blocksresolves to nil whennavigator.clipboard.readrejects, andeditor-on-paste!inserts the pasted text. Without the fix: 2 failures; with: 0.frontend.handler.editor-test:copy-selection-blocksrejects without recording a cut when every write is refused, succeeds when only the structured write is refused, and succeeds when only the write made during the user gesture is accepted (fails without the follow-up commit);cut-selection-blocksdeletes nothing when the copy was refused everywhere and deletes when only the structured write was refused. Without the fix: 2 failures for the first twocopy-selection-blockstests; 1 failure for the gesture test.frontend.handler.editor-async-test:cut-block!rejecting the clipboard write keeps the block and its child on the page and resolves. Fails without the follow-up commit, passes with it.The three namespaces
frontend.clipboard-write-test,frontend.handler.paste-testandfrontend.handler.editor-testpass (117 tests in the last). The whole unit suite ran with two load-related failures (electron-import-and-download-open-real-worker-in-fresh-graphsinfrontend.persist-db-test, a worker request timeout, anddb-worker-node-import-edninfrontend.worker.db-worker-node-test); both namespaces pass when run alone.Not covered: no real Firefox run.
getClipText(raw paste,utils.js) still callsnavigator.permissions.query({name: "clipboard-read"}), which Firefox also does not support; it is outside these three issues and unchanged.Test stub after the rebase on master (2026-11-11)
editor-on-paste-pastes-text-when-clipboard-read-is-rejectedpassed on this branch alone. It failed (expected the pasted text, got nil) only when merged with fix/paste-after-editor-exit, which pastes text only wheninputis non-nil and identical to(state/get-input); the test stubbedstate/get-inputto nil. It now stubs one input object,#js {:value ""}, as the other text-path paste tests do. The paste code is unchanged. TheReferenceError: document is not definedlogged next to the failure comes fromhtml-parser/convertunder node;paste-copied-textcatches it and falls back to the plain text, so it plays no part in the failure. Master's 94ea4a5 (#13177) adds only:cut?topaste-blocksin paste.cljs and does not touch this path.frontend.handler.paste-test,frontend.clipboard-write-test,frontend.handler.editor-async-testandfrontend.handler.editor-testpass. The test also passes with fix/paste-after-editor-exit's guard applied, where the nil stub reproduced the failure.