reinsertoperation.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/reinsertoperation
  7. */
  8. import MoveOperation from './moveoperation';
  9. import RemoveOperation from './removeoperation';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. /**
  12. * Operation to reinsert previously removed nodes back to the non-graveyard root. This operation acts like
  13. * {@link module:engine/model/operation/moveoperation~MoveOperation} but it returns
  14. * {@link module:engine/model/operation/removeoperation~RemoveOperation} when reversed
  15. * and fires different change event.
  16. */
  17. export default class ReinsertOperation extends MoveOperation {
  18. /**
  19. * Position where nodes will be re-inserted.
  20. *
  21. * @type {module:engine/model/position~Position}
  22. */
  23. get position() {
  24. return this.targetPosition;
  25. }
  26. /**
  27. * @param {module:engine/model/position~Position} pos
  28. */
  29. set position( pos ) {
  30. this.targetPosition = pos;
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. get type() {
  36. return 'reinsert';
  37. }
  38. /**
  39. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  40. *
  41. * @returns {module:engine/model/operation/removeoperation~RemoveOperation}
  42. */
  43. getReversed() {
  44. const newTargetPosition = this.sourcePosition._getTransformedByInsertion( this.targetPosition, this.howMany );
  45. return new RemoveOperation( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. _validate() {
  51. super._validate();
  52. if ( !this.sourcePosition.root.document ) {
  53. throw new CKEditorError( 'reinsert-operation-on-detached-item: Cannot reinsert detached item.' );
  54. }
  55. if ( !this.targetPosition.root.document ) {
  56. throw new CKEditorError( 'reinsert-operation-to-detached-parent: Cannot reinsert item to detached parent.' );
  57. }
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. static get className() {
  63. return 'engine.model.operation.ReinsertOperation';
  64. }
  65. }