Przeglądaj źródła

Shrinking the selection should not override browser behavior.

Kuba Niegowski 5 lat temu
rodzic
commit
1afda5c810

+ 6 - 0
packages/ckeditor5-table/src/tablekeyboard.js

@@ -218,6 +218,12 @@ export default class TableKeyboard extends Plugin {
 			return false;
 		}
 
+		// Navigation is in the opposite direction than the selection direction so this is shrinking of the selection.
+		// Selection for sure will not approach cell edge.
+		if ( expandSelection && !selection.isCollapsed && selection.isBackward == isForward ) {
+			return false;
+		}
+
 		const cellRange = model.createRangeIn( tableCell );
 
 		// Let's check if the selection is at the beginning/end of the cell.

+ 1 - 0
packages/ckeditor5-table/tests/manual/tablemocking.html

@@ -65,6 +65,7 @@
 </div>
 
 <div id="editor">
+	<table><tbody><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td><p>aaa bbb ccc&nbsp;</p><p>ddd eee</p></td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table>
 </div>
 
 <div id="table-tools">

+ 28 - 2
packages/ckeditor5-table/tests/tablekeyboard.js

@@ -2451,7 +2451,7 @@ describe( 'TableKeyboard', () => {
 							] ) );
 						} );
 
-						it( 'should expand not collapsed selection to the beginning of the cell content from the selection anchor', () => {
+						it( 'should not prevent default browser behavior for shrinking selection (up arrow)', () => {
 							setModelData( model, modelTable( [
 								[ '00', '01', '02' ],
 								[ '10', 'word [word]' + text, '12' ],
@@ -2460,12 +2460,25 @@ describe( 'TableKeyboard', () => {
 
 							editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
 
+							sinon.assert.notCalled( upArrowDomEvtDataStub.preventDefault );
+							sinon.assert.notCalled( upArrowDomEvtDataStub.stopPropagation );
+						} );
+
+						it( 'should expand not collapsed selection to the beginning of the cell content from the selection anchor', () => {
+							setModelData( model, modelTable( [
+								[ '00', '01', '02' ],
+								[ '10', 'word [word]' + text, '12' ],
+								[ '20', '21', '22' ]
+							] ), { lastRangeBackward: true } );
+
+							editor.editing.view.document.fire( 'keydown', upArrowDomEvtDataStub );
+
 							sinon.assert.calledOnce( upArrowDomEvtDataStub.preventDefault );
 							sinon.assert.calledOnce( upArrowDomEvtDataStub.stopPropagation );
 
 							assertEqualMarkup( getModelData( model ), modelTable( [
 								[ '00', '01', '02' ],
-								[ '10', '[word ]word' + text, '12' ],
+								[ '10', '[word word]' + text, '12' ],
 								[ '20', '21', '22' ]
 							] ) );
 						} );
@@ -2489,6 +2502,19 @@ describe( 'TableKeyboard', () => {
 							] ) );
 						} );
 
+						it( 'should not prevent default browser behavior for shrinking selection (down arrow)', () => {
+							setModelData( model, modelTable( [
+								[ '00', '01', '02' ],
+								[ '10', text + '[word] word', '12' ],
+								[ '20', '21', '22' ]
+							] ), { lastRangeBackward: true } );
+
+							editor.editing.view.document.fire( 'keydown', downArrowDomEvtDataStub );
+
+							sinon.assert.notCalled( downArrowDomEvtDataStub.preventDefault );
+							sinon.assert.notCalled( downArrowDomEvtDataStub.stopPropagation );
+						} );
+
 						it( 'should expand not collapsed selection to the end of the cell content from the selection anchor', () => {
 							setModelData( model, modelTable( [
 								[ '00', '01', '02' ],

+ 6 - 0
packages/ckeditor5-widget/src/verticalnavigation.js

@@ -29,6 +29,12 @@ export default function verticalNavigationHandler( editing ) {
 			return;
 		}
 
+		// Navigation is in the opposite direction than the selection direction so this is shrinking of the selection.
+		// Selection for sure will not approach any object.
+		if ( expandSelection && !selection.isCollapsed && selection.isBackward == arrowDownPressed ) {
+			return;
+		}
+
 		// Find a range between selection and closest limit element.
 		const range = findTextRangeFromSelection( editing, selection, arrowDownPressed );