insertdelta.js 2.2 KB

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