Skip to content

Commit 97c213c

Browse files
authored
Merge pull request #565 from streamich/peritext-slices
Peritext `Point` class implementation
2 parents 0838b13 + 45b1241 commit 97c213c

File tree

12 files changed

+1588
-0
lines changed

12 files changed

+1588
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Anchor} from './constants';
2+
import {Point} from './point/Point';
3+
import {printTree} from '../../util/print/printTree';
4+
import {ArrNode, StrNode} from '../../json-crdt/nodes';
5+
import {type ITimestampStruct} from '../../json-crdt-patch/clock';
6+
import type {Model} from '../../json-crdt/model';
7+
import type {Printable} from '../../util/print/types';
8+
9+
export class Peritext implements Printable {
10+
constructor(
11+
public readonly model: Model,
12+
public readonly str: StrNode,
13+
slices: ArrNode,
14+
) {}
15+
16+
public point(id: ITimestampStruct, anchor: Anchor = Anchor.After): Point {
17+
return new Point(this, id, anchor);
18+
}
19+
20+
public pointAt(pos: number, anchor: Anchor = Anchor.Before): Point {
21+
const str = this.str;
22+
const id = str.find(pos);
23+
if (!id) return this.point(str.id, Anchor.After);
24+
return this.point(id, anchor);
25+
}
26+
27+
public pointAtStart(): Point {
28+
return this.point(this.str.id, Anchor.After);
29+
}
30+
31+
public pointAtEnd(): Point {
32+
return this.point(this.str.id, Anchor.Before);
33+
}
34+
35+
// ---------------------------------------------------------------- Printable
36+
37+
public toString(tab: string = ''): string {
38+
const nl = () => '';
39+
return this.constructor.name + printTree(tab, [(tab) => this.str.toString(tab)]);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const enum Anchor {
2+
Before = 0,
3+
After = 1,
4+
}
5+
6+
export const enum SliceHeaderMask {
7+
X1Anchor = 0b1,
8+
X2Anchor = 0b10,
9+
Behavior = 0b11100,
10+
}
11+
12+
export const enum SliceHeaderShift {
13+
X1Anchor = 0,
14+
X2Anchor = 1,
15+
Behavior = 2,
16+
}
17+
18+
export const enum SliceBehavior {
19+
/**
20+
* A Split slice, which is used to mark a block split position in the document.
21+
* For example, paragraph, heading, blockquote, etc.
22+
*/
23+
Split = 0b000,
24+
25+
/**
26+
* Appends attributes to a stack of attributes for a specific slice type. This
27+
* is useful when the same slice type can have multiple attributes, like
28+
* inline comments, highlights, etc.
29+
*/
30+
Stack = 0b001,
31+
32+
/**
33+
* Overwrites the stack of attributes for a specific slice type. Could be used
34+
* for simple inline formatting, like bold, italic, etc.
35+
*/
36+
Overwrite = 0b010,
37+
38+
/**
39+
* Removes all attributes for a specific slice type. For example, could be
40+
* used to re-verse inline formatting, like bold, italic, etc.
41+
*/
42+
Erase = 0b011,
43+
}

0 commit comments

Comments
 (0)