-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathy-monaco.js
222 lines (217 loc) · 8.46 KB
/
y-monaco.js
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
import * as Y from 'yjs'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'
import * as error from 'lib0/error'
import { createMutex } from 'lib0/mutex'
import { Awareness } from 'y-protocols/awareness' // eslint-disable-line
class RelativeSelection {
/**
* @param {Y.RelativePosition} start
* @param {Y.RelativePosition} end
* @param {monaco.SelectionDirection} direction
*/
constructor (start, end, direction) {
this.start = start
this.end = end
this.direction = direction
}
}
/**
* @param {monaco.editor.IStandaloneCodeEditor} editor
* @param {monaco.editor.ITextModel} monacoModel
* @param {Y.Text} type
*/
const createRelativeSelection = (editor, monacoModel, type) => {
const sel = editor.getSelection()
if (sel !== null) {
const startPos = sel.getStartPosition()
const endPos = sel.getEndPosition()
const start = Y.createRelativePositionFromTypeIndex(type, monacoModel.getOffsetAt(startPos))
const end = Y.createRelativePositionFromTypeIndex(type, monacoModel.getOffsetAt(endPos))
return new RelativeSelection(start, end, sel.getDirection())
}
return null
}
/**
* @param {monaco.editor.IEditor} editor
* @param {Y.Text} type
* @param {RelativeSelection} relSel
* @param {Y.Doc} doc
* @return {null|monaco.Selection}
*/
const createMonacoSelectionFromRelativeSelection = (editor, type, relSel, doc) => {
const start = Y.createAbsolutePositionFromRelativePosition(relSel.start, doc)
const end = Y.createAbsolutePositionFromRelativePosition(relSel.end, doc)
if (start !== null && end !== null && start.type === type && end.type === type) {
const model = /** @type {monaco.editor.ITextModel} */ (editor.getModel())
const startPos = model.getPositionAt(start.index)
const endPos = model.getPositionAt(end.index)
return monaco.Selection.createWithDirection(startPos.lineNumber, startPos.column, endPos.lineNumber, endPos.column, relSel.direction)
}
return null
}
export class MonacoBinding {
/**
* @param {Y.Text} ytext
* @param {monaco.editor.ITextModel} monacoModel
* @param {Set<monaco.editor.IStandaloneCodeEditor>} [editors]
* @param {Awareness?} [awareness]
*/
constructor (ytext, monacoModel, editors = new Set(), awareness = null) {
this.doc = /** @type {Y.Doc} */ (ytext.doc)
this.ytext = ytext
this.monacoModel = monacoModel
this.editors = editors
this.mux = createMutex()
/**
* @type {Map<monaco.editor.IStandaloneCodeEditor, RelativeSelection>}
*/
this._savedSelections = new Map()
this._beforeTransaction = () => {
this.mux(() => {
this._savedSelections = new Map()
editors.forEach(editor => {
if (editor.getModel() === monacoModel) {
const rsel = createRelativeSelection(editor, monacoModel, ytext)
if (rsel !== null) {
this._savedSelections.set(editor, rsel)
}
}
})
})
}
this.doc.on('beforeAllTransactions', this._beforeTransaction)
this._decorations = new Map()
this._rerenderDecorations = () => {
editors.forEach(editor => {
if (awareness && editor.getModel() === monacoModel) {
// render decorations
const currentDecorations = this._decorations.get(editor) || []
/**
* @type {Array<monaco.editor.IModelDeltaDecoration>}
*/
const newDecorations = []
awareness.getStates().forEach((state, clientID) => {
if (clientID !== this.doc.clientID && state.selection != null && state.selection.anchor != null && state.selection.head != null) {
const anchorAbs = Y.createAbsolutePositionFromRelativePosition(state.selection.anchor, this.doc)
const headAbs = Y.createAbsolutePositionFromRelativePosition(state.selection.head, this.doc)
if (anchorAbs !== null && headAbs !== null && anchorAbs.type === ytext && headAbs.type === ytext) {
let start, end, afterContentClassName, beforeContentClassName
if (anchorAbs.index < headAbs.index) {
start = monacoModel.getPositionAt(anchorAbs.index)
end = monacoModel.getPositionAt(headAbs.index)
afterContentClassName = 'yRemoteSelectionHead yRemoteSelectionHead-' + clientID
beforeContentClassName = null
} else {
start = monacoModel.getPositionAt(headAbs.index)
end = monacoModel.getPositionAt(anchorAbs.index)
afterContentClassName = null
beforeContentClassName = 'yRemoteSelectionHead yRemoteSelectionHead-' + clientID
}
newDecorations.push({
range: new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column),
options: {
className: 'yRemoteSelection yRemoteSelection-' + clientID,
afterContentClassName,
beforeContentClassName
}
})
}
}
})
this._decorations.set(editor, editor.deltaDecorations(currentDecorations, newDecorations))
} else {
// ignore decorations
this._decorations.delete(editor)
}
})
}
/**
* @param {Y.YTextEvent} event
*/
this._ytextObserver = event => {
this.mux(() => {
let index = 0
event.delta.forEach(op => {
if (op.retain !== undefined) {
index += op.retain
} else if (op.insert !== undefined) {
const pos = monacoModel.getPositionAt(index)
const range = new monaco.Selection(pos.lineNumber, pos.column, pos.lineNumber, pos.column)
const insert = /** @type {string} */ (op.insert)
monacoModel.applyEdits([{ range, text: insert }])
index += insert.length
} else if (op.delete !== undefined) {
const pos = monacoModel.getPositionAt(index)
const endPos = monacoModel.getPositionAt(index + op.delete)
const range = new monaco.Selection(pos.lineNumber, pos.column, endPos.lineNumber, endPos.column)
monacoModel.applyEdits([{ range, text: '' }])
} else {
throw error.unexpectedCase()
}
})
this._savedSelections.forEach((rsel, editor) => {
const sel = createMonacoSelectionFromRelativeSelection(editor, ytext, rsel, this.doc)
if (sel !== null) {
editor.setSelection(sel)
}
})
})
this._rerenderDecorations()
}
ytext.observe(this._ytextObserver)
{
const ytextValue = ytext.toString()
if (monacoModel.getValue() !== ytextValue) {
monacoModel.setValue(ytextValue)
}
}
this._monacoChangeHandler = monacoModel.onDidChangeContent(event => {
// apply changes from right to left
this.mux(() => {
this.doc.transact(() => {
event.changes.sort((change1, change2) => change2.rangeOffset - change1.rangeOffset).forEach(change => {
ytext.delete(change.rangeOffset, change.rangeLength)
ytext.insert(change.rangeOffset, change.text)
})
}, this)
})
})
this._monacoDisposeHandler = monacoModel.onWillDispose(() => {
this.destroy()
})
if (awareness) {
editors.forEach(editor => {
editor.onDidChangeCursorSelection(() => {
if (editor.getModel() === monacoModel) {
const sel = editor.getSelection()
if (sel === null) {
return
}
let anchor = monacoModel.getOffsetAt(sel.getStartPosition())
let head = monacoModel.getOffsetAt(sel.getEndPosition())
if (sel.getDirection() === monaco.SelectionDirection.RTL) {
const tmp = anchor
anchor = head
head = tmp
}
awareness.setLocalStateField('selection', {
anchor: Y.createRelativePositionFromTypeIndex(ytext, anchor),
head: Y.createRelativePositionFromTypeIndex(ytext, head)
})
}
})
awareness.on('change', this._rerenderDecorations)
})
this.awareness = awareness
}
}
destroy () {
this._monacoChangeHandler.dispose()
this._monacoDisposeHandler.dispose()
this.ytext.unobserve(this._ytextObserver)
this.doc.off('beforeAllTransactions', this._beforeTransaction)
if (this.awareness) {
this.awareness.off('change', this._rerenderDecorations)
}
}
}