Krzysztof Krztoń hace 8 años
padre
commit
47679efc66

+ 6 - 6
packages/ckeditor5-typing/src/changebuffer.js

@@ -72,15 +72,15 @@ export default class ChangeBuffer {
 			this._onBatch( batch );
 		};
 
-		this._resetOnChangeCallback = () => {
+		this._selectionChangeCallback = () => {
 			this._reset();
 		};
 
 		doc.on( 'change', this._changeCallback );
 
-		doc.selection.on( 'change:range', this._resetOnChangeCallback );
+		doc.selection.on( 'change:range', this._selectionChangeCallback );
 
-		doc.selection.on( 'change:attribute', this._resetOnChangeCallback );
+		doc.selection.on( 'change:attribute', this._selectionChangeCallback );
 
 		/**
 		 * The current batch instance.
@@ -100,7 +100,7 @@ export default class ChangeBuffer {
 		 * The callback to document selection change:attribute and change:range events which resets the buffer.
 		 *
 		 * @private
-		 * @member #_resetOnChangeCallback
+		 * @member #_selectionChangeCallback
 		 */
 	}
 
@@ -151,8 +151,8 @@ export default class ChangeBuffer {
 	 */
 	destroy() {
 		this.document.off( 'change', this._changeCallback );
-		this.document.selection.off( 'change:range', this._resetOnChangeCallback );
-		this.document.selection.off( 'change:attribute', this._resetOnChangeCallback );
+		this.document.selection.off( 'change:range', this._selectionChangeCallback );
+		this.document.selection.off( 'change:attribute', this._selectionChangeCallback );
 	}
 
 	/**

+ 2 - 2
packages/ckeditor5-typing/src/input.js

@@ -66,13 +66,13 @@ export default class Input extends Plugin {
 			return;
 		}
 
-		this._buffer.lock();
+		buffer.lock();
 
 		doc.enqueueChanges( () => {
 			this.editor.data.deleteContent( doc.selection, buffer.batch );
 		} );
 
-		this._buffer.unlock();
+		buffer.unlock();
 	}
 
 	/**

+ 4 - 0
packages/ckeditor5-typing/src/inputcommand.js

@@ -78,6 +78,8 @@ export default class InputCommand extends Command {
 		doc.enqueueChanges( () => {
 			const isCollapsedRange = range.isCollapsed;
 
+			this._buffer.lock();
+
 			if ( !isCollapsedRange ) {
 				this._buffer.batch.remove( range );
 			}
@@ -91,6 +93,8 @@ export default class InputCommand extends Command {
 				this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
 			}
 
+			this._buffer.unlock();
+
 			this._buffer.input( textInsertions );
 		} );
 	}

+ 46 - 0
packages/ckeditor5-typing/tests/changebuffer.js

@@ -182,6 +182,52 @@ describe( 'ChangeBuffer', () => {
 			expect( buffer.batch ).to.not.equal( initialBatch );
 			expect( buffer.size ).to.equal( 0 );
 		} );
+
+		it( 'is reset on selection change:range', () => {
+			const initialBatch = buffer.batch;
+
+			doc.selection.fire( 'change:range' );
+
+			expect( buffer.batch ).to.not.equal( initialBatch );
+			expect( buffer.size ).to.equal( 0 );
+		} );
+
+		it( 'is reset on selection change:attribute', () => {
+			const initialBatch = buffer.batch;
+
+			doc.selection.fire( 'change:attribute' );
+
+			expect( buffer.batch ).to.not.equal( initialBatch );
+			expect( buffer.size ).to.equal( 0 );
+		} );
+
+		it( 'is not reset on selection change:range while locked', () => {
+			const initialBatch = buffer.batch;
+			buffer.size = 1;
+
+			buffer.lock();
+
+			doc.selection.fire( 'change:range' );
+
+			buffer.unlock();
+
+			expect( buffer.batch ).to.be.equal( initialBatch );
+			expect( buffer.size ).to.equal( 1 );
+		} );
+
+		it( 'is not reset on selection change:attribute while locked', () => {
+			const initialBatch = buffer.batch;
+			buffer.size = 1;
+
+			buffer.lock();
+
+			doc.selection.fire( 'change:attribute' );
+
+			buffer.unlock();
+
+			expect( buffer.batch ).to.be.equal( initialBatch );
+			expect( buffer.size ).to.equal( 1 );
+		} );
 	} );
 
 	describe( 'destroy', () => {

+ 3 - 5
packages/ckeditor5-typing/tests/input.js

@@ -23,8 +23,6 @@ import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 
-import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
-
 describe( 'Input feature', () => {
 	let editor, model, modelRoot, view, viewRoot, listenter;
 
@@ -438,7 +436,7 @@ describe( 'Input feature', () => {
 		} );
 
 		it( 'should lock buffer if selection is not collapsed', () => {
-			const buffer = editor.plugins.get( Input )._buffer;
+			const buffer = editor.commands.get( 'input' )._buffer;
 			const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
 			const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
 
@@ -454,7 +452,7 @@ describe( 'Input feature', () => {
 		} );
 
 		it( 'should not lock buffer on non printable keys', () => {
-			const buffer = editor.plugins.get( Input )._buffer;
+			const buffer = editor.commands.get( 'input' )._buffer;
 			const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
 			const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
 
@@ -467,7 +465,7 @@ describe( 'Input feature', () => {
 		} );
 
 		it( 'should not lock buffer on collapsed selection', () => {
-			const buffer = editor.plugins.get( Input )._buffer;
+			const buffer = editor.commands.get( 'input' )._buffer;
 			const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
 			const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
 

+ 14 - 0
packages/ckeditor5-typing/tests/inputcommand.js

@@ -71,6 +71,20 @@ describe( 'InputCommand', () => {
 			expect( spy.calledOnce ).to.be.true;
 		} );
 
+		it( 'should lock and unlock buffer', () => {
+			setData( doc, '<p>foo[]bar</p>' );
+
+			const spyLock = testUtils.sinon.spy( buffer, 'lock' );
+			const spyUnlock = testUtils.sinon.spy( buffer, 'unlock' );
+
+			editor.execute( 'input', {
+				text: ''
+			} );
+
+			expect( spyLock.calledOnce ).to.be.true;
+			expect( spyUnlock.calledOnce ).to.be.true;
+		} );
+
 		it( 'inserts text for collapsed range', () => {
 			setData( doc, '<p>foo[]</p>' );