8
0

moveoperation.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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',
  8. 'document/nodelist',
  9. 'ckeditorerror',
  10. 'utils'
  11. ], function( Operation, NodeList, CKEditorError, utils ) {
  12. /**
  13. *
  14. *
  15. * @class document.Operation
  16. */
  17. class MoveOperation extends Operation {
  18. /**
  19. *
  20. */
  21. constructor( sourcePosition, targetPosition, nodeList, baseVersion ) {
  22. super( baseVersion );
  23. this.sourcePosition = sourcePosition;
  24. this.targetPosition = targetPosition;
  25. this.nodeList = new NodeList( nodeList );
  26. }
  27. _execute() {
  28. var sourceElement = this.sourcePosition.parent;
  29. var targetElement = this.targetPosition.parent;
  30. var sourceOffset = this.sourcePosition.offset;
  31. var targetOffset = this.targetPosition.offset;
  32. var nodeList = this.nodeList;
  33. if ( CKEDITOR.isDebug ) {
  34. var i = 0;
  35. for ( var node of this.nodeList ) {
  36. if ( !utils.isEqual( sourceElement.children.get( sourceOffset + i ), node ) ) {
  37. /**
  38. * The node which should be removed does not exists.
  39. *
  40. * @error operation-move-node-does-not-exists:
  41. * @param {document.MoveOperation} moveOperation
  42. * @param {document.Node} node
  43. */
  44. throw new CKEditorError(
  45. 'operation-move-node-does-not-exists: The node which should be moved does not exists.',
  46. { moveOperation: this, node: this.node } );
  47. }
  48. i++;
  49. }
  50. }
  51. sourceElement.removeChildren( sourceOffset, nodeList.length );
  52. // If we move children in the same element and we remove elements on the position before the target we
  53. // need to update a target offset.
  54. if ( sourceElement === targetElement && sourceOffset < targetOffset ) {
  55. targetOffset -= nodeList.length;
  56. }
  57. targetElement.insertChildren( targetOffset, this.nodeList );
  58. }
  59. reverseOperation() {
  60. return new MoveOperation( this.targetPosition, this.sourcePosition, this.nodeList, this.baseVersion + 1 );
  61. }
  62. }
  63. return MoveOperation;
  64. } );