8
0

splitoperation.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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#position 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} position 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( position, 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#position
  38. */
  39. this.position = Position.createFromPosition( position );
  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.position.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 in the graveyard root before the element which should be used as a parent of the nodes after `position`.
  51. * If it is not set, a copy of the the `position` parent will be used.
  52. *
  53. * The default behavior is to clone the split element. Element from graveyard is used during undo.
  54. *
  55. * @member {module:engine/model/position~Position|null} #graveyardPosition
  56. */
  57. this.graveyardPosition = graveyardPosition ? Position.createFromPosition( graveyardPosition ) : null;
  58. if ( this.graveyardPosition ) {
  59. this.graveyardPosition.stickiness = 'toNext';
  60. }
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. get type() {
  66. return 'split';
  67. }
  68. /**
  69. * Position after the split element.
  70. *
  71. * This is a position at which the clone of split element (or element from graveyard) will be inserted.
  72. *
  73. * @readonly
  74. * @type {module:engine/model/position~Position}
  75. */
  76. get insertionPosition() {
  77. const path = this.position.path.slice( 0, -1 );
  78. path[ path.length - 1 ]++;
  79. return new Position( this.position.root, path );
  80. }
  81. /**
  82. * Position inside the new clone of a split element.
  83. *
  84. * This is a position where nodes that are after the split position will be moved to.
  85. *
  86. * @readonly
  87. * @type {module:engine/model/position~Position}
  88. */
  89. get moveTargetPosition() {
  90. const path = this.position.path.slice( 0, -1 );
  91. path[ path.length - 1 ]++;
  92. path.push( 0 );
  93. return new Position( this.position.root, path );
  94. }
  95. /**
  96. * Artificial range that contains all the nodes from the split element that will be moved to the new element.
  97. * The range starts at {@link ~#position} and ends in the same parent, at `POSITIVE_INFINITY` offset.
  98. *
  99. * @readonly
  100. * @type {module:engine/model/range~Range}
  101. */
  102. get movedRange() {
  103. const end = this.position.getShiftedBy( Number.POSITIVE_INFINITY );
  104. return new Range( this.position, end );
  105. }
  106. /**
  107. * Creates and returns an operation that has the same parameters as this operation.
  108. *
  109. * @returns {module:engine/model/operation/splitoperation~SplitOperation} Clone of this operation.
  110. */
  111. clone() {
  112. return new this.constructor( this.position, this.howMany, this.graveyardPosition, this.baseVersion );
  113. }
  114. /**
  115. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  116. *
  117. * @returns {module:engine/model/operation/mergeoperation~MergeOperation}
  118. */
  119. getReversed() {
  120. const graveyard = this.position.root.document.graveyard;
  121. const graveyardPosition = new Position( graveyard, [ 0 ] );
  122. return new MergeOperation( this.moveTargetPosition, this.howMany, this.position, graveyardPosition, this.baseVersion + 1 );
  123. }
  124. /**
  125. * @inheritDoc
  126. */
  127. _validate() {
  128. const element = this.position.parent;
  129. const offset = this.position.offset;
  130. // Validate whether split operation has correct parameters.
  131. if ( !element || element.maxOffset < offset ) {
  132. /**
  133. * Split position is invalid.
  134. *
  135. * @error split-operation-position-invalid
  136. */
  137. throw new CKEditorError( 'split-operation-position-invalid: Split position is invalid.' );
  138. } else if ( !element.parent ) {
  139. /**
  140. * Cannot split root element.
  141. *
  142. * @error split-operation-split-in-root
  143. */
  144. throw new CKEditorError( 'split-operation-split-in-root: Cannot split root element.' );
  145. } else if ( this.howMany != element.maxOffset - this.position.offset ) {
  146. /**
  147. * Split operation specifies wrong number of nodes to move.
  148. *
  149. * @error split-operation-how-many-invalid
  150. */
  151. throw new CKEditorError( 'split-operation-how-many-invalid: Split operation specifies wrong number of nodes to move.' );
  152. } else if ( this.graveyardPosition && !this.graveyardPosition.nodeAfter ) {
  153. /**
  154. * Graveyard position invalid.
  155. *
  156. * @error split-operation-graveyard-position-invalid
  157. */
  158. throw new CKEditorError( 'split-operation-graveyard-position-invalid: Graveyard position invalid.' );
  159. }
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. _execute() {
  165. const splitElement = this.position.parent;
  166. if ( this.graveyardPosition ) {
  167. _move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), this.insertionPosition );
  168. } else {
  169. const newElement = splitElement._clone();
  170. _insert( this.insertionPosition, newElement );
  171. }
  172. const sourceRange = Range.createFromParentsAndOffsets( splitElement, this.position.offset, splitElement, splitElement.maxOffset );
  173. _move( sourceRange, this.moveTargetPosition );
  174. }
  175. /**
  176. * @inheritDoc
  177. */
  178. toJSON() {
  179. const json = super.toJSON();
  180. json.position = this.position.toJSON();
  181. if ( this.graveyardPosition ) {
  182. json.graveyardPosition = this.graveyardPosition.toJSON();
  183. }
  184. return json;
  185. }
  186. /**
  187. * @inheritDoc
  188. */
  189. static get className() {
  190. return 'SplitOperation';
  191. }
  192. /**
  193. * Creates `SplitOperation` object from deserilized object, i.e. from parsed JSON string.
  194. *
  195. * @param {Object} json Deserialized JSON object.
  196. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  197. * @returns {module:engine/model/operation/splitoperation~SplitOperation}
  198. */
  199. static fromJSON( json, document ) {
  200. const position = Position.fromJSON( json.position, document );
  201. const graveyardPosition = json.graveyardPosition ? Position.fromJSON( json.graveyardPosition, document ) : null;
  202. return new this( position, json.howMany, graveyardPosition, json.baseVersion );
  203. }
  204. }