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,EAAY,YAAY,EAAI,oBACrC,EAAO,EAAY,YAAY,EAAI,kBACvC,GAAI,EAAI,iBAAmB,OAAO,mBAAmB,IAAK,CACxD,MAAM,EAAM,EACZ,EAAS,EACT,EAAO,CACT,CACA,EAAU,mBAAmB,YAAa,CACxC,OAAQ,EAAE,oCAAoC,EAAO,GACrD,KAAM,EAAE,oCAAoC,EAAO,IAEvD,KAEF,EAAU,GAAG,SAAU,KAAK,qBAAoB,IAElD,KAAK,UAAY,EAErB,CAEA,OAAA,GACE,KAAK,qBAAqB,UAC1B,KAAK,sBAAsB,UAC3B,KAAK,MAAM,UAAU,KAAK,gBAC1B,KAAK,IAAI,IAAI,wBAAyB,KAAK,oBACvC,KAAK,WACP,KAAK,UAAU,IAAI,SAAU,KAAK,qBAEtC","file":"/npm/y-monaco@0.1.6/src/y-monaco.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/y-monaco@0.1.6/src/y-monaco.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import * as Y from 'yjs'\nimport * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'\nimport * as error from 'lib0/error'\nimport { createMutex } from 'lib0/mutex'\nimport { Awareness } from 'y-protocols/awareness' // eslint-disable-line\n\nclass RelativeSelection {\n  /**\n   * @param {Y.RelativePosition} start\n   * @param {Y.RelativePosition} end\n   * @param {monaco.SelectionDirection} direction\n   */\n  constructor (start, end, direction) {\n    this.start = start\n    this.end = end\n    this.direction = direction\n  }\n}\n\n/**\n * @param {monaco.editor.IStandaloneCodeEditor} editor\n * @param {monaco.editor.ITextModel} monacoModel\n * @param {Y.Text} type\n */\nconst createRelativeSelection = (editor, monacoModel, type) => {\n  const sel = editor.getSelection()\n  if (sel !== null) {\n    const startPos = sel.getStartPosition()\n    const endPos = sel.getEndPosition()\n    const start = Y.createRelativePositionFromTypeIndex(type, monacoModel.getOffsetAt(startPos))\n    const end = Y.createRelativePositionFromTypeIndex(type, monacoModel.getOffsetAt(endPos))\n    return new RelativeSelection(start, end, sel.getDirection())\n  }\n  return null\n}\n\n/**\n * @param {monaco.editor.IEditor} editor\n * @param {Y.Text} type\n * @param {RelativeSelection} relSel\n * @param {Y.Doc} doc\n * @return {null|monaco.Selection}\n */\nconst createMonacoSelectionFromRelativeSelection = (editor, type, relSel, doc) => {\n  const start = Y.createAbsolutePositionFromRelativePosition(relSel.start, doc)\n  const end = Y.createAbsolutePositionFromRelativePosition(relSel.end, doc)\n  if (start !== null && end !== null && start.type === type && end.type === type) {\n    const model = /** @type {monaco.editor.ITextModel} */ (editor.getModel())\n    const startPos = model.getPositionAt(start.index)\n    const endPos = model.getPositionAt(end.index)\n    return monaco.Selection.createWithDirection(startPos.lineNumber, startPos.column, endPos.lineNumber, endPos.column, relSel.direction)\n  }\n  return null\n}\n\nexport class MonacoBinding {\n  /**\n   * @param {Y.Text} ytext\n   * @param {monaco.editor.ITextModel} monacoModel\n   * @param {Set<monaco.editor.IStandaloneCodeEditor>} [editors]\n   * @param {Awareness?} [awareness]\n   */\n  constructor (ytext, monacoModel, editors = new Set(), awareness = null) {\n    this.doc = /** @type {Y.Doc} */ (ytext.doc)\n    this.ytext = ytext\n    this.monacoModel = monacoModel\n    this.editors = editors\n    this.mux = createMutex()\n    /**\n     * @type {Map<monaco.editor.IStandaloneCodeEditor, RelativeSelection>}\n     */\n    this._savedSelections = new Map()\n    this._beforeTransaction = () => {\n      this.mux(() => {\n        this._savedSelections = new Map()\n        editors.forEach(editor => {\n          if (editor.getModel() === monacoModel) {\n            const rsel = createRelativeSelection(editor, monacoModel, ytext)\n            if (rsel !== null) {\n              this._savedSelections.set(editor, rsel)\n            }\n          }\n        })\n      })\n    }\n    this.doc.on('beforeAllTransactions', this._beforeTransaction)\n    this._decorations = new Map()\n    this._rerenderDecorations = () => {\n      editors.forEach(editor => {\n        if (awareness && editor.getModel() === monacoModel) {\n          // render decorations\n          const currentDecorations = this._decorations.get(editor) || []\n          /**\n           * @type {Array<monaco.editor.IModelDeltaDecoration>}\n           */\n          const newDecorations = []\n          awareness.getStates().forEach((state, clientID) => {\n            if (clientID !== this.doc.clientID && state.selection != null && state.selection.anchor != null && state.selection.head != null) {\n              const anchorAbs = Y.createAbsolutePositionFromRelativePosition(state.selection.anchor, this.doc)\n              const headAbs = Y.createAbsolutePositionFromRelativePosition(state.selection.head, this.doc)\n              if (anchorAbs !== null && headAbs !== null && anchorAbs.type === ytext && headAbs.type === ytext) {\n                let start, end, afterContentClassName, beforeContentClassName\n                if (anchorAbs.index < headAbs.index) {\n                  start = monacoModel.getPositionAt(anchorAbs.index)\n                  end = monacoModel.getPositionAt(headAbs.index)\n                  afterContentClassName = 'yRemoteSelectionHead yRemoteSelectionHead-' + clientID\n                  beforeContentClassName = null\n                } else {\n                  start = monacoModel.getPositionAt(headAbs.index)\n                  end = monacoModel.getPositionAt(anchorAbs.index)\n                  afterContentClassName = null\n                  beforeContentClassName = 'yRemoteSelectionHead yRemoteSelectionHead-' + clientID\n                }\n                newDecorations.push({\n                  range: new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column),\n                  options: {\n                    className: 'yRemoteSelection yRemoteSelection-' + clientID,\n                    afterContentClassName,\n                    beforeContentClassName\n                  }\n                })\n              }\n            }\n          })\n          this._decorations.set(editor, editor.deltaDecorations(currentDecorations, newDecorations))\n        } else {\n          // ignore decorations\n          this._decorations.delete(editor)\n        }\n      })\n    }\n    /**\n     * @param {Y.YTextEvent} event\n     */\n    this._ytextObserver = event => {\n      this.mux(() => {\n        let index = 0\n        event.delta.forEach(op => {\n          if (op.retain !== undefined) {\n            index += op.retain\n          } else if (op.insert !== undefined) {\n            const pos = monacoModel.getPositionAt(index)\n            const range = new monaco.Selection(pos.lineNumber, pos.column, pos.lineNumber, pos.column)\n            const insert = /** @type {string} */ (op.insert)\n            monacoModel.applyEdits([{ range, text: insert }])\n            index += insert.length\n          } else if (op.delete !== undefined) {\n            const pos = monacoModel.getPositionAt(index)\n            const endPos = monacoModel.getPositionAt(index + op.delete)\n            const range = new monaco.Selection(pos.lineNumber, pos.column, endPos.lineNumber, endPos.column)\n            monacoModel.applyEdits([{ range, text: '' }])\n          } else {\n            throw error.unexpectedCase()\n          }\n        })\n        this._savedSelections.forEach((rsel, editor) => {\n          const sel = createMonacoSelectionFromRelativeSelection(editor, ytext, rsel, this.doc)\n          if (sel !== null) {\n            editor.setSelection(sel)\n          }\n        })\n      })\n      this._rerenderDecorations()\n    }\n    ytext.observe(this._ytextObserver)\n    {\n      const ytextValue = ytext.toString()\n      if (monacoModel.getValue() !== ytextValue) {\n        monacoModel.setValue(ytextValue)\n      }\n    }\n    this._monacoChangeHandler = monacoModel.onDidChangeContent(event => {\n      // apply changes from right to left\n      this.mux(() => {\n        this.doc.transact(() => {\n          event.changes.sort((change1, change2) => change2.rangeOffset - change1.rangeOffset).forEach(change => {\n            ytext.delete(change.rangeOffset, change.rangeLength)\n            ytext.insert(change.rangeOffset, change.text)\n          })\n        }, this)\n      })\n    })\n    this._monacoDisposeHandler = monacoModel.onWillDispose(() => {\n      this.destroy()\n    })\n    if (awareness) {\n      editors.forEach(editor => {\n        editor.onDidChangeCursorSelection(() => {\n          if (editor.getModel() === monacoModel) {\n            const sel = editor.getSelection()\n            if (sel === null) {\n              return\n            }\n            let anchor = monacoModel.getOffsetAt(sel.getStartPosition())\n            let head = monacoModel.getOffsetAt(sel.getEndPosition())\n            if (sel.getDirection() === monaco.SelectionDirection.RTL) {\n              const tmp = anchor\n              anchor = head\n              head = tmp\n            }\n            awareness.setLocalStateField('selection', {\n              anchor: Y.createRelativePositionFromTypeIndex(ytext, anchor),\n              head: Y.createRelativePositionFromTypeIndex(ytext, head)\n            })\n          }\n        })\n        awareness.on('change', this._rerenderDecorations)\n      })\n      this.awareness = awareness\n    }\n  }\n\n  destroy () {\n    this._monacoChangeHandler.dispose()\n    this._monacoDisposeHandler.dispose()\n    this.ytext.unobserve(this._ytextObserver)\n    this.doc.off('beforeAllTransactions', this._beforeTransaction)\n    if (this.awareness) {\n      this.awareness.off('change', this._rerenderDecorations)\n    }\n  }\n}\n"]}