Skip to content

fix(paste): wait for multi-block content before setting caret - #3035

Open
anshurajbisoyi98-ctrl wants to merge 8 commits into
codex-team:nextfrom
anshurajbisoyi98-ctrl:fix-3032-multi-block-paste-caret
Open

anshurajbisoyi98-ctrl wants to merge 8 commits into
codex-team:nextfrom
anshurajbisoyi98-ctrl:fix-3032-multi-block-paste-caret

Conversation

@anshurajbisoyi98-ctrl

@anshurajbisoyi98-ctrl anshurajbisoyi98-ctrl commented Oct 3, 2026 •

Copy link
Copy Markdown

Fixes #3032.

When pasting multiple blocks, the caret was set before the tools had applied the pasted content, so it could end up at the start of the last block instead of after its text.

What changed:

  • onPaste may now return a Promise. Core inserts the pasted blocks, calls onPaste for all of them in parallel, waits for the returned Promises and then sets the caret at the end of the last block. Tools with a sync onPaste work as before.
  • Block.call() returns the result of the tool method so core can await it.
  • The per-block Caret.setToBlock during multi-block insert is removed.
  • Single-block paste and the insertBlock fallback in inline paste now set the caret at the end of the inserted block.
  • The async onPaste is documented in docs/tools.md, with CHANGELOG entries.

The default Paragraph tool needs editor-js/paragraph#98 for this to work, and the dependency is bumped to ^2.11.8 here.

I added regression tests for multi-block paste with an async tool and for the caret position after single-block paste.

Signed-off-by: Anshu Raj Bisoyi <anshurajbisoyi98@gmail.com>
@anshurajbisoyi98-ctrl
anshurajbisoyi98-ctrl force-pushed the fix-3032-multi-block-paste-caret branch from 62fe386 to 8f15e2c Compare November 8, 2026 02:13
Comment thread src/components/modules/blockManager.ts Resolved

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the update. The multi-block caret works now, but I'd suggest a different approach.

1. Async insert() doesn't fix the root cause. Making insert() awaitable (insertSync(), block.ready, blockReadiness) is a lot of core change, but it only waits until the block is rendered. Paragraph applies the pasted content later, in its own requestAnimationFrame inside onPaste, so core still needs the extra rAF "for Paragraph" (blockManager.ts:468). We don't want core to guess frames for a specific tool.

Instead, let onPaste optionally return a Promise. Core calls onPaste for all inserted blocks in parallel, awaits them once, then sets the caret:

const blocks = dataToInsert.map((content, i) => BlockManager.insert({
  tool: content.tool,
  replace: i === 0 && needToReplaceCurrentBlock,
}));

await Promise.all(blocks.map((block, i) =>
  Promise.resolve(block.call(BlockToolAPI.ON_PASTE, dataToInsert[i].event)).catch(() => undefined)
));

Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END);

(Block.call() needs to return the tool method's result.) Tools with a sync onPaste return nothing and keep working as before. Paragraph would resolve its Promise inside its existing rAF, right after setting innerHTML. That's a separate PR to @editorjs/paragraph.

Please don't await blocks sequentially. In our benchmark with 300 pasted paragraphs, parallel Promise.all finished in ~142 ms, sequential took ~5 s, and current next takes ~940 ms with an ~817 ms freeze. The freeze comes from the per-block Caret.setToBlock in insertBlock, so removing it, as you did, is right; please keep that part.

An awaitable insert() might be useful later (e.g. to await blocks.insert() in the API), but it's an API change worth a separate discussion, not part of this fix. Please revert the async insert()/insertSync()/readiness part. That also removes the blockReadiness.set(block.ready) call with a single argument in the read-only branch, which currently fails tsc (TS2554), and the new member-ordering lint error for ready in block/index.ts.

2. Single-block paste still loses the caret. processSingleBlock awaits insertBlock() but never sets the caret. Pasting <h2>Title</h2> into a paragraph with abc and typing leaves the caret in the old block: typing gives abcX. Please call Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END) after the insert there, and also in the insertBlock fallback of processInlinePaste.

3. Tests. Please add a regression test for single-block paste too (caret at the end of the inserted block). The multi-block test is good. CI hasn't run on this PR yet.

@anshurajbisoyi98-ctrl

Copy link
Copy Markdown
Author

@neSpecc Addressed in 57b97a2: reverted async insert/readiness, awaited onPaste Promises in parallel, and added the single-block caret regression test. Paste spec passes 16/16.

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the update! The core part is done the way we discussed: async insert() is reverted, onPaste Promises are awaited in parallel, the core rAF is gone, and single-block paste now sets the caret.

A few things left before merge:

  1. With the current @editorjs/paragraph (2.11.x), onPaste returns nothing and applies the content in its own rAF. So the caret is still set before the text is there, and multi-block paste behaves like next (typing after pasting First block\n\nSecond block gives XSecond block). The test passes only because it uses a custom async tool. Please open the companion PR to @editorjs/paragraph: resolve the Promise in its rAF right after setting innerHTML. We'll bump the dependency here before merging.

  2. onPaste may now return a Promise, which is a Tools API change. Please document it in docs/tools.md (Pasted data handling) and add an Improvement entry to the CHANGELOG.

  3. The PR description is outdated: it still says the caret waits for the next animation frame. Please update it. CI hasn't run on this head yet.

@anshurajbisoyi98-ctrl

Copy link
Copy Markdown
Author

Thanks! Opened editor-js/paragraph#98: onPaste now returns a Promise that resolves in its rAF right after setting innerHTML. With that build linked locally, pasting First block\n\nSecond block and typing gives Second blockX.

Documented the Promise return in docs/tools.md (Pasted data handling) and added the Improvement entry to the CHANGELOG. The PR description is updated too.

Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
@anshurajbisoyi98-ctrl
anshurajbisoyi98-ctrl force-pushed the fix-3032-multi-block-paste-caret branch from 57b4f7b to 8d4cbcf Compare November 27, 2026 09:22

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! @editorjs/paragraph 2.11.8 is published now, but yarn.lock still resolves @editorjs/paragraph@^2.11.6 to 2.11.6. Please run yarn and commit the updated yarn.lock.

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

Development

Successfully merging this pull request may close these issues.

Floating Promise Anti-Pattern & Premature Caret in Multi-Block Paste

2 participants