splitdelta.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Delta from './delta.js';
  6. import DeltaFactory from './deltafactory.js';
  7. import { register } from '../batch.js';
  8. import Position from '../position.js';
  9. import Element from '../element.js';
  10. import InsertOperation from '../operation/insertoperation.js';
  11. import MoveOperation from '../operation/moveoperation.js';
  12. import CKEditorError from '../../../utils/ckeditorerror.js';
  13. import MergeDelta from '../delta/mergedelta.js';
  14. /**
  15. * @classdesc
  16. * To provide specific OT behavior and better collisions solving, the {@link engine.model.Batch#split} method
  17. * uses `SplitDelta` class which inherits from the `Delta` class and may overwrite some methods.
  18. *
  19. * @memberOf engine.model.delta
  20. */
  21. export default class SplitDelta extends Delta {
  22. /**
  23. * Position of split or `null` if there are no operations in the delta.
  24. *
  25. * @type {engine.model.Position|null}
  26. */
  27. get position() {
  28. return this._moveOperation ? this._moveOperation.sourcePosition : null;
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. getReversed() {
  34. let delta = super.getReversed();
  35. if ( delta.operations.length > 0 ) {
  36. delta.operations[ 0 ].isSticky = true;
  37. }
  38. return delta;
  39. }
  40. /**
  41. * Operation in the delta that adds to model an element into which split nodes will be moved, or `null` if
  42. * there are no operations in the delta.
  43. *
  44. * Most commonly this will be {@link engine.model.operation.InsertOperation an insert operation}, as `SplitDelta`
  45. * has to create a new node. If `SplitDelta` was created through {@link engine.model.delta.Delta#getReversed reversing}
  46. * a {@link engine.model.delta.MergeDelta merge delta}, this will be a {@link engine.model.operation.ReinsertOperation reinsert operation},
  47. * as we will want to re-insert the exact element that was removed by that merge delta.
  48. *
  49. * @protected
  50. * @type {engine.model.operation.InsertOpertaion|engine.model.operation.ReinsertOperation|null}
  51. */
  52. get _cloneOperation() {
  53. return this.operations[ 0 ] || null;
  54. }
  55. /**
  56. * Operation in the delta that moves model items, that are after split position, to their new parent or `null`
  57. * if there are no operations in the delta.
  58. *
  59. * @protected
  60. * @type {engine.model.operation.MoveOperation|null}
  61. */
  62. get _moveOperation() {
  63. return this.operations[ 1 ] || null;
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. get _reverseDeltaClass() {
  69. return MergeDelta;
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. static get className() {
  75. return 'engine.model.delta.SplitDelta';
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. static get _priority() {
  81. return 5;
  82. }
  83. }
  84. /**
  85. * Splits an element at the given position.
  86. *
  87. * The element cannot be a root element, as root element cannot be split. The `batch-split-root` error will be thrown if
  88. * you try to split the root element.
  89. *
  90. * @chainable
  91. * @method engine.model.Batch#split
  92. * @param {engine.model.Position} position Position of split.
  93. */
  94. register( 'split', function( position ) {
  95. const delta = new SplitDelta();
  96. this.addDelta( delta );
  97. const splitElement = position.parent;
  98. if ( !splitElement.parent ) {
  99. /**
  100. * Root element can not be split.
  101. *
  102. * @error batch-split-root
  103. */
  104. throw new CKEditorError( 'batch-split-root: Root element can not be split.' );
  105. }
  106. const copy = new Element( splitElement.name, splitElement.getAttributes() );
  107. const insert = new InsertOperation( Position.createAfter( splitElement ), copy, this.document.version );
  108. delta.addOperation( insert );
  109. this.document.applyOperation( insert );
  110. const move = new MoveOperation(
  111. position,
  112. splitElement.maxOffset - position.offset,
  113. Position.createFromParentAndOffset( copy, 0 ),
  114. this.document.version
  115. );
  116. move.isSticky = true;
  117. delta.addOperation( move );
  118. this.document.applyOperation( move );
  119. return this;
  120. } );
  121. DeltaFactory.register( SplitDelta );