Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

DeleteObserver cancells keydown event when delete event is cancelled. #133

Merged
merged 2 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/deleteobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ export default class DeleteObserver extends Observer {
deleteData.unit = data.altKey ? 'word' : deleteData.unit;
deleteData.sequence = ++sequence;

// Save the event object to check later if it was stopped or not.
let event;
document.once( 'delete', evt => ( event = evt ), { priority: 'highest' } );

document.fire( 'delete', new DomEventData( document, data.domEvent, deleteData ) );

// Stop `keydown` event if `delete` event was stopped.
// https://github.com/ckeditor/ckeditor5/issues/753
if ( event && event.stop.called ) {
evt.stop();
}
} );
}

Expand Down
24 changes: 24 additions & 0 deletions tests/deleteobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ describe( 'DeleteObserver', () => {
expect( spy.args[ 0 ][ 1 ] ).to.have.property( 'sequence', 1 );
expect( spy.args[ 1 ][ 1 ] ).to.have.property( 'sequence', 2 );
} );

it( 'should stop keydown event when delete event is stopped', () => {
const keydownSpy = sinon.spy();
viewDocument.on( 'keydown', keydownSpy );
viewDocument.on( 'delete', evt => evt.stop() );

viewDocument.fire( 'keydown', new DomEventData( viewDocument, getDomEvent(), {
keyCode: getCode( 'delete' )
} ) );

sinon.assert.notCalled( keydownSpy );
} );

it( 'should not stop keydown event when delete event is not stopped', () => {
const keydownSpy = sinon.spy();
viewDocument.on( 'keydown', keydownSpy );
viewDocument.on( 'delete', evt => evt.stop() );

viewDocument.fire( 'keydown', new DomEventData( viewDocument, getDomEvent(), {
keyCode: getCode( 'x' )
} ) );

sinon.assert.calledOnce( keydownSpy );
} );
} );

function getDomEvent() {
Expand Down