1
1
'use client' ;
2
2
3
- import { useExtensionWithDependency } from "@/hooks/useExtensionWithDependency" ;
4
- import { syntaxHighlighting as syntaxHighlightingExtension , defaultHighlightStyle } from "@codemirror/language" ;
5
- import { useSyntaxHighlightingExtension } from "@/hooks/useSyntaxHighlightingExtension" ;
6
- import { useThemeNormalized } from "@/hooks/useThemeNormalized" ;
7
- import { lineOffsetExtension } from "@/lib/extensions/lineOffsetExtension" ;
8
3
import { SearchResultRange } from "@/lib/types" ;
9
- import { javascript } from "@codemirror/lang-javascript" ;
10
- import CodeMirror , { Decoration , DecorationSet , EditorState , EditorView , lineNumbers , ReactCodeMirrorRef , StateField , Transaction , useCodeMirror } from "@uiw/react-codemirror" ;
11
- import { basicLight , basicDark } from '@uiw/codemirror-theme-basic' ;
12
- import { useEffect , useMemo , useRef , useState } from "react" ;
4
+ import { EditorState , StateField , Transaction } from "@codemirror/state" ;
5
+ import { Decoration , DecorationSet , EditorView , lineNumbers } from "@codemirror/view" ;
6
+ import { defaultHighlightStyle , syntaxHighlighting } from "@codemirror/language" ;
7
+ import { useEffect , useRef } from "react" ;
8
+ import { lineOffsetExtension } from "@/lib/extensions/lineOffsetExtension" ;
9
+ import { getSyntaxHighlightingExtension } from "@/hooks/useSyntaxHighlightingExtension" ;
10
+ import { useThemeNormalized } from "@/hooks/useThemeNormalized" ;
13
11
14
12
const markDecoration = Decoration . mark ( {
15
13
class : "cm-searchMatch-selected"
16
14
} ) ;
17
15
16
+
18
17
interface CodePreviewProps {
19
18
content : string ,
20
19
language : string ,
@@ -29,151 +28,78 @@ export const CodePreview = ({
29
28
lineOffset,
30
29
} : CodePreviewProps ) => {
31
30
32
- // const editor = useRef<HTMLDivElement | null>(null);
33
- // const { setContainer } = useCodeMirror({
34
- // container: editor.current,
35
- // extensions,
36
- // value: content,
37
- // theme: "dark"
38
- // });
39
-
40
- // useEffect(() => {
41
- // if (editor.current) {
42
- // setContainer(editor.current);
43
- // }
44
- // }, [editor.current]);
45
-
46
- // return <div ref={editor} />;
47
-
48
- // const editorRef = useRef<ReactCodeMirrorRef>(null);
49
- // const { theme } = useThemeNormalized();
50
-
51
-
52
- // const rangeHighlighting = useExtensionWithDependency(editorRef.current?.view ?? null, () => {
53
- // return [
54
- // StateField.define<DecorationSet>({
55
- // create(editorState: EditorState) {
56
- // const document = editorState.doc;
57
-
58
- // const decorations = ranges
59
- // .sort((a, b) => {
60
- // return a.Start.ByteOffset - b.Start.ByteOffset;
61
- // })
62
- // .filter(({ Start, End }) => {
63
- // const startLine = Start.LineNumber - lineOffset;
64
- // const endLine = End.LineNumber - lineOffset;
65
-
66
- // if (
67
- // startLine < 1 ||
68
- // endLine < 1 ||
69
- // startLine > document.lines ||
70
- // endLine > document.lines
71
- // ) {
72
- // return false;
73
- // }
74
- // return true;
75
- // })
76
- // .map(({ Start, End }) => {
77
- // const startLine = Start.LineNumber - lineOffset;
78
- // const endLine = End.LineNumber - lineOffset;
79
-
80
- // const from = document.line(startLine).from + Start.Column - 1;
81
- // const to = document.line(endLine).from + End.Column - 1;
82
- // return markDecoration.range(from, to);
83
- // });
84
-
85
- // return Decoration.set(decorations);
86
- // },
87
- // update(highlights: DecorationSet, _transaction: Transaction) {
88
- // return highlights;
89
- // },
90
- // provide: (field) => EditorView.decorations.from(field),
91
- // }),
92
- // ];
93
- // }, [ranges, lineOffset]);
94
-
95
- // const extensions = useMemo(() => {
96
- // return [
97
- // syntaxHighlighting,
98
- // // EditorView.lineWrapping,
99
- // lineOffsetExtension(lineOffset),
100
- // rangeHighlighting,
101
- // ];
102
- // }, [syntaxHighlighting, lineOffset, rangeHighlighting]);
103
-
104
-
105
- const editorRef2 = useRef < HTMLDivElement > ( null ) ;
106
- // const syntaxHighlighting = useSyntaxHighlightingExtension(language, editorRef2.current?.view);
31
+ const containerRef = useRef < HTMLDivElement | null > ( null ) ;
32
+ const { theme } = useThemeNormalized ( ) ;
107
33
108
34
useEffect ( ( ) => {
35
+ if ( ! containerRef . current ) {
36
+ return ;
37
+ }
38
+
109
39
const state = EditorState . create ( {
110
40
doc : content ,
111
41
extensions : [
112
42
EditorView . editable . of ( false ) ,
113
- // EditorView.lineWrapping,
114
- basicDark ,
115
- syntaxHighlightingExtension ( defaultHighlightStyle ) ,
116
- javascript ( ) ,
117
43
lineNumbers ( ) ,
118
44
lineOffsetExtension ( lineOffset ) ,
119
- // rangeHighlighting,
45
+ syntaxHighlighting ( defaultHighlightStyle ) ,
46
+ getSyntaxHighlightingExtension ( language ) ,
47
+ StateField . define < DecorationSet > ( {
48
+ create ( editorState : EditorState ) {
49
+ const document = editorState . doc ;
50
+
51
+ const decorations = ranges
52
+ . sort ( ( a , b ) => {
53
+ return a . Start . ByteOffset - b . Start . ByteOffset ;
54
+ } )
55
+ . filter ( ( { Start, End } ) => {
56
+ const startLine = Start . LineNumber - lineOffset ;
57
+ const endLine = End . LineNumber - lineOffset ;
58
+
59
+ if (
60
+ startLine < 1 ||
61
+ endLine < 1 ||
62
+ startLine > document . lines ||
63
+ endLine > document . lines
64
+ ) {
65
+ return false ;
66
+ }
67
+ return true ;
68
+ } )
69
+ . map ( ( { Start, End } ) => {
70
+ const startLine = Start . LineNumber - lineOffset ;
71
+ const endLine = End . LineNumber - lineOffset ;
72
+
73
+ const from = document . line ( startLine ) . from + Start . Column - 1 ;
74
+ const to = document . line ( endLine ) . from + End . Column - 1 ;
75
+ return markDecoration . range ( from , to ) ;
76
+ } ) ;
77
+
78
+ return Decoration . set ( decorations ) ;
79
+ } ,
80
+ update ( highlights : DecorationSet , _transaction : Transaction ) {
81
+ return highlights ;
82
+ } ,
83
+ provide : ( field ) => EditorView . decorations . from ( field ) ,
84
+ } ) ,
85
+ getSyntaxHighlightingExtension ( language ) ,
120
86
] ,
121
87
} ) ;
122
88
123
89
const view = new EditorView ( {
124
90
state,
125
- parent : editorRef2 . current ! ,
91
+ parent : containerRef . current ,
126
92
} ) ;
127
93
128
94
return ( ) => {
129
95
view . destroy ( ) ;
130
96
}
131
- } , [ lineOffset ] ) ;
97
+ } , [ content , language , lineOffset , theme ] ) ;
132
98
133
99
return (
134
100
< div
135
- ref = { editorRef2 }
101
+ ref = { containerRef }
136
102
/>
137
103
)
138
104
139
- // return (
140
- // <CodeMirror
141
- // ref={editorRef}
142
- // readOnly={true}
143
- // editable={false}
144
- // value={content}
145
- // theme={theme === "dark" ? "dark" : "light"}
146
- // basicSetup={{
147
- // lineNumbers: true,
148
- // syntaxHighlighting: true,
149
-
150
- // // Disable all this other stuff...
151
- // ... {
152
- // foldGutter: false,
153
- // highlightActiveLineGutter: false,
154
- // highlightSpecialChars: false,
155
- // history: false,
156
- // drawSelection: false,
157
- // dropCursor: false,
158
- // allowMultipleSelections: false,
159
- // indentOnInput: false,
160
- // bracketMatching: false,
161
- // closeBrackets: false,
162
- // autocompletion: false,
163
- // rectangularSelection: false,
164
- // crosshairCursor: false,
165
- // highlightActiveLine: false,
166
- // highlightSelectionMatches: false,
167
- // closeBracketsKeymap: false,
168
- // defaultKeymap: false,
169
- // searchKeymap: false,
170
- // historyKeymap: false,
171
- // foldKeymap: false,
172
- // completionKeymap: false,
173
- // lintKeymap: false,
174
- // }
175
- // }}
176
- // extensions={extensions}
177
- // />
178
- // )
179
105
}
0 commit comments