Repository navigation
fix(paste): wait for multi-block content before setting caret - #3035
anshurajbisoyi98-ctrl wants to merge 8 commits into
Conversation
Signed-off-by: Anshu Raj Bisoyi <anshurajbisoyi98@gmail.com>
62fe386 to
8f15e2c
Compare
neSpecc
left a comment
There was a problem hiding this comment.
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.
…' into fix-3032-multi-block-paste-caret
neSpecc
left a comment
There was a problem hiding this comment.
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:
-
With the current
@editorjs/paragraph(2.11.x),onPastereturns 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 likenext(typing after pastingFirst block\n\nSecond blockgivesXSecond 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 settinginnerHTML. We'll bump the dependency here before merging. -
onPastemay now return a Promise, which is a Tools API change. Please document it indocs/tools.md(Pasted data handling) and add anImprovemententry to the CHANGELOG. -
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.
|
Thanks! Opened editor-js/paragraph#98: Documented the Promise return in |
Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
57b4f7b to
8d4cbcf
Compare
neSpecc
left a comment
There was a problem hiding this comment.
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.
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:
onPastemay now return a Promise. Core inserts the pasted blocks, callsonPastefor 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 synconPastework as before.Block.call()returns the result of the tool method so core can await it.Caret.setToBlockduring multi-block insert is removed.insertBlockfallback in inline paste now set the caret at the end of the inserted block.onPasteis documented indocs/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.8here.I added regression tests for multi-block paste with an async tool and for the caret position after single-block paste.