insertoperation.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Operation from './operation.js';
  7. import Position from '../position.js';
  8. import NodeList from '../nodelist.js';
  9. import RemoveOperation from './removeoperation.js';
  10. import writer from './../writer.js';
  11. import { normalizeNodes } from './../writer.js';
  12. import Text from '../text.js';
  13. import Element from '../element.js';
  14. /**
  15. * Operation to insert one or more nodes at given position in the model.
  16. *
  17. * @memberOf engine.model.operation
  18. * @extends engine.model.operation.Operation
  19. */
  20. export default class InsertOperation extends Operation {
  21. /**
  22. * Creates an insert operation.
  23. *
  24. * @param {engine.model.Position} position Position of insertion.
  25. * @param {engine.model.NodeSet} nodes The list of nodes to be inserted.
  26. * @param {Number} baseVersion {@link engine.model.Document#version} on which operation can be applied.
  27. */
  28. constructor( position, nodes, baseVersion ) {
  29. super( baseVersion );
  30. /**
  31. * Position of insertion.
  32. *
  33. * @readonly
  34. * @member {engine.model.Position} engine.model.operation.InsertOperation#position
  35. */
  36. this.position = Position.createFromPosition( position );
  37. /**
  38. * List of nodes to insert.
  39. *
  40. * @readonly
  41. * @member {engine.model.NodeList} engine.model.operation.InsertOperation#nodeList
  42. */
  43. this.nodes = new NodeList( normalizeNodes( nodes ) );
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. get type() {
  49. return 'insert';
  50. }
  51. /**
  52. * @inheritDoc
  53. * @returns {engine.model.operation.InsertOperation}
  54. */
  55. clone() {
  56. return new InsertOperation( this.position, this.nodes, this.baseVersion );
  57. }
  58. /**
  59. * @inheritDoc
  60. * @returns {engine.model.operation.RemoveOperation}
  61. */
  62. getReversed() {
  63. return new RemoveOperation( this.position, this.nodes.totalOffset, this.baseVersion + 1 );
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. _execute() {
  69. // What happens here is that we want original nodes be passed to writer because we want original nodes
  70. // to be inserted to the model. But in InsertOperation, we want to keep those nodes as they were added
  71. // to the operation, not modified. For example, text nodes can get merged or cropped while Elements can
  72. // get children. It is important that InsertOperation has the copy of original nodes in intact state.
  73. const originalNodes = this.nodes;
  74. this.nodes = new NodeList( [ ...originalNodes ].map( ( node ) => node.clone( true ) ) );
  75. const range = writer.insert( this.position, originalNodes );
  76. return { range };
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. static get className() {
  82. return 'engine.model.operation.InsertOperation';
  83. }
  84. /**
  85. * Creates `InsertOperation` object from deserilized object, i.e. from parsed JSON string.
  86. *
  87. * @param {Object} json Deserialized JSON object.
  88. * @param {engine.model.Document} document Document on which this operation will be applied.
  89. * @returns {engine.model.operation.InsertOperation}
  90. */
  91. static fromJSON( json, document ) {
  92. let children = [];
  93. for ( let child of json.nodes ) {
  94. if ( child.name ) {
  95. // If child has name property, it is an Element.
  96. children.push( Element.fromJSON( child ) );
  97. } else {
  98. // Otherwise, it is a Text node.
  99. children.push( Text.fromJSON( child ) );
  100. }
  101. }
  102. return new InsertOperation( Position.fromJSON( json.position, document ), children, json.baseVersion );
  103. }
  104. }