changebuffer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module typing/utils/changebuffer
  7. */
  8. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  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( model, 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/model~Model} model
  32. * @param {Number} [limit=20] The maximum number of atomic changes which can be contained in one batch.
  33. */
  34. constructor( model, limit = 20 ) {
  35. /**
  36. * The model instance.
  37. *
  38. * @readonly
  39. * @member {module:engine/model/model~Model} #model
  40. */
  41. this.model = model;
  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. /**
  58. * Whether the buffer is locked. A locked buffer cannot be reset unless it gets unlocked.
  59. *
  60. * @readonly
  61. * @member {Boolean} #isLocked
  62. */
  63. this.isLocked = false;
  64. // The function to be called in order to notify the buffer about batches which appeared in the document.
  65. // The callback will check whether it is a new batch and in that case the buffer will be flushed.
  66. //
  67. // The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
  68. // should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
  69. // the characters typed after inserting the image should be added to a different batch than the characters typed before.
  70. this._changeCallback = ( evt, batch ) => {
  71. if ( batch.type != 'transparent' && batch !== this._batch ) {
  72. this._reset( true );
  73. }
  74. };
  75. this._selectionChangeCallback = () => {
  76. this._reset();
  77. };
  78. this.model.document.on( 'change', this._changeCallback );
  79. this.model.document.selection.on( 'change:range', this._selectionChangeCallback );
  80. this.model.document.selection.on( 'change:attribute', this._selectionChangeCallback );
  81. /**
  82. * The current batch instance.
  83. *
  84. * @private
  85. * @member #_batch
  86. */
  87. /**
  88. * The callback to document the change event which later needs to be removed.
  89. *
  90. * @private
  91. * @member #_changeCallback
  92. */
  93. /**
  94. * The callback to document selection `change:attribute` and `change:range` events which resets the buffer.
  95. *
  96. * @private
  97. * @member #_selectionChangeCallback
  98. */
  99. }
  100. /**
  101. * The current batch to which a feature should add its operations. Once the {@link #size}
  102. * is reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  103. *
  104. * @type {module:engine/model/batch~Batch}
  105. */
  106. get batch() {
  107. if ( !this._batch ) {
  108. this._batch = new Batch();
  109. }
  110. return this._batch;
  111. }
  112. /**
  113. * The input number of changes into the buffer. Once the {@link #size} is
  114. * reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  115. *
  116. * @param {Number} changeCount The number of atomic changes to input.
  117. */
  118. input( changeCount ) {
  119. this.size += changeCount;
  120. if ( this.size >= this.limit ) {
  121. this._reset( true );
  122. }
  123. }
  124. /**
  125. * Locks the buffer.
  126. */
  127. lock() {
  128. this.isLocked = true;
  129. }
  130. /**
  131. * Unlocks the buffer.
  132. */
  133. unlock() {
  134. this.isLocked = false;
  135. }
  136. /**
  137. * Destroys the buffer.
  138. */
  139. destroy() {
  140. this.model.document.off( 'change', this._changeCallback );
  141. this.model.document.selection.off( 'change:range', this._selectionChangeCallback );
  142. this.model.document.selection.off( 'change:attribute', this._selectionChangeCallback );
  143. }
  144. /**
  145. * Resets the change buffer.
  146. *
  147. * @private
  148. * @param {Boolean} [ignoreLock] Whether internal lock {@link #isLocked} should be ignored.
  149. */
  150. _reset( ignoreLock ) {
  151. if ( !this.isLocked || ignoreLock ) {
  152. this._batch = null;
  153. this.size = 0;
  154. }
  155. }
  156. }