diff --git a/src/app/components/editor/utils.ts b/src/app/components/editor/utils.ts index cf3b5e410..20c9cd26c 100644 --- a/src/app/components/editor/utils.ts +++ b/src/app/components/editor/utils.ts @@ -1,4 +1,5 @@ import { BasePoint, BaseRange, Editor, Element, Point, Range, Text, Transforms } from 'slate'; +import { ReactEditor } from 'slate-react'; import { BlockType, MarkType } from './types'; import { CommandElement, @@ -201,8 +202,15 @@ export const replaceWithElement = (editor: Editor, selectRange: BaseRange, eleme }; export const moveCursor = (editor: Editor, withSpace?: boolean) => { - Transforms.move(editor); - if (withSpace) editor.insertText(' '); + // Defer to the next tick so React can flush any pending void-element DOM + // updates (e.g. after inserting a mention) before Slate resolves cursor + // positions via ReactEditor.toDOMNode — otherwise Slate throws + // "Cannot resolve a DOM node from slate node". + setTimeout(() => { + ReactEditor.focus(editor); + Transforms.move(editor); + if (withSpace) editor.insertText(' '); + }, 0); }; interface PointUntilCharOptions {