insertoperation.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/insertoperation
  7. */
  8. import Operation from './operation';
  9. import Position from '../position';
  10. import NodeList from '../nodelist';
  11. import MoveOperation from './moveoperation';
  12. import { _insert, _normalizeNodes } from './utils';
  13. import Text from '../text';
  14. import Element from '../element';
  15. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  16. /**
  17. * Operation to insert one or more nodes at given position in the model.
  18. *
  19. * @extends module:engine/model/operation/operation~Operation
  20. */
  21. export default class InsertOperation extends Operation {
  22. /**
  23. * Creates an insert operation.
  24. *
  25. * @param {module:engine/model/position~Position} position Position of insertion.
  26. * @param {module:engine/model/node~NodeSet} nodes The list of nodes to be inserted.
  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, nodes, baseVersion ) {
  31. super( baseVersion );
  32. /**
  33. * Position of insertion.
  34. *
  35. * @readonly
  36. * @member {module:engine/model/position~Position} module:engine/model/operation/insertoperation~InsertOperation#position
  37. */
  38. this.position = Position.createFromPosition( position );
  39. /**
  40. * List of nodes to insert.
  41. *
  42. * @readonly
  43. * @member {module:engine/model/nodelist~NodeList} module:engine/model/operation/insertoperation~InsertOperation#nodeList
  44. */
  45. this.nodes = new NodeList( _normalizeNodes( nodes ) );
  46. /**
  47. * Flag deciding how the operation should be transformed. If set to `true`, nodes might get additional attributes
  48. * during operational transformation. This happens when the operation insertion position is inside of a range
  49. * where attributes have changed.
  50. *
  51. * @member {Boolean} module:engine/model/operation/insertoperation~InsertOperation#shouldReceiveAttributes
  52. */
  53. this.shouldReceiveAttributes = false;
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. get type() {
  59. return 'insert';
  60. }
  61. /**
  62. * Total offset size of inserted nodes.
  63. *
  64. * @returns {Number}
  65. */
  66. get howMany() {
  67. return this.nodes.maxOffset;
  68. }
  69. /**
  70. * Creates and returns an operation that has the same parameters as this operation.
  71. *
  72. * @returns {module:engine/model/operation/insertoperation~InsertOperation} Clone of this operation.
  73. */
  74. clone() {
  75. const nodes = new NodeList( [ ...this.nodes ].map( node => node._clone( true ) ) );
  76. const insert = new InsertOperation( this.position, nodes, this.baseVersion );
  77. insert.shouldReceiveAttributes = this.shouldReceiveAttributes;
  78. return insert;
  79. }
  80. /**
  81. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  82. *
  83. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  84. */
  85. getReversed() {
  86. const graveyard = this.position.root.document.graveyard;
  87. const gyPosition = new Position( graveyard, [ 0 ] );
  88. return new MoveOperation( this.position, this.nodes.maxOffset, gyPosition, this.baseVersion + 1 );
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. _validate() {
  94. const targetElement = this.position.parent;
  95. if ( !targetElement || targetElement.maxOffset < this.position.offset ) {
  96. /**
  97. * Insertion position is invalid.
  98. *
  99. * @error insert-operation-position-invalid
  100. */
  101. throw new CKEditorError(
  102. 'insert-operation-position-invalid: Insertion position is invalid.'
  103. );
  104. }
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. _execute() {
  110. // What happens here is that we want original nodes be passed to writer because we want original nodes
  111. // to be inserted to the model. But in InsertOperation, we want to keep those nodes as they were added
  112. // to the operation, not modified. For example, text nodes can get merged or cropped while Elements can
  113. // get children. It is important that InsertOperation has the copy of original nodes in intact state.
  114. const originalNodes = this.nodes;
  115. this.nodes = new NodeList( [ ...originalNodes ].map( node => node._clone( true ) ) );
  116. _insert( this.position, originalNodes );
  117. }
  118. /**
  119. * @inheritDoc
  120. */
  121. toJSON() {
  122. const json = super.toJSON();
  123. json.position = this.position.toJSON();
  124. json.nodes = this.nodes.toJSON();
  125. return json;
  126. }
  127. /**
  128. * @inheritDoc
  129. */
  130. static get className() {
  131. return 'engine.model.operation.InsertOperation';
  132. }
  133. /**
  134. * Creates `InsertOperation` object from deserilized object, i.e. from parsed JSON string.
  135. *
  136. * @param {Object} json Deserialized JSON object.
  137. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  138. * @returns {module:engine/model/operation/insertoperation~InsertOperation}
  139. */
  140. static fromJSON( json, document ) {
  141. const children = [];
  142. for ( const child of json.nodes ) {
  143. if ( child.name ) {
  144. // If child has name property, it is an Element.
  145. children.push( Element.fromJSON( child ) );
  146. } else {
  147. // Otherwise, it is a Text node.
  148. children.push( Text.fromJSON( child ) );
  149. }
  150. }
  151. const insert = new InsertOperation( Position.fromJSON( json.position, document ), children, json.baseVersion );
  152. insert.shouldReceiveAttributes = json.shouldReceiveAttributes;
  153. return insert;
  154. }
  155. }