UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

51 lines (50 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PeritextEventTarget = void 0; const TypedEventTarget_1 = require("../../util/events/TypedEventTarget"); let __id = 0; class PeritextEventTarget extends TypedEventTarget_1.SubscriptionEventTarget { constructor() { super(...arguments); this.id = __id++; this.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(lenOrDetail, unit, at) { const detail = typeof lenOrDetail === 'object' ? lenOrDetail : { len: lenOrDetail, unit, at }; this.dispatch('delete', detail); } cursor(detail) { this.dispatch('cursor', detail); } move(len, unit, edge) { this.cursor({ len, unit, edge }); } format(a, behavior, data) { const detail = typeof a === 'object' && !Array.isArray(a) ? a : { type: a, behavior, data }; this.dispatch('format', detail); } marker(detail) { this.dispatch('marker', detail); } buffer(a, b) { const detail = typeof a === 'object' ? a : { action: a, format: b }; this.dispatch('buffer', detail); } } exports.PeritextEventTarget = PeritextEventTarget;