detachoperation.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/detachoperation
  7. */
  8. import Operation from './operation';
  9. import Position from '../position';
  10. import Range from '../range';
  11. import { _remove } from './utils';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. /**
  14. * Operation to permanently remove node from detached root.
  15. * Note this operation is only a local operation and won't be send to the other clients.
  16. *
  17. * @extends module:engine/model/operation/operation~Operation
  18. */
  19. export default class DetachOperation extends Operation {
  20. /**
  21. * Creates an insert operation.
  22. *
  23. * @param {module:engine/model/position~Position} sourcePosition
  24. * Position before the first {@link module:engine/model/item~Item model item} to move.
  25. * @param {Number} howMany Offset size of moved range. Moved range will start from `sourcePosition` and end at
  26. * `sourcePosition` with offset shifted by `howMany`.
  27. * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which operation can be applied.
  28. */
  29. constructor( sourcePosition, howMany, baseVersion ) {
  30. super( baseVersion );
  31. /**
  32. * Position before the first {@link module:engine/model/item~Item model item} to detach.
  33. *
  34. * @member {module:engine/model/position~Position} #sourcePosition
  35. */
  36. this.sourcePosition = Position.createFromPosition( sourcePosition );
  37. /**
  38. * Offset size of moved range.
  39. *
  40. * @member {Number} #howMany
  41. */
  42. this.howMany = howMany;
  43. /**
  44. * @inheritDoc
  45. */
  46. this.isDocumentOperation = false;
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. get type() {
  52. return 'detach';
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. _execute() {
  58. if ( this.sourcePosition.root.document ) {
  59. /**
  60. * Cannot detach document node.
  61. * Use {@link module:engine/model/operation/removeoperation~RemoveOperation remove operation} instead.
  62. *
  63. * @error detach-operation-on-document-node
  64. */
  65. throw new CKEditorError( 'detach-operation-on-document-node: Cannot detach document node.' );
  66. }
  67. const nodes = _remove( Range.createFromPositionAndShift( this.sourcePosition, this.howMany ) );
  68. return { nodes };
  69. }
  70. /**
  71. * @inheritDoc
  72. */
  73. static get className() {
  74. return 'engine.model.operation.DetachOperation';
  75. }
  76. }