| 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 |
ve.dm.Change = function VeDmChange( start, transactions, stores, selections ) { |
| 76 |
this.start = start || 0; |
| 77 |
this.transactions = transactions || []; |
| 78 |
this.store = new ve.dm.HashValueStore(); |
| 79 |
this.storeLengthAtTransaction = []; |
| 80 |
if ( stores ) { |
| 81 |
stores.forEach( ( store ) => { |
| 82 |
this.store.merge( store ); |
| 83 |
this.storeLengthAtTransaction.push( this.store.getLength() ); |
| 84 |
} ); |
| 85 |
} |
| 86 |
this.selections = selections || {}; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.dm.Change.static = {}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
ve.dm.Change.static.deserialize = function ( data, preserveStoreValues, unsafe ) { |
| 105 |
const hasOwn = Object.prototype.hasOwnProperty, |
| 106 |
getTransactionInfo = this.getTransactionInfo, |
| 107 |
deserializeValue = this.deserializeValue, |
| 108 |
selections = {}, |
| 109 |
transactions = [], |
| 110 |
|
| 111 |
stores = data.stores || data.transactions.map( () => null ); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
function annotate( items, annotations ) { |
| 120 |
if ( !annotations || !annotations.length ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
for ( let j = 0, jLen = items.length; j < jLen; j++ ) { |
| 124 |
items[ j ] = [ items[ j ], annotations.slice() ]; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
for ( const authorId in data.selections ) { |
| 129 |
selections[ authorId ] = ve.dm.Selection.static.newFromJSON( |
| 130 |
data.selections[ authorId ] |
| 131 |
); |
| 132 |
} |
| 133 |
const deserializeStore = ve.dm.HashValueStore.static.deserialize.bind( |
| 134 |
null, |
| 135 |
preserveStoreValues ? ( x ) => x : ( x ) => deserializeValue( x, unsafe ) |
| 136 |
); |
| 137 |
let prevInfo; |
| 138 |
for ( let i = 0, iLen = data.transactions.length; i < iLen; i++ ) { |
| 139 |
const txSerialized = data.transactions[ i ]; |
| 140 |
let tx; |
| 141 |
if ( typeof txSerialized === 'string' ) { |
| 142 |
const insertion = txSerialized.split( '' ); |
| 143 |
annotate( |
| 144 |
insertion, |
| 145 |
prevInfo.uniformInsert && prevInfo.uniformInsert.annotations |
| 146 |
); |
| 147 |
tx = new ve.dm.Transaction( [ |
| 148 |
{ type: 'retain', length: prevInfo.end }, |
| 149 |
{ type: 'replace', remove: [], insert: insertion }, |
| 150 |
{ type: 'retain', length: prevInfo.docLength - prevInfo.end } |
| 151 |
], prevInfo.authorId ); |
| 152 |
if ( tx.operations[ 2 ].length === 0 ) { |
| 153 |
tx.operations.pop(); |
| 154 |
} |
| 155 |
} else { |
| 156 |
tx = ve.dm.Transaction.static.deserialize( txSerialized ); |
| 157 |
if ( prevInfo && !hasOwn.call( txSerialized, 'authorId' ) ) { |
| 158 |
tx.authorId = prevInfo.authorId; |
| 159 |
} |
| 160 |
} |
| 161 |
transactions.push( tx ); |
| 162 |
prevInfo = getTransactionInfo( tx ); |
| 163 |
} |
| 164 |
return new ve.dm.Change( |
| 165 |
data.start, |
| 166 |
transactions, |
| 167 |
stores.map( deserializeStore ), |
| 168 |
selections |
| 169 |
); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
ve.dm.Change.static.unsafeDeserialize = function ( data ) { |
| 179 |
return this.deserialize( data, false, true ); |
| 180 |
}; |
| 181 |
|
| 182 |
ve.dm.Change.static.serializeValue = function ( value ) { |
| 183 |
if ( value instanceof ve.dm.Annotation ) { |
| 184 |
return { type: 'annotation', value: value.element }; |
| 185 |
} else if ( Array.isArray( value ) && value[ 0 ] instanceof Node ) { |
| 186 |
return { type: 'domNodes', value: value.map( ve.getNodeHtml ).join( '' ) }; |
| 187 |
} else { |
| 188 |
return { type: 'plain', value: value }; |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
ve.dm.Change.static.deserializeValue = function ( serialized, unsafe ) { |
| 193 |
if ( serialized.type === 'annotation' ) { |
| 194 |
return ve.dm.annotationFactory.createFromElement( serialized.value ); |
| 195 |
} else if ( serialized.type === 'domNodes' ) { |
| 196 |
if ( unsafe ) { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return $.parseHTML( serialized.value, undefined, true ); |
| 201 |
} else { |
| 202 |
|
| 203 |
return Array.prototype.slice.call( ve.sanitizeHtml( serialized.value ) ); |
| 204 |
} |
| 205 |
} else if ( serialized.type === 'plain' ) { |
| 206 |
return serialized.value; |
| 207 |
} else { |
| 208 |
throw new Error( 'Unrecognized type: ' + serialized.type ); |
| 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 |
ve.dm.Change.static.rebaseTransactions = function ( transactionA, transactionB ) { |
| 258 |
transactionA = transactionA.clone(); |
| 259 |
transactionB = transactionB.clone(); |
| 260 |
const infoA = transactionA.getActiveRangeAndLengthDiff(); |
| 261 |
const infoB = transactionB.getActiveRangeAndLengthDiff(); |
| 262 |
|
| 263 |
if ( infoA.start === undefined || infoB.start === undefined ) { |
| 264 |
|
| 265 |
|
| 266 |
transactionA.adjustRetain( 'start', infoB.diff ); |
| 267 |
transactionB.adjustRetain( 'start', infoA.diff ); |
| 268 |
} else if ( infoA.end <= infoB.start ) { |
| 269 |
|
| 270 |
|
| 271 |
transactionB.adjustRetain( 'start', infoA.diff ); |
| 272 |
transactionA.adjustRetain( 'end', infoB.diff ); |
| 273 |
} else if ( infoB.end <= infoA.start ) { |
| 274 |
transactionA.adjustRetain( 'start', infoB.diff ); |
| 275 |
transactionB.adjustRetain( 'end', infoA.diff ); |
| 276 |
} else { |
| 277 |
|
| 278 |
return [ null, null ]; |
| 279 |
} |
| 280 |
return [ transactionA, transactionB ]; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
te |
| 304 |
…,aN |
| 305 |
N. |
| 306 |
ng |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
â‹® |
| 313 |
|
| 314 |
i-1' . |
| 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 |
s) |
| 341 |
er |
| 342 |
d. |
| 343 |
|
| 344 |
|
| 345 |
M). |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
ve.dm.Change.static.rebaseUncommittedChange = function ( history, uncommitted ) { |
| 357 |
if ( history.start !== uncommitted.start ) { |
| 358 |
throw new Error( 'Different starts: ' + history.start + ' and ' + uncommitted.start ); |
| 359 |
} |
| 360 |
|
| 361 |
let transactionsA = history.transactions.slice(); |
| 362 |
const transactionsB = uncommitted.transactions.slice(); |
| 363 |
let storesA = history.getStores(); |
| 364 |
const storesB = uncommitted.getStores(); |
| 365 |
const selectionsA = ve.cloneObject( history.selections ); |
| 366 |
let selectionsB = ve.cloneObject( uncommitted.selections ); |
| 367 |
let rejected = null; |
| 368 |
|
| 369 |
|
| 370 |
se |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
bLoop: |
| 389 |
for ( let i = 0, iLen = transactionsB.length; i < iLen; i++ ) { |
| 390 |
let b = transactionsB[ i ]; |
| 391 |
let storeB = storesB[ i ]; |
| 392 |
const rebasedTransactionsA = []; |
| 393 |
const rebasedStoresA = []; |
| 394 |
for ( let j = 0, jLen = transactionsA.length; j < jLen; j++ ) { |
| 395 |
const a = transactionsA[ j ]; |
| 396 |
const storeA = storesA[ j ]; |
| 397 |
let rebases; |
| 398 |
if ( b.authorId < a.authorId ) { |
| 399 |
rebases = ve.dm.Change.static.rebaseTransactions( b, a ).reverse(); |
| 400 |
} else { |
| 401 |
rebases = ve.dm.Change.static.rebaseTransactions( a, b ); |
| 402 |
} |
| 403 |
if ( rebases[ 0 ] === null ) { |
| 404 |
rejected = uncommitted.mostRecent( uncommitted.start + i ); |
| 405 |
transactionsB.length = i; |
| 406 |
storesB.length = i; |
| 407 |
selectionsB = {}; |
| 408 |
break bLoop; |
| 409 |
} |
| 410 |
rebasedTransactionsA[ j ] = rebases[ 0 ]; |
| 411 |
rebasedStoresA[ j ] = storeA.difference( storeB ); |
| 412 |
b = rebases[ 1 ]; |
| 413 |
storeB = storeB.difference( storeA ); |
| 414 |
} |
| 415 |
transactionsA = rebasedTransactionsA; |
| 416 |
storesA = rebasedStoresA; |
| 417 |
transactionsB[ i ] = b; |
| 418 |
storesB[ i ] = storeB; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
const rebased = new ve.dm.Change( |
| 423 |
uncommitted.start + transactionsA.length, |
| 424 |
transactionsB, |
| 425 |
storesB, |
| 426 |
{} |
| 427 |
); |
| 428 |
const transposedHistory = new ve.dm.Change( |
| 429 |
history.start + transactionsB.length, |
| 430 |
transactionsA, |
| 431 |
storesA, |
| 432 |
{} |
| 433 |
); |
| 434 |
let authorId; |
| 435 |
for ( authorId in selectionsB ) { |
| 436 |
authorId = +authorId; |
| 437 |
rebased.selections[ authorId ] = selectionsB[ authorId ].translateByChange( transposedHistory, authorId ); |
| 438 |
} |
| 439 |
for ( authorId in selectionsA ) { |
| 440 |
authorId = +authorId; |
| 441 |
transposedHistory.selections[ authorId ] = selectionsA[ authorId ].translateByChange( rebased, authorId ); |
| 442 |
} |
| 443 |
return { |
| 444 |
rebased: rebased, |
| 445 |
transposedHistory: transposedHistory, |
| 446 |
rejected: rejected |
| 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 |
ve.dm.Change.static.getTransactionInfo = function ( tx ) { |
| 477 |
|
| 478 |
|
| 479 |
function getAnnotations( item ) { |
| 480 |
if ( typeof item === 'string' ) { |
| 481 |
return []; |
| 482 |
} else if ( item.annotations ) { |
| 483 |
return item.annotations.slice(); |
| 484 |
} else if ( item[ 1 ] ) { |
| 485 |
return item[ 1 ].slice(); |
| 486 |
} else { |
| 487 |
return []; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
function getSingleCodeUnit( item ) { |
| 498 |
if ( typeof item === 'string' && item.length === 1 ) { |
| 499 |
return item; |
| 500 |
} |
| 501 |
if ( Array.isArray( item ) && item[ 0 ].length === 1 ) { |
| 502 |
return item[ 0 ]; |
| 503 |
} |
| 504 |
return null; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
function getUniformText( items ) { |
| 517 |
const codeUnits = []; |
| 518 |
if ( items.length === 0 ) { |
| 519 |
return null; |
| 520 |
} |
| 521 |
let codeUnit = getSingleCodeUnit( items[ 0 ] ); |
| 522 |
if ( codeUnit === null ) { |
| 523 |
return null; |
| 524 |
} |
| 525 |
codeUnits.push( codeUnit ); |
| 526 |
const annotations = getAnnotations( items[ 0 ] ); |
| 527 |
const annotationString = annotations.join( ',' ); |
| 528 |
for ( let i = 1, iLen = items.length; i < iLen; i++ ) { |
| 529 |
codeUnit = getSingleCodeUnit( items[ i ] ); |
| 530 |
if ( codeUnit === null ) { |
| 531 |
return null; |
| 532 |
} |
| 533 |
codeUnits.push( codeUnit ); |
| 534 |
if ( annotationString !== getAnnotations( items[ i ] ).join( ',' ) ) { |
| 535 |
return null; |
| 536 |
} |
| 537 |
} |
| 538 |
return { |
| 539 |
text: codeUnits.join( '' ), |
| 540 |
annotations: annotations, |
| 541 |
annotationString: annotationString |
| 542 |
}; |
| 543 |
} |
| 544 |
|
| 545 |
const op0 = tx.operations[ 0 ]; |
| 546 |
const op1 = tx.operations[ 1 ]; |
| 547 |
const op2 = tx.operations[ 2 ]; |
| 548 |
let replaceOp, start, end, docLength; |
| 549 |
if ( |
| 550 |
op0 && |
| 551 |
op0.type === 'replace' && |
| 552 |
( !op1 || op1.type === 'retain' ) && |
| 553 |
!op2 |
| 554 |
) { |
| 555 |
replaceOp = op0; |
| 556 |
start = 0; |
| 557 |
end = start + replaceOp.insert.length; |
| 558 |
docLength = end; |
| 559 |
} else if ( |
| 560 |
op0 && |
| 561 |
op0.type === 'retain' && |
| 562 |
op1 && |
| 563 |
op1.type === 'replace' && |
| 564 |
( !op2 || op2.type === 'retain' ) |
| 565 |
) { |
| 566 |
replaceOp = op1; |
| 567 |
start = op0.length; |
| 568 |
end = start + replaceOp.insert.length; |
| 569 |
docLength = end + ( op2 ? op2.length : 0 ); |
| 570 |
} else { |
| 571 |
return null; |
| 572 |
} |
| 573 |
|
| 574 |
return { |
| 575 |
start: start, |
| 576 |
end: end, |
| 577 |
docLength: docLength, |
| 578 |
authorId: tx.authorId, |
| 579 |
uniformInsert: getUniformText( replaceOp.insert ) |
| 580 |
}; |
| 581 |
}; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
ve.dm.Change.prototype.clone = function () { |
| 591 |
return this.constructor.static.unsafeDeserialize( this.toJSON() ); |
| 592 |
}; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
ve.dm.Change.prototype.isEmpty = function () { |
| 598 |
return this.transactions.length === 0 && Object.keys( this.selections ).length === 0; |
| 599 |
}; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
ve.dm.Change.prototype.getLength = function () { |
| 605 |
return this.transactions.length; |
| 606 |
}; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
ve.dm.Change.prototype.getStore = function ( n ) { |
| 615 |
return this.store.slice( |
| 616 |
n > 0 ? this.storeLengthAtTransaction[ n - 1 ] : 0, |
| 617 |
this.storeLengthAtTransaction[ n ] |
| 618 |
); |
| 619 |
}; |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
ve.dm.Change.prototype.getStores = function () { |
| 627 |
const stores = []; |
| 628 |
let start = 0; |
| 629 |
for ( let i = 0, len = this.getLength(); i < len; i++ ) { |
| 630 |
const end = this.storeLengthAtTransaction[ i ]; |
| 631 |
stores.push( this.store.slice( start, end ) ); |
| 632 |
start = end; |
| 633 |
} |
| 634 |
return stores; |
| 635 |
}; |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
ve.dm.Change.prototype.firstAuthorId = function () { |
| 641 |
if ( this.transactions.length ) { |
| 642 |
return this.transactions[ 0 ].authorId; |
| 643 |
} |
| 644 |
const authors = Object.keys( this.selections ); |
| 645 |
if ( authors.length ) { |
| 646 |
return +authors[ 0 ]; |
| 647 |
} |
| 648 |
return null; |
| 649 |
}; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
ve.dm.Change.prototype.summarize = function () { |
| 657 |
return '{ start: ' + this.start + ', txs: [ ' + |
| 658 |
this.transactions.map( ( tx ) => tx.summarize() ).join( ', ' ) + ' ] }'; |
| 659 |
}; |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
ve.dm.Change.prototype.reversed = function () { |
| 669 |
return new ve.dm.Change( |
| 670 |
this.start + this.transactions.length, |
| 671 |
this.transactions.map( ( tx ) => ve.dm.Transaction.prototype.reversed.call( tx ) ).reverse(), |
| 672 |
|
| 673 |
this.transactions.map( () => new ve.dm.HashValueStore() ), |
| 674 |
{} |
| 675 |
); |
| 676 |
}; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
ve.dm.Change.prototype.rebasedOnto = function ( other ) { |
| 686 |
const rebases = this.constructor.static.rebaseUncommittedChange( other, this ); |
| 687 |
return rebases.rejected ? null : rebases.rebased; |
| 688 |
}; |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
ve.dm.Change.prototype.concat = function ( other ) { |
| 698 |
if ( other.start !== this.start + this.transactions.length ) { |
| 699 |
throw new Error( 'this ends at ' + ( this.start + this.transactions.length ) + |
| 700 |
' but other starts at ' + other.start ); |
| 701 |
} |
| 702 |
return new ve.dm.Change( |
| 703 |
this.start, |
| 704 |
this.transactions.concat( other.transactions ), |
| 705 |
this.getStores().concat( other.getStores() ), |
| 706 |
other.selections |
| 707 |
); |
| 708 |
}; |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
ve.dm.Change.prototype.pushTransaction = function ( transaction, storeLength ) { |
| 717 |
if ( typeof storeLength !== 'number' ) { |
| 718 |
throw new Error( 'Expected numerical storeLength argument, not ' + storeLength ); |
| 719 |
} |
| 720 |
this.transactions.push( transaction ); |
| 721 |
this.storeLengthAtTransaction.push( storeLength ); |
| 722 |
}; |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
ve.dm.Change.prototype.push = function ( other ) { |
| 731 |
if ( other.start !== this.start + this.getLength() ) { |
| 732 |
throw new Error( 'this ends at ' + ( this.start + this.getLength() ) + |
| 733 |
' but other starts at ' + other.start ); |
| 734 |
} |
| 735 |
const stores = other.getStores(); |
| 736 |
for ( let i = 0, iLen = other.transactions.length; i < iLen; i++ ) { |
| 737 |
const transaction = other.transactions[ i ]; |
| 738 |
const store = stores[ i ]; |
| 739 |
this.store.merge( store ); |
| 740 |
this.pushTransaction( transaction, this.store.getLength() ); |
| 741 |
} |
| 742 |
this.selections = ve.cloneObject( other.selections ); |
| 743 |
}; |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
ve.dm.Change.prototype.concatRebased = function ( other ) { |
| 753 |
return this.concat( other.rebasedOnto( this ) ); |
| 754 |
}; |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
ve.dm.Change.prototype.mostRecent = function ( start ) { |
| 763 |
if ( arguments.length > 1 ) { |
| 764 |
throw new Error( 'storeStart is no longer needed' ); |
| 765 |
} |
| 766 |
return new ve.dm.Change( |
| 767 |
start, |
| 768 |
this.transactions.slice( start - this.start ), |
| 769 |
this.getStores().slice( start - this.start ), |
| 770 |
ve.cloneObject( this.selections ) |
| 771 |
); |
| 772 |
}; |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
ve.dm.Change.prototype.truncate = function ( length ) { |
| 783 |
if ( arguments.length > 1 ) { |
| 784 |
throw new Error( 'storeLength is no longer needed' ); |
| 785 |
} |
| 786 |
return new ve.dm.Change( |
| 787 |
this.start, |
| 788 |
this.transactions.slice( 0, length ), |
| 789 |
this.getStores().slice( 0, length ), |
| 790 |
{} |
| 791 |
); |
| 792 |
}; |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
ve.dm.Change.prototype.applyTo = function ( surface, applySelection ) { |
| 801 |
const doc = surface.getDocument(); |
| 802 |
if ( this.start !== doc.completeHistory.getLength() ) { |
| 803 |
throw new Error( 'Change starts at ' + this.start + ', but doc is at ' + doc.completeHistory.getLength() ); |
| 804 |
} |
| 805 |
this.getStores().forEach( ( store ) => { |
| 806 |
doc.store.merge( store ); |
| 807 |
} ); |
| 808 |
|
| 809 |
surface.breakpoint(); |
| 810 |
this.transactions.forEach( ( tx ) => { |
| 811 |
surface.change( tx ); |
| 812 |
|
| 813 |
tx.applied = false; |
| 814 |
|
| 815 |
|
| 816 |
if ( applySelection ) { |
| 817 |
const range = tx.getModifiedRange( doc ); |
| 818 |
|
| 819 |
if ( range ) { |
| 820 |
const offset = doc.getNearestCursorOffset( range.end, -1 ); |
| 821 |
if ( offset !== -1 ) { |
| 822 |
surface.setSelection( new ve.dm.LinearSelection( new ve.Range( offset ) ) ); |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
} ); |
| 827 |
surface.breakpoint(); |
| 828 |
}; |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
ve.dm.Change.prototype.unapplyTo = function ( surface ) { |
| 836 |
const doc = surface.getDocument(), |
| 837 |
historyLength = doc.completeHistory.getLength() - this.getLength(); |
| 838 |
if ( this.start !== historyLength ) { |
| 839 |
throw new Error( 'Invalid start: change starts at ' + this.start + ', but doc would be at ' + historyLength ); |
| 840 |
} |
| 841 |
this.transactions.slice().reverse().forEach( ( tx ) => { |
| 842 |
surface.change( tx.reversed() ); |
| 843 |
} ); |
| 844 |
doc.completeHistory.transactions.length = historyLength; |
| 845 |
doc.completeHistory.storeLengthAtTransaction.length = historyLength; |
| 846 |
doc.store.truncate( doc.completeHistory.storeLengthAtTransaction[ historyLength - 1 ] ); |
| 847 |
}; |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
ve.dm.Change.prototype.addToHistory = function ( documentModel ) { |
| 856 |
documentModel.completeHistory.push( this ); |
| 857 |
}; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
ve.dm.Change.prototype.removeFromHistory = function ( doc ) { |
| 866 |
if ( this.start + this.getLength() !== doc.completeHistory.getLength() ) { |
| 867 |
throw new Error( 'this ends at ' + ( this.start + this.getLength() ) + |
| 868 |
' but history ends at ' + doc.completeHistory.getLength() ); |
| 869 |
} |
| 870 |
doc.completeHistory.transactions.length -= this.transactions.length; |
| 871 |
doc.completeHistory.storeLengthAtTransaction.length -= this.transactions.length; |
| 872 |
doc.store.truncate( doc.completeHistory.storeLengthAtTransaction[ doc.completeHistory.getLength() - 1 ] ); |
| 873 |
}; |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
ve.dm.Change.prototype.serialize = function ( preserveStoreValues ) { |
| 885 |
const getTransactionInfo = this.constructor.static.getTransactionInfo, |
| 886 |
selections = {}, |
| 887 |
transactions = []; |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
for ( const authorId in this.selections ) { |
| 892 |
selections[ authorId ] = this.selections[ authorId ].toJSON(); |
| 893 |
} |
| 894 |
const serializeStoreValues = preserveStoreValues ? function noop( x ) { |
| 895 |
return x; |
| 896 |
} : this.constructor.static.serializeValue; |
| 897 |
const serializeStore = function ( store ) { |
| 898 |
return store.serialize( serializeStoreValues ); |
| 899 |
}; |
| 900 |
let prevInfo; |
| 901 |
for ( let i = 0, iLen = this.transactions.length; i < iLen; i++ ) { |
| 902 |
const tx = this.transactions[ i ]; |
| 903 |
const info = getTransactionInfo( tx ); |
| 904 |
if ( |
| 905 |
info && |
| 906 |
prevInfo && |
| 907 |
info.authorId === prevInfo.authorId && |
| 908 |
info.start === prevInfo.end && |
| 909 |
info.uniformInsert && |
| 910 |
prevInfo.uniformInsert && |
| 911 |
info.uniformInsert.annotationString === prevInfo.uniformInsert.annotationString |
| 912 |
) { |
| 913 |
transactions.push( info.uniformInsert.text ); |
| 914 |
} else { |
| 915 |
const txSerialized = tx.toJSON(); |
| 916 |
if ( i > 0 && tx.authorId === this.transactions[ i - 1 ].authorId ) { |
| 917 |
delete txSerialized.authorId; |
| 918 |
} |
| 919 |
transactions.push( txSerialized ); |
| 920 |
} |
| 921 |
prevInfo = info; |
| 922 |
} |
| 923 |
const stores = this.getStores().map( serializeStore ); |
| 924 |
const data = { |
| 925 |
start: this.start, |
| 926 |
transactions: transactions |
| 927 |
}; |
| 928 |
|
| 929 |
if ( stores.some( ( store ) => store !== null ) ) { |
| 930 |
data.stores = stores; |
| 931 |
} |
| 932 |
if ( Object.keys( selections ).length ) { |
| 933 |
data.selections = selections; |
| 934 |
} |
| 935 |
return data; |
| 936 |
}; |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
ve.dm.Change.prototype.toJSON = function () { |
| 945 |
|
| 946 |
return this.serialize(); |
| 947 |
}; |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
ve.dm.Change.prototype.squash = function () { |
| 961 |
if ( this.transactions.length <= 1 ) { |
| 962 |
return this.clone(); |
| 963 |
} |
| 964 |
return new ve.dm.Change( |
| 965 |
this.start, |
| 966 |
[ ve.dm.TransactionSquasher.static.squash( this.transactions ) ], |
| 967 |
[ this.store.slice() ], |
| 968 |
|
| 969 |
ve.cloneObject( this.selections ) |
| 970 |
); |
| 971 |
}; |