splitoperation.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/splitoperation
  7. */
  8. import Operation from './operation';
  9. import MergeOperation from './mergeoperation';
  10. import Position from '../position';
  11. import Range from '../range';
  12. import { _insert, _move } from './utils';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. /**
  15. * Operation to split {@link module:engine/model/element~Element an element} at
  16. * given {@link module:engine/model/position~Position position} into two elements, both containing a part of the element's content.
  17. *
  18. * @extends module:engine/model/operation/operation~Operation
  19. */
  20. export default class SplitOperation extends Operation {
  21. /**
  22. * Creates a split operation.
  23. *
  24. * @param {module:engine/model/position~Position} position Position at which an element should be split.
  25. * @param {module:engine/model/position~Position|null} graveyardPosition Position in graveyard before the element which
  26. * should be used as a parent of the nodes after `position`. If it is not set, a copy of the the `position` parent will be used.
  27. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  28. * can be applied or `null` if the operation operates on detached (non-document) tree.
  29. */
  30. constructor( position, graveyardPosition, baseVersion ) {
  31. super( baseVersion );
  32. /**
  33. * Position at which an element should be split.
  34. *
  35. * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#position
  36. */
  37. this.position = Position.createFromPosition( position );
  38. this.position.stickiness = 'toNext';
  39. this.graveyardPosition = graveyardPosition ? Position.createFromPosition( graveyardPosition ) : null;
  40. if ( this.graveyardPosition ) {
  41. this.graveyardPosition.stickiness = 'toNext';
  42. }
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. get type() {
  48. return 'split';
  49. }
  50. /**
  51. * Position after the split element. This is a position at which the clone of split element will be inserted.
  52. * Calculated based on the split position.
  53. *
  54. * @readonly
  55. * @type {module:engine/model/position~Position}
  56. */
  57. get insertionPosition() {
  58. const path = this.position.path.slice( 0, -1 );
  59. path[ path.length - 1 ]++;
  60. return new Position( this.position.root, path );
  61. }
  62. /**
  63. * Position inside the new clone of a split element. This is a position where nodes from after the split position will
  64. * be moved to. Calculated based on the split position.
  65. *
  66. * @readonly
  67. * @type {module:engine/model/position~Position}
  68. */
  69. get moveTargetPosition() {
  70. const path = this.position.path.slice( 0, -1 );
  71. path[ path.length - 1 ]++;
  72. path.push( 0 );
  73. return new Position( this.position.root, path );
  74. }
  75. /**
  76. * Artificial range that contains all the nodes from the split element that will be moved to the new element.
  77. * The range starts at {@link ~#position} and ends in the same parent, at `POSITIVE_INFINITY` offset.
  78. *
  79. * @readonly
  80. * @type {module:engine/model/range~Range}
  81. */
  82. get movedRange() {
  83. const end = this.position.getShiftedBy( Number.POSITIVE_INFINITY );
  84. return new Range( this.position, end );
  85. }
  86. /**
  87. * Creates and returns an operation that has the same parameters as this operation.
  88. *
  89. * @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
  90. */
  91. clone() {
  92. return new this.constructor( this.position, this.graveyardPosition, this.baseVersion );
  93. }
  94. /**
  95. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  96. *
  97. * @returns {module:engine/model/operation/mergeoperation~MergeOperation}
  98. */
  99. getReversed() {
  100. const graveyard = this.position.root.document.graveyard;
  101. const graveyardPosition = new Position( graveyard, [ 0 ] );
  102. return new MergeOperation( this.moveTargetPosition, this.position, graveyardPosition, this.baseVersion + 1 );
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. _validate() {
  108. const element = this.position.parent;
  109. const offset = this.position.offset;
  110. // Validate whether split operation has correct parameters.
  111. if ( !element || element.maxOffset < offset ) {
  112. /**
  113. * Split position is invalid.
  114. *
  115. * @error split-operation-position-invalid
  116. */
  117. throw new CKEditorError( 'split-operation-position-invalid: Split position is invalid.' );
  118. }
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. _execute() {
  124. const splitElement = this.position.parent;
  125. if ( this.graveyardPosition ) {
  126. _move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
  127. } else {
  128. const newElement = splitElement._clone();
  129. _insert( this.insertionPosition, newElement );
  130. }
  131. const sourceRange = Range.createFromParentsAndOffsets( splitElement, this.position.offset, splitElement, splitElement.maxOffset );
  132. _move( sourceRange, this.moveTargetPosition );
  133. }
  134. /**
  135. * @inheritDoc
  136. */
  137. static get className() {
  138. return 'engine.model.operation.SplitOperation';
  139. }
  140. /**
  141. * Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
  142. *
  143. * @param {Object} json Deserialized JSON object.
  144. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  145. * @returns {module:engine/model/operation/splitoperation~SplitOperation}
  146. */
  147. static fromJSON( json, document ) {
  148. const position = Position.fromJSON( json.position, document );
  149. const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
  150. return new this( position, graveyardPosition, json.baseVersion );
  151. }
  152. }