Skip to content

Commit

Permalink
Improved the mechanism. Now it will be triggered by Delete key as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Jul 23, 2020
1 parent 929409f commit 7ddf430
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
17 changes: 0 additions & 17 deletions packages/ckeditor5-link/src/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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</$text>` or <$text url="foo">Foo[]</$text>`).
if ( shouldPreserveAttributes ) {
return;
Expand Down
61 changes: 44 additions & 17 deletions packages/ckeditor5-link/tests/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<paragraph>Foo <$text linkHref="url">Bar</$text> []</paragraph>' );

expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
Expand All @@ -1469,6 +1469,27 @@ 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>' +
Expand Down Expand Up @@ -1534,7 +1555,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', () => {
it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link (Backspace)', () => {
setModelData( model, '<paragraph>Foo <$text linkHref="url">A long URLLs[] description</$text></paragraph>' );

expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
Expand All @@ -1555,35 +1576,41 @@ describe( 'LinkEditing', () => {
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>' );
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.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( '<paragraph>Foo <$text bold="true">Bolded[]</$text>Bar</paragraph>' );
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 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 );
it( 'should do nothing if there is no `linkHref` attribute', () => {
setModelData( model, '<paragraph>Foo <$text bold="true">Bolded.</$text> []Bar</paragraph>' );

view.document.fire( 'delete', new DomEventData( view.document, {
keyCode: keyCodes.delete,
keyCode: keyCodes.backspace,
preventDefault: () => {}
}, { direction: 'forward' } ) );
} ) );

expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Bar[]</$text></paragraph>' );
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( '<paragraph>Foo <$text bold="true">Bolded[]</$text>Bar</paragraph>' );
} );
} );
} );

0 comments on commit 7ddf430

Please sign in to comment.