-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathEditorContentContext.tsx
156 lines (131 loc) · 4 KB
/
EditorContentContext.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
import { loadWASM } from 'onigasm'
import {
ReactNode,
createContext,
useCallback,
useEffect,
useRef,
useState,
} from 'react'
import { EditorProps, Monaco, OnMount } from '@monaco-editor/react'
import { emmetHTML } from 'emmet-monaco-es'
import { KeyCode, KeyMod, editor } from 'monaco-editor'
import { wireTmGrammars } from 'monaco-editor-textmate'
import monacoOmniTheme from '../assets/monaco-themes/monaco-omni.json'
import { doesURLIncludesGist, getGist, isGistViewOnly } from '../utils/Gist'
import Storage, { StorageKeys, StorageState } from '../utils/Storage'
import { registry } from '../utils/monaco-tm-registry'
import {
doesURLIncludesGithub,
getGithub,
isGithubViewOnly,
} from '@/utils/Github'
import onigasmWASM from '../assets/onigasm.wasm?url'
interface EditorContextProviderProps {
children: ReactNode
}
interface EditorContentContextData {
app: StorageState
isEditorReady: boolean
handleEditorDidMount: EditorProps['onMount']
handleValueChange: (language: string, value: string) => void
}
export const EditorContentContext = createContext(
{} as EditorContentContextData
)
export const editorHotkeys = new EventTarget()
export const saveEvent = new CustomEvent('save')
export function EditorContentContextProvider({
children,
}: EditorContextProviderProps) {
const monacoRef = useRef<Monaco>()
const editorRef = useRef<editor.IStandaloneCodeEditor>()
const [app, setApp] = useState<Record<StorageKeys, string>>(Storage.get())
const [isEditorReady, setIsEditorReady] = useState(false)
useEffect(() => {
if (!doesURLIncludesGist) {
return
}
const currentApp = Storage.get()
const keys = Object.keys(currentApp) as StorageKeys[]
const hasDataInStorage = keys.some(key => currentApp[key] !== '')
if (isGistViewOnly || !hasDataInStorage) {
getGist().then(gist => {
setApp(gist)
Storage.add(gist)
})
}
}, [])
useEffect(() => {
if (!doesURLIncludesGithub) return
const currentApp = Storage.get()
const keys = Object.keys(currentApp) as StorageKeys[]
const hasDataInStorage = keys.some(key => currentApp[key] !== '')
if (isGithubViewOnly || !hasDataInStorage) {
getGithub().then(github => {
if (!github) return
setApp(github)
Storage.add(github)
})
}
}, [])
const handleValueChange = useCallback(
async (language: string, value: string) => {
setApp(oldState => {
const updatedValues = { ...oldState, [language]: value }
Storage.add(updatedValues)
return updatedValues
})
},
[]
)
const loadTmGrammars = useCallback(async () => {
await loadWASM(onigasmWASM)
const grammars = new Map()
grammars.set('css', 'source.css')
grammars.set('html', 'source.html')
grammars.set('javascript', 'source.js')
grammars.set('markdown', 'source.md')
monacoRef.current?.languages.register({ id: 'css' })
monacoRef.current?.languages.register({ id: 'html' })
monacoRef.current?.languages.register({ id: 'javascript' })
monacoRef.current?.languages.register({ id: 'markdown' })
await wireTmGrammars(
monacoRef.current!,
registry,
grammars,
editorRef.current
)
}, [])
const handleEditorDidMount = useCallback<OnMount>(
(editor, monaco) => {
editor.addCommand(KeyMod.CtrlCmd | KeyCode.KeyS, () => {
editorHotkeys.dispatchEvent(saveEvent)
})
editorRef.current = editor
monacoRef.current = monaco
monacoRef.current.editor.defineTheme(
'custom-theme',
monacoOmniTheme as editor.IStandaloneThemeData
)
loadTmGrammars().then(() => {
monacoRef.current?.editor.setTheme('custom-theme')
emmetHTML()
setIsEditorReady(true)
})
},
[loadTmGrammars]
)
return (
<EditorContentContext.Provider
value={{
app,
isEditorReady,
handleEditorDidMount,
handleValueChange,
}}
>
{children}
</EditorContentContext.Provider>
)
}