Skip to content

Commit 5d64e69

Browse files
committed
refactor(json-crdt-extensions): 💡 simplify .expand() method
1 parent 6e4ca48 commit 5d64e69

File tree

1 file changed

+3
-67
lines changed
  • src/json-crdt-extensions/peritext/slice

1 file changed

+3
-67
lines changed

src/json-crdt-extensions/peritext/slice/Range.ts

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Point} from '../point/Point';
22
import {Anchor} from '../constants';
3-
import {type ITimestampStruct, tick} from '../../../json-crdt-patch/clock';
3+
import type {ITimestampStruct} from '../../../json-crdt-patch/clock';
44
import type {Printable} from '../../../util/print/types';
55
import type {AbstractRga, Chunk} from '../../../json-crdt/nodes/rga';
66

@@ -161,72 +161,8 @@ export class Range<T = string> implements Printable {
161161
* (2) anchors of non-deleted adjacent chunks.
162162
*/
163163
public expand(): void {
164-
this.expandStart();
165-
this.expandEnd();
166-
}
167-
168-
public expandStart(): void {
169-
const {start, rga: rga} = this;
170-
let chunk = start.chunk();
171-
if (!chunk) return;
172-
if (!chunk.del) {
173-
if (start.anchor === Anchor.After) return;
174-
const pointIsStartOfChunk = start.id.time === chunk.id.time;
175-
if (!pointIsStartOfChunk) {
176-
start.id = tick(start.id, -1);
177-
start.anchor = Anchor.After;
178-
return;
179-
}
180-
}
181-
while (chunk) {
182-
const prev = rga.prev(chunk);
183-
if (!prev) {
184-
start.id = chunk.id;
185-
start.anchor = Anchor.Before;
186-
break;
187-
} else {
188-
if (prev.del) {
189-
chunk = prev;
190-
continue;
191-
} else {
192-
start.id = prev.span > 1 ? tick(prev.id, prev.span - 1) : prev.id;
193-
start.anchor = Anchor.After;
194-
break;
195-
}
196-
}
197-
}
198-
}
199-
200-
public expandEnd(): void {
201-
const {end, rga: rga} = this;
202-
let chunk = end.chunk();
203-
if (!chunk) return;
204-
if (!chunk.del) {
205-
if (end.anchor === Anchor.Before) return;
206-
const pointIsEndOfChunk = end.id.time === chunk.id.time + chunk.span - 1;
207-
if (!pointIsEndOfChunk) {
208-
end.id = tick(end.id, 1);
209-
end.anchor = Anchor.Before;
210-
return;
211-
}
212-
}
213-
while (chunk) {
214-
const next = rga.next(chunk);
215-
if (!next) {
216-
end.id = chunk.span > 1 ? tick(chunk.id, chunk.span - 1) : chunk.id;
217-
end.anchor = Anchor.After;
218-
break;
219-
} else {
220-
if (next.del) {
221-
chunk = next;
222-
continue;
223-
} else {
224-
end.id = next.id;
225-
end.anchor = Anchor.Before;
226-
break;
227-
}
228-
}
229-
}
164+
this.start.refAfter();
165+
this.end.refBefore();
230166
}
231167

232168
// -------------------------------------------------- View coordinate methods

0 commit comments

Comments
 (0)