浏览代码

Filtered out ununseful batches from autosave.

Maciej Bukowski 7 年之前
父节点
当前提交
71d6bd1769
共有 2 个文件被更改,包括 44 次插入1 次删除
  1. 31 1
      packages/ckeditor5-autosave/src/autosave.js
  2. 13 0
      packages/ckeditor5-autosave/tests/autosave.js

+ 31 - 1
packages/ckeditor5-autosave/src/autosave.js

@@ -56,7 +56,7 @@ export default class Autosave extends Plugin {
 		const doc = editor.model.document;
 		const pendingActions = editor.plugins.get( PendingActions );
 
-		this.listenTo( doc, 'change', this._throttledSave );
+		this.listenTo( doc, 'change', this._saveIfDocumentChanged.bind( this ) );
 
 		// TODO: Docs
 		// Flush on the editor's destroy listener with the highest priority to ensure that
@@ -87,6 +87,26 @@ export default class Autosave extends Plugin {
 	}
 
 	/**
+	 * Filter out changes in document, that don't impact on the data (e.g. selection changes).
+	 * Ends once it finds the operation that potentially changes document's data.
+	 *
+	 * @private
+	 */
+	_saveIfDocumentChanged( evt, batch ) {
+		for ( const delta of batch.deltas ) {
+			for ( const operation of delta.operations ) {
+				const name = operation.name;
+
+				if ( !name || !isUserSelectionOperation( operation ) ) {
+					this._throttledSave();
+
+					return;
+				}
+			}
+		}
+	}
+
+	/**
 	 * If the provider is set and new document version exists,
 	 * `_save()` method creates a pending action and calls `provider.save()` method.
 	 * It waits for the result and then removes the created pending action.
@@ -125,3 +145,13 @@ export default class Autosave extends Plugin {
  * @name Provider#save
  * @type {Function}
  */
+
+/**
+ * @private
+ */
+function isUserSelectionOperation( operation ) {
+	return (
+		operation.name.startsWith( 'user:position' ) ||
+		operation.name.startsWith( 'user:range' )
+	);
+}

+ 13 - 0
packages/ckeditor5-autosave/tests/autosave.js

@@ -145,6 +145,19 @@ describe( 'Autosave', () => {
 			} );
 		} );
 
+		it( 'should filter out batches that don\'t change content', () => {
+			autosave.provider = {
+				save: sandbox.spy()
+			};
+
+			editor.model.change( writer => {
+				writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+			} );
+
+			autosave._flush();
+			sinon.assert.notCalled( autosave.provider.save );
+		} );
+
 		it( 'should flush remaining calls after editor\'s destroy', () => {
 			const spy = sandbox.spy();
 			const savedStates = [];