UNPKG

collaborative-monaco

Version:

Binding for collaborative editing support in Monaco Editor. Connects JSON CRDT str node to Monaco Editor.

78 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MonacoEditorFacade = void 0; const monaco = require("monaco-editor"); class MonacoEditorFacade { constructor(editor) { this.editor = editor; this.modelChangeDisposable = editor.onDidChangeModelContent((event) => { var _a; const rawChanges = event.changes.sort((c1, c2) => c2.rangeOffset - c1.rangeOffset); const changes = []; const length = rawChanges.length; for (let i = 0; i < length; i++) { const { rangeOffset, rangeLength, text } = rawChanges[i]; changes.push([rangeOffset, rangeLength, text]); } (_a = this.onchange) === null || _a === void 0 ? void 0 : _a.call(this, changes); }); this.selectionDisposable = editor.onDidChangeCursorSelection(() => { var _a; return (_a = this.onselection) === null || _a === void 0 ? void 0 : _a.call(this); }); } get() { return this.editor.getValue(); } getLength() { return this.editor.getModel().getValueLength(); } set(text) { this.editor.setValue(text); } getRange(offset, length) { const model = this.editor.getModel(); const start = model.getPositionAt(offset); const end = length ? model.getPositionAt(offset + length) : start; return new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column); } ins(pos, text) { var _a; const range = this.getRange(pos, 0); (_a = this.editor.getModel()) === null || _a === void 0 ? void 0 : _a.applyEdits([{ range, text }]); } del(pos, length) { var _a; const range = this.getRange(pos, length); (_a = this.editor.getModel()) === null || _a === void 0 ? void 0 : _a.applyEdits([{ range, text: '' }]); } getSelection() { const editor = this.editor; const selection = editor.getSelection(); if (!selection) return null; const model = editor.getModel(); if (!model) return null; const start = model.getOffsetAt(selection.getSelectionStart()); const end = model.getOffsetAt(selection.getPosition()); const direction = selection.getDirection() === monaco.SelectionDirection.LTR ? 1 : -1; const editorSelection = [start, end, direction]; return editorSelection; } setSelection(start, end, direction) { const editor = this.editor; const model = editor.getModel(); if (!model) return; let startPosition = model.getPositionAt(start); let endPosition = model.getPositionAt(end); if (direction === -1) [startPosition, endPosition] = [endPosition, startPosition]; const selection = new monaco.Selection(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column); editor.setSelection(selection); } dispose() { this.modelChangeDisposable.dispose(); this.selectionDisposable.dispose(); } } exports.MonacoEditorFacade = MonacoEditorFacade; //# sourceMappingURL=MonacoEditorFacade.js.map