2024-02-01 04:47:00 -05:00
|
|
|
import { Prec, Transaction, Annotation, ChangeSpec } from '@codemirror/state'
|
2023-04-13 04:21:25 -04:00
|
|
|
import { EditorView, ViewPlugin } from '@codemirror/view'
|
|
|
|
import { EventEmitter } from 'events'
|
2024-08-26 07:50:34 -04:00
|
|
|
import RangesTracker from '@overleaf/ranges-tracker'
|
2023-04-13 04:21:25 -04:00
|
|
|
import { ShareDoc } from '../../../../../types/share-doc'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-02-08 05:10:46 -05:00
|
|
|
import { DocumentContainer } from '@/features/ide-react/editor/document-container'
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Integrate CodeMirror 6 with the real-time system, via ShareJS.
|
|
|
|
*
|
|
|
|
* Changes from CodeMirror are passed to the shareDoc
|
|
|
|
* via `handleTransaction`, while changes arriving from
|
|
|
|
* real-time are passed to CodeMirror via the EditorFacade.
|
|
|
|
*
|
|
|
|
* We use an `EditorFacade` to integrate with the rest of
|
|
|
|
* the IDE, providing an interface the other systems can work with.
|
|
|
|
*
|
|
|
|
* Related files:
|
|
|
|
* - frontend/js/ide/editor/Document.js
|
|
|
|
* - frontend/js/ide/editor/ShareJsDoc.js
|
|
|
|
* - frontend/js/ide/connection/EditorWatchdogManager.js
|
2023-10-26 04:57:00 -04:00
|
|
|
* - frontend/js/features/ide-react/editor/document.ts
|
|
|
|
* - frontend/js/features/ide-react/editor/share-js-doc.ts
|
|
|
|
* - frontend/js/features/ide-react/connection/editor-watchdog-manager.js
|
2023-04-13 04:21:25 -04:00
|
|
|
*/
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
export type ChangeDescription = {
|
|
|
|
origin: 'remote' | 'undo' | 'reject' | undefined
|
|
|
|
inserted: boolean
|
|
|
|
removed: boolean
|
|
|
|
}
|
|
|
|
|
2023-06-08 04:35:51 -04:00
|
|
|
/**
|
|
|
|
* A custom extension that connects the CodeMirror 6 editor to the currently open ShareJS document.
|
|
|
|
*/
|
2023-04-13 04:21:25 -04:00
|
|
|
export const realtime = (
|
2024-02-08 05:10:46 -05:00
|
|
|
{ currentDoc }: { currentDoc: DocumentContainer },
|
2023-04-13 04:21:25 -04:00
|
|
|
handleError: (error: Error) => void
|
|
|
|
) => {
|
|
|
|
const realtimePlugin = ViewPlugin.define(view => {
|
|
|
|
const editor = new EditorFacade(view)
|
|
|
|
|
|
|
|
currentDoc.attachToCM6(editor)
|
|
|
|
|
|
|
|
return {
|
|
|
|
update(update) {
|
|
|
|
if (update.docChanged) {
|
2024-08-26 07:50:34 -04:00
|
|
|
editor.handleUpdateFromCM(update.transactions, currentDoc.ranges)
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
// TODO: wrap in a timeout so processing can finish?
|
|
|
|
// window.setTimeout(() => {
|
|
|
|
currentDoc.detachFromCM6()
|
|
|
|
// }, 0)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// NOTE: not a view plugin, so shouldn't get removed
|
|
|
|
const ensureRealtimePlugin = EditorView.updateListener.of(update => {
|
|
|
|
if (!update.view.plugin(realtimePlugin)) {
|
|
|
|
const message = 'The realtime extension has been destroyed!!'
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn(message)
|
2023-04-13 04:21:25 -04:00
|
|
|
if (currentDoc.doc) {
|
|
|
|
// display the "out of sync" modal
|
|
|
|
currentDoc.doc.emit('error', message)
|
|
|
|
} else {
|
|
|
|
// display the error boundary
|
|
|
|
handleError(new Error(message))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return Prec.highest([realtimePlugin, ensureRealtimePlugin])
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorFacade extends EventEmitter {
|
|
|
|
public shareDoc: ShareDoc | null
|
|
|
|
public events: EventEmitter
|
|
|
|
private maxDocLength?: number
|
|
|
|
|
|
|
|
constructor(public view: EditorView) {
|
|
|
|
super()
|
|
|
|
this.view = view
|
|
|
|
this.shareDoc = null
|
|
|
|
this.events = new EventEmitter()
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue() {
|
|
|
|
return this.view.state.doc.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch changes to CodeMirror view
|
2024-02-01 04:47:00 -05:00
|
|
|
cmChange(changes: ChangeSpec, origin?: string) {
|
|
|
|
const isRemote = origin === 'remote'
|
|
|
|
|
2023-04-13 04:21:25 -04:00
|
|
|
this.view.dispatch({
|
2024-02-01 04:47:00 -05:00
|
|
|
changes,
|
2023-04-13 04:21:25 -04:00
|
|
|
annotations: [
|
2024-02-01 04:47:00 -05:00
|
|
|
Transaction.remote.of(isRemote),
|
|
|
|
Transaction.addToHistory.of(!isRemote),
|
2023-04-13 04:21:25 -04:00
|
|
|
],
|
2024-02-01 04:47:00 -05:00
|
|
|
effects:
|
|
|
|
// if this is a remote change, restore a snapshot of the current scroll position after the change has been applied
|
|
|
|
isRemote
|
|
|
|
? this.view.scrollSnapshot().map(this.view.state.changes(changes))
|
|
|
|
: undefined,
|
2023-04-13 04:21:25 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-01 04:47:00 -05:00
|
|
|
cmInsert(position: number, text: string, origin?: string) {
|
|
|
|
this.cmChange({ from: position, insert: text }, origin)
|
|
|
|
}
|
|
|
|
|
2023-04-13 04:21:25 -04:00
|
|
|
cmDelete(position: number, text: string, origin?: string) {
|
2024-02-01 04:47:00 -05:00
|
|
|
this.cmChange({ from: position, to: position + text.length }, origin)
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to ShareJS, passing changes to the CodeMirror view
|
|
|
|
// as new transactions.
|
|
|
|
// This is a broad immitation of helper functions supplied in
|
|
|
|
// the sharejs library. (See vendor/libs/sharejs, in particular
|
2023-08-15 05:53:00 -04:00
|
|
|
// the 'attach_ace' helper)
|
2023-04-13 04:21:25 -04:00
|
|
|
attachShareJs(shareDoc: ShareDoc, maxDocLength?: number) {
|
|
|
|
this.shareDoc = shareDoc
|
|
|
|
this.maxDocLength = maxDocLength
|
|
|
|
|
|
|
|
const check = () => {
|
|
|
|
// run in a timeout so it checks the editor content once this update has been applied
|
|
|
|
window.setTimeout(() => {
|
|
|
|
const editorText = this.getValue()
|
|
|
|
const otText = shareDoc.getText()
|
|
|
|
|
|
|
|
if (editorText !== otText) {
|
|
|
|
shareDoc.emit('error', 'Text does not match in CodeMirror 6')
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error('Text does not match!')
|
|
|
|
debugConsole.error('editor: ' + editorText)
|
|
|
|
debugConsole.error('ot: ' + otText)
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onInsert = (pos: number, text: string) => {
|
|
|
|
this.cmInsert(pos, text, 'remote')
|
|
|
|
check()
|
|
|
|
}
|
|
|
|
|
|
|
|
const onDelete = (pos: number, text: string) => {
|
|
|
|
this.cmDelete(pos, text, 'remote')
|
|
|
|
check()
|
|
|
|
}
|
|
|
|
|
|
|
|
check()
|
|
|
|
|
|
|
|
shareDoc.on('insert', onInsert)
|
|
|
|
shareDoc.on('delete', onDelete)
|
|
|
|
|
|
|
|
shareDoc.detach_cm6 = () => {
|
|
|
|
shareDoc.removeListener('insert', onInsert)
|
|
|
|
shareDoc.removeListener('delete', onDelete)
|
|
|
|
delete shareDoc.detach_cm6
|
|
|
|
this.shareDoc = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process an update from CodeMirror, applying changes to the
|
|
|
|
// ShareJs doc if appropriate
|
2024-08-26 07:50:34 -04:00
|
|
|
handleUpdateFromCM(
|
|
|
|
transactions: readonly Transaction[],
|
|
|
|
ranges?: RangesTracker
|
|
|
|
) {
|
2023-04-13 04:21:25 -04:00
|
|
|
const shareDoc = this.shareDoc
|
2024-08-26 07:50:34 -04:00
|
|
|
const trackedDeletesLength =
|
|
|
|
ranges != null ? ranges.getTrackedDeletesLength() : 0
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
if (!shareDoc) {
|
|
|
|
throw new Error('Trying to process updates with no shareDoc')
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const transaction of transactions) {
|
|
|
|
if (transaction.docChanged) {
|
|
|
|
const origin = chooseOrigin(transaction)
|
|
|
|
|
|
|
|
if (origin === 'remote') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-26 07:50:34 -04:00
|
|
|
// This is an approximation. Some deletes could have generated new
|
|
|
|
// tracked deletes since we measured trackedDeletesLength at the top of
|
|
|
|
// the function. Unfortunately, the ranges tracker is only updated
|
|
|
|
// after all transactions are processed, so it's not easy to get an
|
|
|
|
// exact number.
|
|
|
|
const fullDocLength =
|
|
|
|
transaction.changes.desc.newLength + trackedDeletesLength
|
|
|
|
|
|
|
|
if (this.maxDocLength && fullDocLength >= this.maxDocLength) {
|
2023-04-13 04:21:25 -04:00
|
|
|
shareDoc.emit(
|
|
|
|
'error',
|
|
|
|
new Error('document length is greater than maxDocLength')
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let positionShift = 0
|
|
|
|
|
|
|
|
transaction.changes.iterChanges(
|
|
|
|
(fromA, toA, fromB, toB, insertedText) => {
|
|
|
|
const fromUndo = origin === 'undo' || origin === 'reject'
|
|
|
|
|
|
|
|
const insertedLength = insertedText.length
|
|
|
|
const removedLength = toA - fromA
|
|
|
|
|
|
|
|
const inserted = insertedLength > 0
|
|
|
|
const removed = removedLength > 0
|
|
|
|
|
|
|
|
const pos = fromA + positionShift
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
shareDoc.del(pos, removedLength, fromUndo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inserted) {
|
|
|
|
shareDoc.insert(pos, insertedText.toString(), fromUndo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: mapPos instead?
|
|
|
|
positionShift = positionShift - removedLength + insertedLength
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
const changeDescription: ChangeDescription = {
|
|
|
|
origin,
|
|
|
|
inserted,
|
|
|
|
removed,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('change', this, changeDescription)
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const trackChangesAnnotation = Annotation.define()
|
|
|
|
|
|
|
|
const chooseOrigin = (transaction: Transaction) => {
|
|
|
|
if (transaction.annotation(Transaction.remote)) {
|
|
|
|
return 'remote'
|
|
|
|
}
|
|
|
|
if (transaction.annotation(Transaction.userEvent) === 'undo') {
|
|
|
|
return 'undo'
|
|
|
|
}
|
|
|
|
if (transaction.annotation(trackChangesAnnotation) === 'reject') {
|
|
|
|
return 'reject'
|
|
|
|
}
|
|
|
|
}
|