8
0

detachoperation.js 2.1 KB

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