-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
editorview.ts
217 lines (179 loc) · 7.21 KB
/
editorview.ts
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
import {EditorState, Transaction, MetaSlot} from "../../state/src"
import {DocView, EditorViewport} from "./docview"
import {InputState, MouseSelectionUpdate} from "./input"
import {getRoot, Rect} from "./dom"
import {Decoration, DecorationSet} from "./decoration"
import {applyDOMChange} from "./domchange"
import {movePos, posAtCoords} from "./cursor"
import {LineHeight} from "./heightmap"
export class EditorView {
private _state!: EditorState
get state(): EditorState { return this._state }
readonly dispatch: (tr: Transaction) => void
readonly dom: HTMLElement
readonly contentDOM: HTMLElement
// @internal
inputState!: InputState
// @internal
readonly docView: DocView
readonly viewport: EditorViewport
private pluginViews: PluginView[] = []
private scheduledDecoUpdate: number = -1
private updatingState: boolean = false
constructor(state: EditorState, dispatch?: ((tr: Transaction) => void | null), ...plugins: PluginView[]) {
this.dispatch = dispatch || (tr => this.updateState([tr], tr.apply()))
this.contentDOM = document.createElement("pre")
this.contentDOM.className = "CodeMirror-content"
this.contentDOM.style.cssText = contentCSS
this.contentDOM.setAttribute("contenteditable", "true")
this.contentDOM.setAttribute("spellcheck", "false")
this.dom = document.createElement("div")
this.dom.style.cssText = editorCSS
this.dom.className = "CodeMirror"
this.dom.appendChild(this.contentDOM)
this.docView = new DocView(this.contentDOM, {
onDOMChange: (start, end, typeOver) => applyDOMChange(this, start, end, typeOver),
onUpdateState: (prevState: EditorState, transactions: Transaction[]) => {
for (let pluginView of this.pluginViews)
if (pluginView.updateState) pluginView.updateState(this, prevState, transactions)
},
onUpdateDOM: () => {
for (let plugin of this.pluginViews) if (plugin.updateDOM) plugin.updateDOM(this)
},
onUpdateViewport: () => {
for (let plugin of this.pluginViews) if (plugin.updateViewport) plugin.updateViewport(this)
},
getDecorations: () => this.pluginViews.map(v => v.decorations || Decoration.none)
})
this.viewport = this.docView.publicViewport
this.setState(state, ...plugins)
}
setState(state: EditorState, ...plugins: PluginView[]) {
this._state = state
this.withUpdating(() => {
setTabSize(this.contentDOM, state.tabSize)
this.createPluginViews(plugins)
this.inputState = new InputState(this)
this.docView.update(state)
})
}
updateState(transactions: Transaction[], state: EditorState) {
if (transactions.length && transactions[0].startState != this._state)
throw new RangeError("Trying to update state with a transaction that doesn't start from the current state.")
this.withUpdating(() => {
let prevState = this._state
this._state = state
if (transactions.some(tr => tr.getMeta(MetaSlot.changeTabSize) != undefined)) setTabSize(this.contentDOM, state.tabSize)
if (state.doc != prevState.doc || transactions.some(tr => tr.selectionSet && !tr.getMeta(MetaSlot.preserveGoalColumn)))
this.inputState.goalColumns.length = 0
this.docView.update(state, prevState, transactions, transactions.some(tr => tr.scrolledIntoView) ? state.selection.primary.head : -1)
this.inputState.update(transactions)
})
}
/** @internal */
someProp<N extends keyof PluginView, R>(propName: N, f: (value: NonNullable<PluginView[N]>) => R | undefined): R | undefined {
let value: R | undefined = undefined
for (let pluginView of this.pluginViews) {
let prop = pluginView[propName]
if (prop != null && (value = f(prop as NonNullable<PluginView[N]>)) != null) break
}
return value
}
/** @internal */
getProp<N extends keyof PluginView>(propName: N): PluginView[N] {
for (let pluginView of this.pluginViews) {
let prop = pluginView[propName]
if (prop != null) return prop
}
return undefined
}
private withUpdating(f: () => void) {
if (this.updatingState) throw new Error("Recursive calls of EditorView.updateState or EditorView.setState are not allowed")
this.updatingState = true
try { f() }
finally { this.updatingState = false }
}
private createPluginViews(plugins: PluginView[]) {
this.destroyPluginViews()
for (let plugin of plugins) this.pluginViews.push(plugin)
for (let plugin of this.state.plugins) if (plugin.view)
this.pluginViews.push(plugin.view(this))
}
private destroyPluginViews() {
for (let pluginView of this.pluginViews) if (pluginView.destroy)
pluginView.destroy()
this.pluginViews.length = 0
}
domAtPos(pos: number): {node: Node, offset: number} | null {
return this.docView.domFromPos(pos)
}
heightAtPos(pos: number, top: boolean): number {
this.docView.forceLayout()
return this.docView.heightAt(pos, top ? -1 : 1)
}
lineAtHeight(height: number): LineHeight {
this.docView.forceLayout()
return this.docView.lineAtHeight(height)
}
get contentHeight() {
return this.docView.heightMap.height + this.docView.paddingTop + this.docView.paddingBottom
}
movePos(start: number, direction: "forward" | "backward" | "left" | "right",
granularity: "character" | "word" | "line" | "lineboundary" = "character",
action: "move" | "extend" = "move"): number {
return movePos(this, start, direction, granularity, action)
}
posAtCoords(coords: {x: number, y: number}): number {
this.docView.forceLayout()
return posAtCoords(this, coords)
}
coordsAtPos(pos: number): Rect | null { return this.docView.coordsAt(pos) }
get defaultCharacterWidth() { return this.docView.heightOracle.charWidth }
get defaultLineHeight() { return this.docView.heightOracle.lineHeight }
// To be used by plugin views when they update their decorations asynchronously
decorationUpdate() {
if (this.scheduledDecoUpdate < 0) this.scheduledDecoUpdate = requestAnimationFrame(() => {
this.scheduledDecoUpdate = -1
this.docView.update(this.state, this.state)
})
}
startMouseSelection(event: MouseEvent, update: MouseSelectionUpdate) {
this.focus()
this.inputState.startMouseSelection(this, event, update)
}
get root(): DocumentOrShadowRoot {
return getRoot(this.dom)
}
hasFocus(): boolean {
return getRoot(this.dom).activeElement == this.contentDOM
}
focus() {
this.docView.focus()
}
destroy() {
this.destroyPluginViews()
this.inputState.destroy()
this.dom.remove()
this.docView.destroy()
}
}
export interface PluginView {
updateState?: (view: EditorView, prevState: EditorState, transactions: Transaction[]) => void
updateDOM?: (view: EditorView) => void
updateViewport?: (view: EditorView) => void
handleDOMEvents?: {[key: string]: (view: EditorView, event: Event) => boolean}
// This should return a stable value, not compute something on the fly
decorations?: DecorationSet
destroy?: () => void
}
function setTabSize(elt: HTMLElement, size: number) {
(elt.style as any).tabSize = (elt.style as any).MozTabSize = size
}
const editorCSS = `
position: relative;
display: flex;
align-items: flex-start;`
const contentCSS = `
margin: 0;
flex-grow: 2;
min-height: 100%;`