collaborative-codemirror
Version:
Binding for collaborative editing support in Codemirror Editor. Connects JSON CRDT str node to Codemirror Editor.
82 lines • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodemirrorEditorFacade = void 0;
class CodemirrorEditorFacade {
constructor(editor) {
this.editor = editor;
this.disposed = false;
this.d1 = (...specs) => {
var _a, _b;
const res = this.d0.apply(this.editor, specs);
if (this.disposed)
return res;
(_a = this.onchange) === null || _a === void 0 ? void 0 : _a.call(this);
(_b = this.onselection) === null || _b === void 0 ? void 0 : _b.call(this);
return res;
};
this.d0 = editor.dispatch;
Object.defineProperty(editor, 'dispatch', Object.assign(Object.assign({}, Object.getOwnPropertyDescriptor(editor, 'dispatch')), { value: this.d1 }));
}
get() {
return this.editor.state.doc.toString();
}
getLength() {
return this.editor.state.doc.length;
}
set(text) {
const editor = this.editor;
const state = editor.state;
this.d0({
changes: {
from: 0,
to: state.doc.length,
insert: text,
},
});
}
ins(from, insert) {
this.d0({ changes: { from, insert } });
}
del(from, length) {
this.d0({
changes: {
from,
to: from + length,
insert: '',
},
});
}
getSelection() {
var _a;
const state = this.editor.state;
const range = (_a = state.selection) === null || _a === void 0 ? void 0 : _a.ranges[0];
if (!range)
return null;
let start = range.anchor;
let end = range.head;
let direction = 1;
if (end < start) {
[start, end] = [end, start];
direction = -1;
}
return [start, end, direction];
}
setSelection(start, end, direction) {
var _a;
let anchor = start;
let head = end;
if (direction === -1)
[anchor, head] = [head, anchor];
(_a = this.d0) === null || _a === void 0 ? void 0 : _a.call(this, { selection: { anchor, head } });
}
dispose() {
this.disposed = true;
const editor = this.editor;
const descriptor = Object.getOwnPropertyDescriptor(editor, 'dispatch');
if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === this.d1) {
Object.defineProperty(editor, 'dispatch', Object.assign(Object.assign({}, descriptor), { value: this.d0 }));
}
}
}
exports.CodemirrorEditorFacade = CodemirrorEditorFacade;
//# sourceMappingURL=CodemirrorEditorFacade.js.map