8
0

insertoperation.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [
  7. 'document/operation/operation',
  8. 'document/nodelist',
  9. 'document/operation/removeoperation'
  10. ], ( Operation, NodeList ) => {
  11. /**
  12. * Operation to insert list of nodes on the given position in the tree data model.
  13. *
  14. * @class document.operation.InsertOperation
  15. */
  16. class InsertOperation extends Operation {
  17. /**
  18. * Creates an insert operation.
  19. *
  20. * @param {document.Position} position Position of insertion.
  21. * @param {document.Node|document.Text|document.NodeList|String|Iterable} nodes The list of nodes to be inserted.
  22. * List of nodes can be any type accepted by the {@link document.NodeList} constructor.
  23. * @param {Number} baseVersion {@link document.Document#version} on which operation can be applied.
  24. * @constructor
  25. */
  26. constructor( position, nodes, baseVersion ) {
  27. super( baseVersion );
  28. /**
  29. * Position of insertion.
  30. *
  31. * @readonly
  32. * @type {document.Position}
  33. */
  34. this.position = position;
  35. /**
  36. * List of nodes to insert.
  37. *
  38. * @readonly
  39. * @type {document.NodeList}
  40. */
  41. this.nodeList = new NodeList( nodes );
  42. }
  43. _execute() {
  44. this.position.parent.insertChildren( this.position.offset, this.nodeList );
  45. }
  46. getReversed() {
  47. // Because of circular dependencies we need to re-require remove operation here.
  48. const RemoveOperation = CKEDITOR.require( 'document/operation/removeoperation' );
  49. return new RemoveOperation( this.position, this.nodeList.length, this.baseVersion + 1 );
  50. }
  51. clone() {
  52. return new InsertOperation( this.position, this.nodeList, this.baseVersion );
  53. }
  54. }
  55. return InsertOperation;
  56. } );