insertdelta.js 2.4 KB

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