Skip to content

Commit edcc20f

Browse files
committed
fix(files): keep the reader's caret when the markdown editor settles
1 parent 9964728 commit edcc20f

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,15 @@ export function LoadedRichMarkdownEditor({
13501350
// load-bearing and runs on every settle even when the body is unchanged — setContent maps a
13511351
// pre-existing selection onto the new doc, so a prior select-all survives as "select everything",
13521352
// permanently painting every divider/image with the rich-leaf-in-selection decoration (keymap.ts)
1353-
// until the user clicks away. setTextSelection (not .focus()) never steals DOM focus.
1353+
// until the user clicks away. setTextSelection (not .focus()) never steals DOM focus. A caret is
1354+
// already collapsed: moving it to the end would only take away where the reader just clicked,
1355+
// since an editable editor can take a click before its first settle lands.
13541356
runOffRender(() => {
13551357
syncEditorContent(content)
13561358
pendingCollapseRef.current = false
1357-
editor.commands.setTextSelection(editor.state.doc.content.size)
1359+
if (!editor.state.selection.empty) {
1360+
editor.commands.setTextSelection(editor.state.doc.content.size)
1361+
}
13581362
editor.setEditable(effectiveCanEdit && settledVerdict && collabReady)
13591363
if (shouldFocus) editor.commands.focus('end')
13601364
})
@@ -1366,7 +1370,9 @@ export function LoadedRichMarkdownEditor({
13661370
// run bumped the token), so a post-stream select-all can't keep the leaf-in-selection decoration.
13671371
if (pendingCollapseRef.current) {
13681372
pendingCollapseRef.current = false
1369-
editor.commands.setTextSelection(editor.state.doc.content.size)
1373+
if (!editor.state.selection.empty) {
1374+
editor.commands.setTextSelection(editor.state.doc.content.size)
1375+
}
13701376
}
13711377
if (settled) editor.setEditable(effectiveCanEdit && settled.verdict && collabReady)
13721378
})

0 commit comments

Comments
 (0)