소스 검색

Moved similar selection checking to renderer.

Krzysztof Krztoń 8 년 전
부모
커밋
fe62918789
2개의 변경된 파일38개의 추가작업 그리고 33개의 파일을 삭제
  1. 38 1
      packages/ckeditor5-engine/src/view/renderer.js
  2. 0 32
      packages/ckeditor5-engine/src/view/selection.js

+ 38 - 1
packages/ckeditor5-engine/src/view/renderer.js

@@ -9,6 +9,7 @@
 
 import ViewText from './text';
 import ViewPosition from './position';
+import Selection from './selection';
 import { INLINE_FILLER, INLINE_FILLER_LENGTH, startsWithFiller, isInlineFiller, isBlockFiller } from './filler';
 
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
@@ -577,7 +578,7 @@ export default class Renderer {
 		const oldViewSelection = domSelection && this.domConverter.domSelectionToView( domSelection );
 
 		if ( oldViewSelection && ( this.selection.isEqual( oldViewSelection ) ||
-			!this.selection.isCollapsed && this.selection.isSimilar( oldViewSelection ) ) ) {
+			this._areSimilarSelections( oldViewSelection, this.selection ) ) ) {
 			return;
 		}
 
@@ -640,6 +641,42 @@ export default class Renderer {
 			}
 		}
 	}
+
+	/**
+	 * Checks if two given selections are similar. Selections are considered similar if they are non-collapsed
+	 * and their trimmed (see {@link #_trimSelection}) representations are equal.
+	 *
+	 * @private
+	 * @param {module:engine/view/selection~Selection} current
+	 * @param {module:engine/view/selection~Selection} previous
+	 * @returns {Boolean}
+	 */
+	_areSimilarSelections( current, previous ) {
+		return !current.isCollapsed && this._trimSelection( current ).isEqual( this._trimSelection( previous ) );
+	}
+
+	/**
+	 * Creates a copy of a given selection with all of its ranges
+	 * trimmed (see {@link module:engine/view/range~Range#getTrimmed getTrimmed}).
+	 *
+	 * @private
+	 * @param {module:engine/view/selection~Selection} selection
+	 * @returns {module:engine/view/selection~Selection} Selection copy with all ranges trimmed.
+	 */
+	_trimSelection( selection ) {
+		const newSelection = Selection.createFromSelection( selection );
+		const ranges = newSelection.getRanges();
+
+		let trimmedRanges = [];
+
+		for ( let range of ranges ) {
+			trimmedRanges.push( range.getTrimmed() );
+		}
+
+		newSelection.setRanges( trimmedRanges );
+
+		return newSelection;
+	}
 }
 
 mix( Renderer, ObservableMixin );

+ 0 - 32
packages/ckeditor5-engine/src/view/selection.js

@@ -330,17 +330,6 @@ export default class Selection {
 		return true;
 	}
 
-	/**
-	 * Checks whether, this selection is similar to given selection. Selections are similar if they are initially
-	 * equal (see {@link #isEqual isEqual}) or if their trimmed (see {@link #getTrimmed getTrimmed}) representations are equal.
-	 *
-	 * @param {module:engine/view/selection~Selection} otherSelection Selection to compare with.
-	 * @returns {Boolean} `true` if selections are similar, `false` otherwise.
-	 */
-	isSimilar( otherSelection ) {
-		return this.isEqual( otherSelection ) || this.getTrimmed().isEqual( otherSelection.getTrimmed() );
-	}
-
 	/**
 	 * Removes all ranges that were added to the selection.
 	 *
@@ -493,27 +482,6 @@ export default class Selection {
 		return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
 	}
 
-	/**
-	 * Returns a copy of a selection with all of its ranges
-	 * trimmed (see {@link module:engine/view/range~Range#getTrimmed getTrimmed}).
-	 *
-	 * @returns {module:engine/view/selection~Selection} Selection with all ranges shrank.
-	 */
-	getTrimmed() {
-		const selection = Selection.createFromSelection( this );
-		const ranges = this.getRanges();
-
-		let trimmedRanges = [];
-
-		for ( let range of ranges ) {
-			trimmedRanges.push( range.getTrimmed() );
-		}
-
-		selection.setRanges( trimmedRanges );
-
-		return selection;
-	}
-
 	/**
 	 * Creates and returns an instance of `Selection` that is a clone of given selection, meaning that it has same
 	 * ranges and same direction as this selection.