Skip to content

Commit c6c5b62

Browse files
committed
feat(json-crdt-extensions): 🎸 improve Inline.attr() implementation
1 parent 2145d24 commit c6c5b62

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/json-crdt-extensions/peritext/block/Inline.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export class Inline extends Range implements Printable {
7777
return pos + chunkSlice.off;
7878
}
7979

80+
/**
81+
* @returns Returns the attributes of the inline, which are the slice
82+
* annotations and formatting applied to the inline.
83+
*/
8084
public attr(): InlineAttributes {
8185
const attr: InlineAttributes = {};
8286
const point = this.start as OverlayPoint;
@@ -86,6 +90,7 @@ export class Inline extends Range implements Printable {
8690
const slice = slices[i];
8791
const type = slice.type as PathStep;
8892
switch (slice.behavior) {
93+
case SliceBehavior.Cursor:
8994
case SliceBehavior.Stack: {
9095
let dataList: unknown[] = (attr[type] as unknown[]) || (attr[type] = []);
9196
if (!Array.isArray(dataList)) dataList = attr[type] = [dataList];
@@ -106,6 +111,7 @@ export class Inline extends Range implements Printable {
106111
}
107112
}
108113
}
114+
// TODO: Iterate over the markers...
109115
return attr;
110116
}
111117

src/json-crdt-extensions/peritext/block/__tests__/Inline.str.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,67 @@ const runStrTests = (setup: () => Kit) => {
4242
}
4343
});
4444
});
45+
46+
describe('.attr()', () => {
47+
test('returns all STACK annotations of a slice', () => {
48+
const {peritext} = setup();
49+
const overlay = peritext.overlay;
50+
peritext.editor.cursor.setAt(3, 3);
51+
peritext.editor.saved.insStack('bold', 1);
52+
peritext.editor.saved.insStack('bold', 2);
53+
peritext.editor.saved.insStack('em', 1);
54+
overlay.refresh();
55+
const [start, end] = [...overlay.points()];
56+
const inline = Inline.create(peritext, start, end);
57+
const attr = inline.attr();
58+
expect(attr.bold).toEqual([1, 2]);
59+
expect(attr.em).toEqual([1]);
60+
});
61+
62+
test('returns latest OVERWRITE annotation', () => {
63+
const {peritext} = setup();
64+
const overlay = peritext.overlay;
65+
peritext.editor.cursor.setAt(3, 3);
66+
peritext.editor.saved.insOverwrite('bold', 1);
67+
peritext.editor.saved.insOverwrite('bold', 2);
68+
peritext.editor.saved.insOverwrite('em', 1);
69+
overlay.refresh();
70+
const [start, end] = [...overlay.points()];
71+
const inline = Inline.create(peritext, start, end);
72+
const attr = inline.attr();
73+
expect(attr.bold).toBe(2);
74+
expect(attr.em).toBe(1);
75+
});
76+
77+
test('hides annotation hidden with another ERASE annotation', () => {
78+
const {peritext} = setup();
79+
const overlay = peritext.overlay;
80+
peritext.editor.cursor.setAt(3, 3);
81+
peritext.editor.saved.insOverwrite('bold');
82+
peritext.editor.saved.insErase('bold');
83+
peritext.editor.saved.insOverwrite('em');
84+
overlay.refresh();
85+
const [start, end] = [...overlay.points()];
86+
const inline = Inline.create(peritext, start, end);
87+
const attr = inline.attr();
88+
expect(attr.bold).toBe(undefined);
89+
expect(attr.em).toBe(1);
90+
});
91+
92+
test('concatenates with "," steps of nested Path type annotations', () => {
93+
const {peritext} = setup();
94+
const overlay = peritext.overlay;
95+
peritext.editor.cursor.setAt(3, 3);
96+
peritext.editor.saved.insStack(['bold', 'very'], 1);
97+
peritext.editor.saved.insStack(['bold', 'normal'], 2);
98+
overlay.refresh();
99+
const [start, end] = [...overlay.points()];
100+
const inline = Inline.create(peritext, start, end);
101+
const attr = inline.attr();
102+
expect(attr['bold,very']).toEqual([1]);
103+
expect(attr['bold,normal']).toEqual([2]);
104+
});
105+
});
45106
};
46107

47108
describe('Inline', () => {

0 commit comments

Comments
 (0)