diff --git a/src-web/components/core/Editor/Editor.tsx b/src-web/components/core/Editor/Editor.tsx index f64b6a6a..8df9437b 100644 --- a/src-web/components/core/Editor/Editor.tsx +++ b/src-web/components/core/Editor/Editor.tsx @@ -8,12 +8,12 @@ import classNames from 'classnames'; import { EditorView } from 'codemirror'; import type { MutableRefObject, ReactNode } from 'react'; import { + useEffect, Children, cloneElement, forwardRef, isValidElement, useCallback, - useEffect, useImperativeHandle, useMemo, useRef, @@ -343,6 +343,33 @@ export const Editor = forwardRef(function E [forceUpdateKey], ); + // For read-only mode, update content when `defaultValue` changes + useEffect(() => { + if (!readOnly || cm.current?.view == null || defaultValue == null) return; + + // Replace codemirror contents + const currentDoc = cm.current.view.state.doc.toString(); + if (defaultValue.startsWith(currentDoc)) { + // If we're just appending, append only the changes. This preserves + // things like scroll position. + cm.current.view.dispatch({ + changes: cm.current.view.state.changes({ + from: currentDoc.length, + insert: defaultValue.slice(currentDoc.length), + }), + }); + } else { + // If we're replacing everything, reset the entire content + cm.current.view.dispatch({ + changes: cm.current.view.state.changes({ + from: 0, + to: currentDoc.length, + insert: currentDoc, + }), + }); + } + }, [defaultValue, readOnly]); + // Add bg classes to actions, so they appear over the text const decoratedActions = useMemo(() => { const results = []; diff --git a/src-web/components/responseViewers/EventStreamViewer.tsx b/src-web/components/responseViewers/EventStreamViewer.tsx index cb626cf9..88f7a751 100644 --- a/src-web/components/responseViewers/EventStreamViewer.tsx +++ b/src-web/components/responseViewers/EventStreamViewer.tsx @@ -90,7 +90,6 @@ function ActualEventStreamViewer({ response }: Props) { ) : ( diff --git a/src-web/components/responseViewers/TextViewer.tsx b/src-web/components/responseViewers/TextViewer.tsx index e69c2008..79f54898 100644 --- a/src-web/components/responseViewers/TextViewer.tsx +++ b/src-web/components/responseViewers/TextViewer.tsx @@ -160,7 +160,6 @@ export function TextViewer({