changebuffer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module typing/changebuffer
  7. */
  8. import count from '@ckeditor/ckeditor5-utils/src/count';
  9. /**
  10. * Change buffer allows to group atomic changes (like characters that have been typed) into
  11. * {@link module:engine/model/batch~Batch batches}.
  12. *
  13. * Batches represent single undo steps, hence changes added to one single batch are undone together.
  14. *
  15. * The buffer has a configurable limit of atomic changes that it can accommodate. After the limit was
  16. * exceeded (see {@link ~ChangeBuffer#input}), a new batch is created in {@link ~ChangeBuffer#batch}.
  17. *
  18. * To use the change buffer you need to let it know about the number of changes that were added to the batch:
  19. *
  20. * const buffer = new ChangeBuffer( document, LIMIT );
  21. *
  22. * // Later on in your feature:
  23. * buffer.batch.insert( pos, insertedCharacters );
  24. * buffer.input( insertedCharacters.length );
  25. *
  26. */
  27. export default class ChangeBuffer {
  28. /**
  29. * Creates a new instance of the change buffer.
  30. *
  31. * @param {module:engine/model/document~Document} document
  32. * @param {Number} [limit=20] The maximum number of atomic changes which can be contained in one batch.
  33. */
  34. constructor( doc, limit = 20 ) {
  35. /**
  36. * The document instance.
  37. *
  38. * @readonly
  39. * @member {module:engine/model/document~Document} #document
  40. */
  41. this.document = doc;
  42. /**
  43. * The number of atomic changes in the buffer. Once it exceeds the {@link #limit},
  44. * the {@link #batch batch} is set to a new one.
  45. *
  46. * @readonly
  47. * @member {Number} #size
  48. */
  49. this.size = 0;
  50. /**
  51. * The maximum number of atomic changes which can be contained in one batch.
  52. *
  53. * @readonly
  54. * @member {Number} #limit
  55. */
  56. this.limit = limit;
  57. this._changeCallback = ( evt, type, changes, batch ) => {
  58. this._onBatch( batch );
  59. };
  60. doc.on( 'change', this._changeCallback );
  61. /**
  62. * The current batch instance.
  63. *
  64. * @private
  65. * @member #_batch
  66. */
  67. /**
  68. * The callback to document the change event which later needs to be removed.
  69. *
  70. * @private
  71. * @member #_changeCallback
  72. */
  73. }
  74. /**
  75. * The current batch to which a feature should add its deltas. Once the {@link #size}
  76. * is reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  77. *
  78. * @type {engine.treeModel.batch.Batch}
  79. */
  80. get batch() {
  81. if ( !this._batch ) {
  82. this._batch = this.document.batch();
  83. }
  84. return this._batch;
  85. }
  86. /**
  87. * The input number of changes into the buffer. Once the {@link #size} is
  88. * reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  89. *
  90. * @param {Number} changeCount The number of atomic changes to input.
  91. */
  92. input( changeCount ) {
  93. this.size += changeCount;
  94. if ( this.size >= this.limit ) {
  95. this._reset();
  96. }
  97. }
  98. /**
  99. * Destroys the buffer.
  100. */
  101. destroy() {
  102. this.document.off( 'change', this._changeCallback );
  103. }
  104. /**
  105. * The method to be called in order to notify the buffer about batches which appeared in the document.
  106. * The method will check whether it is a new batch and in that case the buffer will be flushed.
  107. *
  108. * The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
  109. * should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
  110. * the characters typed after inserting the image should be added to a different batch than the characters typed before.
  111. *
  112. * @private
  113. * @param {module:engine/model/batch~Batch} batch The batch which appears in the document.
  114. */
  115. _onBatch( batch ) {
  116. // One operation means a newly created batch.
  117. if ( batch.type != 'transparent' && batch !== this._batch && count( batch.getOperations() ) <= 1 ) {
  118. this._reset();
  119. }
  120. }
  121. /**
  122. * Resets the change buffer.
  123. *
  124. * @private
  125. */
  126. _reset() {
  127. this._batch = null;
  128. this.size = 0;
  129. }
  130. }