Skip to content
Open
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
23 changes: 20 additions & 3 deletions packages/studio/src/components/editor/SourceEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useCallback, memo } from "react";
import { useRef, useCallback, useEffect, memo } from "react";
import {
EditorView,
keymap,
Expand Down Expand Up @@ -69,6 +69,9 @@ export const SourceEditor = memo(function SourceEditor({
const onChangeRef = useRef(onChange);
onChangeRef.current = onChange;

const contentRef = useRef(content);
contentRef.current = content;

const mountEditor = useCallback(
(node: HTMLDivElement | null) => {
if (editorRef.current) {
Expand All @@ -87,7 +90,7 @@ export const SourceEditor = memo(function SourceEditor({
});

const state = EditorState.create({
doc: content,
doc: contentRef.current,
extensions: [
lineNumbers(),
highlightActiveLine(),
Expand All @@ -112,8 +115,22 @@ export const SourceEditor = memo(function SourceEditor({

editorRef.current = new EditorView({ state, parent: node });
},
[content, filePath, language, readOnly],
[filePath, language, readOnly],
);

// Sync external content changes into the editor without recreating it.
// Only applies when the new content differs from the current document
// (e.g. file switch or server refresh), not on every keystroke.
useEffect(() => {
const view = editorRef.current;
if (!view) return;
const current = view.state.doc.toString();
if (current !== content) {
view.dispatch({
changes: { from: 0, to: current.length, insert: content },
});
}
}, [content]);

return <div ref={mountEditor} className="h-full w-full overflow-hidden" />;
});
Loading