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

Remove selection contents on '229' keydown only for non-flat selection.

Krzysztof Krztoń 7 лет назад
Родитель
Сommit
88b447a3f3
2 измененных файлов с 40 добавлено и 8 удалено
  1. 6 5
      packages/ckeditor5-typing/src/input.js
  2. 34 3
      packages/ckeditor5-typing/tests/input.js

+ 6 - 5
packages/ckeditor5-typing/src/input.js

@@ -101,16 +101,17 @@ export default class Input extends Plugin {
 			return;
 		}
 
-		// If not during composition we should also delete content on '229' key code (composition start).
-		// While composing, deletion should be prevented as it may remove composed sequence (#83).
+		// If during composition, deletion should be prevented as it may remove composed sequence (#83).
 		if ( isComposing && evtData.keyCode === 229 ) {
 			return;
 		}
 
+		const isSelectionFlat = doc.selection.rangeCount === 1 ? doc.selection.getFirstRange().isFlat : true;
 		// If there is a `keydown` event fired with '229' keycode it might be related to recent composition.
 		// Check if selection is the same as upon ending recent composition, if so do not remove selected content
-		// as it will remove composed sequence (#83).
-		if ( !isComposing && evtData.keyCode === 229 && isSelectionUnchanged ) {
+		// as it will remove composed sequence. Also do not remove selection contents if selection is flat as it might
+		// cause some issues when starting composition. Flat selection is correctly handled by mutation handler itself (#83).
+		if ( !isComposing && evtData.keyCode === 229 && ( isSelectionUnchanged || isSelectionFlat ) ) {
 			return;
 		}
 
@@ -141,7 +142,7 @@ export default class Input extends Plugin {
 		const isFlatSelection = doc.selection.rangeCount === 1 ? doc.selection.getFirstRange().isFlat : true;
 
 		// If on `compositionstart` there is a non-collapsed selection which start and end have different block
-		// parents it means `_handleKeydown()` method did not remove its contents. It happens usually because
+		// parents it means the `_handleKeydown()` method did not remove its contents. It happens usually because
 		// of different order of events (`compositionstart` before `keydown` - in Safari). In such cases
 		// we need to remove selection contents on composition start (#83).
 		if ( doc.selection.isCollapsed || isFlatSelection ) {

+ 34 - 3
packages/ckeditor5-typing/tests/input.js

@@ -685,7 +685,7 @@ describe( 'Input feature', () => {
 		} );
 
 		describe( '#83', () => {
-			it( 'should remove contents on composition start key if not during composition', () => {
+			it( 'should not remove contents on composition start key if not during composition', () => {
 				model.change( writer => {
 					writer.setSelection(
 						ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) );
@@ -693,7 +693,38 @@ describe( 'Input feature', () => {
 
 				viewDocument.fire( 'keydown', { keyCode: 229 } );
 
-				expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
+				expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
+			} );
+
+			it( 'should not remove contents on composition start key if not during composition and no selection', () => {
+				editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
+
+				const documentSelection = model.document.selection;
+
+				// Create empty selection.
+				model.document.selection = new ModelSelection();
+
+				viewDocument.fire( 'keydown', { keyCode: 229 } );
+
+				expect( getModelData( model ) ).to.equal(
+					'<paragraph><$text bold="true">foo</$text> <$text italic="true">bar</$text></paragraph>' );
+
+				// Restore document selection.
+				model.document.selection = documentSelection;
+			} );
+
+			it( 'should remove contents on composition start key if not during composition and selection not flat', () => {
+				editor.setData( '<p><strong>foo</strong></p><p><i>bar</i></p>' );
+
+				model.change( writer => {
+					writer.setSelection(
+						ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 1 ), 2 ) );
+				} );
+
+				viewDocument.fire( 'keydown', { keyCode: 229 } );
+
+				expect( getModelData( model ) ).to.equal(
+					'<paragraph><$text bold="true">fo[]</$text><$text italic="true">r</$text></paragraph>' );
 			} );
 
 			it( 'should not remove contents on composition start key if during composition', () => {
@@ -722,7 +753,7 @@ describe( 'Input feature', () => {
 					'<paragraph><$text bold="true">fo[o</$text> <$text italic="true">b]ar</$text></paragraph>' );
 			} );
 
-			it( 'should not remove contents on compositionstart event if selection is flat (no selection)', () => {
+			it( 'should not remove contents on compositionstart event if no selection', () => {
 				editor.setData( '<p><strong>foo</strong> <i>bar</i></p>' );
 
 				const documentSelection = model.document.selection;