collaborative-editor
Version:
JSON CRDT str node bindings to any generic plain text editor.
142 lines • 5.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputFacade4 = exports.InputFacade3 = exports.InputFacade2 = exports.InputFacade1 = exports.InputFacade0 = void 0;
class InputFacade0 {
constructor(input) {
this.input = input;
}
get() {
return this.input.value;
}
set(text) {
this.input.value = text;
}
}
exports.InputFacade0 = InputFacade0;
class InputFacade1 extends InputFacade0 {
_onInput() {
var _a;
(_a = this.onchange) === null || _a === void 0 ? void 0 : _a.call(this);
}
constructor(input) {
super(input);
this.input = input;
this.onInput = (e) => {
this._onInput(e);
};
input.addEventListener('input', this.onInput);
}
getLength() {
return this.input.value.length;
}
dispose() {
this.input.removeEventListener('input', this.onInput);
}
}
exports.InputFacade1 = InputFacade1;
class InputFacade2 extends InputFacade1 {
constructor(input) {
super(input);
this.input = input;
this.onSelectionChange = () => {
var _a;
(_a = this.onselection) === null || _a === void 0 ? void 0 : _a.call(this);
};
document.addEventListener('selectionchange', this.onSelectionChange);
}
getSelection() {
const input = this.input;
const { selectionStart, selectionEnd, selectionDirection } = input;
const direction = selectionDirection === 'backward' ? -1 : selectionDirection === 'forward' ? 1 : 0;
return [
typeof selectionStart === 'number' ? selectionStart : -1,
typeof selectionEnd === 'number' ? selectionEnd : -1,
direction,
];
}
setSelection(start, end, direction) {
const input = this.input;
input.selectionStart = start > -1 ? start : null;
input.selectionEnd = end > -1 ? end : null;
input.selectionDirection = direction === -1 ? 'backward' : direction === 1 ? 'forward' : 'none';
}
dispose() {
super.dispose();
document.removeEventListener('selectionchange', this.onSelectionChange);
}
}
exports.InputFacade2 = InputFacade2;
class InputFacade3 extends InputFacade2 {
_onInput(e) {
const event = e;
const { input } = this;
const { data, inputType, isComposing } = event;
if (isComposing)
return;
switch (inputType) {
case 'insertText': {
if (!data || data.length !== 1)
break;
const { selectionStart, selectionEnd } = input;
if (selectionStart === null || selectionEnd === null)
break;
if (selectionStart !== selectionEnd)
break;
if (selectionStart <= 0)
break;
const selection = this.selection;
if (selectionStart - data.length !== selection.start)
break;
if (typeof selection.end !== 'number' || typeof selection.end !== 'number')
break;
const remove = selection.end - selection.start;
const change = [selection.start, remove, data];
this.onchange([change]);
return;
}
case 'deleteContentBackward': {
const { start, end } = this.selection;
if (typeof start !== 'number' || typeof end !== 'number')
break;
const change = start === end ? [start - 1, 1, ''] : [start, end - start, ''];
this.onchange([change]);
return;
}
}
this.onchange();
}
}
exports.InputFacade3 = InputFacade3;
class InputFacade4 extends InputFacade3 {
ins(position, text) {
const selection = this.getSelection();
const value = this.get();
const next = value.slice(0, position) + text + value.slice(position);
this.set(next);
if (selection) {
let [start, end] = selection;
const length = text.length;
if (start >= position)
start += length;
if (end > position)
end += length;
this.setSelection(start, end, selection[2]);
}
}
del(position, length) {
const selection = this.getSelection();
const value = this.get();
const next = value.slice(0, position) + value.slice(position + length);
this.set(next);
if (selection) {
let [start, end] = selection;
if (start >= position)
start = Math.max(position, start - length);
if (end >= position)
end = Math.max(position, end - length);
this.setSelection(start, end, selection[2]);
}
}
}
exports.InputFacade4 = InputFacade4;
//# sourceMappingURL=InputFacade.js.map