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