| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.dm.Surface = function VeDmSurface( doc, attachedRoot, config ) { |
| 22 |
|
| 23 |
|
| 24 |
if ( !config && ve.isPlainObject( attachedRoot ) ) { |
| 25 |
config = attachedRoot; |
| 26 |
attachedRoot = undefined; |
| 27 |
} |
| 28 |
|
| 29 |
config = config || {}; |
| 30 |
|
| 31 |
attachedRoot = attachedRoot || doc.getDocumentNode(); |
| 32 |
if ( !( attachedRoot instanceof ve.dm.BranchNode ) ) { |
| 33 |
throw new Error( 'Expected ve.dm.BranchNode for attachedRoot' ); |
| 34 |
} |
| 35 |
doc.setAttachedRoot( attachedRoot ); |
| 36 |
|
| 37 |
|
| 38 |
OO.EventEmitter.call( this ); |
| 39 |
|
| 40 |
|
| 41 |
this.documentModel = doc; |
| 42 |
|
| 43 |
this.attachedRoot = attachedRoot; |
| 44 |
this.sourceMode = !!config.sourceMode; |
| 45 |
this.selection = new ve.dm.NullSelection(); |
| 46 |
|
| 47 |
this.selectionBefore = this.selection; |
| 48 |
this.translatedSelection = null; |
| 49 |
this.branchNodes = {}; |
| 50 |
this.selectedNode = null; |
| 51 |
this.newTransactions = []; |
| 52 |
this.stagingStack = []; |
| 53 |
this.undoStack = []; |
| 54 |
this.undoIndex = 0; |
| 55 |
this.undoConflict = false; |
| 56 |
this.historyTrackingInterval = null; |
| 57 |
this.insertionAnnotations = new ve.dm.AnnotationSet( this.getDocument().getStore() ); |
| 58 |
this.selectedAnnotations = new ve.dm.AnnotationSet( this.getDocument().getStore() ); |
| 59 |
this.isCollapsed = null; |
| 60 |
this.multiUser = false; |
| 61 |
this.readOnly = false; |
| 62 |
this.transacting = false; |
| 63 |
this.queueingContextChanges = false; |
| 64 |
this.contextChangeQueued = false; |
| 65 |
this.authorId = null; |
| 66 |
this.lastStoredChange = doc.getCompleteHistoryLength(); |
| 67 |
this.autosaveFailed = false; |
| 68 |
this.autosavePrefix = ''; |
| 69 |
this.synchronizer = null; |
| 70 |
this.storing = false; |
| 71 |
this.setStorage( ve.init.platform.sessionStorage ); |
| 72 |
|
| 73 |
|
| 74 |
this.getDocument().connect( this, { |
| 75 |
transact: 'onDocumentTransact', |
| 76 |
precommit: 'onDocumentPreCommit' |
| 77 |
} ); |
| 78 |
this.storeChangesListener = this.storeChanges.bind( this ); |
| 79 |
this.storeDocStorageListener = this.storeDocStorage.bind( this ); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
OO.mixinClass( ve.dm.Surface, OO.EventEmitter ); |
| 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 |
ve.dm.Surface.prototype.setReadOnly = function ( readOnly ) { |
| 149 |
if ( !!readOnly !== this.readOnly ) { |
| 150 |
this.readOnly = !!readOnly; |
| 151 |
if ( readOnly ) { |
| 152 |
this.stopHistoryTracking(); |
| 153 |
} else { |
| 154 |
this.startHistoryTracking(); |
| 155 |
} |
| 156 |
this.getDocument().setReadOnly( readOnly ); |
| 157 |
this.emit( 'contextChange' ); |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ve.dm.Surface.prototype.isReadOnly = function () { |
| 167 |
return this.readOnly; |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
ve.dm.Surface.prototype.initialize = function () { |
| 176 |
this.startHistoryTracking(); |
| 177 |
this.emit( 'contextChange' ); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
ve.dm.Surface.prototype.getDom = function () { |
| 186 |
if ( this.sourceMode ) { |
| 187 |
return ve.dm.sourceConverter.getSourceTextFromModel( this.getDocument() ); |
| 188 |
} else { |
| 189 |
return ve.dm.converter.getDomFromModel( this.getDocument() ); |
| 190 |
} |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
ve.dm.Surface.prototype.getHtml = function () { |
| 199 |
return this.sourceMode ? |
| 200 |
this.getDom() : |
| 201 |
ve.properInnerHtml( this.getDom().body ); |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
ve.dm.Surface.prototype.setMultiUser = function ( multiUser ) { |
| 210 |
this.multiUser = multiUser; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
ve.dm.Surface.prototype.isMultiUser = function () { |
| 219 |
return this.multiUser; |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
ve.dm.Surface.prototype.createSynchronizer = function ( documentId, config ) { |
| 231 |
if ( this.synchronizer ) { |
| 232 |
throw new Error( 'Synchronizer already set' ); |
| 233 |
} |
| 234 |
|
| 235 |
this.setNullSelection(); |
| 236 |
this.setMultiUser( true ); |
| 237 |
|
| 238 |
this.synchronizer = new ve.dm.SurfaceSynchronizer( this, documentId, config ); |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
ve.dm.Surface.prototype.startHistoryTracking = function () { |
| 245 |
if ( this.readOnly ) { |
| 246 |
return; |
| 247 |
} |
| 248 |
if ( this.historyTrackingInterval === null ) { |
| 249 |
this.historyTrackingInterval = setInterval( this.breakpoint.bind( this ), 3000 ); |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
ve.dm.Surface.prototype.stopHistoryTracking = function () { |
| 257 |
if ( this.readOnly ) { |
| 258 |
return; |
| 259 |
} |
| 260 |
if ( this.historyTrackingInterval !== null ) { |
| 261 |
clearInterval( this.historyTrackingInterval ); |
| 262 |
this.historyTrackingInterval = null; |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
ve.dm.Surface.prototype.resetHistoryTrackingInterval = function () { |
| 270 |
this.stopHistoryTracking(); |
| 271 |
this.startHistoryTracking(); |
| 272 |
}; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
ve.dm.Surface.prototype.getHistory = function () { |
| 289 |
const appliedUndoStack = this.undoStack.slice( 0, this.undoStack.length - this.undoIndex ); |
| 290 |
if ( this.newTransactions.length > 0 ) { |
| 291 |
appliedUndoStack.push( { transactions: this.newTransactions.slice( 0 ) } ); |
| 292 |
} |
| 293 |
return appliedUndoStack; |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
ve.dm.Surface.prototype.isStaging = function () { |
| 302 |
return this.stagingStack.length > 0; |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
ve.dm.Surface.prototype.getStaging = function () { |
| 319 |
return this.stagingStack[ this.stagingStack.length - 1 ]; |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
ve.dm.Surface.prototype.doesStagingAllowUndo = function () { |
| 328 |
const staging = this.getStaging(); |
| 329 |
return staging && staging.allowUndo; |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
ve.dm.Surface.prototype.getStagingTransactions = function () { |
| 340 |
const staging = this.getStaging(); |
| 341 |
return staging && staging.transactions; |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
ve.dm.Surface.prototype.pushStaging = function ( allowUndo ) { |
| 351 |
|
| 352 |
if ( !this.isStaging() ) { |
| 353 |
if ( this.synchronizer ) { |
| 354 |
this.synchronizer.pauseChanges(); |
| 355 |
} |
| 356 |
|
| 357 |
this.breakpoint(); |
| 358 |
this.stopHistoryTracking(); |
| 359 |
this.emit( 'history' ); |
| 360 |
} |
| 361 |
this.stagingStack.push( { |
| 362 |
transactions: [], |
| 363 |
|
| 364 |
|
| 365 |
selectionBefore: this.isStaging() ? this.getStaging().selectionBefore : this.selectionBefore, |
| 366 |
allowUndo: !!allowUndo |
| 367 |
} ); |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
ve.dm.Surface.prototype.popStaging = function () { |
| 377 |
if ( !this.isStaging() ) { |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
const staging = this.stagingStack.pop(); |
| 382 |
const transactions = staging.transactions; |
| 383 |
const reverseTransactions = []; |
| 384 |
|
| 385 |
|
| 386 |
for ( let i = transactions.length - 1; i >= 0; i-- ) { |
| 387 |
const transaction = transactions[ i ].reversed(); |
| 388 |
reverseTransactions.push( transaction ); |
| 389 |
} |
| 390 |
this.changeInternal( reverseTransactions, staging.selectionBefore, true ); |
| 391 |
|
| 392 |
if ( !this.isStaging() ) { |
| 393 |
if ( this.synchronizer ) { |
| 394 |
this.synchronizer.resumeChanges(); |
| 395 |
} |
| 396 |
this.startHistoryTracking(); |
| 397 |
this.emit( 'history' ); |
| 398 |
} |
| 399 |
|
| 400 |
return transactions; |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
ve.dm.Surface.prototype.applyStaging = function () { |
| 409 |
if ( !this.isStaging() ) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
const staging = this.stagingStack.pop(); |
| 414 |
|
| 415 |
if ( this.isStaging() ) { |
| 416 |
|
| 417 |
ve.batchPush( this.getStagingTransactions(), staging.transactions ); |
| 418 |
|
| 419 |
if ( this.getStaging().selectionBefore.isNull() ) { |
| 420 |
this.getStaging().selectionBefore = staging.selectionBefore; |
| 421 |
} |
| 422 |
} else { |
| 423 |
this.truncateUndoStack(); |
| 424 |
|
| 425 |
this.newTransactions = staging.transactions; |
| 426 |
this.selectionBefore = staging.selectionBefore; |
| 427 |
this.breakpoint(); |
| 428 |
} |
| 429 |
|
| 430 |
if ( !this.isStaging() ) { |
| 431 |
if ( this.synchronizer ) { |
| 432 |
this.synchronizer.resumeChanges(); |
| 433 |
} |
| 434 |
this.startHistoryTracking(); |
| 435 |
this.emit( 'history' ); |
| 436 |
} |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
ve.dm.Surface.prototype.popAllStaging = function () { |
| 445 |
if ( !this.isStaging() ) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
const transactions = []; |
| 450 |
while ( this.isStaging() ) { |
| 451 |
ve.batchSplice( transactions, 0, 0, this.popStaging() ); |
| 452 |
} |
| 453 |
return transactions; |
| 454 |
}; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
ve.dm.Surface.prototype.applyAllStaging = function () { |
| 460 |
while ( this.isStaging() ) { |
| 461 |
this.applyStaging(); |
| 462 |
} |
| 463 |
}; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
ve.dm.Surface.prototype.getInsertionAnnotations = function () { |
| 471 |
return this.insertionAnnotations.clone(); |
| 472 |
}; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
ve.dm.Surface.prototype.setInsertionAnnotations = function ( annotations ) { |
| 482 |
if ( this.readOnly ) { |
| 483 |
return; |
| 484 |
} |
| 485 |
this.insertionAnnotations = annotations !== null ? |
| 486 |
annotations.clone() : |
| 487 |
new ve.dm.AnnotationSet( this.getDocument().getStore() ); |
| 488 |
|
| 489 |
this.emit( 'insertionAnnotationsChange', this.insertionAnnotations ); |
| 490 |
this.emit( 'contextChange' ); |
| 491 |
}; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
ve.dm.Surface.prototype.addInsertionAnnotations = function ( annotations ) { |
| 501 |
if ( this.readOnly ) { |
| 502 |
return; |
| 503 |
} |
| 504 |
if ( annotations instanceof ve.dm.Annotation ) { |
| 505 |
this.insertionAnnotations.push( annotations ); |
| 506 |
} else if ( annotations instanceof ve.dm.AnnotationSet ) { |
| 507 |
this.insertionAnnotations.addSet( annotations ); |
| 508 |
} else { |
| 509 |
throw new Error( 'Invalid annotations' ); |
| 510 |
} |
| 511 |
|
| 512 |
this.emit( 'insertionAnnotationsChange', this.insertionAnnotations ); |
| 513 |
this.emit( 'contextChange' ); |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
ve.dm.Surface.prototype.removeInsertionAnnotations = function ( annotations ) { |
| 524 |
if ( this.readOnly ) { |
| 525 |
return; |
| 526 |
} |
| 527 |
if ( annotations instanceof ve.dm.Annotation ) { |
| 528 |
this.insertionAnnotations.remove( annotations ); |
| 529 |
} else if ( annotations instanceof ve.dm.AnnotationSet ) { |
| 530 |
this.insertionAnnotations.removeSet( annotations ); |
| 531 |
} else { |
| 532 |
throw new Error( 'Invalid annotations' ); |
| 533 |
} |
| 534 |
|
| 535 |
this.emit( 'insertionAnnotationsChange', this.insertionAnnotations ); |
| 536 |
this.emit( 'contextChange' ); |
| 537 |
}; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
ve.dm.Surface.prototype.canRedo = function () { |
| 545 |
return this.undoIndex > 0 && !this.readOnly; |
| 546 |
}; |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
ve.dm.Surface.prototype.canUndo = function () { |
| 554 |
return this.hasBeenModified() && !this.readOnly && ( !this.isStaging() || this.doesStagingAllowUndo() ) && !this.undoConflict; |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
ve.dm.Surface.prototype.hasBeenModified = function () { |
| 565 |
return this.undoStack.length - this.undoIndex > 0 || !!this.newTransactions.length; |
| 566 |
}; |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
ve.dm.Surface.prototype.getDocument = function () { |
| 574 |
return this.documentModel; |
| 575 |
}; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
ve.dm.Surface.prototype.getAttachedRoot = function () { |
| 584 |
OO.ui.warnDeprecation( 've.dm.Surface.getAttachedRoot() is deprecated. Use ve.dm.Surface.getDocument().getAttachedRoot() instead.' ); |
| 585 |
return this.attachedRoot; |
| 586 |
}; |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
ve.dm.Surface.prototype.getSelection = function () { |
| 594 |
return this.selection; |
| 595 |
}; |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
ve.dm.Surface.prototype.getTranslatedSelection = function () { |
| 603 |
return this.translatedSelection || this.selection; |
| 604 |
}; |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
ve.dm.Surface.prototype.getFragment = function ( selection, noAutoSelect, excludeInsertions ) { |
| 615 |
selection = selection || this.selection; |
| 616 |
|
| 617 |
return this.sourceMode ? |
| 618 |
new ve.dm.SourceSurfaceFragment( this, selection, noAutoSelect, excludeInsertions ) : |
| 619 |
new ve.dm.SurfaceFragment( this, selection, noAutoSelect, excludeInsertions ); |
| 620 |
}; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
ve.dm.Surface.prototype.getLinearFragment = function ( range, noAutoSelect, excludeInsertions ) { |
| 631 |
return this.getFragment( new ve.dm.LinearSelection( range ), noAutoSelect, excludeInsertions ); |
| 632 |
}; |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
ve.dm.Surface.prototype.truncateUndoStack = function () { |
| 642 |
if ( this.undoIndex ) { |
| 643 |
this.undoStack = this.undoStack.slice( 0, this.undoStack.length - this.undoIndex ); |
| 644 |
this.undoIndex = 0; |
| 645 |
this.emit( 'undoStackChange' ); |
| 646 |
} |
| 647 |
}; |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
ve.dm.Surface.prototype.startQueueingContextChanges = function () { |
| 663 |
if ( !this.queueingContextChanges ) { |
| 664 |
this.queueingContextChanges = true; |
| 665 |
this.contextChangeQueued = false; |
| 666 |
} |
| 667 |
}; |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
ve.dm.Surface.prototype.emitContextChange = function () { |
| 677 |
if ( this.queueingContextChanges ) { |
| 678 |
this.contextChangeQueued = true; |
| 679 |
} else { |
| 680 |
this.emit( 'contextChange' ); |
| 681 |
} |
| 682 |
}; |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
ve.dm.Surface.prototype.stopQueueingContextChanges = function () { |
| 693 |
if ( this.queueingContextChanges ) { |
| 694 |
this.queueingContextChanges = false; |
| 695 |
if ( this.contextChangeQueued ) { |
| 696 |
this.contextChangeQueued = false; |
| 697 |
this.emit( 'contextChange' ); |
| 698 |
} |
| 699 |
} |
| 700 |
}; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
ve.dm.Surface.prototype.setLinearSelection = function ( range ) { |
| 708 |
this.setSelection( new ve.dm.LinearSelection( range ) ); |
| 709 |
}; |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
ve.dm.Surface.prototype.setNullSelection = function () { |
| 715 |
this.setSelection( new ve.dm.NullSelection() ); |
| 716 |
}; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
ve.dm.Surface.prototype.fixupRangeForLinks = function ( range ) { |
| 725 |
if ( range.isCollapsed() ) { |
| 726 |
return range; |
| 727 |
} |
| 728 |
|
| 729 |
const linearData = this.getDocument().data; |
| 730 |
|
| 731 |
function getLinks( offset ) { |
| 732 |
return linearData.getAnnotationsFromOffset( offset ).filter( ( ann ) => ann.name === 'link' ); |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
let start = range.start; |
| 738 |
let end = range.end; |
| 739 |
const rangeAnnotations = linearData.getAnnotationsFromRange( range ); |
| 740 |
const startLink = getLinks( start ).diffWith( rangeAnnotations ).getHash( 0 ); |
| 741 |
const endLink = getLinks( end ).diffWith( rangeAnnotations ).getHash( 0 ); |
| 742 |
|
| 743 |
if ( startLink === undefined && endLink === undefined ) { |
| 744 |
return range; |
| 745 |
} |
| 746 |
|
| 747 |
if ( startLink !== undefined ) { |
| 748 |
while ( start > 0 && getLinks( start - 1 ).containsHash( startLink ) ) { |
| 749 |
start--; |
| 750 |
} |
| 751 |
} |
| 752 |
if ( endLink !== undefined ) { |
| 753 |
while ( end < linearData.getLength() && getLinks( end ).containsHash( endLink ) ) { |
| 754 |
end++; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
if ( range.isBackwards() ) { |
| 759 |
return new ve.Range( end, start ); |
| 760 |
} else { |
| 761 |
return new ve.Range( start, end ); |
| 762 |
} |
| 763 |
}; |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
ve.dm.Surface.prototype.setSelection = function ( selection ) { |
| 775 |
const oldSelection = this.selection; |
| 776 |
let maxOffset; |
| 777 |
if ( |
| 778 |
selection instanceof ve.dm.LinearSelection && |
| 779 |
( maxOffset = this.getDocument().getDocumentRange().end ) && |
| 780 |
maxOffset < selection.getRange().end |
| 781 |
) { |
| 782 |
|
| 783 |
ve.error( 'Attempted to set an out of bounds selection: ' + JSON.stringify( selection ) + ', adjusting' ); |
| 784 |
|
| 785 |
|
| 786 |
selection = new ve.dm.LinearSelection( new ve.Range( |
| 787 |
Math.min( maxOffset, selection.getRange().start ), |
| 788 |
maxOffset |
| 789 |
) ); |
| 790 |
|
| 791 |
} |
| 792 |
this.translatedSelection = null; |
| 793 |
|
| 794 |
if ( this.transacting ) { |
| 795 |
|
| 796 |
this.selection = selection; |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
let selectionChange = false; |
| 801 |
|
| 802 |
if ( !oldSelection.equals( selection ) ) { |
| 803 |
selectionChange = true; |
| 804 |
this.selection = selection; |
| 805 |
} |
| 806 |
|
| 807 |
function intersectRanges( rangeA, rangeB ) { |
| 808 |
const rangeStart = Math.max( rangeA.start, rangeB.start ); |
| 809 |
const rangeEnd = Math.min( rangeA.end, rangeB.end ); |
| 810 |
|
| 811 |
return new ve.Range( |
| 812 |
rangeStart, |
| 813 |
|
| 814 |
|
| 815 |
Math.max( rangeStart, rangeEnd ) |
| 816 |
); |
| 817 |
} |
| 818 |
|
| 819 |
let range; |
| 820 |
let selectedNode; |
| 821 |
const branchNodes = {}; |
| 822 |
let contextChange = false; |
| 823 |
if ( selection instanceof ve.dm.LinearSelection ) { |
| 824 |
range = selection.getRange(); |
| 825 |
|
| 826 |
|
| 827 |
branchNodes.start = this.getDocument().getBranchNodeFromOffset( range.start ); |
| 828 |
branchNodes.startRange = intersectRanges( branchNodes.start.getRange(), range ); |
| 829 |
if ( !range.isCollapsed() ) { |
| 830 |
branchNodes.end = this.getDocument().getBranchNodeFromOffset( range.end ); |
| 831 |
branchNodes.endRange = intersectRanges( branchNodes.end.getRange(), range ); |
| 832 |
} else { |
| 833 |
branchNodes.end = branchNodes.start; |
| 834 |
branchNodes.endRange = branchNodes.startRange; |
| 835 |
} |
| 836 |
selectedNode = this.getSelectedNodeFromSelection( selection ); |
| 837 |
|
| 838 |
|
| 839 |
if ( !this.sourceMode ) { |
| 840 |
const linearData = this.getDocument().data; |
| 841 |
|
| 842 |
const insertionAnnotations = linearData.getInsertionAnnotationsFromRange( range ); |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
if ( !insertionAnnotations.equalsInOrder( this.insertionAnnotations ) ) { |
| 847 |
this.setInsertionAnnotations( insertionAnnotations ); |
| 848 |
} |
| 849 |
|
| 850 |
let selectedAnnotations; |
| 851 |
|
| 852 |
if ( range.isCollapsed() ) { |
| 853 |
selectedAnnotations = linearData.getAnnotationsFromOffset( range.start ); |
| 854 |
} else { |
| 855 |
selectedAnnotations = linearData.getAnnotationsFromRange( range, true ); |
| 856 |
} |
| 857 |
if ( !selectedAnnotations.compareTo( this.selectedAnnotations ) ) { |
| 858 |
this.selectedAnnotations = selectedAnnotations; |
| 859 |
contextChange = true; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
if ( selectionChange && !range.isCollapsed() && oldSelection instanceof ve.dm.LinearSelection ) { |
| 866 |
const rangeFocus = new ve.Range( range.to ); |
| 867 |
const oldRangeFocus = new ve.Range( oldSelection.getRange().to ); |
| 868 |
const focusRangeMovingBack = rangeFocus.to < oldRangeFocus.to; |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
if ( |
| 873 |
!linearData.getInsertionAnnotationsFromRange( rangeFocus, focusRangeMovingBack ).compareTo( linearData.getInsertionAnnotationsFromRange( oldRangeFocus, focusRangeMovingBack ) ) |
| 874 |
) { |
| 875 |
contextChange = true; |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
} else if ( selection instanceof ve.dm.TableSelection ) { |
| 880 |
selectedNode = selection.getMatrixCells( this.getDocument() )[ 0 ].node; |
| 881 |
contextChange = true; |
| 882 |
} else if ( selection.isNull() ) { |
| 883 |
contextChange = true; |
| 884 |
} |
| 885 |
|
| 886 |
if ( range && range.isCollapsed() !== this.isCollapsed ) { |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
this.isCollapsed = range.isCollapsed(); |
| 891 |
contextChange = true; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
if ( |
| 896 |
selectedNode !== this.selectedNode || |
| 897 |
branchNodes.start !== this.branchNodes.start || |
| 898 |
branchNodes.end !== this.branchNodes.end |
| 899 |
) { |
| 900 |
this.branchNodes = branchNodes; |
| 901 |
this.selectedNode = selectedNode; |
| 902 |
contextChange = true; |
| 903 |
} else if ( |
| 904 |
branchNodes.startRange && this.branchNodes.startRange && ( |
| 905 |
branchNodes.startRange.isCollapsed() !== this.branchNodes.startRange.isCollapsed() || |
| 906 |
branchNodes.endRange.isCollapsed() !== this.branchNodes.endRange.isCollapsed() |
| 907 |
) |
| 908 |
) { |
| 909 |
|
| 910 |
|
| 911 |
this.branchNodes = branchNodes; |
| 912 |
contextChange = true; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
if ( selectionChange ) { |
| 917 |
this.emit( 'select', this.selection ); |
| 918 |
if ( oldSelection.isNull() ) { |
| 919 |
this.emit( 'focus' ); |
| 920 |
} |
| 921 |
if ( selection.isNull() ) { |
| 922 |
this.emit( 'blur' ); |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
if ( contextChange ) { |
| 927 |
this.emitContextChange(); |
| 928 |
} |
| 929 |
|
| 930 |
}; |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
ve.dm.Surface.prototype.change = function ( transactions, selection ) { |
| 940 |
this.changeInternal( transactions, selection, false ); |
| 941 |
}; |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
ve.dm.Surface.prototype.changeInternal = function ( transactions, selection, skipUndoStack ) { |
| 954 |
const selectionBefore = this.selection; |
| 955 |
let contextChange = false; |
| 956 |
|
| 957 |
this.startQueueingContextChanges(); |
| 958 |
|
| 959 |
|
| 960 |
if ( transactions && !this.readOnly ) { |
| 961 |
if ( transactions instanceof ve.dm.Transaction ) { |
| 962 |
transactions = [ transactions ]; |
| 963 |
} |
| 964 |
this.transacting = true; |
| 965 |
for ( let i = 0, len = transactions.length; i < len; i++ ) { |
| 966 |
if ( !transactions[ i ].isNoOp() ) { |
| 967 |
let committed; |
| 968 |
|
| 969 |
try { |
| 970 |
committed = false; |
| 971 |
this.getDocument().commit( transactions[ i ], this.isStaging() ); |
| 972 |
committed = true; |
| 973 |
} finally { |
| 974 |
if ( !committed ) { |
| 975 |
this.stopQueueingContextChanges(); |
| 976 |
} |
| 977 |
} |
| 978 |
if ( !skipUndoStack ) { |
| 979 |
if ( this.isStaging() ) { |
| 980 |
if ( !this.getStagingTransactions().length ) { |
| 981 |
this.getStaging().selectionBefore = selectionBefore; |
| 982 |
} |
| 983 |
this.getStagingTransactions().push( transactions[ i ] ); |
| 984 |
} else { |
| 985 |
this.truncateUndoStack(); |
| 986 |
if ( !this.newTransactions.length ) { |
| 987 |
this.selectionBefore = selectionBefore; |
| 988 |
} |
| 989 |
this.newTransactions.push( transactions[ i ] ); |
| 990 |
} |
| 991 |
} |
| 992 |
if ( transactions[ i ].hasElementAttributeOperations() ) { |
| 993 |
contextChange = true; |
| 994 |
} |
| 995 |
} |
| 996 |
} |
| 997 |
this.transacting = false; |
| 998 |
this.undoConflict = false; |
| 999 |
this.emit( 'history' ); |
| 1000 |
} |
| 1001 |
const selectionAfter = this.selection; |
| 1002 |
|
| 1003 |
|
| 1004 |
if ( selection ) { |
| 1005 |
this.setSelection( selection ); |
| 1006 |
} else if ( transactions ) { |
| 1007 |
|
| 1008 |
this.setSelection( this.selection ); |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
if ( |
| 1015 |
!selectionBefore.equals( selectionAfter ) && |
| 1016 |
selectionAfter.equals( this.selection ) |
| 1017 |
) { |
| 1018 |
this.emit( 'select', this.selection ); |
| 1019 |
} |
| 1020 |
|
| 1021 |
if ( contextChange ) { |
| 1022 |
this.emitContextChange(); |
| 1023 |
} |
| 1024 |
|
| 1025 |
this.stopQueueingContextChanges(); |
| 1026 |
}; |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
ve.dm.Surface.prototype.breakpoint = function () { |
| 1035 |
if ( this.readOnly ) { |
| 1036 |
return false; |
| 1037 |
} |
| 1038 |
let breakpointSet = false; |
| 1039 |
this.resetHistoryTrackingInterval(); |
| 1040 |
if ( this.newTransactions.length > 0 ) { |
| 1041 |
this.undoStack.push( { |
| 1042 |
start: this.getDocument().getCompleteHistoryLength() - this.newTransactions.length, |
| 1043 |
transactions: this.newTransactions, |
| 1044 |
selection: this.selection, |
| 1045 |
selectionBefore: this.selectionBefore |
| 1046 |
} ); |
| 1047 |
this.newTransactions = []; |
| 1048 |
this.emit( 'undoStackChange' ); |
| 1049 |
breakpointSet = true; |
| 1050 |
} |
| 1051 |
|
| 1052 |
this.selectionBefore = this.selection; |
| 1053 |
return breakpointSet; |
| 1054 |
}; |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
ve.dm.Surface.prototype.undo = function () { |
| 1063 |
if ( !this.canUndo() ) { |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if ( this.isStaging() ) { |
| 1068 |
this.popAllStaging(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
this.breakpoint(); |
| 1072 |
this.undoIndex++; |
| 1073 |
|
| 1074 |
const transactions = []; |
| 1075 |
let item; |
| 1076 |
if ( !this.isMultiUser() ) { |
| 1077 |
item = this.undoStack[ this.undoStack.length - this.undoIndex ]; |
| 1078 |
if ( item ) { |
| 1079 |
|
| 1080 |
for ( let i = item.transactions.length - 1; i >= 0; i-- ) { |
| 1081 |
const transaction = item.transactions[ i ].reversed(); |
| 1082 |
transactions.push( transaction ); |
| 1083 |
} |
| 1084 |
this.changeInternal( transactions, item.selectionBefore, true ); |
| 1085 |
this.emit( 'undoStackChange' ); |
| 1086 |
} |
| 1087 |
} else { |
| 1088 |
|
| 1089 |
while ( this.undoIndex <= this.undoStack.length ) { |
| 1090 |
item = this.undoStack[ this.undoStack.length - this.undoIndex ]; |
| 1091 |
|
| 1092 |
const authorId = item.transactions[ 0 ].authorId; |
| 1093 |
if ( authorId === null || authorId === this.getAuthorId() ) { |
| 1094 |
break; |
| 1095 |
} |
| 1096 |
item = null; |
| 1097 |
this.undoIndex++; |
| 1098 |
} |
| 1099 |
if ( item ) { |
| 1100 |
const history = this.getDocument().getChangeSince( item.start + item.transactions.length ); |
| 1101 |
const done = new ve.dm.Change( |
| 1102 |
item.start, |
| 1103 |
item.transactions, |
| 1104 |
|
| 1105 |
item.transactions.map( () => new ve.dm.HashValueStore() ), |
| 1106 |
{} |
| 1107 |
); |
| 1108 |
const result = ve.dm.Change.static.rebaseUncommittedChange( history, done.reversed() ); |
| 1109 |
if ( result.rejected ) { |
| 1110 |
|
| 1111 |
this.undoIndex--; |
| 1112 |
this.undoConflict = true; |
| 1113 |
|
| 1114 |
this.emit( 'history' ); |
| 1115 |
} else { |
| 1116 |
const selection = item.selectionBefore.translateByChange( result.transposedHistory ); |
| 1117 |
|
| 1118 |
this.changeInternal( result.rebased.transactions, selection, true ); |
| 1119 |
this.emit( 'undoStackChange' ); |
| 1120 |
} |
| 1121 |
} else { |
| 1122 |
|
| 1123 |
this.emit( 'history' ); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
}; |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
ve.dm.Surface.prototype.redo = function () { |
| 1134 |
if ( !this.canRedo() ) { |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
|
| 1138 |
this.breakpoint(); |
| 1139 |
|
| 1140 |
const item = this.undoStack[ this.undoStack.length - this.undoIndex ]; |
| 1141 |
if ( item ) { |
| 1142 |
this.undoIndex--; |
| 1143 |
|
| 1144 |
this.changeInternal( ve.copy( item.transactions ), item.selection, true ); |
| 1145 |
this.emit( 'undoStackChange' ); |
| 1146 |
} |
| 1147 |
}; |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
ve.dm.Surface.prototype.onDocumentTransact = function ( tx ) { |
| 1157 |
this.setSelection( this.getSelection().translateByTransactionWithAuthor( tx, this.authorId ) ); |
| 1158 |
this.emit( 'documentUpdate', tx ); |
| 1159 |
}; |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
ve.dm.Surface.prototype.getSelectedNode = function () { |
| 1167 |
return this.selectedNode; |
| 1168 |
}; |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
ve.dm.Surface.prototype.getSelectedNodeFromSelection = function ( selection ) { |
| 1177 |
selection = selection || this.getSelection(); |
| 1178 |
|
| 1179 |
if ( !( selection instanceof ve.dm.LinearSelection ) ) { |
| 1180 |
return null; |
| 1181 |
} |
| 1182 |
|
| 1183 |
let selectedNode = null; |
| 1184 |
const range = selection.getRange(); |
| 1185 |
if ( !range.isCollapsed() ) { |
| 1186 |
const startNode = this.getDocument().documentNode.getNodeFromOffset( range.start + 1 ); |
| 1187 |
if ( startNode && startNode.getOuterRange().equalsSelection( range ) ) { |
| 1188 |
selectedNode = startNode; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
return selectedNode; |
| 1192 |
}; |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
ve.dm.Surface.prototype.onDocumentPreCommit = function ( tx ) { |
| 1203 |
this.translatedSelection = this.selection.translateByTransaction( tx ); |
| 1204 |
}; |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
ve.dm.Surface.prototype.getModifiedRanges = function ( options ) { |
| 1217 |
const doc = this.getDocument(); |
| 1218 |
const ranges = []; |
| 1219 |
|
| 1220 |
options = options || {}; |
| 1221 |
|
| 1222 |
this.getHistory().forEach( ( stackItem ) => { |
| 1223 |
stackItem.transactions.forEach( ( tx ) => { |
| 1224 |
const newRange = tx.getModifiedRange( doc, options ); |
| 1225 |
|
| 1226 |
if ( newRange ) { |
| 1227 |
|
| 1228 |
ranges.forEach( ( range, i, arr ) => { |
| 1229 |
arr[ i ] = tx.translateRange( range, true ); |
| 1230 |
} ); |
| 1231 |
if ( options.includeCollapsed || !newRange.isCollapsed() ) { |
| 1232 |
ranges.push( newRange ); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
} ); |
| 1236 |
} ); |
| 1237 |
|
| 1238 |
|
| 1239 |
const compactRanges = []; |
| 1240 |
let lastRange = null; |
| 1241 |
ranges |
| 1242 |
.sort( ( a, b ) => a.start - b.start ) |
| 1243 |
.forEach( ( range ) => { |
| 1244 |
if ( options.includeCollapsed || !range.isCollapsed() ) { |
| 1245 |
if ( lastRange && lastRange.touchesRange( range ) ) { |
| 1246 |
compactRanges.pop(); |
| 1247 |
range = lastRange.expand( range ); |
| 1248 |
} |
| 1249 |
compactRanges.push( range ); |
| 1250 |
lastRange = range; |
| 1251 |
} |
| 1252 |
} ); |
| 1253 |
|
| 1254 |
return compactRanges; |
| 1255 |
}; |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
ve.dm.Surface.prototype.getOffsetFromSourceOffset = function ( offset ) { |
| 1265 |
if ( offset < 0 ) { |
| 1266 |
throw new Error( 'Offset out of bounds' ); |
| 1267 |
} |
| 1268 |
|
| 1269 |
const lines = this.getDocument().getDocumentNode().getChildren(); |
| 1270 |
let lineOffset = 0, |
| 1271 |
line = 0; |
| 1272 |
|
| 1273 |
while ( lineOffset < offset + 1 ) { |
| 1274 |
if ( !lines[ line ] || lines[ line ].isInternal() ) { |
| 1275 |
throw new Error( 'Offset out of bounds' ); |
| 1276 |
} |
| 1277 |
lineOffset += lines[ line ].getLength() + 1; |
| 1278 |
line++; |
| 1279 |
} |
| 1280 |
return offset + line; |
| 1281 |
}; |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
ve.dm.Surface.prototype.getSourceOffsetFromOffset = function ( offset ) { |
| 1291 |
if ( offset < 0 ) { |
| 1292 |
throw new Error( 'Offset out of bounds' ); |
| 1293 |
} |
| 1294 |
|
| 1295 |
const lines = this.getDocument().getDocumentNode().getChildren(); |
| 1296 |
let lineOffset = 0, |
| 1297 |
line = 0; |
| 1298 |
|
| 1299 |
while ( lineOffset < offset ) { |
| 1300 |
if ( !lines[ line ] || lines[ line ].isInternal() ) { |
| 1301 |
throw new Error( 'Offset out of bounds' ); |
| 1302 |
} |
| 1303 |
lineOffset += lines[ line ].getOuterLength(); |
| 1304 |
line++; |
| 1305 |
} |
| 1306 |
return offset - line; |
| 1307 |
}; |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
ve.dm.Surface.prototype.getRangeFromSourceOffsets = function ( from, to ) { |
| 1317 |
const fromOffset = this.getOffsetFromSourceOffset( from ); |
| 1318 |
return new ve.Range( |
| 1319 |
fromOffset, |
| 1320 |
|
| 1321 |
to === undefined || to === from ? |
| 1322 |
fromOffset : |
| 1323 |
this.getOffsetFromSourceOffset( to ) |
| 1324 |
); |
| 1325 |
}; |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
ve.dm.Surface.prototype.getAuthorId = function () { |
| 1333 |
return this.authorId; |
| 1334 |
}; |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
ve.dm.Surface.prototype.setAuthorId = function ( authorId ) { |
| 1342 |
this.authorId = authorId; |
| 1343 |
}; |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
ve.dm.Surface.prototype.storeChanges = function () { |
| 1351 |
if ( this.autosaveFailed ) { |
| 1352 |
return; |
| 1353 |
} |
| 1354 |
|
| 1355 |
const dmDoc = this.getDocument(); |
| 1356 |
const change = dmDoc.getChangeSince( this.lastStoredChange ); |
| 1357 |
if ( !change.isEmpty() ) { |
| 1358 |
const changes = this.storage.getObject( this.autosavePrefix + 've-changes' ) || []; |
| 1359 |
changes.push( change ); |
| 1360 |
if ( this.storage.setObject( this.autosavePrefix + 've-changes', changes, this.storageExpiry ) ) { |
| 1361 |
this.lastStoredChange = dmDoc.getCompleteHistoryLength(); |
| 1362 |
this.storage.setObject( this.autosavePrefix + 've-selection', this.getSelection(), this.storageExpiry ); |
| 1363 |
this.updateExpiry( [ 've-changes', 've-selection' ] ); |
| 1364 |
} else { |
| 1365 |
|
| 1366 |
|
| 1367 |
this.autosaveFailed = true; |
| 1368 |
this.emit( 'autosaveFailed' ); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
}; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
ve.dm.Surface.prototype.storeDocStorage = function () { |
| 1377 |
if ( this.autosaveFailed ) { |
| 1378 |
return; |
| 1379 |
} |
| 1380 |
|
| 1381 |
const dmDoc = this.getDocument(); |
| 1382 |
this.storage.setObject( this.autosavePrefix + 've-docstorage', dmDoc.getStorage(), this.storageExpiry ); |
| 1383 |
this.updateExpiry( [ 've-docstorage' ] ); |
| 1384 |
}; |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
ve.dm.Surface.prototype.setAutosaveDocId = function ( docId ) { |
| 1395 |
this.autosavePrefix = docId + '/'; |
| 1396 |
}; |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
ve.dm.Surface.prototype.setStorage = function ( storage, storageExpiry ) { |
| 1405 |
if ( this.storing ) { |
| 1406 |
throw new Error( 'Can\'t change storage interface after auto-save has stared' ); |
| 1407 |
} |
| 1408 |
this.storage = storage; |
| 1409 |
this.storageExpiry = storageExpiry; |
| 1410 |
|
| 1411 |
let isLocalStorage = false; |
| 1412 |
try { |
| 1413 |
|
| 1414 |
|
| 1415 |
isLocalStorage = this.storage.store === window.localStorage; |
| 1416 |
} catch ( e ) {} |
| 1417 |
|
| 1418 |
if ( isLocalStorage ) { |
| 1419 |
const conflictableKeys = {}; |
| 1420 |
conflictableKeys[ this.autosavePrefix + 've-docstate' ] = true; |
| 1421 |
conflictableKeys[ this.autosavePrefix + 've-dochtml' ] = true; |
| 1422 |
conflictableKeys[ this.autosavePrefix + 've-selection' ] = true; |
| 1423 |
conflictableKeys[ this.autosavePrefix + 've-changes' ] = true; |
| 1424 |
this.storage.addConflictableKeys( conflictableKeys ); |
| 1425 |
} |
| 1426 |
}; |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
ve.dm.Surface.prototype.startStoringChanges = function () { |
| 1432 |
this.storing = true; |
| 1433 |
this.on( 'undoStackChange', this.storeChangesListener ); |
| 1434 |
this.getDocument().on( 'storage', this.storeDocStorageListener ); |
| 1435 |
this.updateExpiry(); |
| 1436 |
}; |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
ve.dm.Surface.prototype.stopStoringChanges = function () { |
| 1442 |
this.storing = false; |
| 1443 |
this.off( 'undoStackChange', this.storeChangesListener ); |
| 1444 |
this.getDocument().off( 'storage', this.storeDocStorageListener ); |
| 1445 |
}; |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
ve.dm.Surface.prototype.restoreChanges = function () { |
| 1454 |
const changes = this.storage.getObject( this.autosavePrefix + 've-changes' ) || []; |
| 1455 |
let restored = false; |
| 1456 |
|
| 1457 |
try { |
| 1458 |
changes.forEach( ( data ) => { |
| 1459 |
const change = ve.dm.Change.static.unsafeDeserialize( data ); |
| 1460 |
change.applyTo( this, true ); |
| 1461 |
this.breakpoint(); |
| 1462 |
} ); |
| 1463 |
restored = !!changes.length; |
| 1464 |
|
| 1465 |
this.getDocument().setStorage( |
| 1466 |
this.storage.getObject( this.autosavePrefix + 've-docstorage' ) || {} |
| 1467 |
); |
| 1468 |
|
| 1469 |
let selection; |
| 1470 |
try { |
| 1471 |
selection = ve.dm.Selection.static.newFromJSON( |
| 1472 |
this.storage.getObject( this.autosavePrefix + 've-selection' ) |
| 1473 |
); |
| 1474 |
} catch ( e ) { |
| 1475 |
|
| 1476 |
} |
| 1477 |
if ( selection ) { |
| 1478 |
|
| 1479 |
setTimeout( () => { |
| 1480 |
this.setSelection( selection ); |
| 1481 |
} ); |
| 1482 |
} |
| 1483 |
} catch ( e ) { |
| 1484 |
throw new Error( 'Failed to restore auto-saved session: ' + e ); |
| 1485 |
} |
| 1486 |
|
| 1487 |
this.lastStoredChange = this.getDocument().getCompleteHistoryLength(); |
| 1488 |
|
| 1489 |
return restored; |
| 1490 |
}; |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
ve.dm.Surface.prototype.storeDocState = function ( state, html ) { |
| 1503 |
|
| 1504 |
this.removeDocStateAndChanges(); |
| 1505 |
if ( state ) { |
| 1506 |
if ( !this.updateDocState( state ) ) { |
| 1507 |
this.stopStoringChanges(); |
| 1508 |
return false; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
const useLatestHtml = html === undefined; |
| 1512 |
|
| 1513 |
if ( !this.storage.set( this.autosavePrefix + 've-dochtml', useLatestHtml ? this.getHtml() : html, this.storageExpiry ) ) { |
| 1514 |
|
| 1515 |
this.storage.remove( this.autosavePrefix + 've-docstate' ); |
| 1516 |
this.stopStoringChanges(); |
| 1517 |
return false; |
| 1518 |
} |
| 1519 |
this.updateExpiry( [ 've-dochtml' ] ); |
| 1520 |
|
| 1521 |
if ( useLatestHtml ) { |
| 1522 |
|
| 1523 |
|
| 1524 |
this.lastStoredChange = this.getDocument().getCompleteHistoryLength(); |
| 1525 |
} |
| 1526 |
|
| 1527 |
return true; |
| 1528 |
}; |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
ve.dm.Surface.prototype.updateDocState = function ( state ) { |
| 1537 |
if ( this.storage.set( this.autosavePrefix + 've-docstate', JSON.stringify( state ), this.storageExpiry ) ) { |
| 1538 |
this.updateExpiry( [ 've-docstate' ] ); |
| 1539 |
return true; |
| 1540 |
} |
| 1541 |
return false; |
| 1542 |
}; |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
ve.dm.Surface.prototype.updateExpiry = function ( skipKeys ) { |
| 1550 |
if ( !this.storageExpiry ) { |
| 1551 |
return; |
| 1552 |
} |
| 1553 |
skipKeys = skipKeys || []; |
| 1554 |
[ 've-docstate', 've-dochtml', 've-selection', 've-changes' ].forEach( ( key ) => { |
| 1555 |
if ( skipKeys.indexOf( key ) === -1 ) { |
| 1556 |
this.storage.setExpires( this.autosavePrefix + key, this.storageExpiry ); |
| 1557 |
} |
| 1558 |
} ); |
| 1559 |
}; |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
ve.dm.Surface.prototype.removeDocStateAndChanges = function () { |
| 1565 |
this.storage.remove( this.autosavePrefix + 've-docstate' ); |
| 1566 |
this.storage.remove( this.autosavePrefix + 've-dochtml' ); |
| 1567 |
this.storage.remove( this.autosavePrefix + 've-selection' ); |
| 1568 |
this.storage.remove( this.autosavePrefix + 've-changes' ); |
| 1569 |
}; |