diff --git a/src/json-crdt-peritext-ui/events/PeritextEventDefaults.ts b/src/json-crdt-peritext-ui/events/PeritextEventDefaults.ts index 8507ee9c0b..77a6f81d5e 100644 --- a/src/json-crdt-peritext-ui/events/PeritextEventDefaults.ts +++ b/src/json-crdt-peritext-ui/events/PeritextEventDefaults.ts @@ -23,13 +23,16 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap { public readonly insert = (event: CustomEvent) => { const text = event.detail.text; this.txt.editor.insert(text); - this.et.change(event); }; public readonly delete = (event: CustomEvent) => { - const {len = -1, unit = 'char'} = event.detail; - this.txt.editor.delete(len, unit); - this.et.change(event); + const {len = -1, unit = 'char', at} = event.detail; + const editor = this.txt.editor; + if (at !== undefined) { + const point = editor.point(at); + editor.cursor.set(point); + } + editor.delete(len, unit); }; public readonly cursor = (event: CustomEvent) => { @@ -62,7 +65,6 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap { } } } - this.et.change(event); return; } @@ -70,7 +72,6 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap { const isSpecificEdgeSelected = edge === 'focus' || edge === 'anchor'; if (isSpecificEdgeSelected) { editor.move(len, unit ?? 'char', edge === 'focus' ? 0 : 1, false); - this.et.change(event); return; } @@ -82,14 +83,12 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap { if (len > 0) cursor.collapseToEnd(); else cursor.collapseToStart(); } - this.et.change(event); return; } // If `unit` is specified. if (unit) { editor.select(unit); - this.et.change(event); return; } }; @@ -108,7 +107,6 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap { default: slices.insOverwrite(type, data); } - this.et.change(event); }; public readonly marker = (event: CustomEvent) => { diff --git a/src/json-crdt-peritext-ui/events/PeritextEventTarget.ts b/src/json-crdt-peritext-ui/events/PeritextEventTarget.ts index a6fa7a6a47..8492037cd1 100644 --- a/src/json-crdt-peritext-ui/events/PeritextEventTarget.ts +++ b/src/json-crdt-peritext-ui/events/PeritextEventTarget.ts @@ -5,21 +5,24 @@ export type PeritextEventHandlerMap = { [K in keyof PeritextEventMap]: (event: CustomEvent) => void; }; -let id = 0; +let __id = 0; export class PeritextEventTarget extends TypedEventTarget { - public readonly id: number = id++; + public readonly id: number = __id++; public defaults: Partial = {}; - public dispatch(type: K, detail: PeritextEventMap[K]): void { + public dispatch>(type: K, detail: Omit[K]): void { const event = new CustomEvent(type, {detail}); this.dispatchEvent(event); if (!event.defaultPrevented) this.defaults[type]?.(event); + this.change(event); } public change(ev?: CustomEvent): void { - this.dispatch('change', {ev}); + const event = new CustomEvent('change', {detail: {ev}}); + this.dispatchEvent(event); + if (!event.defaultPrevented) this.defaults.change?.(event); } public insert(text: string): void {