ソースを参照

Simplify code. Remove TreeWalker.

Maksymilian Barnaś 9 年 前
コミット
9f659d4193
1 ファイル変更9 行追加18 行削除
  1. 9 18
      packages/ckeditor5-autoformat/src/autoformatengine.js

+ 9 - 18
packages/ckeditor5-autoformat/src/autoformatengine.js

@@ -3,8 +3,6 @@
  * For licensing, see LICENSE.md.
  */
 
-import TreeWalker from '../engine/model/treewalker.js';
-import Position from '../engine/model/position.js';
 import Range from '../engine/model/range.js';
 import LivePosition from '../engine/model/liveposition.js';
 
@@ -42,14 +40,14 @@ export default class AutoformatEngine {
 			}
 
 			for ( let value of changes.range.getItems() ) {
-				const walker = new TreeWalker( {
-					direction: 'backward',
-					startPosition: Position.createAfter( value )
-				} );
-				const currentValue = walker.next().value;
-				const text = currentValue.item.data;
+				if ( !value.textNode ) {
+					return;
+				}
+
+				const element = value.textNode;
+				const text = element.data;
 
-				if ( currentValue.item.parent.name !== 'paragraph' || !text ) {
+				if ( element.parent.name !== 'paragraph' || !text ) {
 					return;
 				}
 
@@ -60,12 +58,9 @@ export default class AutoformatEngine {
 				}
 
 				// Get range of recently added text.
-				let lastPath = _getLastPathPart( currentValue.nextPosition.path );
-				let liveStartPosition = LivePosition.createFromParentAndOffset( currentValue.item.parent, lastPath + match.index );
-
 				editor.document.enqueueChanges( function() {
-					const range = Range.createFromPositionAndShift( liveStartPosition, match[ 0 ].length );
-					const element = currentValue.item;
+					const startPosition = LivePosition.createFromParentAndOffset( element.parent, element.startOffset );
+					const range = Range.createFromPositionAndShift( startPosition, match[ 0 ].length );
 
 					// Create new batch to separate typing batch from the Autoformat changes.
 					const batch = editor.document.batch();
@@ -76,7 +71,3 @@ export default class AutoformatEngine {
 		} );
 	}
 }
-
-function _getLastPathPart( path ) {
-	return path[ path.length - 1 ];
-}