Przeglądaj źródła

TableSelection should be cleared on external selection change.

Maciej Gołaszewski 5 lat temu
rodzic
commit
5cb26423c6

+ 19 - 2
packages/ckeditor5-table/src/tableselection.js

@@ -90,9 +90,14 @@ export default class TableSelection extends Plugin {
 	 * @inheritDoc
 	 */
 	init() {
-		this._tableUtils = this.editor.plugins.get( 'TableUtils' );
+		const editor = this.editor;
+		const selection = editor.model.document.selection;
+
+		this._tableUtils = editor.plugins.get( 'TableUtils' );
+
+		setupTableSelectionHighlighting( editor, this );
 
-		setupTableSelectionHighlighting( this.editor, this );
+		selection.on( 'change:range', () => this._clearSelectionOnExternalChange( selection ) );
 	}
 
 	/**
@@ -234,4 +239,16 @@ export default class TableSelection extends Plugin {
 			writer.setSelection( modelRanges );
 		} );
 	}
+
+	/**
+	 * Checks if selection has changed from an external source and it is required to clear internal state.
+	 *
+	 * @param {module:engine/model/documentselection~DocumentSelection} selection
+	 * @private
+	 */
+	_clearSelectionOnExternalChange( selection ) {
+		if ( selection.rangeCount <= 1 && this.hasMultiCellSelection ) {
+			this.clearSelection();
+		}
+	}
 }

+ 2 - 0
packages/ckeditor5-table/src/tableselection/mouseselectionhandler.js

@@ -153,6 +153,8 @@ export default class MouseSelectionHandler {
 	}
 }
 
+mix( MouseSelectionHandler, ObservableMixin );
+
 function isButtonPressed( domEventData ) {
 	return !!domEventData.domEvent.buttons;
 }

+ 27 - 1
packages/ckeditor5-table/tests/tableselection.js

@@ -9,7 +9,9 @@ import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-util
 
 import TableEditing from '../src/tableediting';
 import TableSelection from '../src/tableselection';
-import { modelTable } from './_utils/utils';
+import { modelTable, viewTable } from './_utils/utils';
+import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 
 describe( 'table selection', () => {
 	let editor, model, tableSelection, modelRoot;
@@ -295,6 +297,30 @@ describe( 'table selection', () => {
 				] );
 			} );
 		} );
+
+		describe.only( 'behavior', () => {
+			it( 'should clear selection on external changes', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				editor.model.change( writer => {
+					writer.setSelection( modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] ), 0 );
+				} );
+
+				assertSelectedCells( [
+					[ 0, 0, 0 ],
+					[ 0, 0, 0 ],
+					[ 0, 0, 0 ]
+				] );
+
+				expect( editor.editing.view.document.selection.isFake ).to.be.false;
+				assertEqualMarkup( getViewData( editor.editing.view ), viewTable( [
+					[ '{}11', '12', '13' ],
+					[ '21', '22', '23' ],
+					[ '31', '32', '33' ]
+				], { asWidget: true } ) );
+			} );
+		} );
 	} );
 
 	// Helper method for asserting selected table cells.