Skip to content

fix(clipboard): copy, cut and paste keep working when Firefox refuses a clipboard call - #13892

Open
yuxi-liu-wired wants to merge 7 commits into
logseq:masterfrom
yuxi-liu-wired:fix/dbtest-firefox
Open

yuxi-liu-wired wants to merge 7 commits into
logseq:masterfrom
yuxi-liu-wired:fix/dbtest-firefox

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Paste (push失败 #919): pasting text copied outside Logseq did nothing. The console showed NotAllowedError: Clipboard read operation is not allowed.
  • Copy (Directory handle not exists (local filesystem) #918): copying blocks wrote the structured clipboard item with the non-standard web application/logseq type, which Firefox rejects. The rejection was only logged, so the copy looked fine to the app.
  • Cut (enhance: use title in rename dialog #1468): cutting blocks copied them but left them in the graph. The reverse was also possible: a cut whose clipboard write was refused deleted blocks that never reached the clipboard.

Cause

  • get-copied-blocks (src/main/frontend/handler/paste.cljs) awaits navigator.clipboard.read() to look for the Logseq format when nothing is cached in memory. Firefox rejects it; the rejection reached the catch of paste-copied-blocks-or-text, which logs "Paste failed" and never calls paste-copied-text, so the text of the paste event was dropped.
  • writeClipboard (src/main/frontend/utils.js) built one ClipboardItem with the web application/logseq key 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-blocks deletes 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:

  • Directory handle not exists (local filesystem) #918: writeClipboard tries the richest format first and falls back on a rejected promise or a synchronous throw: Logseq format + text + html, then text + html, then writeText. 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.
  • push失败 #919: get-copied-blocks catches a failed clipboard.read() or getType, logs a warning and resolves to nil (its existing "no copied blocks" result), so the paste continues with the text of the paste event.
  • enhance: use title in rename dialog #1468: copy-selection-blocks waits for the clipboard writes. It succeeds when one of them succeeded, and rejects when the browser refused all of them. A rejected copy aborts cut-selection-blocks before the delete, and no cut op is recorded.
  • Review follow-up (enhance: use title in rename dialog #1468): the write that copy-selection-blocks starts 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).
  • Review follow-up (enhance: use title in rename dialog #1468): 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 the ClipboardItem constructor throws, text and html are written without it; when no ClipboardItem can be written, writeText gets the text; when everything is refused the promise rejects. Without the fix: 4 failures; with: 0.
  • frontend.handler.paste-test: get-copied-blocks resolves to nil when navigator.clipboard.read rejects, and editor-on-paste! inserts the pasted text. Without the fix: 2 failures; with: 0.
  • frontend.handler.editor-test: copy-selection-blocks rejects 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-blocks deletes nothing when the copy was refused everywhere and deletes when only the structured write was refused. Without the fix: 2 failures for the first two copy-selection-blocks tests; 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-test and frontend.handler.editor-test pass (117 tests in the last). The whole unit suite ran with two load-related failures (electron-import-and-download-open-real-worker-in-fresh-graphs in frontend.persist-db-test, a worker request timeout, and db-worker-node-import-edn in frontend.worker.db-worker-node-test); both namespaces pass when run alone.

Not covered: no real Firefox run. getClipText (raw paste, utils.js) still calls navigator.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-rejected passed 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 when input is non-nil and identical to (state/get-input); the test stubbed state/get-input to nil. It now stubs one input object, #js {:value ""}, as the other text-path paste tests do. The paste code is unchanged. The ReferenceError: document is not defined logged next to the failure comes from html-parser/convert under node; paste-copied-text catches it and falls back to the plain text, so it plays no part in the failure. Master's 94ea4a5 (#13177) adds only :cut? to paste-blocks in paste.cljs and does not touch this path. frontend.handler.paste-test, frontend.clipboard-write-test, frontend.handler.editor-async-test and frontend.handler.editor-test pass. The test also passes with fix/paste-after-editor-exit's guard applied, where the nil stub reproduced the failure.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

Devin Review

Comment thread src/main/frontend/handler/editor.cljs Outdated
Comment on lines +1241 to +1242
(when-not (or blocks-written? text-written?)
(throw (ex-info "The clipboard refused the copied blocks" {:block-uuids top-level-block-uuids})))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +335 to +336
attempts.push(() => navigator.clipboard.writeText(text))
return writeClipboardWithFallback(attempts)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔍 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.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Thanks, both points were right. Two follow-up commits on the branch.

  1. editor.cljs (successful early write blocks the cut): copy-selection-blocks now keeps the promise of the early copy-cached-selection-text! write and counts it with the other two writes. The copy is reported as failed, and the cut aborted, only when all three were refused. The new test copy-selection-blocks-counts-the-write-made-during-the-user-gesture-test accepts the first plain text write and refuses the later ones; it fails without the change and passes with it. One limit to know about: the early write holds only the titles of the selected blocks (not their children), so when only that write succeeds the cut goes on with a clipboard that lacks the children's text. I took that over aborting the cut, because the cut is undoable and the text of what was selected is on the clipboard. Say so if you would rather abort in that case.
  2. cut-block! (single-block cut): it now waits for the clipboard write and deletes only when the write succeeded. When the browser refuses it, nothing is deleted and the refusal is logged (Cut cancelled, the clipboard refused the block), so there is no unhandled rejection. The new test cut-block-keeps-the-block-when-the-clipboard-refuses-the-copy rejects the write and checks that the block and its child stay and that cut-block! resolves; it fails without the change and passes with it.

Checked with the three namespaces (frontend.clipboard-write-test, frontend.handler.paste-test, frontend.handler.editor-test: 0 failures) and CI's whole unit suite. In the suite run, frontend.persist-db-test (a worker request timeout) and frontend.worker.db-worker-node-test (db-worker-node-import-edn) failed under load; both pass when run alone. No Firefox run.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/dbtest-firefox branch 3 times, most recently from ae2f2d6 to b39eab5 Compare November 3, 2026 12:02
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/dbtest-firefox branch 6 times, most recently from d0f7077 to fd7f6b9 Compare November 18, 2026 14:01
…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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant