-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathCodeMirrorEditor.tsx
More file actions
155 lines (137 loc) · 4.22 KB
/
CodeMirrorEditor.tsx
File metadata and controls
155 lines (137 loc) · 4.22 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
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
import React, { useCallback, useEffect, useRef } from 'react'
import CodeMirror from '../../editor/CodeMirror'
import { CodeMirrorBinding } from 'y-codemirror'
import { useEffectOnce } from 'react-use'
import { WebsocketProvider } from 'y-websocket'
import throttle from 'lodash.throttle'
import { UndoManager, YEvent } from 'yjs'
import { YText } from 'yjs/dist/src/internals'
interface EditorProps {
config?: CodeMirror.EditorConfiguration
bind?: (editor: CodeMirror.Editor) => void
realtime?: WebsocketProvider
lineScrollTarget?: number
onLineScroll?: (line: number) => void
onYTextChange?: (event: YEvent, ytext: YText) => void
}
const CodeMirrorEditor = ({
config = {},
realtime,
bind,
lineScrollTarget,
onLineScroll,
onYTextChange,
}: EditorProps) => {
const editorRootRef = useRef<HTMLDivElement>(null)
const editorRef = useRef<CodeMirror.Editor>()
const onScrollLineRef = useRef(onLineScroll)
const skipOnScrollRef = useRef(false)
const applySpellcheckToInput = useCallback((enabled: boolean) => {
if (editorRef.current == null) {
return
}
const inputField = (editorRef.current as any).getInputField?.()
if (inputField != null) {
inputField.setAttribute('spellcheck', enabled ? 'true' : 'false')
inputField.setAttribute('autocorrect', enabled ? 'on' : 'off')
inputField.setAttribute('autocapitalize', enabled ? 'sentences' : 'off')
}
}, [])
useEffect(() => {
onScrollLineRef.current = onLineScroll
}, [onLineScroll])
useEffectOnce(() => {
if (editorRootRef.current != null) {
editorRef.current = CodeMirror(editorRootRef.current, {
extraKeys: {
Enter: 'newlineAndIndentContinueMarkdownList',
Tab: 'indentMore',
},
...config,
})
editorRef.current.on('keydown', (cm, event) => {
if (cm.state.vim && cm.state.vim.insertMode) {
event.stopPropagation()
event.stopImmediatePropagation()
}
})
editorRef.current.on(
'scroll',
throttle(
(editor) => {
if (onScrollLineRef.current != null && !skipOnScrollRef.current) {
const rect = editor.getScrollInfo()
const line = editor.lineAtHeight(rect.top, 'local')
onScrollLineRef.current(line + 1)
}
skipOnScrollRef.current = false
},
10,
{ leading: true, trailing: true }
)
)
applySpellcheckToInput(Boolean(config.spellcheck))
}
})
useEffect(() => {
if (editorRef.current != null && bind != null) {
bind(editorRef.current)
}
}, [bind])
useEffect(() => {
if (editorRef.current != null) {
Object.entries(config).forEach(([key, val]) => {
editorRef.current!.setOption(
key as keyof CodeMirror.EditorConfiguration,
val
)
})
applySpellcheckToInput(Boolean(config.spellcheck))
}
}, [config, applySpellcheckToInput])
useEffect(() => {
if (realtime == null || editorRef.current == null) {
return undefined
}
const content = realtime.doc.getText('content')
const undoManager = new UndoManager(content)
const binding = new CodeMirrorBinding(
content,
editorRef.current,
realtime.awareness,
{
yUndoManager: undoManager,
}
)
let firstChange = true
const observer = (event: YEvent) => {
if (onYTextChange == null) {
return
}
// The first event is triggered for loading initial content. So it is not a change event actually.
if (firstChange) {
firstChange = false
return
}
onYTextChange(event, content)
}
content.observe(observer)
return () => {
content.unobserve(observer)
binding.destroy()
}
}, [realtime, onYTextChange])
useEffect(() => {
if (lineScrollTarget != null && editorRef.current != null) {
const height = editorRef.current.heightAtLine(
Math.max(0, lineScrollTarget - 1),
'local'
)
// animate
skipOnScrollRef.current = true
editorRef.current.scrollTo(0, height)
}
}, [lineScrollTarget])
return <div className='CodeMirrorWrapper' ref={editorRootRef} />
}
export default CodeMirrorEditor