insertoperation.js 5.3 KB

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