Skip to content

Commit ca8b147

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 improve "delete" event interface
1 parent 0b72bc9 commit ca8b147

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

src/json-crdt-peritext-ui/events/PeritextEventTarget.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class PeritextEventTarget extends SubscriptionEventTarget<PeritextEventMa
4444
this.dispatch('insert', {text});
4545
}
4646

47+
public delete(len: number, unit?: SelectionMoveInstruction[1]): void;
4748
public delete(
4849
edge: SelectionMoveInstruction[0],
4950
unit: SelectionMoveInstruction[1],
@@ -52,11 +53,13 @@ export class PeritextEventTarget extends SubscriptionEventTarget<PeritextEventMa
5253
): void;
5354
public delete(detail?: DeleteDetail): void;
5455
public delete(a: any = {}, b?: any, len?: any, collapse?: any): void {
55-
if (typeof a === 'string') {
56+
if (typeof a === 'number') {
57+
this.dispatch('delete', {move: [['focus', b ?? 'char', a]]});
58+
} else if (typeof a === 'string') {
5659
const move: SelectionMoveInstruction[] = [[a as SelectionMoveInstruction[0], b, len, collapse]];
5760
this.dispatch('delete', {move});
5861
} else {
59-
this.dispatch('delete', {move: a, at: b});
62+
this.dispatch('delete', a);
6063
}
6164
}
6265

src/json-crdt-peritext-ui/events/__tests__/delete.spec.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,12 @@ const testSuite = (getKit: () => Kit) => {
155155
kit.et.insert(' ');
156156
kit.editor.cursor.setAt(8);
157157
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz');
158-
const DIRECTION = 0;
159-
kit.et.delete(DIRECTION, 'word');
158+
kit.et.delete({
159+
move: [
160+
['start', 'word', -1],
161+
['end', 'word', 1],
162+
],
163+
});
160164
expect(kit.editor.text()).toBe('abcde klmnopqrstuvwxyz');
161165
});
162166

@@ -168,15 +172,36 @@ const testSuite = (getKit: () => Kit) => {
168172
kit.et.insert(' ');
169173
kit.editor.delCursors();
170174
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz');
171-
kit.et.delete(0, 'word', 8);
175+
kit.et.delete({
176+
at: [8],
177+
move: [
178+
['start', 'word', -1],
179+
['end', 'word', 1],
180+
],
181+
add: true,
182+
});
172183
expect(kit.editor.text()).toBe('abcde klmnopqrstuvwxyz');
173184
expect(kit.editor.cursor.start.viewPos()).toBe(6);
174185
expect(kit.editor.cursor.isCollapsed()).toBe(true);
175-
kit.et.delete(0, 'word', 3);
186+
kit.et.delete({
187+
at: [0],
188+
move: [
189+
['start', 'word', -1],
190+
['end', 'word', 1],
191+
],
192+
add: true,
193+
});
176194
expect(kit.editor.text()).toBe(' klmnopqrstuvwxyz');
177195
expect(kit.editor.cursor.start.viewPos()).toBe(0);
178196
expect(kit.editor.cursor.isCollapsed()).toBe(true);
179-
kit.et.delete(0, 'word', 10);
197+
kit.et.delete({
198+
at: [10],
199+
move: [
200+
['start', 'word', -1],
201+
['end', 'word', 1],
202+
],
203+
add: true,
204+
});
180205
expect(kit.editor.text()).toBe(' ');
181206
expect(kit.editor.cursor.start.viewPos()).toBe(2);
182207
expect(kit.editor.cursor.isCollapsed()).toBe(true);
@@ -216,8 +241,12 @@ const testSuite = (getKit: () => Kit) => {
216241
kit.et.insert('\n');
217242
kit.editor.cursor.setAt(8);
218243
expect(kit.editor.text()).toBe('abcde\nfghij\nklmnopqrstuvwxyz');
219-
const DIRECTION = 0;
220-
kit.et.delete(DIRECTION, 'line');
244+
kit.et.delete({
245+
move: [
246+
['start', 'line', -1],
247+
['end', 'line', 1],
248+
],
249+
});
221250
expect(kit.editor.text()).toBe('abcde\n\nklmnopqrstuvwxyz');
222251
});
223252
});

src/json-crdt-peritext-ui/events/defaults/PeritextEventDefaults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
133133
};
134134

135135
public readonly delete = ({detail}: CustomEvent<events.DeleteDetail>) => {
136-
const {move} = detail;
136+
const {move, add, at} = detail;
137137
const set = [...this.getSelSet(detail)];
138138
const editor = this.txt.editor;
139139
let deleted: boolean = false;
@@ -158,6 +158,7 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
158158
start.refAfter();
159159
range.set(start);
160160
}
161+
if (add && at) editor.cursor.setRange(set[0]);
161162
this.undo?.capture();
162163
};
163164

src/json-crdt-peritext-ui/events/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ export interface InsertDetail extends RangeEventDetail {
146146
* are collapsed to a single point, while deleting all text and any annotations
147147
* contained in the selections.
148148
*/
149-
export interface DeleteDetail extends RangeEventDetail {}
149+
export interface DeleteDetail extends RangeEventDetail {
150+
/**
151+
* If true and `at` is specified, the resulting `at` range will be set
152+
* as the document cursor.
153+
*/
154+
add?: boolean;
155+
}
150156

151157
/**
152158
* The `cursor` event is emitted when caret or selection is changed. The event

0 commit comments

Comments
 (0)