-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathy-codemirror.js
More file actions
484 lines (454 loc) · 16.7 KB
/
y-codemirror.js
File metadata and controls
484 lines (454 loc) · 16.7 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
* @module bindings/textarea
*/
import { createMutex } from 'lib0/mutex'
import * as math from 'lib0/math'
import * as Y from 'yjs'
import * as func from 'lib0/function'
import * as eventloop from 'lib0/eventloop'
import { Observable } from 'lib0/observable'
import * as diff from 'lib0/diff'
import CodeMirror from 'codemirror'
export const cmOrigin = 'y-codemirror'
const updateCursors = (y, cm, cursors, awareness) => {
cursors.forEach((cursor, clientId) => {
if (clientId !== y.clientID) {
const prevHeadpos = cursor.headpos;
const nextHeadpos = cm.posFromIndex(Y.createAbsolutePositionFromRelativePosition(JSON.parse(cursor.awCursor.head), y).index);
if (!func.equalityFlat(prevHeadpos, nextHeadpos)) {
const cursorCoords = cm.cursorCoords(nextHeadpos, "local");
cursors.set(clientId, { ...cursor, headpos: nextHeadpos })
setCaretPosition(cursor.caret, cursorCoords, cm);
}
}
})
}
/**
* @param {CodemirrorBinding} binding
* @param {any} event
*/
const typeObserver = (binding, event) => {
binding._mux(() => {
const cmDoc = binding.cmDoc
const cm = cmDoc.getEditor()
// Normally the position is right-associated
// But when remote changes happen, it looks like the remote user is hijacking your position.
// Just for remote insertions, we make the collapsed cursor left-associated.
// If selection is not collapsed, we only make "to" left associated
let anchor = cm.indexFromPos(cm.getCursor('anchor'))
let head = cm.indexFromPos(cm.getCursor('head'))
const switchSel = head < anchor
// normalize selection so that anchor < head, switch back later
if (switchSel) {
const tmp = head
head = anchor
anchor = tmp
}
const performChange = () => {
const delta = event.delta
let index = 0
for (let i = 0; i < event.delta.length; i++) {
const d = delta[i]
if (d.retain) {
index += d.retain
} else if (d.insert) {
if (index < anchor || (anchor < head && index === anchor)) {
anchor += d.insert.length
}
if (index < head) {
head += d.insert.length
}
const pos = cmDoc.posFromIndex(index)
cmDoc.replaceRange(d.insert, pos, pos, cmOrigin)
index += d.insert.length
} else if (d.delete) {
if (index < anchor) {
anchor = math.max(anchor - d.delete, index)
}
if (index < head) {
head = math.max(head - d.delete, index)
}
const start = cmDoc.posFromIndex(index)
const end = cmDoc.posFromIndex(index + d.delete)
cmDoc.replaceRange('', start, end, cmOrigin)
}
}
updateCursors(binding.doc, binding.cm, binding._cursors);
}
// if possible, bundle the changes using cm.operation
if (cm) {
cm.operation(performChange)
} else {
performChange()
}
if (switchSel) {
const tmp = head
head = anchor
anchor = tmp
}
cm.setSelection(cm.posFromIndex(anchor), cm.posFromIndex(head), { scroll: false })
})
}
/**
* @param {CodemirrorBinding} binding
* @param {Array<any>} changes
*/
const targetObserver = (binding, changes) => {
binding._mux(() => {
binding.doc.transact(() => {
const hasPaste = binding.yUndoManager && changes.some(change => change.origin === 'paste')
if (hasPaste) {
binding.yUndoManager.stopCapturing()
}
if (changes.length > 1) {
// If there are several consecutive changes, we can't reliably compute the positions anymore. See y-codemirror#11
// Instead, we will compute the diff and apply the changes
const d = diff.simpleDiffString(binding.type.toString(), binding.cmDoc.getValue())
binding.type.delete(d.index, d.remove)
binding.type.insert(d.index, d.insert)
} else {
const change = changes[0]
const start = binding.cmDoc.indexFromPos(change.from)
const delLen = change.removed.map(s => s.length).reduce(math.add) + change.removed.length - 1
if (delLen > 0) {
binding.type.delete(start, delLen)
}
if (change.text.length > 0) {
binding.type.insert(start, change.text.join('\n'))
}
}
if (hasPaste) {
binding.yUndoManager.stopCapturing()
}
}, binding)
})
updateCursors(binding.doc, binding.cm, binding._cursors);
if (binding._pendingCursorEvent) {
binding._pendingCursorEvent = false
binding.emit('cursorActivity', [binding])
}
}
const createRemoteCaret = (username, color, cursorCoords) => {
const height = cursorCoords.bottom - cursorCoords.top;
const caret = document.createElement('span')
caret.classList.add('remote-caret')
caret.setAttribute('style', `background-color: ${color}`)
caret.style.height = height + "px";
const userDiv = document.createElement('div')
userDiv.setAttribute('style', `background-color: ${color}`)
userDiv.insertBefore(document.createTextNode(username), null)
caret.insertBefore(userDiv, null)
return caret
}
const updateCaret = (caret, username, color) => {
const userDiv = caret.firstChild;
caret.style.backgroundColor = color;
userDiv.style.backgroundColor = color;
userDiv.innerText = username;
}
function setCaretPosition(caret, cursorCoords, cm) {
if (!caret) return;
caret.style.left = cursorCoords.left + "px";
caret.style.top = cursorCoords.top + "px";
const distanceFromTop = caret.offsetTop - cm.getScrollInfo().top;
const userDiv = caret.firstChild;
if (distanceFromTop - userDiv.offsetHeight <= 4) {
userDiv.style.top = `${userDiv.offsetHeight}px`;
} else {
userDiv.style.top = `-${userDiv.offsetHeight}px`;
}
const hideTimer = caret.hideTimer;
if (hideTimer) {
clearTimeout(hideTimer);
}
if (caret.classList.contains('hide-name')) {
caret.classList.remove('hide-name');
}
caret.hideTimer = setTimeout(() => {
caret.classList.add('hide-name')
caret.hideTimer = null;
}, 2000)
}
const createEmptyLinePlaceholder = (color) => {
const placeholder = document.createElement('span')
placeholder.setAttribute('style', 'user-select: none;')
const emptyTxt = document.createElement('span')
emptyTxt.insertBefore(document.createTextNode(''), null)
const sel = document.createElement('span')
sel.setAttribute('class', 'y-line-selection')
sel.setAttribute('style', `display: inline-block; position: absolute; left: 4px; right: 4px; top: 0; bottom: 0; background-color: ${color}70`)
placeholder.insertBefore(sel, null)
placeholder.insertBefore(emptyTxt, null)
return placeholder
}
const removeCaret = (domNode) => {
if (domNode && domNode.parentNode) {
domNode.parentNode.removeChild(domNode);
}
}
const updateRemoteSelection = (y, cm, type, cursors, clientId, awareness) => {
// redraw caret and selection for clientId
const aw = awareness.getStates().get(clientId)
// destroy current text mark
const m = cursors.get(clientId)
let caretEl;
if (m !== undefined) {
caretEl = m.caret;
m.sel.forEach(sel => sel.clear())
cursors.delete(clientId)
}
if (aw === undefined) {
removeCaret(caretEl);
return
}
const user = aw.user || {}
if (user.color == null) {
user.color = '#ffa500'
}
if (user.name == null) {
user.name = `User: ${clientId}`
}
const cursor = aw.cursor
if (cursor == null || cursor.anchor == null || cursor.head == null) {
removeCaret(caretEl);
return
}
const anchor = Y.createAbsolutePositionFromRelativePosition(JSON.parse(cursor.anchor), y)
const head = Y.createAbsolutePositionFromRelativePosition(JSON.parse(cursor.head), y)
if (anchor !== null && head !== null && anchor.type === type && head.type === type) {
const headpos = cm.posFromIndex(head.index)
const anchorpos = cm.posFromIndex(anchor.index)
let from, to
if (head.index < anchor.index) {
from = headpos
to = anchorpos
} else {
from = anchorpos
to = headpos
}
// if position was "relatively" the same, do not show name again and hide instead
if (m && func.equalityFlat(aw.cursor.anchor, m.awCursor.anchor) && func.equalityFlat(aw.cursor.head, m.awCursor.head)) {
caretEl.classList.add('hide-name')
}
const sel = []
if (head.index !== anchor.index) {
if (from.line !== to.line && from.ch !== 0) {
// start of selection will only be a simple text-selection
sel.push(cm.markText(from, new CodeMirror.Pos(from.line + 1, 0), { css: `background-color: ${user.color}70;`, inclusiveRight: false, inclusiveLeft: false }))
from = new CodeMirror.Pos(from.line + 1, 0)
}
while (from.line !== to.line) {
// middle of selection is always a whole-line selection. We add a widget at the first position which will fill the background.
sel.push(cm.setBookmark(new CodeMirror.Pos(from.line, 0), { widget: createEmptyLinePlaceholder(user.color) }))
from = new CodeMirror.Pos(from.line + 1, 0)
}
sel.push(cm.markText(from, to, { css: `background-color: ${user.color}70;`, inclusiveRight: false, inclusiveLeft: false }))
}
// only render caret if not the complete last line was selected (in this case headpos.ch === 0)
if (sel.length > 0 && to === headpos && headpos.ch === 0 && caretEl) {
removeCaret(caretEl);
} else {
const cursorCoords = cm.cursorCoords(headpos, "local");
if (!caretEl) {
caretEl = createRemoteCaret(user.name, user.color, cursorCoords);
cm.addWidget({line: 0, ch: 0}, caretEl, false)
}
updateCaret(caretEl, user.name, user.color);
setCaretPosition(caretEl, cursorCoords, cm);
}
cursors.set(clientId, { caret: caretEl, sel, awCursor: cursor, headpos })
}
}
const codemirrorCursorActivity = (y, cm, type, awareness) => {
const aw = awareness.getLocalState()
if (!cm.hasFocus() || aw == null || !cm.display.wrapper.ownerDocument.hasFocus()) {
return
}
const newAnchor = Y.createRelativePositionFromTypeIndex(type, cm.indexFromPos(cm.getCursor('anchor')))
const newHead = Y.createRelativePositionFromTypeIndex(type, cm.indexFromPos(cm.getCursor('head')))
let currentAnchor = null
let currentHead = null
if (aw.cursor != null) {
currentAnchor = Y.createRelativePositionFromJSON(JSON.parse(aw.cursor.anchor))
currentHead = Y.createRelativePositionFromJSON(JSON.parse(aw.cursor.head))
}
if (aw.cursor == null || !Y.compareRelativePositions(currentAnchor, newAnchor) || !Y.compareRelativePositions(currentHead, newHead)) {
awareness.setLocalStateField('cursor', {
anchor: JSON.stringify(newAnchor),
head: JSON.stringify(newHead)
})
}
}
/**
* A binding that binds a YText to a CodeMirror editor.
*
* @example
* const ytext = ydocument.define('codemirror', Y.Text)
* const editor = new CodeMirror(document.querySelector('#container'), {
* mode: 'javascript',
* lineNumbers: true
* })
* const binding = new CodemirrorBinding(ytext, editor)
*
*/
export class CodemirrorBinding extends Observable {
/**
* @param {Y.Text} textType
* @param {import('codemirror').Editor} codeMirror
* @param {any | null} [awareness]
* @param {{ yUndoManager?: Y.UndoManager }} [options]
*/
constructor (textType, codeMirror, awareness = null, { yUndoManager = null } = {}) {
super()
const doc = textType.doc
const cmDoc = codeMirror.getDoc()
this.doc = doc
this.type = textType
this.cm = codeMirror
this.cmDoc = cmDoc
this.awareness = awareness || null
this.yUndoManager = yUndoManager
this._onStackItemAdded = ({ stackItem, changedParentTypes }) => {
// only store metadata if this type was affected
if (changedParentTypes.has(textType) && this._beforeChangeSelection) {
stackItem.meta.set(this, this._beforeChangeSelection)
}
}
this._onStackItemPopped = ({ stackItem }) => {
const sel = stackItem.meta.get(this)
if (sel) {
const anchor = Y.createAbsolutePositionFromRelativePosition(sel.anchor, doc).index
const head = Y.createAbsolutePositionFromRelativePosition(sel.head, doc).index
codeMirror.setSelection(codeMirror.posFromIndex(anchor), codeMirror.posFromIndex(head))
this._beforeChange()
}
}
if (yUndoManager) {
yUndoManager.trackedOrigins.add(this) // track changes performed by this editor binding
const editorUndo = cm => {
// Keymaps always start with an active operation.
// End the current operation so that the event is fired at the correct moment.
// @todo check cm.curOp in typeListener and endOperation always.
cm.endOperation()
yUndoManager.undo()
cm.startOperation()
}
const editorRedo = cm => {
cm.endOperation()
yUndoManager.redo()
cm.startOperation()
}
codeMirror.addKeyMap({
// pc
'Ctrl-Z': editorUndo,
'Shift-Ctrl-Z': editorRedo,
'Ctrl-Y': editorRedo,
// mac
'Cmd-Z': editorUndo,
'Shift-Cmd-Z': editorRedo,
'Cmd-Y': editorRedo
})
yUndoManager.on('stack-item-added', this._onStackItemAdded)
yUndoManager.on('stack-item-popped', this._onStackItemPopped)
}
this._mux = createMutex()
// set initial value
cmDoc.setValue(textType.toString())
// observe type and target
this._typeObserver = event => typeObserver(this, event)
this._targetObserver = (instance, changes) => {
if (instance.getDoc() === cmDoc) {
targetObserver(this, changes)
}
}
this._cursors = new Map()
this._changedCursors = new Set()
this._debounceCursorEvent = eventloop.createDebouncer(10)
this._awarenessListener = event => {
if (codeMirror.getDoc() !== cmDoc) {
return
}
const f = clientId => {
if (clientId !== doc.clientID) {
this._changedCursors.add(clientId)
}
}
event.added.forEach(f)
event.removed.forEach(f)
event.updated.forEach(f)
if (this._changedCursors.size > 0) {
this._debounceCursorEvent(() => {
this._changedCursors.forEach(clientId => {
updateRemoteSelection(doc, codeMirror, textType, this._cursors, clientId, awareness)
})
this._changedCursors.clear()
})
}
}
this._pendingCursorEvent = false
this._cursorListener = () => {
if (codeMirror.getDoc() === cmDoc) {
this._pendingCursorEvent = true
setTimeout(() => {
if (this._pendingCursorEvent) {
this._pendingCursorEvent = false
this.emit('cursorActivity', [codeMirror])
}
}, 0)
}
}
this.on('cursorActivity', () => {
codemirrorCursorActivity(doc, codeMirror, textType, awareness)
})
this._blurListeer = () => awareness.setLocalStateField('cursor', null)
textType.observe(this._typeObserver)
// @ts-ignore
codeMirror.on('changes', this._targetObserver)
/**
* @type {{ anchor: Y.RelativePosition, head: Y.RelativePosition } | null}
*/
this._beforeChangeSelection = null
this._beforeChange = () => {
// update the the beforeChangeSelection that is stored befor each change to the editor (except when applying remote changes)
this._mux(() => {
// store the selection before the change is applied so we can restore it with the undo manager.
const anchor = Y.createRelativePositionFromTypeIndex(textType, codeMirror.indexFromPos(codeMirror.getCursor('anchor')))
const head = Y.createRelativePositionFromTypeIndex(textType, codeMirror.indexFromPos(codeMirror.getCursor('head')))
this._beforeChangeSelection = { anchor, head }
})
}
codeMirror.on('beforeChange', this._beforeChange)
if (awareness) {
codeMirror.on('swapDoc', this._blurListeer)
awareness.on('change', this._awarenessListener)
// @ts-ignore
codeMirror.on('cursorActivity', this._cursorListener)
codeMirror.on('blur', this._blurListeer)
codeMirror.on('focus', this._cursorListener)
}
}
destroy () {
this.type.unobserve(this._typeObserver)
this.cm.off('swapDoc', this._blurListeer)
// @ts-ignore
this.cm.off('changes', this._targetObserver)
this.cm.off('beforeChange', this._beforeChange)
// @ts-ignore
this.cm.off('cursorActivity', this._cursorListener)
this.cm.off('focus', this._cursorListener)
this.cm.off('blur', this._blurListeer)
if (this.awareness) {
this.awareness.off('change', this._awarenessListener)
}
if (this.yUndoManager) {
this.yUndoManager.off('stack-item-added', this._onStackItemAdded)
this.yUndoManager.off('stack-item-popped', this._onStackItemPopped)
this.yUndoManager.trackedOrigins.delete(this)
}
this.type = null
this.cm = null
this.cmDoc = null
super.destroy()
}
}
export const CodeMirrorBinding = CodemirrorBinding