Skip to content

Commit ef3ba48

Browse files
committed
codemirror dynamic conf
1 parent f94873c commit ef3ba48

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ export const EditorPanel = ({ editorWidth }: { editorWidth: number }) => {
4141
};
4242
}, []);
4343

44+
// Calculate number of lines in current document
45+
const lineCount = useMemo(() => {
46+
return localValue.split('\n').length;
47+
}, [localValue]);
48+
49+
// Dynamic options based on document size
50+
const dynamicEditorOptions = useMemo(() => {
51+
const isLargeDocument = lineCount > 50; // Threshold for "large" document
52+
const isVeryLargeDocument = lineCount > 200; // Very large threshold
53+
54+
return {
55+
...editorOptions,
56+
// Disable expensive features for large documents
57+
lineNumbers: !isLargeDocument, // Line numbers are VERY expensive!
58+
foldGutter: !isLargeDocument, // Code folding gutter
59+
gutters: isLargeDocument ? [] : ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
60+
61+
// More aggressive optimization for very large documents
62+
viewportMargin: isVeryLargeDocument ? 5 : 10, // Smaller buffer for huge docs
63+
theme: isVeryLargeDocument ? "default" : editorOptions.theme, // Simpler theme
64+
};
65+
}, [lineCount]);
66+
4467
// Handler for editor changes - updates local state immediately and debounces actual config update
4568
const handleEditorChange = useCallback(
4669
(_editor: any, _data: any, value: string) => {
@@ -116,7 +139,7 @@ export const EditorPanel = ({ editorWidth }: { editorWidth: number }) => {
116139
return (
117140
<div ref={containerRef} className="flex flex-col min-w-0 h-full" style={{ width: `${editorWidth}%` }}>
118141
{/* CodeEditor (top) */}
119-
<div className="flex-1 min-h-0">
142+
<div className="flex-1 min-h-0 relative">
120143
<CodeEditor
121144
ref={editorRef}
122145
value={localValue}
@@ -128,8 +151,14 @@ export const EditorPanel = ({ editorWidth }: { editorWidth: number }) => {
128151
smartIndent
129152
detach
130153
extensions={editorExtensions}
131-
options={editorOptions}
154+
options={dynamicEditorOptions}
132155
/>
156+
{/* Performance mode indicator */}
157+
{lineCount > 50 && (
158+
<div className="absolute top-2 right-2 bg-yellow-100 border border-yellow-400 text-yellow-800 px-2 py-1 rounded text-xs z-10">
159+
⚡ Performance mode ({lineCount} lines)
160+
</div>
161+
)}
133162
</div>
134163
{/* Divider for resizing (only when not collapsed) */}
135164
{!isCollapsed && (

0 commit comments

Comments
 (0)