8
0

wrapoperation.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/wrapoperation
  7. */
  8. import Operation from './operation';
  9. import UnwrapOperation from './unwrapoperation';
  10. import Position from '../position';
  11. import Range from '../range';
  12. import Element from '../element';
  13. import { _insert, _move } from './utils';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. /**
  16. * Operation to wrap a range of {@link module:engine/model/node~Node nodes} with an {@link module:engine/model/element~Element element}.
  17. *
  18. * @extends module:engine/model/operation/operation~Operation
  19. */
  20. export default class WrapOperation extends Operation {
  21. /**
  22. * Creates a wrap operation.
  23. *
  24. * @param {module:engine/model/position~Position} position Position before
  25. * the first {@link module:engine/model/item~Item model item} to wrap.
  26. * @param {Number} howMany Offset size of wrapped range. Wrapped range will start at `position.offset` and end at
  27. * `position.offset + howMany`.
  28. * @param {module:engine/model/element~Element|module:engine/model/position~Position} elementOrPosition Wrapper
  29. * element or position in graveyard before the element which should be used as a wrapper.
  30. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  31. * can be applied or `null` if the operation operates on detached (non-document) tree.
  32. */
  33. constructor( position, howMany, elementOrPosition, baseVersion ) {
  34. super( baseVersion );
  35. /**
  36. * Position before the first {@link module:engine/model/node~Node node} to wrap.
  37. *
  38. * @member {module:engine/model/position~Position} module:engine/model/operation/wrapoperation~WrapOperation#position
  39. */
  40. this.position = Position.createFromPosition( position );
  41. this.position.stickiness = 'toNext';
  42. /**
  43. * Total offset size of the wrapped range.
  44. *
  45. * Wrapped range will start at `position.offset` and end at `position.offset + howMany`.
  46. *
  47. * @member {Number} module:engine/model/operation/wrapoperation~WrapOperation#howMany
  48. */
  49. this.howMany = howMany;
  50. /**
  51. * Wrapper element that will be used to wrap nodes.
  52. *
  53. * Is `null` if `elementOrPosition` was a {@link module:engine/model/position~Position}.
  54. *
  55. * @member {module:engine/model/element~Element} module:engine/model/operation/wrapoperation~WrapOperation#element
  56. */
  57. this.element = elementOrPosition instanceof Element ? elementOrPosition : null;
  58. /**
  59. * Position in the graveyard root before the element that will be used as a wrapper element.
  60. *
  61. * Is `null` if `elementOrPosition` was a {@link module:engine/model/element~Element}.
  62. *
  63. * @member {module:engine/model/element~Element} module:engine/model/operation/wrapoperation~WrapOperation#graveyardPosition
  64. */
  65. this.graveyardPosition = elementOrPosition instanceof Element ? null : Position.createFromPosition( elementOrPosition );
  66. if ( this.graveyardPosition ) {
  67. this.graveyardPosition.stickiness = 'toNext';
  68. }
  69. }
  70. /**
  71. * @inheritDoc
  72. */
  73. get type() {
  74. return 'wrap';
  75. }
  76. /**
  77. * Position to which the wrapped elements will be moved. This is a position at the beginning of the wrapping element.
  78. *
  79. * @readonly
  80. * @type {module:engine/model/position~Position}
  81. */
  82. get targetPosition() {
  83. const path = this.position.path.slice();
  84. path.push( 0 );
  85. return new Position( this.position.root, path );
  86. }
  87. /**
  88. * A range containing all nodes that will be wrapped.
  89. *
  90. * @readonly
  91. * @type {module:engine/model/range~Range}
  92. */
  93. get wrappedRange() {
  94. return Range.createFromPositionAndShift( this.position, this.howMany );
  95. }
  96. /**
  97. * Creates and returns an operation that has the same parameters as this operation.
  98. *
  99. * @returns {module:engine/model/operation/wrapoperation~WrapOperation} Clone of this operation.
  100. */
  101. clone() {
  102. const elementOrPosition = this.element ? this.element._clone() : this.graveyardPosition;
  103. return new this.constructor( this.position, this.howMany, elementOrPosition, this.baseVersion );
  104. }
  105. /**
  106. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  107. *
  108. * @returns {module:engine/model/operation/unwrapoperation~UnwrapOperation}
  109. */
  110. getReversed() {
  111. const graveyard = this.position.root.document.graveyard;
  112. const graveyardPosition = new Position( graveyard, [ 0 ] );
  113. return new UnwrapOperation( this.targetPosition, this.howMany, graveyardPosition, this.baseVersion + 1 );
  114. }
  115. /**
  116. * @inheritDoc
  117. */
  118. _validate() {
  119. const element = this.position.parent;
  120. const offset = this.position.offset;
  121. // Validate whether wrap operation has correct parameters.
  122. if ( !element || offset + this.howMany > element.maxOffset ) {
  123. /**
  124. * Wrap range is invalid.
  125. *
  126. * @error wrap-operation-range-invalid
  127. */
  128. throw new CKEditorError( 'wrap-operation-range-invalid: Wrap range is invalid.' );
  129. }
  130. }
  131. /**
  132. * @inheritDoc
  133. */
  134. _execute() {
  135. const wrappedRange = this.wrappedRange;
  136. const insertPosition = Position.createFromPosition( wrappedRange.end );
  137. const targetPath = insertPosition.path.slice();
  138. targetPath.push( 0 );
  139. const targetPosition = new Position( this.position.root, targetPath );
  140. if ( this.element ) {
  141. const originalElement = this.element;
  142. this.element = this.element._clone();
  143. _insert( insertPosition, originalElement );
  144. } else {
  145. _move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), insertPosition );
  146. }
  147. _move( wrappedRange, targetPosition );
  148. }
  149. /**
  150. * @inheritDoc
  151. */
  152. toJSON() {
  153. const json = super.toJSON();
  154. json.position = this.position.toJSON();
  155. if ( this.element ) {
  156. json.element = this.element.toJSON();
  157. delete json.graveyardPosition;
  158. } else {
  159. json.graveyardPosition = this.graveyardPosition.toJSON();
  160. delete json.element;
  161. }
  162. return json;
  163. }
  164. /**
  165. * @inheritDoc
  166. */
  167. static get className() {
  168. return 'engine.model.operation.WrapOperation';
  169. }
  170. /**
  171. * Creates `WrapOperation` object from deserilized object, i.e. from parsed JSON string.
  172. *
  173. * @param {Object} json Deserialized JSON object.
  174. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  175. * @returns {module:engine/model/operation/wrapoperation~WrapOperation}
  176. */
  177. static fromJSON( json, document ) {
  178. const position = Position.fromJSON( json.position, document );
  179. const elementOrPosition = json.element ? Element.fromJSON( json.element ) : Position.fromJSON( json.graveyardPosition, document );
  180. return new this( position, json.howMany, elementOrPosition, json.baseVersion );
  181. }
  182. }