Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 add inter-block iteration for inline el…
Browse files Browse the repository at this point in the history
…ements
  • Loading branch information
streamich committed Jun 7, 2024
1 parent 71e74e0 commit 22f6fe8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/json-crdt-extensions/peritext/block/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {CONST, updateJson, updateNum} from '../../../json-hash';
import {MarkerOverlayPoint} from '../overlay/MarkerOverlayPoint';
import {OverlayPoint} from '../overlay/OverlayPoint';
import {UndefEndIter, type UndefIterator} from '../../../util/iterator';
import {Inline} from './Inline';
import type {Path} from '../../../json-pointer';
import type {Printable} from 'tree-dump';
import type {Peritext} from '../Peritext';
Expand Down Expand Up @@ -93,6 +94,19 @@ export class Block<Attr = unknown> implements IBlock, Printable, Stateful {
return new UndefEndIter(this.tuples0());
}

public inline0(): UndefIterator<Inline> {
const txt = this.txt;
const iterator = this.tuples0();
return () => {
const pair = iterator();
return pair && Inline.create(txt, pair[0], pair[1]);
};
}

public inline(): IterableIterator<Inline> {
return new UndefEndIter(this.inline0());
}

// ----------------------------------------------------------------- Stateful

public hash: number = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ describe('tuples', () => {
expect(text2).toBe('\nworld');
});
});

describe('inline', () => {
test('in markup-less document', () => {
const {peritext} = setupHelloWorldKit();
peritext.refresh();
const blocks = peritext.blocks;
const block = blocks.root.children[0]!;
const text = [...block.inline()].map(inline => inline.text()).join('');
expect(text).toBe('hello world');
});

test('can iterate through all text chunks in two-block documents', () => {
const {peritext} = setupTwoBlockDocument();
expect(peritext.blocks.root.children.length).toBe(2);
const block1 = peritext.blocks.root.children[0]!;
const block2 = peritext.blocks.root.children[1]!;
const text1 = [...block1.inline()].map(inline => inline.text()).join('');
const text2 = [...block2.inline()].map(inline => inline.text()).join('');
expect(text1).toBe('hello ');
expect(text2).toBe('\nworld');
});
});

0 comments on commit 22f6fe8

Please sign in to comment.