-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #632 from streamich/peritext-block-2
Peritext blocks layer iteration methods
- Loading branch information
Showing
14 changed files
with
316 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/json-crdt-extensions/peritext/block/__tests__/Block.iteration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {setupHelloWorldKit} from '../../__tests__/setup'; | ||
import {MarkerOverlayPoint} from '../../overlay/MarkerOverlayPoint'; | ||
import {OverlayPoint} from '../../overlay/OverlayPoint'; | ||
import {Inline} from '../Inline'; | ||
|
||
const setupTwoBlockDocument = () => { | ||
const kit = setupHelloWorldKit(); | ||
const {peritext} = kit; | ||
peritext.editor.cursor.setAt(1, 2); | ||
peritext.editor.saved.insStack('bold'); | ||
peritext.editor.cursor.setAt(7, 2); | ||
peritext.editor.saved.insStack('italic'); | ||
peritext.editor.cursor.setAt(8, 2); | ||
peritext.editor.saved.insStack('underline'); | ||
peritext.editor.cursor.setAt(6); | ||
peritext.editor.saved.insMarker('p'); | ||
peritext.editor.delCursors(); | ||
peritext.refresh(); | ||
return kit; | ||
}; | ||
|
||
describe('points', () => { | ||
test('no iteration in empty document', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.refresh(); | ||
const blocks = peritext.blocks; | ||
const block = blocks.root.children[0]!; | ||
const iterator = block.points0(); | ||
expect(iterator()).toBe(undefined); | ||
}); | ||
|
||
test('using flag, can receive START marker in empty document', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.refresh(); | ||
const blocks = peritext.blocks; | ||
const block = blocks.root.children[0]!; | ||
const iterator = block.points0(true); | ||
expect(iterator()).toBeInstanceOf(OverlayPoint); | ||
expect(iterator()).toBe(undefined); | ||
}); | ||
|
||
test('returns all overlay points in single block document', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.editor.cursor.setAt(3, 3); | ||
peritext.editor.saved.insStack('bold'); | ||
peritext.refresh(); | ||
const block = peritext.blocks.root.children[0]!; | ||
const iterator = block.points0(); | ||
const point1 = iterator(); | ||
const point2 = iterator(); | ||
const point3 = iterator(); | ||
expect(point1).toBeInstanceOf(OverlayPoint); | ||
expect(point2).toBeInstanceOf(OverlayPoint); | ||
expect(point3).toBe(undefined); | ||
}); | ||
|
||
test('returns all overlay points in single block document, including start marker', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.editor.cursor.setAt(3, 3); | ||
peritext.editor.saved.insStack('bold'); | ||
peritext.refresh(); | ||
const block = peritext.blocks.root.children[0]!; | ||
const iterator = block.points0(true); | ||
expect(iterator()).toBeInstanceOf(OverlayPoint); | ||
expect(iterator()).toBeInstanceOf(OverlayPoint); | ||
expect(iterator()).toBeInstanceOf(OverlayPoint); | ||
expect(iterator()).toBe(undefined); | ||
}); | ||
|
||
test('returns only points within that block, in two-block document', () => { | ||
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 points1 = [...block1.points()]; | ||
const points2 = [...block2.points()]; | ||
expect(points1.length).toBe(2); | ||
expect(points2.length).toBe(4); | ||
}); | ||
|
||
test('can iterate including marker points, in two-block document', () => { | ||
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 points1 = [...block1.points(true)]; | ||
const points2 = [...block2.points(true)]; | ||
expect(points1.length).toBe(3); | ||
expect(points2.length).toBe(5); | ||
expect(points1[0]).toBeInstanceOf(OverlayPoint); | ||
expect(points1[0]).toBe(peritext.overlay.START); | ||
expect(points2[0]).toBeInstanceOf(MarkerOverlayPoint); | ||
}); | ||
}); | ||
|
||
describe('tuples', () => { | ||
test('in markup-less document, returns a single pair', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.refresh(); | ||
const blocks = peritext.blocks; | ||
const block = blocks.root.children[0]!; | ||
const pairs = [...block.tuples()]; | ||
expect(pairs.length).toBe(1); | ||
expect(pairs[0]).toEqual([peritext.overlay.START, peritext.overlay.END]); | ||
}); | ||
|
||
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 tuples1 = [...block1.tuples()]; | ||
const tuples2 = [...block2.tuples()]; | ||
expect(tuples1.length).toBe(3); | ||
const text1 = tuples1.map(([p1, p2]) => Inline.create(peritext, p1, p2).text()).join(''); | ||
const text2 = tuples2.map(([p1, p2]) => Inline.create(peritext, p1, p2).text()).join(''); | ||
expect(text1).toBe('hello '); | ||
expect(text2).toBe('\nworld'); | ||
}); | ||
}); | ||
|
||
describe('texts', () => { | ||
test('in markup-less document', () => { | ||
const {peritext} = setupHelloWorldKit(); | ||
peritext.refresh(); | ||
const blocks = peritext.blocks; | ||
const block = blocks.root.children[0]!; | ||
const text = [...block.texts()].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.texts()].map((inline) => inline.text()).join(''); | ||
const text2 = [...block2.texts()].map((inline) => inline.text()).join(''); | ||
expect(text1).toBe('hello '); | ||
expect(text2).toBe('\nworld'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.