4. Floating Promise Anti-Pattern & Premature Caret in Multi-Block Paste
π Affected Locations
src/components/modules/paste.ts (Lines 251β261)
π Deep Technical Diagnosis
In src/components/modules/paste.ts:
// src/components/modules/paste.ts: Lines 251-261
const isCurrentBlockDefault = BlockManager.currentBlock && BlockManager.currentBlock.tool.isDefault;
const needToReplaceCurrentBlock = isCurrentBlockDefault && BlockManager.currentBlock.isEmpty;
dataToInsert.map(
async (content, i) => this.insertBlock(content, i === 0 && needToReplaceCurrentBlock)
);
if (BlockManager.currentBlock) {
Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END);
}
Flaws:
- Misused
.map() as .forEach(): .map() allocates a new array of returned Promises that are completely discarded.
- Unawaited Floating Promises: The callback is marked
async, meaning this.insertBlock(...) returns an unresolved Promise for each item.
- Premature Caret Placement:
Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END) executes immediately and synchronously on line 259 before the async map operations have resolved.
- If any custom tool initializes asynchronously during insertion or yields to the event loop, the caret focuses the wrong block or moves before blocks are even attached to the DOM.
π‘ Proposed Solution & Patch
src/components/modules/paste.ts
@@ -251,9 +251,9 @@ export default class Paste extends Module {
const isCurrentBlockDefault = BlockManager.currentBlock && BlockManager.currentBlock.tool.isDefault;
const needToReplaceCurrentBlock = isCurrentBlockDefault && BlockManager.currentBlock.isEmpty;
- dataToInsert.map(
- async (content, i) => this.insertBlock(content, i === 0 && needToReplaceCurrentBlock)
- );
+ for (let i = 0; i < dataToInsert.length; i++) {
+ this.insertBlock(dataToInsert[i], i === 0 && needToReplaceCurrentBlock);
+ }
if (BlockManager.currentBlock) {
Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END);
4. Floating Promise Anti-Pattern & Premature Caret in Multi-Block Paste
π Affected Locations
src/components/modules/paste.ts(Lines 251β261)π Deep Technical Diagnosis
In
src/components/modules/paste.ts:Flaws:
.map()as.forEach():.map()allocates a new array of returned Promises that are completely discarded.async, meaningthis.insertBlock(...)returns an unresolved Promise for each item.Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END)executes immediately and synchronously on line 259 before the async map operations have resolved.π‘ Proposed Solution & Patch
src/components/modules/paste.ts