8
0

splitdelta.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/delta/delta',
  8. 'document/delta/register',
  9. 'document/position',
  10. 'document/element',
  11. 'document/operation/insertoperation',
  12. 'document/operation/moveoperation',
  13. 'ckeditorerror'
  14. ], ( Delta, register, Position, Element, InsertOperation, MoveOperation, CKEditorError ) => {
  15. /**
  16. * To provide specific OT behavior and better collisions solving, the {@link document.Transaction#split} method
  17. * uses `SplitDelta` class which inherits from the `Delta` class and may overwrite some methods.
  18. *
  19. * @class document.delta.SplitDelta
  20. */
  21. class SplitDelta extends Delta {}
  22. /**
  23. * Splits a node at the given position.
  24. *
  25. * This cannot be a position inside the root element. The `transaction-split-root` error will be thrown if
  26. * you try to split the root element.
  27. *
  28. * @chainable
  29. * @method split
  30. * @memberOf document.Transaction
  31. * @param {document.Position} position Position of split.
  32. */
  33. register( 'split', function( position ) {
  34. const delta = new SplitDelta();
  35. const splitElement = position.parent;
  36. if ( !splitElement.parent ) {
  37. /**
  38. * Root element can not be split.
  39. *
  40. * @error transaction-split-root
  41. */
  42. throw new CKEditorError( 'transaction-split-root: Root element can not be split.' );
  43. }
  44. const copy = new Element( splitElement.name, splitElement.getAttrs() );
  45. const insert = new InsertOperation( Position.createAfter( splitElement ), copy, this.doc.version );
  46. this.doc.applyOperation( insert );
  47. delta.addOperation( insert );
  48. const move = new MoveOperation(
  49. position,
  50. Position.createFromParentAndOffset( copy, 0 ),
  51. splitElement.getChildCount() - position.offset,
  52. this.doc.version
  53. );
  54. this.doc.applyOperation( move );
  55. delta.addOperation( move );
  56. this.addDelta( delta );
  57. return this;
  58. } );
  59. return SplitDelta;
  60. } );