8
0

splitoperation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 {Number}
  26. * @param {module:engine/model/position~Position|null} graveyardPosition Position in graveyard before the element which
  27. * 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.
  28. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  29. * can be applied or `null` if the operation operates on detached (non-document) tree.
  30. */
  31. constructor( position, howMany, graveyardPosition, baseVersion ) {
  32. super( baseVersion );
  33. /**
  34. * Position at which an element should be split.
  35. *
  36. * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#position
  37. */
  38. this.position = Position.createFromPosition( position );
  39. this.position.stickiness = 'toNext';
  40. this.graveyardPosition = graveyardPosition ? Position.createFromPosition( graveyardPosition ) : null;
  41. if ( this.graveyardPosition ) {
  42. this.graveyardPosition.stickiness = 'toNext';
  43. }
  44. this.howMany = howMany;
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. get type() {
  50. return 'split';
  51. }
  52. /**
  53. * Position after the split element. This is a position at which the clone of split element will be inserted.
  54. * Calculated based on the split position.
  55. *
  56. * @readonly
  57. * @type {module:engine/model/position~Position}
  58. */
  59. get insertionPosition() {
  60. const path = this.position.path.slice( 0, -1 );
  61. path[ path.length - 1 ]++;
  62. return new Position( this.position.root, path );
  63. }
  64. /**
  65. * Position inside the new clone of a split element. This is a position where nodes from after the split position will
  66. * be moved to. Calculated based on the split position.
  67. *
  68. * @readonly
  69. * @type {module:engine/model/position~Position}
  70. */
  71. get moveTargetPosition() {
  72. const path = this.position.path.slice( 0, -1 );
  73. path[ path.length - 1 ]++;
  74. path.push( 0 );
  75. return new Position( this.position.root, path );
  76. }
  77. /**
  78. * Artificial range that contains all the nodes from the split element that will be moved to the new element.
  79. * The range starts at {@link ~#position} and ends in the same parent, at `POSITIVE_INFINITY` offset.
  80. *
  81. * @readonly
  82. * @type {module:engine/model/range~Range}
  83. */
  84. get movedRange() {
  85. const end = this.position.getShiftedBy( Number.POSITIVE_INFINITY );
  86. return new Range( this.position, end );
  87. }
  88. /**
  89. * Creates and returns an operation that has the same parameters as this operation.
  90. *
  91. * @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
  92. */
  93. clone() {
  94. return new this.constructor( this.position, this.howMany, this.graveyardPosition, this.baseVersion );
  95. }
  96. /**
  97. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  98. *
  99. * @returns {module:engine/model/operation/mergeoperation~MergeOperation}
  100. */
  101. getReversed() {
  102. const graveyard = this.position.root.document.graveyard;
  103. const graveyardPosition = new Position( graveyard, [ 0 ] );
  104. return new MergeOperation( this.moveTargetPosition, this.howMany, this.position, graveyardPosition, this.baseVersion + 1 );
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. _validate() {
  110. const element = this.position.parent;
  111. const offset = this.position.offset;
  112. // Validate whether split operation has correct parameters.
  113. if ( !element || element.maxOffset < offset ) {
  114. /**
  115. * Split position is invalid.
  116. *
  117. * @error split-operation-position-invalid
  118. */
  119. throw new CKEditorError( 'split-operation-position-invalid: Split position is invalid.' );
  120. } else if ( this.howMany != element.maxOffset - this.position.offset ) {
  121. /**
  122. * Split operation specifies wrong number of nodes to move.
  123. *
  124. * @error split-operation-how-many-invalid
  125. */
  126. throw new CKEditorError( 'split-operation-how-many-invalid: Split operation specifies wrong number of nodes to move.' );
  127. }
  128. }
  129. /**
  130. * @inheritDoc
  131. */
  132. _execute() {
  133. const splitElement = this.position.parent;
  134. if ( this.graveyardPosition ) {
  135. _move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
  136. } else {
  137. const newElement = splitElement._clone();
  138. _insert( this.insertionPosition, newElement );
  139. }
  140. const sourceRange = Range.createFromParentsAndOffsets( splitElement, this.position.offset, splitElement, splitElement.maxOffset );
  141. _move( sourceRange, this.moveTargetPosition );
  142. }
  143. /**
  144. * @inheritDoc
  145. */
  146. toJSON() {
  147. const json = super.toJSON();
  148. json.position = this.position.toJSON();
  149. if ( this.graveyardPosition ) {
  150. json.graveyardPosition = this.graveyardPosition.toJSON();
  151. }
  152. return json;
  153. }
  154. /**
  155. * @inheritDoc
  156. */
  157. static get className() {
  158. return 'engine.model.operation.SplitOperation';
  159. }
  160. /**
  161. * Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
  162. *
  163. * @param {Object} json Deserialized JSON object.
  164. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  165. * @returns {module:engine/model/operation/splitoperation~SplitOperation}
  166. */
  167. static fromJSON( json, document ) {
  168. const position = Position.fromJSON( json.position, document );
  169. const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
  170. return new this( position, json.howMany, graveyardPosition, json.baseVersion );
  171. }
  172. }