Skip to content

Commit f94873c

Browse files
committed
codemirror
1 parent 9aced34 commit f94873c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

web/apps/playground/src/components/EditorPanel/EditorPanel.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,44 @@ export const EditorPanel = ({ editorWidth }: { editorWidth: number }) => {
2121
const startHeight = useRef(0);
2222
const containerRef = useRef<HTMLDivElement>(null);
2323

24+
// Local state for immediate editor updates (no lag while typing)
25+
const [localValue, setLocalValue] = useState(config);
26+
27+
// Ref for debounce timer
28+
const debounceTimer = useRef<NodeJS.Timeout | null>(null);
29+
30+
// Sync local value when config changes from external source
31+
useEffect(() => {
32+
setLocalValue(config);
33+
}, [config]);
34+
35+
// Cleanup debounce timer on unmount
36+
useEffect(() => {
37+
return () => {
38+
if (debounceTimer.current) {
39+
clearTimeout(debounceTimer.current);
40+
}
41+
};
42+
}, []);
43+
44+
// Handler for editor changes - updates local state immediately and debounces actual config update
45+
const handleEditorChange = useCallback(
46+
(_editor: any, _data: any, value: string) => {
47+
setLocalValue(value); // Update immediately for responsive typing
48+
49+
// Clear previous timer
50+
if (debounceTimer.current) {
51+
clearTimeout(debounceTimer.current);
52+
}
53+
54+
// Set new timer to update config after 300ms of no typing
55+
debounceTimer.current = setTimeout(() => {
56+
setConfig(value);
57+
}, 300);
58+
},
59+
[setConfig]
60+
);
61+
2462
// Drag logic for vertical resize
2563
const handleMouseDown = useCallback(
2664
(e: MouseEvent) => {
@@ -81,8 +119,8 @@ export const EditorPanel = ({ editorWidth }: { editorWidth: number }) => {
81119
<div className="flex-1 min-h-0">
82120
<CodeEditor
83121
ref={editorRef}
84-
value={config}
85-
onBeforeChange={(_editor: any, _data: any, value: string) => setConfig(value)}
122+
value={localValue}
123+
onBeforeChange={handleEditorChange}
86124
border={false}
87125
controlled
88126
// @ts-ignore

web/apps/playground/src/utils/codeEditor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ export const editorOptions = {
3535
"Ctrl-Space": "autocomplete",
3636
},
3737
hintOptions: { schemaInfo: tags },
38+
// Performance optimizations for large XML documents
39+
viewportMargin: 10, // Only render visible lines + 10 line buffer (HUGE performance boost!)
40+
lineWrapping: false, // Faster rendering without wrapping
41+
lineSeparator: "\n", // Faster line splitting
42+
undoDepth: 200, // Limit undo history (default is 200, but being explicit)
3843
};

0 commit comments

Comments
 (0)