Skip to content

Commit cb17207

Browse files
authored
fix(rich-md-editor): stop the editor flashing during an agent rewrite (#5160)
* fix(rich-md-editor): stop the editor flashing during an agent rewrite Only reveal streamed chunks that extend what's already shown. A divergent chunk (an agent rewrite/edit, e.g. removing appended text) would collapse the document to the partial result and flash on every chunk; now the current content is held in place and the final result is applied once on settle. Fresh writes still reveal live. * fix(rich-markdown-editor): seed shown-body on settled mount and track local edits Seed lastSyncedBodyRef from a settled (non-streaming) mount and update it on local edits via onUpdate, so the streaming hold engages on the very first agent rewrite chunk (no collapse/flash) and the settle still applies the rewrite that removes a local edit.
1 parent 39cfae9 commit cb17207

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ export function LoadedRichMarkdownEditor({
158158
const [initialContent] = useState<JSONContent | string>(() =>
159159
streamingAtMountRef.current ? '' : parseMarkdownToDoc(splitFrontmatter(content).body)
160160
)
161+
/**
162+
* The body currently shown in the editor: seeded from a settled mount, updated on local edits (via
163+
* onUpdate) and on each streamed sync. The streaming tick reveals a chunk only when it extends this,
164+
* so an agent rewrite holds the current content instead of collapsing to a partial result.
165+
*/
166+
const lastSyncedBodyRef = useRef<string | null>(
167+
streamingAtMountRef.current ? null : splitFrontmatter(content).body
168+
)
161169
const onChangeRef = useRef(onChange)
162170
onChangeRef.current = onChange
163171
const onSaveShortcutRef = useRef(onSaveShortcut)
@@ -263,13 +271,12 @@ export function LoadedRichMarkdownEditor({
263271
},
264272
onUpdate: ({ editor }) => {
265273
const md = postProcessSerializedMarkdown(editor.getMarkdown())
274+
lastSyncedBodyRef.current = md
266275
onChangeRef.current(applyFrontmatter(settledRef.current?.frontmatter ?? '', md))
267276
},
268277
})
269278
editorInstanceRef.current = editor
270279

271-
const lastSyncedBodyRef = useRef<string | null>(null)
272-
273280
const wasStreamingRef = useRef(streamingAtMountRef.current)
274281

275282
const pendingStreamBodyRef = useRef<string | null>(null)
@@ -291,6 +298,12 @@ export function LoadedRichMarkdownEditor({
291298
streamRafRef.current = null
292299
return
293300
}
301+
const shownBody = lastSyncedBodyRef.current
302+
const extendsShown = shownBody === null || pending.startsWith(shownBody)
303+
if (!extendsShown) {
304+
streamRafRef.current = null
305+
return
306+
}
294307
if (
295308
pending.length > STREAM_REPARSE_THROTTLE_THRESHOLD &&
296309
performance.now() - lastStreamParseAtRef.current < STREAM_REPARSE_THROTTLE_MS
@@ -303,7 +316,7 @@ export function LoadedRichMarkdownEditor({
303316
lastStreamParseAtRef.current = performance.now()
304317
const el = containerRef.current
305318
const pinnedToBottom = el ? el.scrollHeight - el.scrollTop - el.clientHeight < 80 : false
306-
editor.setEditable(false)
319+
if (editor.isEditable) editor.setEditable(false)
307320
editor.commands.setContent(parseMarkdownToDoc(pending), {
308321
contentType: 'json',
309322
emitUpdate: false,

0 commit comments

Comments
 (0)