UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

68 lines (67 loc) 2.15 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(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, type, stack, data) { const detail = typeof a === 'object' && !Array.isArray(a) ? a : { action: a, type, stack, 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); } } exports.PeritextEventTarget = PeritextEventTarget;