json-joy
Version:
Collection of libraries for building collaborative editing apps.
59 lines (58 loc) • 1.87 kB
JavaScript
import { SubscriptionEventTarget } from '../../util/events/TypedEventTarget';
let __id = 0;
export class PeritextEventTarget extends SubscriptionEventTarget {
id = __id++;
defaults = {};
dispatch(type, detail) {
const event = new CustomEvent(type, { detail });
this.dispatchEvent(event);
if (!event.defaultPrevented)
this.defaults[type]?.(event);
this.change(event);
}
change(ev) {
const event = new CustomEvent('change', { detail: { ev } });
this.dispatchEvent(event);
if (!event.defaultPrevented)
this.defaults.change?.(event);
}
insert(text) {
this.dispatch('insert', { text });
}
delete(a = {}, b, len, collapse) {
if (typeof a === 'number') {
this.dispatch('delete', { move: [['focus', b ?? 'char', a]] });
}
else if (typeof a === 'string') {
const move = [[a, b, len, collapse]];
this.dispatch('delete', { move });
}
else {
this.dispatch('delete', a);
}
}
cursor(detail) {
this.dispatch('cursor', detail);
}
move(a, b, len, collapse) {
if (typeof a === 'string') {
const move = [[a, b, len, collapse]];
this.cursor({ move });
}
else {
this.cursor({ move: a, at: b });
}
}
format(a, behavior, data) {
const detail = typeof a === 'object' && !Array.isArray(a) ? a : { type: a, behavior, data };
this.dispatch('format', detail);
}
marker(a, b, c) {
const detail = typeof a === 'object' ? a : { action: a, type: b, data: c };
this.dispatch('marker', detail);
}
buffer(a, b) {
const detail = typeof a === 'object' ? a : { action: a, format: b };
this.dispatch('buffer', detail);
}
}