Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 improve Inline.attr() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 11, 2024
1 parent 2145d24 commit c6c5b62
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/json-crdt-extensions/peritext/block/Inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export class Inline extends Range implements Printable {
return pos + chunkSlice.off;
}

/**
* @returns Returns the attributes of the inline, which are the slice
* annotations and formatting applied to the inline.
*/
public attr(): InlineAttributes {
const attr: InlineAttributes = {};
const point = this.start as OverlayPoint;
Expand All @@ -86,6 +90,7 @@ export class Inline extends Range implements Printable {
const slice = slices[i];
const type = slice.type as PathStep;
switch (slice.behavior) {
case SliceBehavior.Cursor:
case SliceBehavior.Stack: {
let dataList: unknown[] = (attr[type] as unknown[]) || (attr[type] = []);
if (!Array.isArray(dataList)) dataList = attr[type] = [dataList];
Expand All @@ -106,6 +111,7 @@ export class Inline extends Range implements Printable {
}
}
}
// TODO: Iterate over the markers...
return attr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,67 @@ const runStrTests = (setup: () => Kit) => {
}
});
});

describe('.attr()', () => {
test('returns all STACK annotations of a slice', () => {
const {peritext} = setup();
const overlay = peritext.overlay;
peritext.editor.cursor.setAt(3, 3);
peritext.editor.saved.insStack('bold', 1);
peritext.editor.saved.insStack('bold', 2);
peritext.editor.saved.insStack('em', 1);
overlay.refresh();
const [start, end] = [...overlay.points()];
const inline = Inline.create(peritext, start, end);
const attr = inline.attr();
expect(attr.bold).toEqual([1, 2]);
expect(attr.em).toEqual([1]);
});

test('returns latest OVERWRITE annotation', () => {
const {peritext} = setup();
const overlay = peritext.overlay;
peritext.editor.cursor.setAt(3, 3);
peritext.editor.saved.insOverwrite('bold', 1);
peritext.editor.saved.insOverwrite('bold', 2);
peritext.editor.saved.insOverwrite('em', 1);
overlay.refresh();
const [start, end] = [...overlay.points()];
const inline = Inline.create(peritext, start, end);
const attr = inline.attr();
expect(attr.bold).toBe(2);
expect(attr.em).toBe(1);
});

test('hides annotation hidden with another ERASE annotation', () => {
const {peritext} = setup();
const overlay = peritext.overlay;
peritext.editor.cursor.setAt(3, 3);
peritext.editor.saved.insOverwrite('bold');
peritext.editor.saved.insErase('bold');
peritext.editor.saved.insOverwrite('em');
overlay.refresh();
const [start, end] = [...overlay.points()];
const inline = Inline.create(peritext, start, end);
const attr = inline.attr();
expect(attr.bold).toBe(undefined);
expect(attr.em).toBe(1);
});

test('concatenates with "," steps of nested Path type annotations', () => {
const {peritext} = setup();
const overlay = peritext.overlay;
peritext.editor.cursor.setAt(3, 3);
peritext.editor.saved.insStack(['bold', 'very'], 1);
peritext.editor.saved.insStack(['bold', 'normal'], 2);
overlay.refresh();
const [start, end] = [...overlay.points()];
const inline = Inline.create(peritext, start, end);
const attr = inline.attr();
expect(attr['bold,very']).toEqual([1]);
expect(attr['bold,normal']).toEqual([2]);
});
});
};

describe('Inline', () => {
Expand Down

0 comments on commit c6c5b62

Please sign in to comment.