-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.tsx
269 lines (228 loc) · 8.38 KB
/
Code.tsx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// CoverageVisualizer is not only a coverage visualizer, the coverage feature is opt-in.
// you can also disable it to get a code viewer.
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import { CSSProperties, MutableRefObject, useEffect, useRef, useState } from "react";
import { FileDetailGetter, ITreeNode } from "../../support/file";
import { BiExpandAlt } from "react-icons/bi"
import { CurrentRef, useCurrent } from "../../../mock-editor/react-hooks";
import { ContentDecorator, normalizeCodeContent, useMonacoModel } from "./model";
import "./code.css"
// import "../../monaco-tree/monaco-editor-v0.20.0/esm/vs/base/browser/ui/codiconLabel/codicon/codicon.css"
// import "monaco-editor/esm/vs/base/browser/ui/codicons/codicon/codicon.css"
// import loaders just not working
// import "./fix-icons.css"
// however import tff via ts works
import "./load-monaco-tff"
export interface RenderTarget {
monacoIconLabel: { title: string }
label: HTMLElement
}
export interface RenderFile {
path: string
name: string
}
// export interface PathDecorator {
// // optional
// renderPath?: (target: RenderTarget, file: RenderFile) => Promise<void>
// }
export interface CodeFileTree {
getRoot(): Promise<ITreeNode>
// optional refresh
refresh?: () => Promise<void>
}
export interface Control {
notifyFileChanged: () => Promise<void>
updateContent: () => Promise<void>
updateOldContent: () => Promise<void>
setContent: (content: string) => void
}
export function useCodeControl(): MutableRefObject<Control> {
return useRef<Control>()
}
export interface IProps {
file?: string
// conent
fileDetailGetter?: FileDetailGetter
contentDecorator?: ContentDecorator
// deprecated use controlRef
control?: Control
controlRef?: MutableRefObject<Control>
containerStyle?: CSSProperties
containerClassName?: string
readonly?: boolean
editorRef?: MutableRefObject<monaco.editor.IStandaloneCodeEditor>
onEditorCreated?: (editor: monaco.editor.IStandaloneCodeEditor) => void
onContentChange?: (content: string) => void
showExpandIcon?: boolean // true=>show expand,false=> show collapse, undefined don't show
onClickExpand?: () => void
top?: any // element on top
initContent?: string
}
// a global map: editor => config
// if one is put on top, its value gets applied
// editor does not interfere with others
// put who on top
function setEditorDiagnose(editor: monaco.editor.IStandaloneCodeEditor,) {
}
function setOnTop(editor: monaco.editor.IStandaloneCodeEditor) {
}
// Code will automatically resize based on container dynamic size:
// a typical usage is to use Code inside a flex-column container, where declare flexBasis as 50%, then set
// flex-shrink and flex-grow to 1, and set Code's height to 100% to flow the flex container:
//
// <Code containerStyle={{ flexGrow: 1, flexShrink: 1, flexBasis: "50%" }} style={{ height: "100%" }}/>
// <Code containerStyle={{ flexGrow: 1, flexShrink: 1, flexBasis: "50%" }} style={{ height: "100%" }}/>
// Code as a central registry
export default function Code(props: IProps) {
const modelRefreshRef = useRef<() => any>()
const [editor, setEditor] = useState(null as monaco.editor.IStandaloneCodeEditor)
const [version, setVersion] = useState(0)
const editorRef = useCurrent(editor)
const model = useMonacoModel({
editor: editor,
uriPrefix: "code/",
file: props.file,
fileDetailGetter: props.fileDetailGetter,
contentDecorator: props.contentDecorator,
refreshDecorations: modelRefreshRef,
readonly: props.readonly,
})
// for debug
// useEffect(() => {
// console.log("file change:", props.file)
// }, [props.file])
const versionRef = useCurrent(version)
useEffect(() => {
// debug
// console.log("may file update:", props.file)
setVersion(versionRef.current + 1)
}, [props.file, model])
// apply model
useEffect(() => {
// console.log("DEBUG version change:", version, model?.fileKey, model, editorRef.current)
if (model && model.model && !model.model.isDisposed()) {
editorRef.current.setModel?.(model.model)
// apply diff model
// debug
// console.log("refresh models:", version)
modelRefreshRef?.current?.()
// don't set null, we want remain when loading
// return () => diffEditor?.setModel?.(null)
}
// comment the following code,remain still
// else {
// diffEditor?.setModel?.(null)
// }
}, [version])
if (props.control) {
// props.control.updateContent = updateContent
// props.control.updateOldContent = updateOldContent
props.control.setContent = (content) => {
editor.getModel().setValue(normalizeCodeContent(content))
}
}
if (props.controlRef) {
props.controlRef.current = {
setContent: (content) => {
const c = normalizeCodeContent(content)
editor.setValue(c)
// setTimeout(() => editor.setValue(c), 20)
// editor.getModel().setValue(normalizeCodeContent(content))
}
} as Control
}
const containerRef = useRef<HTMLDivElement>()
const onContentChangeRef = useCurrent(props.onContentChange)
const onEditorCreatedRef = useCurrent(props.onEditorCreated)
// create monaco on mount
useEffect(() => {
const editor = createAutoLayoutEditor(containerRef.current, props.initContent, {
onContentChangeRef: onContentChangeRef,
editorRef: props.editorRef,
onEditorCreatedRef: onEditorCreatedRef,
})
setEditor(editor)
setupEditorAutoLayout(editor, () => {
if (props.editorRef != null) {
props.editorRef.current = undefined
}
})
}, [])
return <div
// id="debug" // NOTE: the editor will auto layout based on parent height
style={{
position: "relative",
...props.containerStyle,
// height: undefined,
// width: "500px",
}} className={`code-container ${props.containerClassName || ""}`} >
{
props.showExpandIcon && <div className="code-container-zoom">
<BiExpandAlt style={{ cursor: "pointer" }} onClick={() => {
props.onClickExpand?.()
}} />
</div>
}
{
props.top
}
<div
ref={containerRef}
className="code-container"
style={{
width: "100%",
height: "100%",
overflow: "hidden",
}}
/>
</ div>
}
function createAutoLayoutEditor(containerEl: HTMLElement, value: string | undefined, opts?: {
onContentChangeRef?: CurrentRef<(value: string) => void>
editorRef?: MutableRefObject<monaco.editor.IStandaloneCodeEditor>
onEditorCreatedRef?: MutableRefObject<(editor: monaco.editor.IStandaloneCodeEditor) => void>
}): monaco.editor.IStandaloneCodeEditor {
const editor = monaco.editor.create(
containerEl,
{
readOnly: false,
scrollbar: {
// see this, make scroll propagate to
// parent possible:
// https://github.com/microsoft/monaco-editor/issues/1853#issuecomment-593484147
alwaysConsumeMouseWheel: false,
},
automaticLayout: true,
}
);
if (value !== undefined) {
editor.setValue(value)
}
editor.onDidChangeModelContent(() => {
if (opts?.onContentChangeRef?.current != null) {
opts.onContentChangeRef.current?.(editor.getValue())
}
})
editor.layout()
if (opts?.editorRef != null) {
opts.editorRef.current = editor
}
if (opts?.onEditorCreatedRef?.current != null) {
opts.onEditorCreatedRef.current?.(editor)
}
return editor
}
function setupEditorAutoLayout(editor: monaco.editor.IStandaloneCodeEditor, onDispose: () => void) {
const handler = (e) => {
editor.layout()
}
// resize
window.addEventListener('resize', handler);
return () => {
window.removeEventListener('resize', handler)
editor.dispose()
if (onDispose != null) {
onDispose()
}
}
}