-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCodeMirrorViewer.tsx
More file actions
149 lines (130 loc) · 4.05 KB
/
Copy pathCodeMirrorViewer.tsx
File metadata and controls
149 lines (130 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { EditorState, type Extension, Compartment } from "@codemirror/state";
import {
EditorView,
lineNumbers,
highlightActiveLine,
highlightSpecialChars,
} from "@codemirror/view";
import {
syntaxHighlighting,
defaultHighlightStyle,
LanguageDescription,
} from "@codemirror/language";
import { oneDark } from "@codemirror/theme-one-dark";
import { memo, useEffect, useRef } from "react";
const themeCompartment = new Compartment();
const languageCompartment = new Compartment();
const baseExtensions: Extension[] = [
lineNumbers(),
highlightActiveLine(),
highlightSpecialChars(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
EditorView.editable.of(false),
EditorState.readOnly.of(true),
EditorView.theme({
"&": {
height: "100%",
fontSize: "12px",
},
".cm-scroller": {
fontFamily: "ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace",
overflow: "auto",
},
".cm-gutters": {
borderRight: "1px solid var(--border, #e5e7eb)",
backgroundColor: "transparent",
},
".cm-lineNumbers .cm-gutterElement": {
padding: "0 8px 0 12px",
minWidth: "3ch",
color: "var(--muted-foreground, #6b7280)",
opacity: "0.5",
fontSize: "11px",
},
}),
];
function getThemeExtension(resolvedTheme: "light" | "dark"): Extension {
return resolvedTheme === "dark" ? oneDark : [];
}
async function loadLanguageExtension(filePath: string): Promise<Extension> {
const languages = (await import("@codemirror/language-data")).languages;
const match = LanguageDescription.matchFilename(languages, filePath);
if (!match) return [];
const support = await match.load();
return support;
}
export const CodeMirrorViewer = memo(function CodeMirrorViewer(props: {
contents: string;
filePath: string;
resolvedTheme: "light" | "dark";
}) {
const containerRef = useRef<HTMLDivElement>(null);
const viewRef = useRef<EditorView | null>(null);
const filePathRef = useRef<string | null>(null);
// Create editor on mount
useEffect(() => {
if (!containerRef.current) return;
const state = EditorState.create({
doc: props.contents,
extensions: [
...baseExtensions,
themeCompartment.of(getThemeExtension(props.resolvedTheme)),
languageCompartment.of([]),
],
});
const view = new EditorView({
state,
parent: containerRef.current,
});
viewRef.current = view;
// Load language support asynchronously
void loadLanguageExtension(props.filePath).then((langExt) => {
if (viewRef.current === view) {
view.dispatch({
effects: languageCompartment.reconfigure(langExt),
});
}
});
filePathRef.current = props.filePath;
return () => {
view.destroy();
viewRef.current = null;
};
// Only re-create on mount/unmount — updates handled below
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Update contents when they change
useEffect(() => {
const view = viewRef.current;
if (!view) return;
const currentDoc = view.state.doc.toString();
if (currentDoc !== props.contents) {
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: props.contents },
});
}
}, [props.contents]);
// Update theme when it changes
useEffect(() => {
const view = viewRef.current;
if (!view) return;
view.dispatch({
effects: themeCompartment.reconfigure(getThemeExtension(props.resolvedTheme)),
});
}, [props.resolvedTheme]);
// Update language when file path changes
useEffect(() => {
if (filePathRef.current === props.filePath) return;
filePathRef.current = props.filePath;
const view = viewRef.current;
if (!view) return;
void loadLanguageExtension(props.filePath).then((langExt) => {
if (viewRef.current === view) {
view.dispatch({
effects: languageCompartment.reconfigure(langExt),
});
}
});
}, [props.filePath]);
return <div ref={containerRef} className="h-full min-h-0 overflow-hidden" />;
});