Skip to content

Commit f3514d9

Browse files
committed
fix(editor): preserve insert order in accept batch handler
Track lastInserted per anchor so consecutive inserts chain correctly instead of all inserting after the same original anchor (reverse order). Made-with: Cursor
1 parent d720638 commit f3514d9

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/components/editor/rich/RichEditorWithAgent.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ export const RichEditorWithAgent = defineComponent({
383383
const editor = editorInstance
384384
editor.update(() => {
385385
const root = $getRoot()
386+
const lastInserted = new Map<string, LexicalNode>()
386387
for (const entry of batch.entries) {
387388
const { op } = entry
388389
if (op.op === 'insert') {
@@ -396,10 +397,17 @@ export const RichEditorWithAgent = defineComponent({
396397
if (idx >= children.length) root.append(newNode)
397398
else children[idx].insertBefore(newNode)
398399
} else {
399-
const target = $findBlockByBlockId(op.position.blockId)
400-
if (!target) continue
401-
if (op.position.type === 'after') target.insertAfter(newNode)
402-
else target.insertBefore(newNode)
400+
const anchorKey = `${op.position.type}:${op.position.blockId}`
401+
const prev = lastInserted.get(anchorKey)
402+
if (prev) {
403+
prev.insertAfter(newNode)
404+
} else {
405+
const target = $findBlockByBlockId(op.position.blockId)
406+
if (!target) continue
407+
if (op.position.type === 'after') target.insertAfter(newNode)
408+
else target.insertBefore(newNode)
409+
}
410+
lastInserted.set(anchorKey, newNode)
403411
}
404412
} else if (op.op === 'replace') {
405413
if (!op.node?.type) continue

0 commit comments

Comments
 (0)