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

Commit

Permalink
EnterObserver stops keydown event if enter event is stopped.
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkups committed Jan 4, 2018
1 parent 05d4265 commit fc422b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/enterobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ export default class EnterObserver extends Observer {

document.on( 'keydown', ( evt, data ) => {
if ( this.isEnabled && data.keyCode == keyCodes.enter ) {
// Save the event object to check later if it was stopped or not.
let event;
document.once( 'enter', evt => ( event = evt ), { priority: 'highest' } );

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

// Stop `keydown` event if `enter` 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/enterobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ describe( 'EnterObserver', () => {

expect( spy.calledOnce ).to.be.false;
} );

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

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

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

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

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

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

function getDomEvent() {
Expand Down

0 comments on commit fc422b5

Please sign in to comment.