insertdelta.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Delta from './delta.js';
  7. import DeltaFactory from './deltafactory.js';
  8. import RemoveDelta from './removedelta.js';
  9. import { register } from '../batch.js';
  10. import InsertOperation from '../operation/insertoperation.js';
  11. /**
  12. * @classdesc
  13. * To provide specific OT behavior and better collisions solving, the {@link engine.treeModel.Batch#insert Batch#insert} method
  14. * uses the `InsertDelta` class which inherits from the `Delta` class and may overwrite some methods.
  15. *
  16. * @memberOf engine.treeModel.delta
  17. */
  18. export default class InsertDelta extends Delta {
  19. /**
  20. * Position where the delta inserts nodes or `null` if there are no operations in the delta.
  21. *
  22. * @type {engine.treeModel.Position|null}
  23. */
  24. get position() {
  25. return this._insertOperation ? this._insertOperation.position : null;
  26. }
  27. /**
  28. * Node list containing all the nodes inserted by the delta or `null` if there are no operations in the delta.
  29. *
  30. * @type {engine.treeModel.NodeList|null}
  31. */
  32. get nodeList() {
  33. return this._insertOperation ? this._insertOperation.nodeList : null;
  34. }
  35. /**
  36. * Insert operation that is saved in this delta or `null` if there are no operations in the delta.
  37. *
  38. * @protected
  39. * @type {engine.treeModel.operation.InsertOperation|null}
  40. */
  41. get _insertOperation() {
  42. return this.operations[ 0 ] || null;
  43. }
  44. get _reverseDeltaClass() {
  45. return RemoveDelta;
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. static get className() {
  51. return 'engine.treeModel.delta.InsertDelta';
  52. }
  53. static get _priority() {
  54. return 20;
  55. }
  56. }
  57. /**
  58. * Inserts a node or nodes at the given position.
  59. *
  60. * @chainable
  61. * @method engine.treeModel.Batch#insert
  62. * @param {engine.treeModel.Position} position Position of insertion.
  63. * @param {engine.treeModel.NodeSet} nodes The list of nodes to be inserted.
  64. */
  65. register( 'insert', function( position, nodes ) {
  66. const delta = new InsertDelta();
  67. const insert = new InsertOperation( position, nodes, this.doc.version );
  68. this.addDelta( delta );
  69. delta.addOperation( insert );
  70. this.doc.applyOperation( insert );
  71. return this;
  72. } );
  73. DeltaFactory.register( InsertDelta );