11import { s } from '../../../json-crdt-patch' ;
2+ import { Model } from '../../../json-crdt/model' ;
3+ import { SchemaToJsonNode } from '../../../json-crdt/schema/types' ;
24import { ModelWithExt , ext } from '../../ModelWithExt' ;
35
4- /**
5- * Creates a Peritext instance with text "0123456789", with single-char and
6- * block-wise chunks, as well as with plenty of tombstones.
7- */
8- export const setupNumbersWithTombstones = ( ) => {
9- const schema = s . obj ( {
10- text : ext . peritext . new ( '1234' ) ,
11- } ) ;
12- const model = ModelWithExt . create ( schema ) ;
13- const str = model . s . text . toExt ( ) . text ( ) ;
14- str . ins ( 1 , '234' ) ;
15- str . ins ( 2 , '345' ) ;
16- str . ins ( 3 , '456' ) ;
17- str . ins ( 4 , '567' ) ;
18- str . ins ( 5 , '678' ) ;
19- str . ins ( 6 , '789' ) ;
20- str . del ( 7 , 1 ) ;
21- str . del ( 8 , 1 ) ;
22- str . ins ( 0 , '0' ) ;
23- str . del ( 1 , 4 ) ;
24- str . del ( 2 , 1 ) ;
25- str . ins ( 1 , '1' ) ;
26- str . del ( 0 , 1 ) ;
27- str . ins ( 0 , '0' ) ;
28- str . ins ( 2 , '234' ) ;
29- str . del ( 4 , 7 ) ;
30- str . del ( 4 , 2 ) ;
31- str . del ( 7 , 3 ) ;
32- str . ins ( 6 , '6789' ) ;
33- str . del ( 7 , 2 ) ;
34- str . ins ( 7 , '78' ) ;
35- str . del ( 10 , 2 ) ;
36- str . del ( 2 , 3 ) ;
37- str . ins ( 2 , '234' ) ;
38- if ( str . view ( ) !== '0123456789' ) throw new Error ( 'Invalid text' ) ;
6+ export type Schema = ReturnType < typeof schema > ;
7+ export type Kit = ReturnType < typeof setupKit > ;
8+
9+ const schema = ( text : string ) => s . obj ( {
10+ text : ext . peritext . new ( text ) ,
11+ } ) ;
12+
13+ export const setupKit = ( initialText : string = '' , edits : ( model : Model < SchemaToJsonNode < Schema > > ) => void = ( ) => { } ) => {
14+ const model = ModelWithExt . create ( schema ( initialText ) ) ;
15+ edits ( model ) ;
3916 const api = model . api ;
4017 const peritextApi = model . s . text . toExt ( ) ;
4118 const peritext = peritextApi . txt ;
@@ -49,3 +26,70 @@ export const setupNumbersWithTombstones = () => {
4926 editor,
5027 } ;
5128} ;
29+
30+ export const setupHelloWorldKit = ( ) : Kit => {
31+ return setupKit ( '' , ( model ) => {
32+ const str = model . s . text . toExt ( ) . text ( ) ;
33+ str . ins ( 0 , 'hello world' ) ;
34+ if ( str . view ( ) !== 'hello world' ) throw new Error ( 'Invalid text' ) ;
35+ } ) ;
36+ } ;
37+
38+ export const setupHelloWorldWithFewEditsKit = ( ) : Kit => {
39+ return setupKit ( '' , ( model ) => {
40+ const str = model . s . text . toExt ( ) . text ( ) ;
41+ str . ins ( 0 , 'wworld' ) ;
42+ str . ins ( 0 , 'helo ' ) ;
43+ str . ins ( 2 , 'l' ) ;
44+ str . del ( 7 , 1 ) ;
45+ if ( str . view ( ) !== 'hello world' ) throw new Error ( 'Invalid text' ) ;
46+ } ) ;
47+ } ;
48+
49+ /**
50+ * Creates a Peritext instance with text "0123456789", no edits.
51+ */
52+ export const setupNumbersKit = ( ) : Kit => {
53+ return setupKit ( '' , ( model ) => {
54+ const str = model . s . text . toExt ( ) . text ( ) ;
55+ str . ins ( 0 , '0123456789' ) ;
56+ if ( str . view ( ) !== '0123456789' ) throw new Error ( 'Invalid text' ) ;
57+ } ) ;
58+ } ;
59+
60+ /**
61+ * Creates a Peritext instance with text "0123456789", with single-char and
62+ * block-wise chunks, as well as with plenty of tombstones.
63+ */
64+ export const setupNumbersWithTombstonesKit = ( ) : Kit => {
65+ return setupKit ( '1234' , ( model ) => {
66+ const str = model . s . text . toExt ( ) . text ( ) ;
67+ str . ins ( 0 , '234' ) ;
68+ str . ins ( 1 , '234' ) ;
69+ str . ins ( 2 , '345' ) ;
70+ str . ins ( 3 , '456' ) ;
71+ str . ins ( 4 , '567' ) ;
72+ str . ins ( 5 , '678' ) ;
73+ str . ins ( 6 , '789' ) ;
74+ str . del ( 7 , 1 ) ;
75+ str . del ( 8 , 1 ) ;
76+ str . ins ( 0 , '0' ) ;
77+ str . del ( 1 , 4 ) ;
78+ str . del ( 2 , 1 ) ;
79+ str . ins ( 1 , '1' ) ;
80+ str . del ( 0 , 1 ) ;
81+ str . ins ( 0 , '0' ) ;
82+ str . ins ( 2 , '234' ) ;
83+ str . del ( 4 , 7 ) ;
84+ str . del ( 4 , 2 ) ;
85+ str . del ( 7 , 3 ) ;
86+ str . ins ( 6 , '6789' ) ;
87+ str . del ( 7 , 2 ) ;
88+ str . ins ( 7 , '78' ) ;
89+ str . del ( 10 , 2 ) ;
90+ str . del ( 2 , 3 ) ;
91+ str . ins ( 2 , '234' ) ;
92+ str . del ( 10 , 3 ) ;
93+ if ( str . view ( ) !== '0123456789' ) throw new Error ( 'Invalid text' ) ;
94+ } ) ;
95+ } ;
0 commit comments