Skip to content

Commit 37aed34

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 introduce selections in "marker" events
1 parent 8b1b8cf commit 37aed34

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

src/json-crdt-extensions/peritext/editor/Editor.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -853,22 +853,22 @@ export class Editor<T = string> implements Printable {
853853
return true;
854854
}
855855

856-
public split(type?: SliceType, data?: unknown, slices: EditorSlices<T> = this.saved): void {
856+
public split(type?: SliceType, data?: unknown, selection: Range<T>[] | IterableIterator<Range<T>> = this.cursors(), slices: EditorSlices<T> = this.saved): void {
857857
if (type === void 0) {
858-
for (let i = this.cursors0(), cursor = i(); cursor; cursor = i()) {
859-
this.collapseCursor(cursor);
860-
const didInsertMarker = this.splitAt(cursor.start, slices);
861-
if (didInsertMarker) cursor.move(1);
858+
for (const range of selection) {
859+
this.collapseCursor(range);
860+
const didInsertMarker = this.splitAt(range.start, slices);
861+
if (didInsertMarker && range instanceof Cursor) range.move(1);
862862
}
863863
} else {
864-
for (let i = this.cursors0(), cursor = i(); cursor; cursor = i()) {
865-
this.collapseCursor(cursor);
864+
for (const range of selection) {
865+
this.collapseCursor(range);
866866
if (type === void 0) {
867867
// TODO: detect current block type
868868
type = CommonSliceType.p;
869869
}
870870
slices.insMarker(type, data);
871-
cursor.move(1);
871+
if (range instanceof Cursor) range.move(1);
872872
}
873873
}
874874
}
@@ -920,11 +920,12 @@ export class Editor<T = string> implements Printable {
920920
public tglMarker(
921921
type: SliceType,
922922
data?: unknown,
923+
selection: Range<T>[] | IterableIterator<Range<T>> = this.cursors(),
923924
slices: EditorSlices<T> = this.saved,
924925
def: SliceTypeStep = SliceTypeCon.p,
925926
): void {
926-
for (let i = this.cursors0(), cursor = i(); cursor; cursor = i())
927-
this.tglMarkerAt(cursor.start, type, data, slices, def);
927+
for (const range of selection)
928+
this.tglMarkerAt(range.start, type, data, slices, def);
928929
}
929930

930931
/**
@@ -935,15 +936,15 @@ export class Editor<T = string> implements Printable {
935936
* @param slices The slices set to use, if new marker is inserted at the start
936937
* of the document.
937938
*/
938-
public updMarker(type: SliceType, data?: unknown, slices: EditorSlices<T> = this.saved): void {
939-
for (let i = this.cursors0(), cursor = i(); cursor; cursor = i())
940-
this.updMarkerAt(cursor.start, type, data, slices);
939+
public updMarker(type: SliceType, data?: unknown, selection: Range<T>[] | IterableIterator<Range<T>> = this.cursors(), slices: EditorSlices<T> = this.saved): void {
940+
for (const range of selection)
941+
this.updMarkerAt(range.start, type, data, slices);
941942
}
942943

943-
public delMarker(): void {
944+
public delMarker(selection: Range<T>[] | IterableIterator<Range<T>> = this.cursors()): void {
944945
const markerPoints = new Set<MarkerOverlayPoint<T>>();
945-
for (let i = this.cursors0(), cursor = i(); cursor; cursor = i()) {
946-
const markerPoint = this.txt.overlay.getOrNextLowerMarker(cursor.start);
946+
for (const range of selection) {
947+
const markerPoint = this.txt.overlay.getOrNextLowerMarker(range.start);
947948
if (markerPoint) markerPoints.add(markerPoint);
948949
}
949950
for (const markerPoint of markerPoints) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,28 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
198198
this.undo?.capture();
199199
};
200200

201-
public readonly marker = (event: CustomEvent<events.MarkerDetail>) => {
202-
const {action, type, data} = event.detail;
201+
public readonly marker = ({detail}: CustomEvent<events.MarkerDetail>) => {
202+
const selection = [...this.getSelSet(detail)];
203+
this.moveSelSet(selection, detail);
204+
const {action, type, data} = detail;
203205
const editor = this.txt.editor;
204206
switch (action) {
205207
case 'ins': {
206-
editor.split(type, data);
208+
editor.split(type, data, selection);
207209
break;
208210
}
209211
case 'tog': {
210212
if (type === undefined) throw new Error('TYPE_REQUIRED');
211-
editor.tglMarker(type, data);
213+
editor.tglMarker(type, data, selection);
212214
break;
213215
}
214216
case 'upd': {
215217
if (type === undefined) throw new Error('TYPE_REQUIRED');
216-
editor.updMarker(type, data);
218+
editor.updMarker(type, data, selection);
217219
break;
218220
}
219221
case 'del': {
220-
editor.delMarker();
222+
editor.delMarker(selection);
221223
break;
222224
}
223225
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export interface FormatDetail extends SelectionDetailPart, SelectionMoveDetailPa
299299
* block. Removing a marker results into a "join" action, which merges two
300300
* adjacent blocks into one.
301301
*/
302-
export interface MarkerDetail {
302+
export interface MarkerDetail extends SelectionDetailPart, SelectionMoveDetailPart {
303303
/**
304304
* The action to perform.
305305
*

0 commit comments

Comments
 (0)