1- import { createDataKey , IndexBuilder } from '@editorjs/model' ;
1+ import { createDataKey , IndexBuilder , TextRange } from '@editorjs/model' ;
22import { BatchedOperation } from './BatchedOperation.js' ;
33import type { SerializedOperation } from './Operation.js' ;
44import { Operation , OperationType } from './Operation.js' ;
55
6- const templateIndex = new IndexBuilder ( )
6+ const createIndexByRange = ( range : TextRange ) => new IndexBuilder ( )
77 . addBlockIndex ( 0 )
88 . addDataKey ( createDataKey ( 'key' ) )
9- . addTextRange ( [ 0 , 0 ] )
9+ . addTextRange ( range )
1010 . build ( ) ;
1111
12+ const templateIndex = createIndexByRange ( [ 0 , 0 ] )
13+
1214const userId = 'user' ;
1315
1416describe ( 'Batch' , ( ) => {
1517 it ( 'should add Insert operation to batch' , ( ) => {
1618 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
17- const op2 = new Operation (
18- OperationType . Insert ,
19- new IndexBuilder ( ) . from ( templateIndex )
20- . addTextRange ( [ 1 , 1 ] )
21- . build ( ) ,
22- { payload : 'b' } ,
23- userId
24- ) ;
19+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
2520
2621 const batch = new BatchedOperation ( op1 ) ;
2722
@@ -34,14 +29,7 @@ describe('Batch', () => {
3429
3530 it ( 'should add Delete operation to batch' , ( ) => {
3631 const op1 = new Operation ( OperationType . Delete , templateIndex , { payload : 'a' } , userId ) ;
37- const op2 = new Operation (
38- OperationType . Delete ,
39- new IndexBuilder ( ) . from ( templateIndex )
40- . addTextRange ( [ 1 , 1 ] )
41- . build ( ) ,
42- { payload : 'b' } ,
43- userId
44- ) ;
32+ const op2 = new Operation ( OperationType . Delete , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
4533
4634 const batch = new BatchedOperation ( op1 ) ;
4735
@@ -55,14 +43,8 @@ describe('Batch', () => {
5543 describe ( 'from()' , ( ) => {
5644 it ( 'should create a new batch from an existing batch' , ( ) => {
5745 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
58- const op2 = new Operation (
59- OperationType . Insert ,
60- new IndexBuilder ( ) . from ( templateIndex )
61- . addTextRange ( [ 1 , 1 ] )
62- . build ( ) ,
63- { payload : 'b' } ,
64- userId
65- ) ;
46+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
47+
6648 const originalBatch = new BatchedOperation ( op1 ) ;
6749
6850 originalBatch . add ( op2 ) ;
@@ -87,14 +69,8 @@ describe('Batch', () => {
8769 describe ( 'inverse()' , ( ) => {
8870 it ( 'should inverse all operations in the batch' , ( ) => {
8971 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
90- const op2 = new Operation (
91- OperationType . Insert ,
92- new IndexBuilder ( ) . from ( templateIndex )
93- . addTextRange ( [ 1 , 1 ] )
94- . build ( ) ,
95- { payload : 'b' } ,
96- userId
97- ) ;
72+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
73+
9874 const batch = new BatchedOperation ( op1 ) ;
9975
10076 batch . add ( op2 ) ;
@@ -108,52 +84,37 @@ describe('Batch', () => {
10884
10985 describe ( 'transform()' , ( ) => {
11086 it ( 'should transform operations against another operation' , ( ) => {
111- const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
112- const op2 = new Operation (
113- OperationType . Insert ,
114- new IndexBuilder ( ) . from ( templateIndex )
115- . addTextRange ( [ 1 , 1 ] )
116- . build ( ) ,
117- { payload : 'b' } ,
118- userId
119- ) ;
87+ const op1 = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'a' } , userId ) ;
88+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 2 , 2 ] ) , { payload : 'b' } , userId ) ;
89+
12090 const batch = new BatchedOperation ( op1 ) ;
12191
12292 batch . add ( op2 ) ;
12393
124- const againstOp = new Operation (
125- OperationType . Insert ,
126- new IndexBuilder ( ) . from ( templateIndex )
127- . addTextRange ( [ 0 , 0 ] )
128- . build ( ) ,
129- { payload : 'x' } ,
130- 'other-user'
131- ) ;
94+ const againstOp = new Operation ( OperationType . Insert , createIndexByRange ( [ 0 , 0 ] ) , { payload : 'x' } , 'other-user' ) ;
13295
13396 const transformedBatch = batch . transform ( againstOp ) ;
13497
13598 expect ( transformedBatch ) . not . toBeNull ( ) ;
13699 expect ( transformedBatch ! . operations . length ) . toBe ( 2 ) ;
137100 // Check if text ranges were shifted by 1 due to insertion
138- expect ( transformedBatch ! . operations [ 0 ] . index . textRange ! [ 0 ] ) . toBe ( 1 ) ;
139- expect ( transformedBatch ! . operations [ 1 ] . index . textRange ! [ 0 ] ) . toBe ( 2 ) ;
101+ expect ( transformedBatch ! . operations [ 0 ] . index . textRange ! [ 0 ] ) . toBe ( 2 ) ;
102+ expect ( transformedBatch ! . operations [ 1 ] . index . textRange ! [ 0 ] ) . toBe ( 3 ) ;
140103 } ) ;
141104
142105 it ( 'should return batch with Neutral operations if no operations can be transformed' , ( ) => {
143- const op = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
106+ const op = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'a' } , userId ) ;
107+
144108 const batch = new BatchedOperation ( op ) ;
145109
146- const deleteIndex = new IndexBuilder ( )
147- . from ( templateIndex )
148- . addTextRange ( [ 0 , 2 ] )
149- . build ( ) ;
110+ const deleteIndex = createIndexByRange ( [ 0 , 2 ] )
150111
151112 // An operation that would make transformation impossible
152113 const againstOp = new Operation ( OperationType . Delete , deleteIndex , { payload : 'a' } , 'other-user' ) ;
153114
154115 const transformedBatch = batch . transform ( againstOp ) ;
155116
156- const neutralOp = new Operation ( OperationType . Neutral , templateIndex , { payload : 'a' } , userId ) ;
117+ const neutralOp = new Operation ( OperationType . Neutral , createIndexByRange ( [ 1 , 1 ] ) , { payload : [ ] } , userId ) ;
157118
158119 expect ( transformedBatch . operations [ 0 ] ) . toEqual ( neutralOp ) ;
159120 } ) ;
@@ -162,59 +123,35 @@ describe('Batch', () => {
162123 describe ( 'canAdd()' , ( ) => {
163124 it ( 'should return true for consecutive text operations of same type' , ( ) => {
164125 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
165- const op2 = new Operation (
166- OperationType . Insert ,
167- new IndexBuilder ( ) . from ( templateIndex )
168- . addTextRange ( [ 1 , 1 ] )
169- . build ( ) ,
170- { payload : 'b' } ,
171- userId
172- ) ;
126+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
127+
173128 const batch = new BatchedOperation ( op1 ) ;
174129
175130 expect ( batch . canAdd ( op2 ) ) . toBe ( true ) ;
176131 } ) ;
177132
178133 it ( 'should return false for non-consecutive text operations' , ( ) => {
179134 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
180- const op2 = new Operation (
181- OperationType . Insert ,
182- new IndexBuilder ( ) . from ( templateIndex )
183- . addTextRange ( [ 2 , 2 ] )
184- . build ( ) ,
185- { payload : 'b' } ,
186- userId
187- ) ;
135+ const op2 = new Operation ( OperationType . Insert , createIndexByRange ( [ 2 , 2 ] ) , { payload : 'b' } , userId ) ;
136+
188137 const batch = new BatchedOperation ( op1 ) ;
189138
190139 expect ( batch . canAdd ( op2 ) ) . toBe ( false ) ;
191140 } ) ;
192141
193142 it ( 'should return false for different operation types' , ( ) => {
194143 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
195- const op2 = new Operation (
196- OperationType . Delete ,
197- new IndexBuilder ( ) . from ( templateIndex )
198- . addTextRange ( [ 1 , 1 ] )
199- . build ( ) ,
200- { payload : 'b' } ,
201- userId
202- ) ;
144+ const op2 = new Operation ( OperationType . Delete , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
145+
203146 const batch = new BatchedOperation ( op1 ) ;
204147
205148 expect ( batch . canAdd ( op2 ) ) . toBe ( false ) ;
206149 } ) ;
207150
208151 it ( 'should return false for modify operations' , ( ) => {
209152 const op1 = new Operation ( OperationType . Insert , templateIndex , { payload : 'a' } , userId ) ;
210- const op2 = new Operation (
211- OperationType . Modify ,
212- new IndexBuilder ( ) . from ( templateIndex )
213- . addTextRange ( [ 1 , 1 ] )
214- . build ( ) ,
215- { payload : { tool : 'bold' } } ,
216- userId
217- ) ;
153+ const op2 = new Operation ( OperationType . Modify , createIndexByRange ( [ 1 , 1 ] ) , { payload : 'b' } , userId ) ;
154+
218155 const batch = new BatchedOperation ( op1 ) ;
219156
220157 expect ( batch . canAdd ( op2 ) ) . toBe ( false ) ;
0 commit comments