From 7ddf430b5091ec07d4e5882a9c9a23322848c2d8 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 23 Jul 2020 08:33:58 +0200 Subject: [PATCH] Improved the mechanism. Now it will be triggered by Delete key as well. --- packages/ckeditor5-link/src/linkediting.js | 17 ------ packages/ckeditor5-link/tests/linkediting.js | 61 ++++++++++++++------ 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/packages/ckeditor5-link/src/linkediting.js b/packages/ckeditor5-link/src/linkediting.js index d3043545862..bfe785c9801 100644 --- a/packages/ckeditor5-link/src/linkediting.js +++ b/packages/ckeditor5-link/src/linkediting.js @@ -17,7 +17,6 @@ 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'; @@ -454,20 +453,11 @@ 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', () => { @@ -490,13 +480,6 @@ 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` or <$text url="foo">Foo[]`). if ( shouldPreserveAttributes ) { return; diff --git a/packages/ckeditor5-link/tests/linkediting.js b/packages/ckeditor5-link/tests/linkediting.js index 7d0dc37c65f..6ad54e880ea 100644 --- a/packages/ckeditor5-link/tests/linkediting.js +++ b/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', () => { + it( 'should not preserve the `linkHref` attribute when deleting content after the link (Backspace check)', () => { setModelData( model, 'Foo <$text linkHref="url">Bar []' ); expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false ); @@ -1469,6 +1469,27 @@ describe( 'LinkEditing', () => { expect( getModelData( model ) ).to.equal( 'Foo <$text linkHref="url">Ba[]' ); } ); + it( 'should not preserve the `linkHref` attribute when deleting content after the link (Delete check)', () => { + setModelData( model, 'Foo <$text linkHref="url">Bar[ ]' ); + + 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( 'Foo <$text linkHref="url">Bar[]' ); + } ); + it( 'should not preserve the `linkHref` attribute when deleting content after the link (decorators check)', () => { setModelData( model, '' + @@ -1534,7 +1555,7 @@ describe( 'LinkEditing', () => { expect( getModelData( model ) ).to.equal( 'Foo <$text linkHref="url">Ba[]' ); } ); - it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link', () => { + it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link (Backspace)', () => { setModelData( model, 'Foo <$text linkHref="url">A long URLLs[] description' ); expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true ); @@ -1555,35 +1576,41 @@ describe( 'LinkEditing', () => { expect( getModelData( model ) ).to.equal( 'Foo <$text linkHref="url">A long URL[] description' ); } ); - it( 'should do nothing if there is no `linkHref` attribute', () => { - setModelData( model, 'Foo <$text bold="true">Bolded. []Bar' ); + it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link (Delete)', () => { + setModelData( model, 'Foo <$text linkHref="url">A long URL[]Ls description' ); + + expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true ); view.document.fire( 'delete', new DomEventData( view.document, { - keyCode: keyCodes.backspace, + 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.backspace, + keyCode: keyCodes.delete, preventDefault: () => {} - } ) ); + }, { direction: 'forward' } ) ); - expect( getModelData( model ) ).to.equal( 'Foo <$text bold="true">Bolded[]Bar' ); + expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true ); + expect( getModelData( model ) ).to.equal( 'Foo <$text linkHref="url">A long URL[] description' ); } ); - it( 'should preserve the `linkHref` attribute when deleting content using "Delete" key', () => { - setModelData( model, 'Foo <$text linkHref="url">Bar[ ]' ); - - expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false ); + it( 'should do nothing if there is no `linkHref` attribute', () => { + setModelData( model, 'Foo <$text bold="true">Bolded. []Bar' ); view.document.fire( 'delete', new DomEventData( view.document, { - keyCode: keyCodes.delete, + keyCode: keyCodes.backspace, preventDefault: () => {} - }, { direction: 'forward' } ) ); + } ) ); - expect( getModelData( model ) ).to.equal( 'Foo <$text linkHref="url">Bar[]' ); + view.document.fire( 'delete', new DomEventData( view.document, { + keyCode: keyCodes.backspace, + preventDefault: () => {} + } ) ); - expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true ); + expect( getModelData( model ) ).to.equal( 'Foo <$text bold="true">Bolded[]Bar' ); } ); } ); } );