Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | 1x 3821x 3821x 3695x 3821x 3821x 3821x 1x 1x 1x 168x 126x 42x 269x 159x 110x 84x 26x 103x 99x 4x 1x 2263x 2263x 2263x 1x 2262x 775x 2262x 659x 2262x 1893x 369x 1x 368x 238x 130x 1x 4358x 2242x 1x 42x 48x 34x 14x 74x 46x 28x 24x 4x 27x 27x 2x 25x 1x 1x 2397x 1x 1x 57x 1x 533x 1x 183x 183x 183x 660x 660x 660x 660x 516x 516x 660x 183x 183x 1x 1214x 1214x 58x 1156x 1x 1491x 1x 1094x 3490x 36x 1058x 1x 1x 1094x 1x 1313x 1x 1301x 1x 4676x 4676x 4676x 10371x 10371x 6359x 6359x 2114x 4245x 4245x 4012x 3935x 3935x 3935x 3935x 3935x 2048x 932x 1116x 1887x 357x 214x 143x 1530x 147x 1383x 10x 1x 1368x 1368x 1368x 1x 941x 941x 941x 941x 1x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 469x 469x 303x 152x 151x 151x 151x 3x 3x 2x 3x 3x 163x 163x 161x 163x 163x 163x 163x 14x 149x 163x 164x 1x 163x 1x 432x 432x 432x 1296x 1296x 1296x 432x 432x 1296x 864x 432x 432x 432x 1296x 1296x 432x 432x 432x 1x 412x 15x 397x 397x 397x 397x 397x 397x 397x 397x 397x 1x 1x 1x | /*! * VisualEditor DataModel Transaction class. * * @copyright See AUTHORS.txt */ /** * Transaction on ve.dm.ElementLinearData, preserving ve.dm.Document tree validity * * A transaction represents a mapping on ve.dm.ElementLinearData, from one state (the start * state) to another (the end state). The transaction is guaranteed not to break tree validity: * if the start state represents a syntactically valid ve.dm.Document tree (without unbalanced * tags, bare listItems, bare table cells etc), then the end state tree must be syntactically * valid too. * * A transaction is comprised of a list of operations, which must preserve tree validity as a * whole, though each individual operation may not. For example, a DivNode wrapping can be * removed by one operation removing the 'div' and another removing the '/div'. The * ve.dm.TransactionBuilder.static.newFrom* methods help build transactions that preserve tree validity. * * @class * @constructor * @param {Object[]} [operations] Operations preserving tree validity as a whole; default [] * @param {number|null} [authorId] Positive integer author ID; default null */ ve.dm.Transaction = function VeDmTransaction( operations, authorId ) { this.operations = operations || []; // TODO: remove this backwards-incompatibility check this.operations.forEach( ( op ) => { Iif ( op.type && /meta/i.test( op.type ) ) { throw new Error( 'Metadata ops are no longer supported' ); } } ); this.applied = false; this.authorId = authorId || null; this.isReversed = false; }; /* Inheritance */ OO.initClass( ve.dm.Transaction ); /* Static Properties */ /** * Specification for how each type of operation should be reversed. * * This object maps operation types to objects, which map property names to reversal instructions. * A reversal instruction is either a string (which means the value of that property should be used) * or an object (which maps old values to new values). For instance, { from: 'to' } * means that the .from property of the reversed operation should be set to the .to property of the * original operation, and { method: { set: 'clear' } } means that if the .method property of * the original operation was 'set', the reversed operation's .method property should be 'clear'. * * If a property's treatment isn't specified, its value is simply copied without modification. * If an operation type's treatment isn't specified, all properties are copied without modification. * * @type {Object.<string,Object>} */ ve.dm.Transaction.static.reversers = { attribute: { from: 'to', to: 'from' }, // Swap .from with .to replace: { // Swap .insert with .remove insert: 'remove', remove: 'insert' } }; /* Static Methods */ /** * Deserialize a transaction from a JSONable object * * Values are either new or deep copied, so there is no reference into the serialized structure * * @param {Object|Array} data Transaction serialized as a JSONable object * @return {ve.dm.Transaction} Deserialized transaction */ ve.dm.Transaction.static.deserialize = function ( data ) { function deminifyLinearData( element ) { if ( typeof element === 'string' ) { return element.split( '' ); } // Else deep copy. For this plain, serializable array, stringify+parse profiles // faster than ve.copy return JSON.parse( JSON.stringify( element ) ); } function deminify( op ) { if ( typeof op === 'number' ) { return { type: 'retain', length: op }; } if ( Array.isArray( op ) ) { return { type: 'replace', remove: deminifyLinearData( op[ 0 ] ), insert: deminifyLinearData( op[ 1 ] ) }; } // Else deep copy. For this plain, serializable array, stringify+parse profiles // faster than ve.copy return JSON.parse( JSON.stringify( op ) ); } if ( Array.isArray( data ) ) { return new ve.dm.Transaction( data.map( deminify ) ); } else { return new ve.dm.Transaction( // operations data.o.map( deminify ), // authorId data.a ); } }; /** * Simpified comparison of linear data elements * * Identical to ve.dm.ElementLinearData.static.compareElementsUnannotated, but without * the complex comparison of node elements that requires the model registry. * * For the purposes of translateOffset it is just sufficient that we catch obvious * cases of annotations being set/clear. * * @param {Object|Array|string} a First element * @param {Object|Array|string} b Second element * @return {boolean} Elements are comparable */ ve.dm.Transaction.static.compareElementsForTranslate = function ( a, b ) { let aPlain = a, bPlain = b; if ( a === b ) { return true; } if ( Array.isArray( a ) ) { aPlain = a[ 0 ]; } if ( Array.isArray( b ) ) { bPlain = b[ 0 ]; } if ( typeof aPlain === 'string' && typeof bPlain === 'string' ) { return aPlain === bPlain; } if ( typeof a !== typeof b ) { // Different types return false; } // By this point, both must be objects, so must have equal types if ( a.type !== b.type ) { return false; } // Elements of the same type, consider them equal for the purpose of offset translation return true; }; /** * Check if an operation only changes annotations * * @param {Object} op Operation object * @return {boolean} Operation is annotation-only */ ve.dm.Transaction.static.isAnnotationOnlyOperation = function ( op ) { return op.type === 'replace' && op.insert.length === op.remove.length && op.insert.every( ( insert, j ) => ve.dm.Transaction.static.compareElementsForTranslate( insert, op.remove[ j ] ) ); }; /* Methods */ /** * Serialize the transaction into a JSONable object * * Values are not necessarily deep copied * * @param {string} [key] Key in parent object * @return {Object|Array} JSONable object */ ve.dm.Transaction.prototype.toJSON = function () { function isSingleCodePoint( x ) { return typeof x === 'string' && x.length === 1; } function minifyLinearData( data ) { if ( data.every( isSingleCodePoint ) ) { return data.join( '' ); } return data; } function minify( op ) { if ( op.type === 'retain' ) { return op.length; } if ( op.type === 'replace' && !op.insertedDataOffset && ( op.insertedDataLength === undefined || op.insertedDataLength === op.insert.length ) ) { return [ minifyLinearData( op.remove ), minifyLinearData( op.insert ) ]; } return op; } const operations = this.operations.map( minify ); if ( this.authorId !== null ) { return { o: operations, a: this.authorId }; } else { return operations; } }; // Deprecated alias ve.dm.Transaction.prototype.serialize = ve.dm.Transaction.prototype.toJSON; /** * Push a retain operation * * @param {number} length Length > 0 of content data to retain */ ve.dm.Transaction.prototype.pushRetainOp = function ( length ) { this.operations.push( { type: 'retain', length: length } ); }; /** * Build a replace operation * * The `insertedDataOffset` and `insertedDataLength` parameters indicate the intended insertion * is wrapped with fixup data to preserve HTML validity. For instance, an intended table cell * insertion may have been fixed up by wrapping inside a table row, table section and table. * * @param {Array} remove Data to remove * @param {Array} insert Data to insert, possibly fixed up * @param {number} [insertedDataOffset] Offset of intended insertion within fixed up data * @param {number} [insertedDataLength] Length of intended insertion within fixed up data */ ve.dm.Transaction.prototype.pushReplaceOp = function ( remove, insert, insertedDataOffset, insertedDataLength ) { const op = { type: 'replace', remove: remove, insert: insert }; if ( insertedDataOffset !== undefined && insertedDataLength !== undefined ) { op.insertedDataOffset = insertedDataOffset; op.insertedDataLength = insertedDataLength; } this.operations.push( op ); }; /** * Build an attribute operation * * @param {string} key Name of attribute to change * @param {any} from Value to change attribute from, or undefined if not previously set * @param {any} to Value to change attribute to, or undefined to remove */ ve.dm.Transaction.prototype.pushAttributeOp = function ( key, from, to ) { this.operations.push( { type: 'attribute', key: key, from: from, to: to } ); }; /** * Create a clone of this transaction. * * The returned transaction will be exactly the same as this one, except that its 'applied' flag * will be cleared. This means that if a transaction has already been committed, it will still * be possible to commit the clone. This is used for redoing transactions that were undone. * * @return {ve.dm.Transaction} Clone of this transaction */ ve.dm.Transaction.prototype.clone = function () { return new this.constructor( // For this plain, serializable array, stringify+parse profiles faster than ve.copy JSON.parse( JSON.stringify( this.operations ) ), this.authorId ); }; /** * Create a reversed version of this transaction. * * The returned transaction will be the same as this one but with all operations reversed. This * means that applying the original transaction and then applying the reversed transaction will * result in no net changes. This is used to undo transactions. * * @return {ve.dm.Transaction} Reverse of this transaction */ ve.dm.Transaction.prototype.reversed = function () { const tx = new this.constructor(); tx.isReversed = !this.isReversed; for ( let i = 0, len = this.operations.length; i < len; i++ ) { const op = this.operations[ i ]; const newOp = ve.copy( op ); const reverse = this.constructor.static.reversers[ op.type ] || {}; for ( const prop in reverse ) { if ( typeof reverse[ prop ] === 'string' ) { newOp[ prop ] = op[ reverse[ prop ] ]; } else E{ newOp[ prop ] = reverse[ prop ][ op[ prop ] ]; } } tx.operations.push( newOp ); } tx.authorId = this.authorId; return tx; }; /** * Check if the transaction would make any actual changes if processed. * * There may be more sophisticated checks that can be done, like looking for things being replaced * with identical content, but such transactions probably should not be created in the first place. * * @return {boolean} Transaction is no-op */ ve.dm.Transaction.prototype.isNoOp = function () { Iif ( this.operations.length === 0 ) { return true; } if ( this.operations.length === 1 ) { return this.operations[ 0 ].type === 'retain'; } return false; }; /** * Get all operations. * * @return {Object[]} List of operations */ ve.dm.Transaction.prototype.getOperations = function () { return this.operations; }; /** * Check if the transaction has any operations with a certain type. * * @param {string} type Operation type * @return {boolean} Has operations of a given type */ ve.dm.Transaction.prototype.hasOperationWithType = function ( type ) { for ( let i = 0, len = this.operations.length; i < len; i++ ) { if ( this.operations[ i ].type === type ) { return true; } } return false; }; /** * Check if the transaction has any content data operations, such as insertion or deletion. * * @return {boolean} Has content data operations */ ve.dm.Transaction.prototype.hasContentDataOperations = function () { return this.hasOperationWithType( 'replace' ); }; /** * Check if the transaction has any element attribute operations. * * @return {boolean} Has element attribute operations */ ve.dm.Transaction.prototype.hasElementAttributeOperations = function () { return this.hasOperationWithType( 'attribute' ); }; /** * Check whether the transaction has already been applied. * * @return {boolean} */ ve.dm.Transaction.prototype.hasBeenApplied = function () { return this.applied; }; /** * Mark the transaction as having been applied. * * Should only be called after committing the transaction. * * @see ve.dm.Transaction#hasBeenApplied */ ve.dm.Transaction.prototype.markAsApplied = function () { this.applied = true; }; /** * Translate an offset based on a transaction. * * This is useful when you want to anticipate what an offset will be after a transaction is * processed. * * @param {number} offset Offset in the linear model before the transaction has been processed * @param {boolean} [excludeInsertion] Map the offset immediately before an insertion to * right before the insertion rather than right after * @return {number} Translated offset, as it will be after processing transaction */ ve.dm.Transaction.prototype.translateOffset = function ( offset, excludeInsertion ) { let cursor = 0, adjustment = 0; for ( let i = 0; i < this.operations.length; i++ ) { const op = this.operations[ i ]; // If a 'replace' only changes annotations, treat it like a 'retain' // This imitates the behaviour of the old 'annotate' operation type. if ( op.type === 'retain' || ve.dm.Transaction.static.isAnnotationOnlyOperation( op ) ) { const retainLength = op.type === 'retain' ? op.length : op.remove.length; if ( offset >= cursor && offset < cursor + retainLength ) { return offset + adjustment; } cursor += retainLength; continue; } else if ( op.type === 'replace' ) { const insertLength = op.insert.length; const removeLength = op.remove.length; const prevAdjustment = adjustment; adjustment += insertLength - removeLength; if ( offset === cursor + removeLength ) { // Offset points to right after the removal or right before the insertion if ( excludeInsertion && insertLength > removeLength ) { // Translate it to before the insertion return offset + adjustment - insertLength + removeLength; } else { // Translate it to after the removal/insertion return offset + adjustment; } } else if ( offset === cursor ) { // The offset points to right before the removal or replacement if ( insertLength === 0 ) { // Translate it to after the removal return cursor + removeLength + adjustment; } else { // Translate it to before the replacement // To translate this correctly, we have to use adjustment as it was before // we adjusted it for this replacement return cursor + prevAdjustment; } } else if ( offset > cursor && offset < cursor + removeLength ) { // The offset points inside of the removal // Translate it to after the removal return cursor + removeLength + adjustment; } cursor += removeLength; } } return offset + adjustment; }; /** * Translate a range based on the transaction, with grow/shrink preference at changes * * This is useful when you want to anticipate what a selection will be after a transaction is * processed. * * @see #translateOffset * @param {ve.Range} range Range in the linear model before the transaction has been processed * @param {boolean} [excludeInsertion] Do not grow the range to cover insertions * on the boundaries of the range. * @return {ve.Range} Translated range, as it will be after processing transaction */ ve.dm.Transaction.prototype.translateRange = function ( range, excludeInsertion ) { const start = this.translateOffset( range.start, !excludeInsertion ), end = this.translateOffset( range.end, excludeInsertion ); return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end ); }; /** * Translate a range based on the transaction, with bias depending on author ID comparison * * Biases backward if !authorId || !this.authorId || authorId <= this.authorId * * @see #translateOffset * @param {ve.Range} range Range in the linear model before the transaction has been processed * @param {number} [authorId] Author ID of the range * @return {ve.Range} Translated range, as it will be after processing transaction */ ve.dm.Transaction.prototype.translateRangeWithAuthor = function ( range, authorId ) { const backward = !this.authorId || !authorId || authorId < this.authorId, start = this.translateOffset( range.start, backward ), end = this.translateOffset( range.end, backward ); return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end ); }; /** * Get the range that covers modifications made by this transaction. * * In the case of insertions, the range covers content the user intended to insert. * It ignores wrappers added by ve.dm.Document#fixUpInsertion. * * The returned range is relative to the new state, after the transaction is applied. So for a * simple insertion transaction, the range will cover the newly inserted data, and for a simple * removal transaction it will be a zero-length range. * * @param {ve.dm.Document} doc The document in the state to which the transaction applies * @param {Object} [options] Options * @param {boolean} [options.includeInternalList] Include changes within the internal list * @param {boolean} [options.excludeAnnotations] Exclude annotation-only changes * @param {boolean} [options.excludeAttributes] Exclude attribute changes * @return {ve.Range|null} Range covering modifications, or null for a no-op transaction */ ve.dm.Transaction.prototype.getModifiedRange = function ( doc, options ) { let docEndOffset = doc.data.getLength(), oldOffset = 0, offset = 0; Iif ( typeof options === 'boolean' ) { // Backwards compatibility options = { includeInternalList: options }; } else { options = options || {}; } Eif ( !options.includeInternalList ) { const internalListNode = doc.getInternalList().getListNode(); Eif ( internalListNode ) { docEndOffset = internalListNode.getOuterRange().start; } } let start, end; opLoop: for ( let i = 0, len = this.operations.length; i < len; i++ ) { const op = this.operations[ i ]; switch ( op.type ) { case 'retain': if ( oldOffset + op.length > docEndOffset ) { break opLoop; } offset += op.length; oldOffset += op.length; break; case 'attribute': Iif ( options.excludeAttributes ) { break; } if ( start === undefined ) { start = offset; } // Attribute changes modify the element to their right but don't move the cursor end = offset + 1; break; default: Iif ( options.excludeAnnotations && this.constructor.static.isAnnotationOnlyOperation( op ) ) { // Treat as 'retain' if ( oldOffset + op.length > docEndOffset ) { break opLoop; } offset += op.length; oldOffset += op.length; break; } if ( start === undefined ) { // This is the first non-retain operation, set start to right before it start = offset + ( op.insertedDataOffset || 0 ); } Eif ( op.type === 'replace' ) { offset += op.insert.length; oldOffset += op.remove.length; } // Set end, so it'll end up being right after the last non-retain operation if ( op.insertedDataLength ) { end = start + op.insertedDataLength; } else { end = offset; } break; } } if ( start === undefined || end === undefined ) { // No-op transaction return null; } return new ve.Range( start, end ); }; /** * @typedef {Object} RangeAndLengthDiff * @memberof ve.dm.Transaction * @property {number} [start] Start offset of the active range * @property {number} [end] End offset of the active range * @property {number} [startOpIndex] Start operation index of the active range * @property {number} [endOpIndex] End operation index of the active range * @property {number} diff Length change the transaction causes */ /** * Calculate active range and length change * * @return {ve.dm.Transaction.RangeAndLengthDiff} Active range and length change */ ve.dm.Transaction.prototype.getActiveRangeAndLengthDiff = function () { let offset = 0, diff = 0; let start, end, startOpIndex, endOpIndex; for ( let i = 0, len = this.operations.length; i < len; i++ ) { const op = this.operations[ i ]; const active = op.type !== 'retain'; // Place start marker if ( active && start === undefined ) { start = offset; startOpIndex = i; } // Adjust offset and diff if ( op.type === 'retain' ) { offset += op.length; } else Eif ( op.type === 'replace' ) { offset += op.remove.length; diff += op.insert.length - op.remove.length; } // Place/move end marker Iif ( op.type === 'attribute' || op.type === 'replaceMetadata' ) { // Op with length 0 but that effectively modifies 1 position end = offset + 1; endOpIndex = i + 1; } else if ( active ) { end = offset; endOpIndex = i + 1; } } return { start: start, end: end, startOpIndex: startOpIndex, endOpIndex: endOpIndex, diff: diff }; }; // TODO: Use adjustRetain to replace ve.dm.TransactionBuilder#pushRetain /** * Adjust (in place) the retain length at the start/end of an operations list * * @param {string} place Where to adjust, start|end * @param {number} diff Adjustment; must not cause negative retain length */ ve.dm.Transaction.prototype.adjustRetain = function ( place, diff ) { if ( diff === 0 ) { return; } const start = place === 'start', ops = this.operations; let i = start ? 0 : ops.length - 1; Iif ( !start && ops[ i ] && ops[ i ].type === 'retainMetadata' ) { i = ops.length - 2; } Eif ( ops[ i ] && ops[ i ].type === 'retain' ) { ops[ i ].length += diff; Iif ( ops[ i ].length < 0 ) { throw new Error( 'Negative retain length' ); } else Iif ( ops[ i ].length === 0 ) { ops.splice( i, 1 ); } return; } if ( diff < 0 ) { throw new Error( 'Negative retain length' ); } ops.splice( start ? 0 : ops.length, 0, { type: 'retain', length: diff } ); }; /** * Split (in place) the retain at the given offset, if any * * Offset cannot be in the interior of a replace operation (i.e. the interior of its removed content). * * @param {number} offset The offset at which to split * @return {number} Index in operations starting at offset * @throws {Error} Offset is in the interior of a replace operation */ ve.dm.Transaction.prototype.trySplit = function ( offset ) { let n = 0; let i, iLen; for ( i = 0, iLen = this.operations.length; i < iLen; i++ ) { const op = this.operations[ i ]; const opLen = ( op.type === 'retain' ? op.length : op.type === 'replace' ? op.remove.length : 0 ); if ( n + opLen <= offset ) { n += opLen; continue; } if ( n === offset ) { // At start edge; no need to split return i; } // Else n < offset < n + opLen if ( op.type !== 'retain' ) { throw new Error( 'Cannot split operation of type ' + op.type ); } // Split the retain operation op.length -= n + opLen - offset; this.operations.splice( i + 1, 0, { type: 'retain', length: n + opLen - offset } ); return i + 1; } if ( n === offset ) { return iLen + 1; } throw new Error( 'Offset beyond end of transaction' ); }; /** * Unsplit (in place) the two operations around the given index, if possible * * @param {number} index The index at which to unsplit */ ve.dm.Transaction.prototype.tryUnsplit = function ( index ) { const op1 = this.operations[ index - 1 ], op2 = this.operations[ index ]; if ( !op1 || !op2 || op1.type !== op2.type ) { return; } if ( op1.type === 'retain' ) { op1.length += op2.length; this.operations.splice( index, 1 ); } else if ( op1.type === 'replace' ) { ve.batchSplice( op1.remove, op1.remove.length, 0, op2.remove ); ve.batchSplice( op1.insert, op1.insert.length, 0, op2.insert ); this.operations.splice( index, 1 ); } }; /** * Insert (in place) operations at the given offset * * Merges into existing operations where possible. Offset cannot be in the interior of a replace * operation (i.e. the interior of its removed content). * * @param {number} offset The offset at which to insert * @param {Object[]} operations The operations to insert * @throws {Error} Offset is in the interior of a replace operation */ ve.dm.Transaction.prototype.insertOperations = function ( offset, operations ) { if ( operations.length === 0 ) { return; } const opIndex = this.trySplit( offset ); ve.batchSplice( this.operations, opIndex, 0, ve.copy( operations ) ); this.tryUnsplit( opIndex + operations.length ); this.tryUnsplit( opIndex ); }; |