splitoperation.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 given
  16. * {@link module:engine/model/operation/splitoperation~SplitOperation#splitPosition split position} into two elements,
  17. * both containing a part of the element's original content.
  18. *
  19. * @extends module:engine/model/operation/operation~Operation
  20. */
  21. export default class SplitOperation extends Operation {
  22. /**
  23. * Creates a split operation.
  24. *
  25. * @param {module:engine/model/position~Position} splitPosition Position at which an element should be split.
  26. * @param {Number} howMany Total offset size of elements that are in the split element after `position`.
  27. * @param {module:engine/model/position~Position|null} graveyardPosition Position in the graveyard root before the element which
  28. * 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.
  29. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  30. * can be applied or `null` if the operation operates on detached (non-document) tree.
  31. */
  32. constructor( splitPosition, howMany, graveyardPosition, baseVersion ) {
  33. super( baseVersion );
  34. /**
  35. * Position at which an element should be split.
  36. *
  37. * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#splitPosition
  38. */
  39. this.splitPosition = splitPosition.clone();
  40. // Keep position sticking to the next node. This way any new content added at the place where the element is split
  41. // will be left in the original element.
  42. this.splitPosition.stickiness = 'toNext';
  43. /**
  44. * Total offset size of elements that are in the split element after `position`.
  45. *
  46. * @member {Number} module:engine/model/operation/splitoperation~SplitOperation#howMany
  47. */
  48. this.howMany = howMany;
  49. /**
  50. * Position at which the clone of split element (or element from graveyard) will be inserted.
  51. *
  52. * @member {module:engine/model/position~Position} module:engine/model/operation/splitoperation~SplitOperation#insertionPosition
  53. */
  54. this.insertionPosition = SplitOperation.getInsertionPosition( splitPosition );
  55. this.insertionPosition.stickiness = 'toNone';
  56. /**
  57. * Position in the graveyard root before the element which should be used as a parent of the nodes after `position`.
  58. * If it is not set, a copy of the the `position` parent will be used.
  59. *
  60. * The default behavior is to clone the split element. Element from graveyard is used during undo.
  61. *
  62. * @member {module:engine/model/position~Position|null} #graveyardPosition
  63. */
  64. this.graveyardPosition = graveyardPosition ? graveyardPosition.clone() : null;
  65. if ( this.graveyardPosition ) {
  66. this.graveyardPosition.stickiness = 'toNext';
  67. }
  68. }
  69. /**
  70. * @inheritDoc
  71. */
  72. get type() {
  73. return 'split';
  74. }
  75. /**
  76. * Position inside the new clone of a split element.
  77. *
  78. * This is a position where nodes that are after the split position will be moved to.
  79. *
  80. * @readonly
  81. * @type {module:engine/model/position~Position}
  82. */
  83. get moveTargetPosition() {
  84. const path = this.insertionPosition.path.slice();
  85. path.push( 0 );
  86. return new Position( this.insertionPosition.root, path );
  87. }
  88. /**
  89. * Artificial range that contains all the nodes from the split element that will be moved to the new element.
  90. * The range starts at {@link ~#splitPosition} and ends in the same parent, at `POSITIVE_INFINITY` offset.
  91. *
  92. * @readonly
  93. * @type {module:engine/model/range~Range}
  94. */
  95. get movedRange() {
  96. const end = this.splitPosition.getShiftedBy( Number.POSITIVE_INFINITY );
  97. return new Range( this.splitPosition, end );
  98. }
  99. /**
  100. * Creates and returns an operation that has the same parameters as this operation.
  101. *
  102. * @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
  103. */
  104. clone() {
  105. const split = new this.constructor( this.splitPosition, this.howMany, this.graveyardPosition, this.baseVersion );
  106. split.insertionPosition = this.insertionPosition;
  107. return split;
  108. }
  109. /**
  110. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  111. *
  112. * @returns {module:engine/model/operation/mergeoperation~MergeOperation}
  113. */
  114. getReversed() {
  115. const graveyard = this.splitPosition.root.document.graveyard;
  116. const graveyardPosition = new Position( graveyard, [ 0 ] );
  117. return new MergeOperation( this.moveTargetPosition, this.howMany, this.splitPosition, graveyardPosition, this.baseVersion + 1 );
  118. }
  119. /**
  120. * @inheritDoc
  121. */
  122. _validate() {
  123. const element = this.splitPosition.parent;
  124. const offset = this.splitPosition.offset;
  125. // Validate whether split operation has correct parameters.
  126. if ( !element || element.maxOffset < offset ) {
  127. /**
  128. * Split position is invalid.
  129. *
  130. * @error split-operation-position-invalid
  131. */
  132. throw new CKEditorError( 'split-operation-position-invalid: Split position is invalid.' );
  133. } else if ( !element.parent ) {
  134. /**
  135. * Cannot split root element.
  136. *
  137. * @error split-operation-split-in-root
  138. */
  139. throw new CKEditorError( 'split-operation-split-in-root: Cannot split root element.' );
  140. } else if ( this.howMany != element.maxOffset - this.splitPosition.offset ) {
  141. /**
  142. * Split operation specifies wrong number of nodes to move.
  143. *
  144. * @error split-operation-how-many-invalid
  145. */
  146. throw new CKEditorError( 'split-operation-how-many-invalid: Split operation specifies wrong number of nodes to move.' );
  147. } else if ( this.graveyardPosition && !this.graveyardPosition.nodeAfter ) {
  148. /**
  149. * Graveyard position invalid.
  150. *
  151. * @error split-operation-graveyard-position-invalid
  152. */
  153. throw new CKEditorError( 'split-operation-graveyard-position-invalid: Graveyard position invalid.' );
  154. }
  155. }
  156. /**
  157. * @inheritDoc
  158. */
  159. _execute() {
  160. const splitElement = this.splitPosition.parent;
  161. if ( this.graveyardPosition ) {
  162. _move( Range._createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
  163. } else {
  164. const newElement = splitElement._clone();
  165. _insert( this.insertionPosition, newElement );
  166. }
  167. const sourceRange = new Range(
  168. Position._createAt( splitElement, this.splitPosition.offset ),
  169. Position._createAt( splitElement, splitElement.maxOffset )
  170. );
  171. _move( sourceRange, this.moveTargetPosition );
  172. }
  173. /**
  174. * @inheritDoc
  175. */
  176. toJSON() {
  177. const json = super.toJSON();
  178. json.splitPosition = this.splitPosition.toJSON();
  179. json.insertionPosition = this.insertionPosition.toJSON();
  180. if ( this.graveyardPosition ) {
  181. json.graveyardPosition = this.graveyardPosition.toJSON();
  182. }
  183. return json;
  184. }
  185. /**
  186. * @inheritDoc
  187. */
  188. static get className() {
  189. return 'SplitOperation';
  190. }
  191. /**
  192. * Helper function that returns a default insertion position basing on given `splitPosition`. The default insertion
  193. * position is after the split element.
  194. *
  195. * @param {module:engine/model/position~Position} splitPosition
  196. * @returns {module:engine/model/position~Position}
  197. */
  198. static getInsertionPosition( splitPosition ) {
  199. const path = splitPosition.path.slice( 0, -1 );
  200. path[ path.length - 1 ]++;
  201. return new Position( splitPosition.root, path );
  202. }
  203. /**
  204. * Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
  205. *
  206. * @param {Object} json Deserialized JSON object.
  207. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  208. * @returns {module:engine/model/operation/splitoperation~SplitOperation}
  209. */
  210. static fromJSON( json, document ) {
  211. const splitPosition = Position.fromJSON( json.splitPosition, document );
  212. const insertionPosition = Position.fromJSON( json.insertionPosition, document );
  213. const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
  214. const split = new this( splitPosition, json.howMany, graveyardPosition, json.baseVersion );
  215. split.insertionPosition = insertionPosition;
  216. return split;
  217. }
  218. }