| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
ve.dm.Transaction = function VeDmTransaction( operations, authorId ) { |
| 27 |
this.operations = operations || []; |
| 28 |
|
| 29 |
this.operations.forEach( ( op ) => { |
| 30 |
if ( op.type && /meta/i.test( op.type ) ) { |
| 31 |
throw new Error( 'Metadata ops are no longer supported' ); |
| 32 |
} |
| 33 |
} ); |
| 34 |
this.applied = false; |
| 35 |
this.authorId = authorId || null; |
| 36 |
this.isReversed = false; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
OO.initClass( ve.dm.Transaction ); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
ve.dm.Transaction.static.reversers = { |
| 61 |
attribute: { from: 'to', to: 'from' }, |
| 62 |
replace: { |
| 63 |
insert: 'remove', |
| 64 |
remove: 'insert' |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.dm.Transaction.static.deserialize = function ( data ) { |
| 79 |
function deminifyLinearData( element ) { |
| 80 |
if ( typeof element === 'string' ) { |
| 81 |
return element.split( '' ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
return JSON.parse( JSON.stringify( element ) ); |
| 86 |
} |
| 87 |
|
| 88 |
function deminify( op ) { |
| 89 |
if ( typeof op === 'number' ) { |
| 90 |
return { type: 'retain', length: op }; |
| 91 |
} |
| 92 |
if ( Array.isArray( op ) ) { |
| 93 |
return { |
| 94 |
type: 'replace', |
| 95 |
remove: deminifyLinearData( op[ 0 ] ), |
| 96 |
insert: deminifyLinearData( op[ 1 ] ) |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
return JSON.parse( JSON.stringify( op ) ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( Array.isArray( data ) ) { |
| 105 |
return new ve.dm.Transaction( |
| 106 |
data.map( deminify ) |
| 107 |
); |
| 108 |
} else { |
| 109 |
return new ve.dm.Transaction( |
| 110 |
|
| 111 |
data.o.map( deminify ), |
| 112 |
|
| 113 |
data.a |
| 114 |
); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ve.dm.Transaction.static.compareElementsForTranslate = function ( a, b ) { |
| 132 |
let aPlain = a, |
| 133 |
bPlain = b; |
| 134 |
|
| 135 |
if ( a === b ) { |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
if ( Array.isArray( a ) ) { |
| 140 |
aPlain = a[ 0 ]; |
| 141 |
} |
| 142 |
if ( Array.isArray( b ) ) { |
| 143 |
bPlain = b[ 0 ]; |
| 144 |
} |
| 145 |
if ( typeof aPlain === 'string' && typeof bPlain === 'string' ) { |
| 146 |
return aPlain === bPlain; |
| 147 |
} |
| 148 |
|
| 149 |
if ( typeof a !== typeof b ) { |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if ( a.type !== b.type ) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
return true; |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
ve.dm.Transaction.static.isAnnotationOnlyOperation = function ( op ) { |
| 170 |
return op.type === 'replace' && |
| 171 |
op.insert.length === op.remove.length && |
| 172 |
op.insert.every( ( insert, j ) => ve.dm.Transaction.static.compareElementsForTranslate( insert, op.remove[ j ] ) ); |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
ve.dm.Transaction.prototype.toJSON = function () { |
| 186 |
function isSingleCodePoint( x ) { |
| 187 |
return typeof x === 'string' && x.length === 1; |
| 188 |
} |
| 189 |
function minifyLinearData( data ) { |
| 190 |
if ( data.every( isSingleCodePoint ) ) { |
| 191 |
return data.join( '' ); |
| 192 |
} |
| 193 |
return data; |
| 194 |
} |
| 195 |
|
| 196 |
function minify( op ) { |
| 197 |
if ( op.type === 'retain' ) { |
| 198 |
return op.length; |
| 199 |
} |
| 200 |
if ( |
| 201 |
op.type === 'replace' && |
| 202 |
!op.insertedDataOffset && |
| 203 |
( |
| 204 |
op.insertedDataLength === undefined || |
| 205 |
op.insertedDataLength === op.insert.length |
| 206 |
) |
| 207 |
) { |
| 208 |
return [ minifyLinearData( op.remove ), minifyLinearData( op.insert ) ]; |
| 209 |
} |
| 210 |
return op; |
| 211 |
} |
| 212 |
|
| 213 |
const operations = this.operations.map( minify ); |
| 214 |
|
| 215 |
if ( this.authorId !== null ) { |
| 216 |
return { |
| 217 |
o: operations, |
| 218 |
a: this.authorId |
| 219 |
}; |
| 220 |
} else { |
| 221 |
return operations; |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
ve.dm.Transaction.prototype.serialize = ve.dm.Transaction.prototype.toJSON; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
ve.dm.Transaction.prototype.pushRetainOp = function ( length ) { |
| 234 |
this.operations.push( { type: 'retain', length: length } ); |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
ve.dm.Transaction.prototype.pushReplaceOp = function ( remove, insert, insertedDataOffset, insertedDataLength ) { |
| 250 |
const op = { type: 'replace', remove: remove, insert: insert }; |
| 251 |
if ( insertedDataOffset !== undefined && insertedDataLength !== undefined ) { |
| 252 |
op.insertedDataOffset = insertedDataOffset; |
| 253 |
op.insertedDataLength = insertedDataLength; |
| 254 |
} |
| 255 |
this.operations.push( op ); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
ve.dm.Transaction.prototype.pushAttributeOp = function ( key, from, to ) { |
| 266 |
this.operations.push( { type: 'attribute', key: key, from: from, to: to } ); |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
ve.dm.Transaction.prototype.clone = function () { |
| 279 |
return new this.constructor( |
| 280 |
|
| 281 |
JSON.parse( JSON.stringify( this.operations ) ), |
| 282 |
this.authorId |
| 283 |
); |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
ve.dm.Transaction.prototype.reversed = function () { |
| 296 |
const tx = new this.constructor(); |
| 297 |
|
| 298 |
tx.isReversed = !this.isReversed; |
| 299 |
for ( let i = 0, len = this.operations.length; i < len; i++ ) { |
| 300 |
const op = this.operations[ i ]; |
| 301 |
const newOp = ve.copy( op ); |
| 302 |
const reverse = this.constructor.static.reversers[ op.type ] || {}; |
| 303 |
for ( const prop in reverse ) { |
| 304 |
if ( typeof reverse[ prop ] === 'string' ) { |
| 305 |
newOp[ prop ] = op[ reverse[ prop ] ]; |
| 306 |
} else { |
| 307 |
newOp[ prop ] = reverse[ prop ][ op[ prop ] ]; |
| 308 |
} |
| 309 |
} |
| 310 |
tx.operations.push( newOp ); |
| 311 |
} |
| 312 |
tx.authorId = this.authorId; |
| 313 |
return tx; |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
ve.dm.Transaction.prototype.isNoOp = function () { |
| 325 |
if ( this.operations.length === 0 ) { |
| 326 |
return true; |
| 327 |
} |
| 328 |
if ( this.operations.length === 1 ) { |
| 329 |
return this.operations[ 0 ].type === 'retain'; |
| 330 |
} |
| 331 |
return false; |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
ve.dm.Transaction.prototype.getOperations = function () { |
| 340 |
return this.operations; |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
ve.dm.Transaction.prototype.hasOperationWithType = function ( type ) { |
| 350 |
for ( let i = 0, len = this.operations.length; i < len; i++ ) { |
| 351 |
if ( this.operations[ i ].type === type ) { |
| 352 |
return true; |
| 353 |
} |
| 354 |
} |
| 355 |
return false; |
| 356 |
}; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
ve.dm.Transaction.prototype.hasContentDataOperations = function () { |
| 364 |
return this.hasOperationWithType( 'replace' ); |
| 365 |
}; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
ve.dm.Transaction.prototype.hasElementAttributeOperations = function () { |
| 373 |
return this.hasOperationWithType( 'attribute' ); |
| 374 |
}; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
ve.dm.Transaction.prototype.hasBeenApplied = function () { |
| 382 |
return this.applied; |
| 383 |
}; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
ve.dm.Transaction.prototype.markAsApplied = function () { |
| 393 |
this.applied = true; |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
ve.dm.Transaction.prototype.translateOffset = function ( offset, excludeInsertion ) { |
| 408 |
let cursor = 0, |
| 409 |
adjustment = 0; |
| 410 |
|
| 411 |
for ( let i = 0; i < this.operations.length; i++ ) { |
| 412 |
const op = this.operations[ i ]; |
| 413 |
|
| 414 |
|
| 415 |
if ( op.type === 'retain' || ve.dm.Transaction.static.isAnnotationOnlyOperation( op ) ) { |
| 416 |
const retainLength = op.type === 'retain' ? op.length : op.remove.length; |
| 417 |
if ( offset >= cursor && offset < cursor + retainLength ) { |
| 418 |
return offset + adjustment; |
| 419 |
} |
| 420 |
cursor += retainLength; |
| 421 |
continue; |
| 422 |
} else if ( op.type === 'replace' ) { |
| 423 |
const insertLength = op.insert.length; |
| 424 |
const removeLength = op.remove.length; |
| 425 |
const prevAdjustment = adjustment; |
| 426 |
adjustment += insertLength - removeLength; |
| 427 |
if ( offset === cursor + removeLength ) { |
| 428 |
|
| 429 |
if ( excludeInsertion && insertLength > removeLength ) { |
| 430 |
|
| 431 |
return offset + adjustment - insertLength + removeLength; |
| 432 |
} else { |
| 433 |
|
| 434 |
return offset + adjustment; |
| 435 |
} |
| 436 |
} else if ( offset === cursor ) { |
| 437 |
|
| 438 |
if ( insertLength === 0 ) { |
| 439 |
|
| 440 |
return cursor + removeLength + adjustment; |
| 441 |
} else { |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
return cursor + prevAdjustment; |
| 446 |
} |
| 447 |
} else if ( offset > cursor && offset < cursor + removeLength ) { |
| 448 |
|
| 449 |
|
| 450 |
return cursor + removeLength + adjustment; |
| 451 |
} |
| 452 |
cursor += removeLength; |
| 453 |
} |
| 454 |
} |
| 455 |
return offset + adjustment; |
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
ve.dm.Transaction.prototype.translateRange = function ( range, excludeInsertion ) { |
| 471 |
const start = this.translateOffset( range.start, !excludeInsertion ), |
| 472 |
end = this.translateOffset( range.end, excludeInsertion ); |
| 473 |
return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end ); |
| 474 |
}; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
ve.dm.Transaction.prototype.translateRangeWithAuthor = function ( range, authorId ) { |
| 487 |
const backward = !this.authorId || !authorId || authorId < this.authorId, |
| 488 |
start = this.translateOffset( range.start, backward ), |
| 489 |
end = this.translateOffset( range.end, backward ); |
| 490 |
return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end ); |
| 491 |
}; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
ve.dm.Transaction.prototype.getModifiedRange = function ( doc, options ) { |
| 511 |
let docEndOffset = doc.data.getLength(), |
| 512 |
oldOffset = 0, |
| 513 |
offset = 0; |
| 514 |
|
| 515 |
if ( typeof options === 'boolean' ) { |
| 516 |
|
| 517 |
options = { includeInternalList: options }; |
| 518 |
} else { |
| 519 |
options = options || {}; |
| 520 |
} |
| 521 |
|
| 522 |
if ( !options.includeInternalList ) { |
| 523 |
const internalListNode = doc.getInternalList().getListNode(); |
| 524 |
if ( internalListNode ) { |
| 525 |
docEndOffset = internalListNode.getOuterRange().start; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
let start, end; |
| 530 |
opLoop: |
| 531 |
for ( let i = 0, len = this.operations.length; i < len; i++ ) { |
| 532 |
const op = this.operations[ i ]; |
| 533 |
switch ( op.type ) { |
| 534 |
case 'retain': |
| 535 |
if ( oldOffset + op.length > docEndOffset ) { |
| 536 |
break opLoop; |
| 537 |
} |
| 538 |
offset += op.length; |
| 539 |
oldOffset += op.length; |
| 540 |
break; |
| 541 |
|
| 542 |
case 'attribute': |
| 543 |
if ( options.excludeAttributes ) { |
| 544 |
break; |
| 545 |
} |
| 546 |
if ( start === undefined ) { |
| 547 |
start = offset; |
| 548 |
} |
| 549 |
|
| 550 |
end = offset + 1; |
| 551 |
break; |
| 552 |
|
| 553 |
default: |
| 554 |
if ( options.excludeAnnotations && this.constructor.static.isAnnotationOnlyOperation( op ) ) { |
| 555 |
|
| 556 |
if ( oldOffset + op.length > docEndOffset ) { |
| 557 |
break opLoop; |
| 558 |
} |
| 559 |
offset += op.length; |
| 560 |
oldOffset += op.length; |
| 561 |
break; |
| 562 |
} |
| 563 |
if ( start === undefined ) { |
| 564 |
|
| 565 |
start = offset + ( op.insertedDataOffset || 0 ); |
| 566 |
} |
| 567 |
if ( op.type === 'replace' ) { |
| 568 |
offset += op.insert.length; |
| 569 |
oldOffset += op.remove.length; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
if ( op.insertedDataLength ) { |
| 574 |
end = start + op.insertedDataLength; |
| 575 |
} else { |
| 576 |
end = offset; |
| 577 |
} |
| 578 |
break; |
| 579 |
} |
| 580 |
} |
| 581 |
if ( start === undefined || end === undefined ) { |
| 582 |
|
| 583 |
return null; |
| 584 |
} |
| 585 |
return new ve.Range( start, end ); |
| 586 |
}; |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
ve.dm.Transaction.prototype.getActiveRangeAndLengthDiff = function () { |
| 604 |
let offset = 0, |
| 605 |
diff = 0; |
| 606 |
|
| 607 |
let start, end, startOpIndex, endOpIndex; |
| 608 |
for ( let i = 0, len = this.operations.length; i < len; i++ ) { |
| 609 |
const op = this.operations[ i ]; |
| 610 |
const active = op.type !== 'retain'; |
| 611 |
|
| 612 |
if ( active && start === undefined ) { |
| 613 |
start = offset; |
| 614 |
startOpIndex = i; |
| 615 |
} |
| 616 |
|
| 617 |
if ( op.type === 'retain' ) { |
| 618 |
offset += op.length; |
| 619 |
} else if ( op.type === 'replace' ) { |
| 620 |
offset += op.remove.length; |
| 621 |
diff += op.insert.length - op.remove.length; |
| 622 |
} |
| 623 |
|
| 624 |
if ( op.type === 'attribute' || op.type === 'replaceMetadata' ) { |
| 625 |
|
| 626 |
end = offset + 1; |
| 627 |
endOpIndex = i + 1; |
| 628 |
} else if ( active ) { |
| 629 |
end = offset; |
| 630 |
endOpIndex = i + 1; |
| 631 |
} |
| 632 |
} |
| 633 |
return { |
| 634 |
start: start, |
| 635 |
end: end, |
| 636 |
startOpIndex: startOpIndex, |
| 637 |
endOpIndex: endOpIndex, |
| 638 |
diff: diff |
| 639 |
}; |
| 640 |
}; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
ve.dm.Transaction.prototype.adjustRetain = function ( place, diff ) { |
| 651 |
if ( diff === 0 ) { |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
const start = place === 'start', |
| 656 |
ops = this.operations; |
| 657 |
let i = start ? 0 : ops.length - 1; |
| 658 |
|
| 659 |
if ( !start && ops[ i ] && ops[ i ].type === 'retainMetadata' ) { |
| 660 |
i = ops.length - 2; |
| 661 |
} |
| 662 |
if ( ops[ i ] && ops[ i ].type === 'retain' ) { |
| 663 |
ops[ i ].length += diff; |
| 664 |
if ( ops[ i ].length < 0 ) { |
| 665 |
throw new Error( 'Negative retain length' ); |
| 666 |
} else if ( ops[ i ].length === 0 ) { |
| 667 |
ops.splice( i, 1 ); |
| 668 |
} |
| 669 |
return; |
| 670 |
} |
| 671 |
if ( diff < 0 ) { |
| 672 |
throw new Error( 'Negative retain length' ); |
| 673 |
} |
| 674 |
ops.splice( start ? 0 : ops.length, 0, { type: 'retain', length: diff } ); |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
ve.dm.Transaction.prototype.trySplit = function ( offset ) { |
| 687 |
let n = 0; |
| 688 |
let i, iLen; |
| 689 |
for ( i = 0, iLen = this.operations.length; i < iLen; i++ ) { |
| 690 |
const op = this.operations[ i ]; |
| 691 |
const opLen = ( op.type === 'retain' ? op.length : op.type === 'replace' ? op.remove.length : 0 ); |
| 692 |
if ( n + opLen <= offset ) { |
| 693 |
n += opLen; |
| 694 |
continue; |
| 695 |
} |
| 696 |
if ( n === offset ) { |
| 697 |
|
| 698 |
return i; |
| 699 |
} |
| 700 |
|
| 701 |
if ( op.type !== 'retain' ) { |
| 702 |
throw new Error( 'Cannot split operation of type ' + op.type ); |
| 703 |
} |
| 704 |
|
| 705 |
op.length -= n + opLen - offset; |
| 706 |
this.operations.splice( i + 1, 0, { type: 'retain', length: n + opLen - offset } ); |
| 707 |
return i + 1; |
| 708 |
} |
| 709 |
if ( n === offset ) { |
| 710 |
return iLen + 1; |
| 711 |
} |
| 712 |
throw new Error( 'Offset beyond end of transaction' ); |
| 713 |
}; |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
ve.dm.Transaction.prototype.tryUnsplit = function ( index ) { |
| 721 |
const op1 = this.operations[ index - 1 ], |
| 722 |
op2 = this.operations[ index ]; |
| 723 |
if ( !op1 || !op2 || op1.type !== op2.type ) { |
| 724 |
return; |
| 725 |
} |
| 726 |
if ( op1.type === 'retain' ) { |
| 727 |
op1.length += op2.length; |
| 728 |
this.operations.splice( index, 1 ); |
| 729 |
} else if ( op1.type === 'replace' ) { |
| 730 |
ve.batchSplice( op1.remove, op1.remove.length, 0, op2.remove ); |
| 731 |
ve.batchSplice( op1.insert, op1.insert.length, 0, op2.insert ); |
| 732 |
this.operations.splice( index, 1 ); |
| 733 |
} |
| 734 |
}; |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
ve.dm.Transaction.prototype.insertOperations = function ( offset, operations ) { |
| 747 |
if ( operations.length === 0 ) { |
| 748 |
return; |
| 749 |
} |
| 750 |
const opIndex = this.trySplit( offset ); |
| 751 |
ve.batchSplice( this.operations, opIndex, 0, ve.copy( operations ) ); |
| 752 |
this.tryUnsplit( opIndex + operations.length ); |
| 753 |
this.tryUnsplit( opIndex ); |
| 754 |
}; |