Преглед на файлове

Revert "Improved the mechanism. Now it will be triggered by Delete key as well."

This reverts commit 7ddf430b5091ec07d4e5882a9c9a23322848c2d8.
Kamil Piechaczek преди 5 години
родител
ревизия
4df3fa299d
променени са 2 файла, в които са добавени 34 реда и са изтрити 44 реда
  1. 17 0
      packages/ckeditor5-link/src/linkediting.js
  2. 17 44
      packages/ckeditor5-link/tests/linkediting.js

+ 17 - 0
packages/ckeditor5-link/src/linkediting.js

@@ -17,6 +17,7 @@ import LinkCommand from './linkcommand';
 import UnlinkCommand from './unlinkcommand';
 import ManualDecorator from './utils/manualdecorator';
 import findAttributeRange from '@ckeditor/ckeditor5-typing/src/utils/findattributerange';
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import { createLinkElement, ensureSafeUrl, getLocalizedDecorators, normalizeDecorators } from './utils';
 
 import '../theme/link.css';
@@ -453,11 +454,20 @@ export default class LinkEditing extends Plugin {
 		const editor = this.editor;
 		const model = editor.model;
 		const selection = model.document.selection;
+		const view = editor.editing.view;
 		const linkCommand = editor.commands.get( 'link' );
 
 		// A flag whether attributes `linkHref` attribute should be preserved.
 		let shouldPreserveAttributes = false;
 
+		// A flag whether the `Backspace` key was pressed.
+		let hasBackspacePressed = false;
+
+		// Detect pressing `Backspace`.
+		this.listenTo( view.document, 'delete', ( evt, data ) => {
+			hasBackspacePressed = data.domEvent.keyCode === keyCodes.backspace;
+		}, { priority: 'high' } );
+
 		// Before removing the content, check whether the selection is inside a link or at the end of link but with 2-SCM enabled.
 		// If so, we want to preserve link attributes.
 		this.listenTo( model, 'deleteContent', () => {
@@ -480,6 +490,13 @@ export default class LinkEditing extends Plugin {
 
 		// After removing the content, check whether the current selection should preserve the `linkHref` attribute.
 		this.listenTo( model, 'deleteContent', () => {
+			// If didn't press `Backspace`.
+			if ( !hasBackspacePressed ) {
+				return;
+			}
+
+			hasBackspacePressed = false;
+
 			// Disable the mechanism if inside a link (`<$text url="foo">F[]oo</$text>` or <$text url="foo">Foo[]</$text>`).
 			if ( shouldPreserveAttributes ) {
 				return;

+ 17 - 44
packages/ckeditor5-link/tests/linkediting.js

@@ -1448,7 +1448,7 @@ describe( 'LinkEditing', () => {
 			await editor.destroy();
 		} );
 
-		it( 'should not preserve the `linkHref` attribute when deleting content after the link (Backspace check)', () => {
+		it( 'should not preserve the `linkHref` attribute when deleting content after the link', () => {
 			setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text> []</paragraph>' );
 
 			expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
@@ -1469,27 +1469,6 @@ describe( 'LinkEditing', () => {
 			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Ba</$text>[]</paragraph>' );
 		} );
 
-		it( 'should not preserve the `linkHref` attribute when deleting content after the link (Delete check)', () => {
-			setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text>[ ]</paragraph>' );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
-
-			view.document.fire( 'delete', new DomEventData( view.document, {
-				keyCode: keyCodes.delete,
-				preventDefault: () => {}
-			}, { direction: 'forward' } ) );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( false );
-
-			view.document.fire( 'delete', new DomEventData( view.document, {
-				keyCode: keyCodes.backspace,
-				preventDefault: () => {}
-			}, { direction: 'forward' } ) );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( false );
-			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Bar</$text>[]</paragraph>' );
-		} );
-
 		it( 'should not preserve the `linkHref` attribute when deleting content after the link (decorators check)', () => {
 			setModelData( model,
 				'<paragraph>' +
@@ -1555,7 +1534,7 @@ describe( 'LinkEditing', () => {
 			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Ba[]</$text></paragraph>' );
 		} );
 
-		it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link (Backspace)', () => {
+		it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link', () => {
 			setModelData( model, '<paragraph>Foo <$text linkHref="url">A long URLLs[] description</$text></paragraph>' );
 
 			expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
@@ -1576,27 +1555,6 @@ describe( 'LinkEditing', () => {
 			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">A long URL[] description</$text></paragraph>' );
 		} );
 
-		it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link (Delete)', () => {
-			setModelData( model, '<paragraph>Foo <$text linkHref="url">A long URL[]Ls description</$text></paragraph>' );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
-
-			view.document.fire( 'delete', new DomEventData( view.document, {
-				keyCode: keyCodes.delete,
-				preventDefault: () => {}
-			}, { direction: 'forward' } ) );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
-
-			view.document.fire( 'delete', new DomEventData( view.document, {
-				keyCode: keyCodes.delete,
-				preventDefault: () => {}
-			}, { direction: 'forward' } ) );
-
-			expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true );
-			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">A long URL[] description</$text></paragraph>' );
-		} );
-
 		it( 'should do nothing if there is no `linkHref` attribute', () => {
 			setModelData( model, '<paragraph>Foo <$text bold="true">Bolded.</$text> []Bar</paragraph>' );
 
@@ -1612,5 +1570,20 @@ describe( 'LinkEditing', () => {
 
 			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text bold="true">Bolded[]</$text>Bar</paragraph>' );
 		} );
+
+		it( 'should preserve the `linkHref` attribute when deleting content using "Delete" key', () => {
+			setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text>[ ]</paragraph>' );
+
+			expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
+
+			view.document.fire( 'delete', new DomEventData( view.document, {
+				keyCode: keyCodes.delete,
+				preventDefault: () => {}
+			}, { direction: 'forward' } ) );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Bar[]</$text></paragraph>' );
+
+			expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
+		} );
 	} );
 } );