@@ -32,23 +32,15 @@ describe("PositionStorage with local editor", () => {
32
32
33
33
describe ( "set and get positions" , ( ) => {
34
34
it ( "should store and retrieve positions without Y.js" , ( ) => {
35
- positionStorage . set ( "test-id" , 10 ) ;
36
- expect ( positionStorage . get ( "test-id" ) ) . toBe ( 10 ) ;
37
- } ) ;
35
+ const getPos = positionStorage . track ( 10 ) ;
38
36
39
- it ( "should handle right side positions" , ( ) => {
40
- positionStorage . set ( "test-id" , 10 , "right" ) ;
41
- expect ( positionStorage . get ( "test-id" ) ) . toBe ( 10 ) ;
37
+ expect ( getPos ( ) ) . toBe ( 10 ) ;
42
38
} ) ;
43
39
44
- it ( "should be undefined when getting a non-existent position" , ( ) => {
45
- expect ( positionStorage . get ( "non-existent" ) ) . toBeUndefined ( ) ;
46
- } ) ;
40
+ it ( "should handle right side positions" , ( ) => {
41
+ const getPos = positionStorage . track ( 10 , "right" ) ;
47
42
48
- it ( "should remove positions" , ( ) => {
49
- positionStorage . set ( "test-id" , 10 ) ;
50
- positionStorage . remove ( "test-id" ) ;
51
- expect ( positionStorage . get ( "test-id" ) ) . toBeUndefined ( ) ;
43
+ expect ( getPos ( ) ) . toBe ( 10 ) ;
52
44
} ) ;
53
45
} ) ;
54
46
@@ -73,7 +65,7 @@ describe("PositionStorage with local editor", () => {
73
65
) ;
74
66
75
67
// Start tracking
76
- positionStorage . set ( "test-id" , 10 ) ;
68
+ const getPos = positionStorage . track ( 10 ) ;
77
69
78
70
// Move the cursor to the start of the document
79
71
editor . setTextCursorPosition ( editor . document [ 0 ] , "start" ) ;
@@ -88,7 +80,7 @@ describe("PositionStorage with local editor", () => {
88
80
] ) ;
89
81
90
82
// Position should be updated according to mapping
91
- expect ( positionStorage . get ( "test-id" ) ) . toBe ( 14 ) ;
83
+ expect ( getPos ( ) ) . toBe ( 14 ) ;
92
84
} ) ;
93
85
94
86
it ( "should not update mapping for local transactions after the position" , ( ) => {
@@ -111,7 +103,7 @@ describe("PositionStorage with local editor", () => {
111
103
"before"
112
104
) ;
113
105
// Start tracking
114
- positionStorage . set ( "test-id" , 10 ) ;
106
+ const getPos = positionStorage . track ( 10 ) ;
115
107
116
108
// Move the cursor to the end of the document
117
109
editor . setTextCursorPosition ( editor . document [ 0 ] , "end" ) ;
@@ -126,7 +118,7 @@ describe("PositionStorage with local editor", () => {
126
118
] ) ;
127
119
128
120
// Position should not be updated
129
- expect ( positionStorage . get ( "test-id" ) ) . toBe ( 10 ) ;
121
+ expect ( getPos ( ) ) . toBe ( 10 ) ;
130
122
} ) ;
131
123
132
124
it ( "should track positions on each side" , ( ) => {
@@ -138,21 +130,20 @@ describe("PositionStorage with local editor", () => {
138
130
] ) ;
139
131
140
132
// Store position at "Hello| World"
141
- positionStorage . set ( "cursor" , 6 ) ;
142
- positionStorage . set ( "start" , 3 ) ;
143
- positionStorage . set ( "start-right" , 3 , "right" ) ;
144
- positionStorage . set ( "pos-after" , 4 ) ;
145
- positionStorage . set ( "pos-after-right" , 4 , "right" ) ;
146
-
133
+ const getCursorPos = positionStorage . track ( 6 ) ;
134
+ const getStartPos = positionStorage . track ( 3 ) ;
135
+ const getStartRightPos = positionStorage . track ( 3 , "right" ) ;
136
+ const getPosAfterPos = positionStorage . track ( 4 ) ;
137
+ const getPosAfterRightPos = positionStorage . track ( 4 , "right" ) ;
147
138
// Insert text at the beginning
148
139
editor . _tiptapEditor . commands . insertContentAt ( 3 , "Test " ) ;
149
140
150
141
// Position should be updated
151
- expect ( positionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
152
- expect ( positionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
153
- expect ( positionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
154
- expect ( positionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
155
- expect ( positionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
142
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
143
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
144
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
145
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
146
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
156
147
} ) ;
157
148
158
149
it ( "should handle multiple transactions" , ( ) => {
@@ -164,11 +155,11 @@ describe("PositionStorage with local editor", () => {
164
155
] ) ;
165
156
166
157
// Store position at "Hello| World"
167
- positionStorage . set ( "cursor" , 6 ) ;
168
- positionStorage . set ( "start" , 3 ) ;
169
- positionStorage . set ( "start-right" , 3 , "right" ) ;
170
- positionStorage . set ( "pos-after" , 4 ) ;
171
- positionStorage . set ( "pos-after-right" , 4 , "right" ) ;
158
+ const getCursorPos = positionStorage . track ( 6 ) ;
159
+ const getStartPos = positionStorage . track ( 3 ) ;
160
+ const getStartRightPos = positionStorage . track ( 3 , "right" ) ;
161
+ const getPosAfterPos = positionStorage . track ( 4 ) ;
162
+ const getPosAfterRightPos = positionStorage . track ( 4 , "right" ) ;
172
163
173
164
// Insert text at the beginning
174
165
editor . _tiptapEditor . commands . insertContentAt ( 3 , "T" ) ;
@@ -178,11 +169,11 @@ describe("PositionStorage with local editor", () => {
178
169
editor . _tiptapEditor . commands . insertContentAt ( 7 , " " ) ;
179
170
180
171
// Position should be updated
181
- expect ( positionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
182
- expect ( positionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
183
- expect ( positionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
184
- expect ( positionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
185
- expect ( positionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
172
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
173
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
174
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
175
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
176
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
186
177
} ) ;
187
178
} ) ;
188
179
@@ -268,25 +259,25 @@ describe("PositionStorage with remote editor", () => {
268
259
] ) ;
269
260
270
261
// Store position at "Hello| World"
271
- localPositionStorage . set ( "cursor" , 6 ) ;
262
+ const getCursorPos = localPositionStorage . track ( 6 ) ;
272
263
// Store position at "|Hello World"
273
- localPositionStorage . set ( "start" , 3 ) ;
264
+ const getStartPos = localPositionStorage . track ( 3 ) ;
274
265
// Store position at "|Hello World" (but on the right side)
275
- localPositionStorage . set ( "start-right" , 3 , "right" ) ;
266
+ const getStartRightPos = localPositionStorage . track ( 3 , "right" ) ;
276
267
// Store position at "H|ello World"
277
- localPositionStorage . set ( "pos-after" , 4 ) ;
268
+ const getPosAfterPos = localPositionStorage . track ( 4 ) ;
278
269
// Store position at "H|ello World" (but on the right side)
279
- localPositionStorage . set ( "pos-after-right" , 4 , "right" ) ;
270
+ const getPosAfterRightPos = localPositionStorage . track ( 4 , "right" ) ;
280
271
281
272
// Insert text at the beginning
282
273
localEditor . _tiptapEditor . commands . insertContentAt ( 3 , "Test " ) ;
283
274
284
275
// Position should be updated
285
- expect ( localPositionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
286
- expect ( localPositionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
287
- expect ( localPositionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
288
- expect ( localPositionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
289
- expect ( localPositionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
276
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
277
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
278
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
279
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
280
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
290
281
} ) ;
291
282
292
283
it ( "should handle multiple transactions when collaborating" , ( ) => {
@@ -298,15 +289,15 @@ describe("PositionStorage with remote editor", () => {
298
289
] ) ;
299
290
300
291
// Store position at "Hello| World"
301
- localPositionStorage . set ( "cursor" , 6 ) ;
292
+ const getCursorPos = localPositionStorage . track ( 6 ) ;
302
293
// Store position at "|Hello World"
303
- localPositionStorage . set ( "start" , 3 ) ;
294
+ const getStartPos = localPositionStorage . track ( 3 ) ;
304
295
// Store position at "|Hello World" (but on the right side)
305
- localPositionStorage . set ( "start-right" , 3 , "right" ) ;
296
+ const getStartRightPos = localPositionStorage . track ( 3 , "right" ) ;
306
297
// Store position at "H|ello World"
307
- localPositionStorage . set ( "pos-after" , 4 ) ;
298
+ const getPosAfterPos = localPositionStorage . track ( 4 ) ;
308
299
// Store position at "H|ello World" (but on the right side)
309
- localPositionStorage . set ( "pos-after-right" , 4 , "right" ) ;
300
+ const getPosAfterRightPos = localPositionStorage . track ( 4 , "right" ) ;
310
301
311
302
// Insert text at the beginning
312
303
localEditor . _tiptapEditor . commands . insertContentAt ( 3 , "T" ) ;
@@ -316,11 +307,11 @@ describe("PositionStorage with remote editor", () => {
316
307
localEditor . _tiptapEditor . commands . insertContentAt ( 7 , " " ) ;
317
308
318
309
// Position should be updated
319
- expect ( localPositionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
320
- expect ( localPositionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
321
- expect ( localPositionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
322
- expect ( localPositionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
323
- expect ( localPositionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
310
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
311
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
312
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
313
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
314
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
324
315
} ) ;
325
316
326
317
it ( "should update the local position from a remote transaction" , ( ) => {
@@ -332,25 +323,25 @@ describe("PositionStorage with remote editor", () => {
332
323
] ) ;
333
324
334
325
// Store position at "Hello| World"
335
- localPositionStorage . set ( "cursor" , 6 ) ;
326
+ const getCursorPos = localPositionStorage . track ( 6 ) ;
336
327
// Store position at "|Hello World"
337
- localPositionStorage . set ( "start" , 3 ) ;
328
+ const getStartPos = localPositionStorage . track ( 3 ) ;
338
329
// Store position at "|Hello World" (but on the right side)
339
- localPositionStorage . set ( "start-right" , 3 , "right" ) ;
330
+ const getStartRightPos = localPositionStorage . track ( 3 , "right" ) ;
340
331
// Store position at "H|ello World"
341
- localPositionStorage . set ( "pos-after" , 4 ) ;
332
+ const getPosAfterPos = localPositionStorage . track ( 4 ) ;
342
333
// Store position at "H|ello World" (but on the right side)
343
- localPositionStorage . set ( "pos-after-right" , 4 , "right" ) ;
334
+ const getPosAfterRightPos = localPositionStorage . track ( 4 , "right" ) ;
344
335
345
336
// Insert text at the beginning
346
337
localEditor . _tiptapEditor . commands . insertContentAt ( 3 , "Test " ) ;
347
338
348
339
// Position should be updated
349
- expect ( localPositionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
350
- expect ( localPositionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
351
- expect ( localPositionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
352
- expect ( localPositionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
353
- expect ( localPositionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
340
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
341
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
342
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
343
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
344
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
354
345
} ) ;
355
346
356
347
it ( "should update the remote position from a remote transaction" , ( ) => {
@@ -362,25 +353,25 @@ describe("PositionStorage with remote editor", () => {
362
353
] ) ;
363
354
364
355
// Store position at "Hello| World"
365
- remotePositionStorage . set ( "cursor" , 6 ) ;
356
+ const getCursorPos = remotePositionStorage . track ( 6 ) ;
366
357
// Store position at "|Hello World"
367
- remotePositionStorage . set ( "start" , 3 ) ;
358
+ const getStartPos = remotePositionStorage . track ( 3 ) ;
368
359
// Store position at "|Hello World" (but on the right side)
369
- remotePositionStorage . set ( "start-right" , 3 , "right" ) ;
360
+ const getStartRightPos = remotePositionStorage . track ( 3 , "right" ) ;
370
361
// Store position at "H|ello World"
371
- remotePositionStorage . set ( "pos-after" , 4 ) ;
362
+ const getPosAfterPos = remotePositionStorage . track ( 4 ) ;
372
363
// Store position at "H|ello World" (but on the right side)
373
- remotePositionStorage . set ( "pos-after-right" , 4 , "right" ) ;
364
+ const getPosAfterRightPos = remotePositionStorage . track ( 4 , "right" ) ;
374
365
375
366
// Insert text at the beginning
376
367
localEditor . _tiptapEditor . commands . insertContentAt ( 3 , "Test " ) ;
377
368
378
369
// Position should be updated
379
- expect ( remotePositionStorage . get ( "cursor" ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
380
- expect ( remotePositionStorage . get ( "start" ) ) . toBe ( 3 ) ; // 3
381
- expect ( remotePositionStorage . get ( "start-right" ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
382
- expect ( remotePositionStorage . get ( "pos-after" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
383
- expect ( remotePositionStorage . get ( "pos-after-right" ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
370
+ expect ( getCursorPos ( ) ) . toBe ( 11 ) ; // 6 + 5 ("Test " length)
371
+ expect ( getStartPos ( ) ) . toBe ( 3 ) ; // 3
372
+ expect ( getStartRightPos ( ) ) . toBe ( 8 ) ; // 3 + 5 ("Test " length)
373
+ expect ( getPosAfterPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
374
+ expect ( getPosAfterRightPos ( ) ) . toBe ( 9 ) ; // 4 + 5 ("Test " length)
384
375
} ) ;
385
376
} ) ;
386
377
} ) ;
0 commit comments