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

Changed: Used `view.Selection#isSimilar` in `SelectionObserver` and `Renderer`.

Szymon Cofalik 8 лет назад
Родитель
Сommit
8ac4dc6fd2

+ 1 - 1
packages/ckeditor5-engine/src/view/observer/selectionobserver.js

@@ -169,7 +169,7 @@ export default class SelectionObserver extends Observer {
 			return;
 			return;
 		}
 		}
 
 
-		if ( this.selection.isEqual( newViewSelection ) ) {
+		if ( this.selection.isSimilar( newViewSelection ) ) {
 			// If selection was equal and we are at this point of algorithm, it means that it was incorrect.
 			// If selection was equal and we are at this point of algorithm, it means that it was incorrect.
 			// Just re-render it, no need to fire any events, etc.
 			// Just re-render it, no need to fire any events, etc.
 			this.document.render();
 			this.document.render();

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

@@ -9,7 +9,6 @@
 
 
 import ViewText from './text';
 import ViewText from './text';
 import ViewPosition from './position';
 import ViewPosition from './position';
-import Selection from './selection';
 import { INLINE_FILLER, INLINE_FILLER_LENGTH, startsWithFiller, isInlineFiller, isBlockFiller } from './filler';
 import { INLINE_FILLER, INLINE_FILLER_LENGTH, startsWithFiller, isInlineFiller, isBlockFiller } from './filler';
 
 
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
@@ -615,7 +614,7 @@ export default class Renderer {
 			if ( this.selection.isCollapsed && this.selection.isEqual( viewSelectionFromDom ) ) {
 			if ( this.selection.isCollapsed && this.selection.isEqual( viewSelectionFromDom ) ) {
 				// Selection did not changed and is correct, do not update.
 				// Selection did not changed and is correct, do not update.
 				return;
 				return;
-			} else if ( areSimilarSelections( viewSelectionFromDom, this.selection ) ) {
+			} else if ( !this.selection.isCollapsed && this.selection.isSimilar( viewSelectionFromDom ) ) {
 				const data = {
 				const data = {
 					oldSelection: viewSelectionFromDom,
 					oldSelection: viewSelectionFromDom,
 					currentSelection: this.selection
 					currentSelection: this.selection
@@ -693,35 +692,3 @@ export default class Renderer {
 }
 }
 
 
 mix( Renderer, ObservableMixin );
 mix( Renderer, ObservableMixin );
-
-// 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} selection1
-// @param {module:engine/view/selection~Selection} selection2
-// @returns {Boolean}
-function areSimilarSelections( selection1, selection2 ) {
-	return !selection1.isCollapsed && trimSelection( selection1 ).isEqual( trimSelection( selection2 ) );
-}
-
-// 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.
-function trimSelection( selection ) {
-	const newSelection = Selection.createFromSelection( selection );
-	const ranges = newSelection.getRanges();
-
-	const trimmedRanges = [];
-
-	for ( const range of ranges ) {
-		trimmedRanges.push( range.getTrimmed() );
-	}
-
-	newSelection.setRanges( trimmedRanges, newSelection.isBackward );
-
-	return newSelection;
-}

+ 6 - 7
packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

@@ -320,25 +320,24 @@ describe( 'SelectionObserver', () => {
 		} );
 		} );
 
 
 		viewDocument.once( 'selectionChange', () => {
 		viewDocument.once( 'selectionChange', () => {
-			// 2. Selection change has been handled and proper event has been fired.
+			// 2. Selection change has been handled.
 
 
-			// 3. Now add listener for `domDocument` because `selectionChange` event will not be fired.
 			selectionObserver.listenTo( domDocument, 'selectionchange', () => {
 			selectionObserver.listenTo( domDocument, 'selectionchange', () => {
-				// 5. Check if view was re-rendered.
+				// 4. Check if view was re-rendered.
 				expect( viewDocument.render.called ).to.be.true;
 				expect( viewDocument.render.called ).to.be.true;
 
 
 				done();
 				done();
 			}, { priority: 'lowest' } );
 			}, { priority: 'lowest' } );
 
 
-			// 4. Now, collapse selection in similar position, but in UI element.
-			// Current and new selection position are same in view.
+			// 3. Now, collapse selection in similar position, but in UI element.
+			// Current and new selection position are similar in view (but not equal!).
 			// Also add a spy to `viewDocument#render` to see if view will be re-rendered.
 			// Also add a spy to `viewDocument#render` to see if view will be re-rendered.
 			sel.collapse( domMain.childNodes[ 0 ].childNodes[ 1 ], 0 );
 			sel.collapse( domMain.childNodes[ 0 ].childNodes[ 1 ], 0 );
 			sinon.spy( viewDocument, 'render' );
 			sinon.spy( viewDocument, 'render' );
 		}, { priority: 'lowest' } );
 		}, { priority: 'lowest' } );
 
 
-		// 1. Collapse before ui element and wait for async selectionchange to fire selection change handling.
-		sel.collapse( domMain.childNodes[ 0 ], 1 );
+		// 1. Collapse in a text node, before ui element, and wait for async selectionchange to fire selection change handling.
+		sel.collapse( domMain.childNodes[ 0 ].childNodes[ 0 ], 3 );
 	} );
 	} );
 
 
 	function changeDomSelection() {
 	function changeDomSelection() {