Skip to content

Commit

Permalink
[ISSUE #7] ITextModel.pushEditOperations will eventually create delet…
Browse files Browse the repository at this point in the history
…e ops when local ops are applied, which can lead to inconsistent ITextModels on the different clients. Using the ITextModel.applyEdits prevents these ops
  • Loading branch information
raedle committed Jan 7, 2021
1 parent 577f4ae commit 009ae48
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/y-monaco.js
Expand Up @@ -10,7 +10,7 @@ class RelativeSelection {
* @param {Y.RelativePosition} end
* @param {monaco.SelectionDirection} direction
*/
constructor (start, end, direction) {
constructor(start, end, direction) {
this.start = start
this.end = end
this.direction = direction
Expand Down Expand Up @@ -60,7 +60,7 @@ export class MonacoBinding {
* @param {Set<monaco.editor.IStandaloneCodeEditor>} [editors]
* @param {Awareness?} [awareness]
*/
constructor (ytext, monacoModel, editors = new Set(), awareness = null) {
constructor(ytext, monacoModel, editors = new Set(), awareness = null) {
this.doc = /** @type {Y.Doc} */ (ytext.doc)
this.ytext = ytext
this.monacoModel = monacoModel
Expand Down Expand Up @@ -135,18 +135,17 @@ export class MonacoBinding {
} else if (op.insert !== undefined) {
const pos = monacoModel.getPositionAt(index)
const range = new monaco.Selection(pos.lineNumber, pos.column, pos.lineNumber, pos.column)
monacoModel.pushEditOperations([], [{ range, text: op.insert }], () => null)
monacoModel.applyEdits([{ range, text: op.insert }])
index += op.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.pushEditOperations([], [{ range, text: '' }], () => null)
monacoModel.applyEdits([{ range, text: '' }])
} else {
throw error.unexpectedCase()
}
})
monacoModel.pushStackElement()
this._savedSelections.forEach((rsel, editor) => {
const sel = createMonacoSelectionFromRelativeSelection(editor, ytext, rsel, this.doc)
if (sel !== null) {
Expand Down Expand Up @@ -199,7 +198,7 @@ export class MonacoBinding {
}
}

destroy () {
destroy() {
this._monacoChangeHandler.dispose()
this.ytext.unobserve(this._ytextObserver)
this.doc.off('beforeAllTransactions', this._beforeTransaction)
Expand Down

0 comments on commit 009ae48

Please sign in to comment.