insertoperation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 operation insertion position points to inside a
  49. * range which 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. get howMany() {
  62. return this.nodes.maxOffset;
  63. }
  64. /**
  65. * Creates and returns an operation that has the same parameters as this operation.
  66. *
  67. * @returns {module:engine/model/operation/insertoperation~InsertOperation} Clone of this operation.
  68. */
  69. clone() {
  70. const nodes = new NodeList( [ ...this.nodes ].map( node => node._clone( true ) ) );
  71. const insert = new InsertOperation( this.position, nodes, this.baseVersion );
  72. insert.shouldReceiveAttributes = this.shouldReceiveAttributes;
  73. return insert;
  74. }
  75. /**
  76. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  77. *
  78. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  79. */
  80. getReversed() {
  81. const graveyard = this.position.root.document.graveyard;
  82. const gyPosition = new Position( graveyard, [ 0 ] );
  83. return new MoveOperation( this.position, this.nodes.maxOffset, gyPosition, this.baseVersion + 1 );
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. _validate() {
  89. const targetElement = this.position.parent;
  90. if ( !targetElement || targetElement.maxOffset < this.position.offset ) {
  91. /**
  92. * Insertion position is invalid.
  93. *
  94. * @error insert-operation-position-invalid
  95. */
  96. throw new CKEditorError(
  97. 'insert-operation-position-invalid: Insertion position is invalid.'
  98. );
  99. }
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. _execute() {
  105. // What happens here is that we want original nodes be passed to writer because we want original nodes
  106. // to be inserted to the model. But in InsertOperation, we want to keep those nodes as they were added
  107. // to the operation, not modified. For example, text nodes can get merged or cropped while Elements can
  108. // get children. It is important that InsertOperation has the copy of original nodes in intact state.
  109. const originalNodes = this.nodes;
  110. this.nodes = new NodeList( [ ...originalNodes ].map( node => node._clone( true ) ) );
  111. _insert( this.position, originalNodes );
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. toJSON() {
  117. const json = super.toJSON();
  118. json.position = this.position.toJSON();
  119. json.nodes = this.nodes.toJSON();
  120. return json;
  121. }
  122. /**
  123. * @inheritDoc
  124. */
  125. static get className() {
  126. return 'InsertOperation';
  127. }
  128. /**
  129. * Creates `InsertOperation` object from deserilized object, i.e. from parsed JSON string.
  130. *
  131. * @param {Object} json Deserialized JSON object.
  132. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  133. * @returns {module:engine/model/operation/insertoperation~InsertOperation}
  134. */
  135. static fromJSON( json, document ) {
  136. const children = [];
  137. for ( const child of json.nodes ) {
  138. if ( child.name ) {
  139. // If child has name property, it is an Element.
  140. children.push( Element.fromJSON( child ) );
  141. } else {
  142. // Otherwise, it is a Text node.
  143. children.push( Text.fromJSON( child ) );
  144. }
  145. }
  146. const insert = new InsertOperation( Position.fromJSON( json.position, document ), children, json.baseVersion );
  147. insert.shouldReceiveAttributes = json.shouldReceiveAttributes;
  148. return insert;
  149. }
  150. }