changebuffer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  58. * Whether the buffer is locked. The locked buffer cannot be reset unless it gets unlocked.
  59. *
  60. * @readonly
  61. * @member {Boolean} #isLocked
  62. */
  63. this.isLocked = false;
  64. this._changeCallback = ( evt, type, changes, batch ) => {
  65. this._onBatch( batch );
  66. };
  67. this._selectionChangeCallback = () => {
  68. this._reset();
  69. };
  70. doc.on( 'change', this._changeCallback );
  71. doc.selection.on( 'change:range', this._selectionChangeCallback );
  72. doc.selection.on( 'change:attribute', this._selectionChangeCallback );
  73. /**
  74. * The current batch instance.
  75. *
  76. * @private
  77. * @member #_batch
  78. */
  79. /**
  80. * The callback to document the change event which later needs to be removed.
  81. *
  82. * @private
  83. * @member #_changeCallback
  84. */
  85. /**
  86. * The callback to document selection change:attribute and change:range events which resets the buffer.
  87. *
  88. * @private
  89. * @member #_selectionChangeCallback
  90. */
  91. }
  92. /**
  93. * The current batch to which a feature should add its deltas. Once the {@link #size}
  94. * is reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  95. *
  96. * @type {module:engine/model/batch~Batch}
  97. */
  98. get batch() {
  99. if ( !this._batch ) {
  100. this._batch = this.document.batch();
  101. }
  102. return this._batch;
  103. }
  104. /**
  105. * The input number of changes into the buffer. Once the {@link #size} is
  106. * reached or exceeds the {@link #limit}, the batch is set to a new instance and the size is reset.
  107. *
  108. * @param {Number} changeCount The number of atomic changes to input.
  109. */
  110. input( changeCount ) {
  111. this.size += changeCount;
  112. if ( this.size >= this.limit ) {
  113. this._reset( true );
  114. }
  115. }
  116. /**
  117. * Locks the buffer.
  118. */
  119. lock() {
  120. this.isLocked = true;
  121. }
  122. /**
  123. * Unlocks the buffer.
  124. */
  125. unlock() {
  126. this.isLocked = false;
  127. }
  128. /**
  129. * Destroys the buffer.
  130. */
  131. destroy() {
  132. this.document.off( 'change', this._changeCallback );
  133. this.document.selection.off( 'change:range', this._selectionChangeCallback );
  134. this.document.selection.off( 'change:attribute', this._selectionChangeCallback );
  135. }
  136. /**
  137. * The method to be called in order to notify the buffer about batches which appeared in the document.
  138. * The method will check whether it is a new batch and in that case the buffer will be flushed.
  139. *
  140. * The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
  141. * should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
  142. * the characters typed after inserting the image should be added to a different batch than the characters typed before.
  143. *
  144. * @private
  145. * @param {module:engine/model/batch~Batch} batch The batch which appears in the document.
  146. */
  147. _onBatch( batch ) {
  148. // One operation means a newly created batch.
  149. if ( batch.type != 'transparent' && batch !== this._batch && count( batch.getOperations() ) <= 1 ) {
  150. this._reset( true );
  151. }
  152. }
  153. /**
  154. * Resets the change buffer.
  155. *
  156. * @private
  157. * @param {Boolean} [ignoreLock] Whether internal lock {@link #isLocked} should be ignored.
  158. */
  159. _reset( ignoreLock ) {
  160. if ( !this.isLocked || ignoreLock ) {
  161. this._batch = null;
  162. this.size = 0;
  163. }
  164. }
  165. }