detachoperation.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. */
  28. constructor( sourcePosition, howMany ) {
  29. super( null );
  30. /**
  31. * Position before the first {@link module:engine/model/item~Item model item} to detach.
  32. *
  33. * @member {module:engine/model/position~Position} #sourcePosition
  34. */
  35. this.sourcePosition = Position.createFromPosition( sourcePosition );
  36. /**
  37. * Offset size of moved range.
  38. *
  39. * @member {Number} #howMany
  40. */
  41. this.howMany = howMany;
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. get type() {
  47. return 'detach';
  48. }
  49. /**
  50. * @inheritDoc
  51. */
  52. _validate() {
  53. if ( this.sourcePosition.root.document ) {
  54. /**
  55. * Cannot detach document node.
  56. *
  57. * @error detach-operation-on-document-node
  58. */
  59. throw new CKEditorError( 'detach-operation-on-document-node: Cannot detach document node.' );
  60. }
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. _execute() {
  66. _remove( Range.createFromPositionAndShift( this.sourcePosition, this.howMany ) );
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. static get className() {
  72. return 'engine.model.operation.DetachOperation';
  73. }
  74. }