Sfoglia il codice sorgente

Add a method to determine input batches.

Maciej Gołaszewski 6 anni fa
parent
commit
1b545a827f

+ 6 - 0
packages/ckeditor5-typing/src/input.js

@@ -40,4 +40,10 @@ export default class Input extends Plugin {
 		injectUnsafeKeystrokesHandling( editor );
 		injectTypingMutationsHandling( editor );
 	}
+
+	isInput( batch ) {
+		const inputCommand = this.editor.commands.get( 'input' );
+
+		return inputCommand._batches.has( batch );
+	}
 }

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

@@ -35,6 +35,8 @@ export default class InputCommand extends Command {
 		 * @member {module:typing/utils/changebuffer~ChangeBuffer} #_buffer
 		 */
 		this._buffer = new ChangeBuffer( editor.model, undoStepSize );
+
+		this._batches = new WeakSet();
 	}
 
 	/**
@@ -98,6 +100,8 @@ export default class InputCommand extends Command {
 			this._buffer.unlock();
 
 			this._buffer.input( textInsertions );
+
+			this._batches.add( this._buffer.batch );
 		} );
 	}
 }

+ 2 - 1
packages/ckeditor5-typing/src/texttransformation.js

@@ -101,6 +101,7 @@ export default class TextTransformation extends Plugin {
 	init() {
 		const editor = this.editor;
 		const model = editor.model;
+		const input = editor.plugins.get( 'Input' );
 
 		const configuredTransformations = getConfiguredTransformations( editor.config.get( 'typing.transformations' ) );
 
@@ -111,7 +112,7 @@ export default class TextTransformation extends Plugin {
 			const watcher = new TextWatcher( editor.model, text => from.test( text ) );
 
 			watcher.on( 'matched:data', ( evt, data ) => {
-				if ( !data.isTyping ) {
+				if ( !input.isInput( data.batch ) ) {
 					return;
 				}
 

+ 1 - 20
packages/ckeditor5-typing/src/textwatcher.js

@@ -66,9 +66,7 @@ export default class TextWatcher {
 				return;
 			}
 
-			const isTyping = checkTyping( model );
-
-			this._evaluateTextBeforeSelection( 'data', { isTyping } );
+			this._evaluateTextBeforeSelection( 'data', { batch } );
 		} );
 	}
 
@@ -153,22 +151,5 @@ function _getText( range ) {
 	}, '' );
 }
 
-// Returns true if the change was done by typing.
-//
-// @param {module:engine/model/model~Model} model
-function checkTyping( model ) {
-	const changes = Array.from( model.document.differ.getChanges() );
-
-	// Typing is represented by only a single change...
-	if ( changes.length != 1 ) {
-		return false;
-	}
-
-	const entry = changes[ 0 ];
-
-	// ...and is an insert of a text.
-	return entry.type === 'insert' && entry.name == '$text' && entry.length == 1;
-}
-
 mix( TextWatcher, EmitterMixin );
 

+ 7 - 6
packages/ckeditor5-typing/tests/texttransformation.js

@@ -6,6 +6,7 @@
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 
+import Typing from '../src/typing';
 import TextTransformation from '../src/texttransformation';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
@@ -115,16 +116,18 @@ describe( 'Text transformation feature', () => {
 			simulateTyping( '"' );
 
 			expect( getData( model, { withoutSelection: true } ) )
-				.to.equal( '<paragraph>Foo “<$text bold="true">Bar</$text></paragraph>' );
+				.to.equal( '<paragraph>Foo “<$text bold="true">Bar</$text></paragraph>' );
 		} );
 
 		it( 'should keep styles of the replaced text #1', () => {
 			setData( model, '<paragraph>Foo <$text bold="true">"</$text>Bar[]</paragraph>' );
 
 			model.change( writer => {
-				writer.insertText( '"', { bold: true }, doc.selection.focus );
+				writer.setSelectionAttribute( { bold: true } );
 			} );
 
+			simulateTyping( '"' );
+
 			expect( getData( model, { withoutSelection: true } ) )
 				.to.equal( '<paragraph>Foo <$text bold="true">“</$text>Bar<$text bold="true">”</$text></paragraph>' );
 		} );
@@ -336,7 +339,7 @@ describe( 'Text transformation feature', () => {
 	function createEditorInstance( additionalConfig = {} ) {
 		return ClassicTestEditor
 			.create( editorElement, Object.assign( {
-				plugins: [ Paragraph, Bold, TextTransformation ]
+				plugins: [ Typing, Paragraph, Bold, TextTransformation ]
 			}, additionalConfig ) )
 			.then( newEditor => {
 				editor = newEditor;
@@ -350,9 +353,7 @@ describe( 'Text transformation feature', () => {
 		const letters = transformFrom.split( '' );
 
 		for ( const letter of letters ) {
-			model.enqueueChange( model.createBatch(), writer => {
-				writer.insertText( letter, doc.selection.focus );
-			} );
+			editor.execute( 'input', { text: letter } );
 		}
 	}
 } );