UNPKG

collaborative-ace

Version:

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

67 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AceEditorFacade = void 0; class AceEditorFacade { constructor(editor) { this.editor = editor; this.onChange = (delta) => { var _a, _b, _c, _d; if (delta && typeof delta === 'object' && delta.start && delta.end) { const doc = this.editor.session.doc; const start = doc.positionToIndex(delta.start); const text = delta.lines.join('\n'); switch (delta.action) { case 'insert': // console.log([start, 0, text]); (_a = this.onchange) === null || _a === void 0 ? void 0 : _a.call(this, [[start, 0, text]]); break; case 'remove': // console.log([start, text.length, '']); (_b = this.onchange) === null || _b === void 0 ? void 0 : _b.call(this, [[start, text.length, '']]); break; default: (_c = this.onchange) === null || _c === void 0 ? void 0 : _c.call(this); } } else (_d = this.onchange) === null || _d === void 0 ? void 0 : _d.call(this); }; editor.on('change', this.onChange); } get() { return this.editor.getValue(); } getLength() { const doc = this.editor.session.doc; const nl = doc.getNewLineCharacter().length; const lines = doc.getAllLines(); let length = 0; for (let i = 0; i < lines.length; i++) length += lines[i].length + nl; length -= nl; return length; } set(text) { this.editor.setValue(text); } ins(pos, text) { const session = this.editor.session; const doc = session.doc; const position = doc.indexToPosition(pos, 0); session.insert(position, text); } del(pos, len) { const session = this.editor.session; const doc = session.doc; const R = session.selection.getRange().constructor; const start = doc.indexToPosition(pos, 0); const end = doc.indexToPosition(pos + len, 0); const range = new R(start.row, start.column, end.row, end.column); session.remove(range); } dispose() { this.editor.off('change', this.onChange); } } exports.AceEditorFacade = AceEditorFacade; //# sourceMappingURL=AceEditorFacade.js.map