changebuffer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module typing/utils/changebuffer
  7. */
  8. /**
  9. * Change buffer allows to group atomic changes (like characters that have been typed) into
  10. * {@link module:engine/model/batch~Batch batches}.
  11. *
  12. * Batches represent single undo steps, hence changes added to one single batch are undone together.
  13. *
  14. * The buffer has a configurable limit of atomic changes that it can accommodate. After the limit was
  15. * exceeded (see {@link ~ChangeBuffer#input}), a new batch is created in {@link ~ChangeBuffer#batch}.
  16. *
  17. * To use the change buffer you need to let it know about the number of changes that were added to the batch:
  18. *
  19. * const buffer = new ChangeBuffer( model, LIMIT );
  20. *
  21. * // Later on in your feature:
  22. * buffer.batch.insert( pos, insertedCharacters );
  23. * buffer.input( insertedCharacters.length );
  24. *
  25. */
  26. export default class ChangeBuffer {
  27. /**
  28. * Creates a new instance of the change buffer.
  29. *
  30. * @param {module:engine/model/model~Model} model
  31. * @param {Number} [limit=20] The maximum number of atomic changes which can be contained in one batch.
  32. */
  33. constructor( model, limit = 20 ) {
  34. /**
  35. * The model instance.
  36. *
  37. * @readonly
  38. * @member {module:engine/model/model~Model} #model
  39. */
  40. this.model = model;
  41. /**
  42. * The number of atomic changes in the buffer. Once it exceeds the {@link #limit},
  43. * the {@link #batch batch} is set to a new one.
  44. *
  45. * @readonly
  46. * @member {Number} #size
  47. */
  48. this.size = 0;
  49. /**
  50. * The maximum number of atomic changes which can be contained in one batch.
  51. *
  52. * @readonly
  53. * @member {Number} #limit
  54. */
  55. this.limit = limit;
  56. /**
  57. * Whether the buffer is locked. A locked buffer cannot be reset unless it gets unlocked.
  58. *
  59. * @readonly
  60. * @member {Boolean} #isLocked
  61. */
  62. this.isLocked = false;
  63. // The function to be called in order to notify the buffer about batches which appeared in the document.
  64. // The callback will check whether it is a new batch and in that case the buffer will be flushed.
  65. //
  66. // The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
  67. // should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
  68. // the characters typed after inserting the image should be added to a different batch than the characters typed before.
  69. this._changeCallback = ( evt, batch ) => {
  70. if ( batch.type != 'transparent' && batch !== this._batch ) {
  71. this._reset( true );
  72. }
  73. };
  74. this._selectionChangeCallback = () => {
  75. this._reset();
  76. };
  77. this.model.document.on( 'change', this._changeCallback );
  78. this.model.document.selection.on( 'change:range', this._selectionChangeCallback );
  79. this.model.document.selection.on( 'change:attribute', this._selectionChangeCallback );
  80. /**
  81. * The current batch instance.
  82. *
  83. * @private
  84. * @member #_batch
  85. */
  86. /**
  87. * The callback to document the change event which later needs to be removed.
  88. *
  89. * @private
  90. * @member #_changeCallback
  91. */
  92. /**
  93. * The callback to document selection `change:attribute` and `change:range` events which resets the buffer.
  94. *
  95. * @private
  96. * @member #_selectionChangeCallback
  97. */
  98. }
  99. /**
  100. * The current batch to which a feature should add its operations. Once the {@link #size}
  101. * is reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  102. *
  103. * @type {module:engine/model/batch~Batch}
  104. */
  105. get batch() {
  106. if ( !this._batch ) {
  107. this._batch = this.model.createBatch();
  108. }
  109. return this._batch;
  110. }
  111. /**
  112. * The input number of changes into the buffer. Once the {@link #size} is
  113. * reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  114. *
  115. * @param {Number} changeCount The number of atomic changes to input.
  116. */
  117. input( changeCount ) {
  118. this.size += changeCount;
  119. if ( this.size >= this.limit ) {
  120. this._reset( true );
  121. }
  122. }
  123. /**
  124. * Locks the buffer.
  125. */
  126. lock() {
  127. this.isLocked = true;
  128. }
  129. /**
  130. * Unlocks the buffer.
  131. */
  132. unlock() {
  133. this.isLocked = false;
  134. }
  135. /**
  136. * Destroys the buffer.
  137. */
  138. destroy() {
  139. this.model.document.off( 'change', this._changeCallback );
  140. this.model.document.selection.off( 'change:range', this._selectionChangeCallback );
  141. this.model.document.selection.off( 'change:attribute', this._selectionChangeCallback );
  142. }
  143. /**
  144. * Resets the change buffer.
  145. *
  146. * @private
  147. * @param {Boolean} [ignoreLock] Whether internal lock {@link #isLocked} should be ignored.
  148. */
  149. _reset( ignoreLock ) {
  150. if ( !this.isLocked || ignoreLock ) {
  151. this._batch = null;
  152. this.size = 0;
  153. }
  154. }
  155. }