8
0

insertoperation.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 'treemodel/operation/operation',
  8. 'treemodel/nodelist',
  9. 'treemodel/range',
  10. 'treemodel/operation/removeoperation'
  11. ], ( Operation, NodeList, Range ) => {
  12. /**
  13. * Operation to insert list of nodes on the given position in the tree data model.
  14. *
  15. * @class treeModel.operation.InsertOperation
  16. */
  17. class InsertOperation extends Operation {
  18. /**
  19. * Creates an insert operation.
  20. *
  21. * @param {treeModel.Position} position Position of insertion.
  22. * @param {treeModel.Node|treeModel.Text|treeModel.NodeList|String|Iterable} nodes The list of nodes to be inserted.
  23. * List of nodes can be any type accepted by the {@link treeModel.NodeList} constructor.
  24. * @param {Number} baseVersion {@link treeModel.Document#version} on which operation can be applied.
  25. * @constructor
  26. */
  27. constructor( position, nodes, baseVersion ) {
  28. super( baseVersion );
  29. /**
  30. * Position of insertion.
  31. *
  32. * @readonly
  33. * @type {treeModel.Position}
  34. */
  35. this.position = position;
  36. /**
  37. * List of nodes to insert.
  38. *
  39. * @readonly
  40. * @type {treeModel.NodeList}
  41. */
  42. this.nodeList = new NodeList( nodes );
  43. }
  44. get type() {
  45. return 'insert';
  46. }
  47. clone() {
  48. return new InsertOperation( this.position, this.nodeList, this.baseVersion );
  49. }
  50. getReversed() {
  51. // Because of circular dependencies we need to re-require remove operation here.
  52. const RemoveOperation = CKEDITOR.require( 'treemodel/operation/removeoperation' );
  53. return new RemoveOperation( this.position, this.nodeList.length, this.baseVersion + 1 );
  54. }
  55. _execute() {
  56. this.position.parent.insertChildren( this.position.offset, this.nodeList );
  57. return {
  58. range: Range.createFromPositionAndShift( this.position, this.nodeList.length )
  59. };
  60. }
  61. }
  62. return InsertOperation;
  63. } );