8
0
Просмотр исходного кода

Fix selection after text insertion.

Krzysztof Krztoń 9 лет назад
Родитель
Сommit
052dacaad1
2 измененных файлов с 16 добавлено и 16 удалено
  1. 3 15
      packages/ckeditor5-typing/src/input.js
  2. 13 1
      packages/ckeditor5-typing/src/inputcommand.js

+ 3 - 15
packages/ckeditor5-typing/src/input.js

@@ -76,9 +76,7 @@ export default class Input extends Plugin {
 	 * @param {module:engine/controller/editingcontroller~EditingController} viewSelection
 	 */
 	_handleMutations( mutations, viewSelection ) {
-		const handler = new MutationHandler( this.editor );
-
-		handler.handle( mutations, viewSelection );
+		new MutationHandler( this.editor ).handle( mutations, viewSelection );
 	}
 }
 
@@ -188,14 +186,8 @@ class MutationHandler {
 
 		this.editor.execute( 'input', {
 			text: mutation.newText.substr( firstChangeAt, insertions ),
-			range: deletions > 0 ? ModelRange.createFromPositionAndShift( modelPos, deletions ) : null
-		} );
-
-		this.editor.document.enqueueChanges( () => {
-			// If there was `viewSelection` and it got correctly mapped, collapse selection at found model position.
-			if ( modelSelectionPosition ) {
-				this.editing.model.selection.collapse( modelSelectionPosition );
-			}
+			range: deletions > 0 ? ModelRange.createFromPositionAndShift( modelPos, deletions ) : null,
+			selectionAnchor: modelSelectionPosition
 		} );
 	}
 
@@ -237,10 +229,6 @@ class MutationHandler {
 			text: insertedText.replace( /\u00A0/g, ' ' ),
 			range: new ModelRange( modelPos )
 		} );
-
-		this.editor.document.enqueueChanges( () => {
-			this.editing.model.selection.collapse( modelPos.getShiftedBy( insertedText.length ) );
-		} );
 	}
 }
 

+ 13 - 1
packages/ckeditor5-typing/src/inputcommand.js

@@ -64,16 +64,21 @@ export default class InputCommand extends Command {
 	 * @param {String} [options.text=''] Text to be inserted.
 	 * @param {module:engine/model/range~Range} [options.range=null] Range in which the text is inserted. Defaults
 	 * to first range in the current selection.
+	 * @param {module:engine/model/position~Position} [options.selectionAnchor] Selection anchor which will be used
+	 * to set selection on a data model.
 	 */
 	_doExecute( options = {} ) {
 		const doc = this.editor.document;
 		const range = options.range || doc.selection.getFirstRange();
 		const text = options.text || '';
+		const selectionAnchor = options.selectionAnchor;
 		let textLength = 0;
 
 		if ( range ) {
 			doc.enqueueChanges( () => {
-				if ( !range.isCollapsed ) {
+				const isCollapsedRange = range.isCollapsed;
+
+				if ( !isCollapsedRange ) {
 					textLength -= this._getTextWithinRange( range ).length;
 					this._buffer.batch.remove( range );
 				}
@@ -83,6 +88,13 @@ export default class InputCommand extends Command {
 					this._buffer.batch.weakInsert( range.start, text );
 				}
 
+				if ( selectionAnchor ) {
+					this.editor.data.model.selection.collapse( selectionAnchor );
+				} else if ( isCollapsedRange ) {
+					// If range was collapsed just shift the selection by the number of inserted characters.
+					this.editor.data.model.selection.collapse( range.start.getShiftedBy( textLength ) );
+				}
+
 				this._buffer.input( Math.max( textLength, 0 ) );
 			} );
 		}