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

Commit

Permalink
Merge pull request #51 from ckeditor/t/ckeditor5/753
Browse files Browse the repository at this point in the history
Fix: `EnterObserver` will stop the `keydown` event when the `enter` event is stopped. Closes: ckeditor/ckeditor5#753.
  • Loading branch information
Reinmar authored Jan 16, 2018
2 parents a219eb5 + b314548 commit b9a7a1e
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 @@ -51,6 +51,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 b9a7a1e

Please sign in to comment.