Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export function LoadedRichMarkdownEditor({
const [initialContent] = useState<JSONContent | string>(() =>
streamingAtMountRef.current ? '' : parseMarkdownToDoc(splitFrontmatter(content).body)
)
/**
* The body currently shown in the editor: seeded from a settled mount, updated on local edits (via
* onUpdate) and on each streamed sync. The streaming tick reveals a chunk only when it extends this,
* so an agent rewrite holds the current content instead of collapsing to a partial result.
*/
const lastSyncedBodyRef = useRef<string | null>(
streamingAtMountRef.current ? null : splitFrontmatter(content).body
)
const onChangeRef = useRef(onChange)
onChangeRef.current = onChange
const onSaveShortcutRef = useRef(onSaveShortcut)
Expand Down Expand Up @@ -263,13 +271,12 @@ export function LoadedRichMarkdownEditor({
},
onUpdate: ({ editor }) => {
const md = postProcessSerializedMarkdown(editor.getMarkdown())
lastSyncedBodyRef.current = md
onChangeRef.current(applyFrontmatter(settledRef.current?.frontmatter ?? '', md))
},
})
editorInstanceRef.current = editor

const lastSyncedBodyRef = useRef<string | null>(null)

const wasStreamingRef = useRef(streamingAtMountRef.current)

const pendingStreamBodyRef = useRef<string | null>(null)
Expand All @@ -291,6 +298,12 @@ export function LoadedRichMarkdownEditor({
streamRafRef.current = null
return
}
const shownBody = lastSyncedBodyRef.current
const extendsShown = shownBody === null || pending.startsWith(shownBody)
if (!extendsShown) {
streamRafRef.current = null
return
}
if (
pending.length > STREAM_REPARSE_THROTTLE_THRESHOLD &&
performance.now() - lastStreamParseAtRef.current < STREAM_REPARSE_THROTTLE_MS
Expand All @@ -303,7 +316,7 @@ export function LoadedRichMarkdownEditor({
lastStreamParseAtRef.current = performance.now()
const el = containerRef.current
const pinnedToBottom = el ? el.scrollHeight - el.scrollTop - el.clientHeight < 80 : false
editor.setEditable(false)
if (editor.isEditable) editor.setEditable(false)
editor.commands.setContent(parseMarkdownToDoc(pending), {
contentType: 'json',
emitUpdate: false,
Expand Down
Loading