-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathEditor.tsx
More file actions
64 lines (58 loc) · 2 KB
/
Editor.tsx
File metadata and controls
64 lines (58 loc) · 2 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
import { useRef } from "react"
import MonacoEditor, { useMonaco, OnMount } from "@monaco-editor/react"
import { editor } from "monaco-editor/esm/vs/editor/editor.api"
type EditorProps = {
value?: string
language?: string
options: any
onChange?(value: string | undefined): void
height?: number | string
}
export default function Editor(props: EditorProps) {
const monaco = useMonaco()
const valueGetter = useRef<() => string>(undefined)
// requires re-render. does not sync with localStorage
// the storage event is not triggered on the window where the change is made
const dark = localStorage.getItem('dark-theme') === 'true';
const onMount: OnMount = (editor: editor.IStandaloneCodeEditor) => {
valueGetter.current = () => editor.getValue()
if (!monaco) {
return
}
editor.addAction({
id: "my-unique-id",
label: "my label",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
precondition: undefined,
keybindingContext: undefined,
contextMenuGroupId: "navigation",
contextMenuOrder: 1.5,
run: ed => {
console.log("Ctrl+S => " + ed.getPosition())
},
})
}
return (
<MonacoEditor
value={props.value}
language={props.language || "json"}
theme={dark ? 'vs-dark' : 'light'}
options={{
//renderLineHighlight : 'none',
...props.options,
language: props.language || "json",
automaticLayout: true,
scrollBeyondLastLine: false,
stickyScroll: {
defaultModel: 'indentationModel',
enabled: true,
maxLineCount: 10,
scrollWithEditor: true,
}
}}
height={props.height}
onMount={onMount}
onChange={props.onChange}
/>
)
}