8
0

splitdelta.js 3.6 KB

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