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 #1027 from ckeditor/t/1026
Browse files Browse the repository at this point in the history
Feature: Introduced `view.Document#keyup` event (fired by the `KeyObserver`). Closes #1026.
  • Loading branch information
Reinmar authored Jul 19, 2017
2 parents 8751faa + bccf3f4 commit cc766ab
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/view/observer/keyobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default class KeyObserver extends DomEventObserver {
constructor( document ) {
super( document );

this.domEventType = 'keydown';
this.domEventType = [ 'keydown', 'keyup' ];
}

onDomEvent( domEvt ) {
this.fire( 'keydown', domEvt, {
this.fire( domEvt.type, domEvt, {
keyCode: domEvt.keyCode,

altKey: domEvt.altKey,
Expand Down Expand Up @@ -54,7 +54,22 @@ export default class KeyObserver extends DomEventObserver {
*/

/**
* The value of the {@link module:engine/view/document~Document#event:keydown} event.
* Fired when a key has been released.
*
* Introduced by {@link module:engine/view/observer/keyobserver~KeyObserver}.
*
* Note that because {@link module:engine/view/observer/keyobserver~KeyObserver} is attached by the
* {@link module:engine/view/document~Document}
* this event is available by default.
*
* @see module:engine/view/observer/keyobserver~KeyObserver
* @event module:engine/view/document~Document#event:keyup
* @param {module:engine/view/observer/keyobserver~KeyEventData} keyEventData
*/

/**
* The value of both events - {@link module:engine/view/document~Document#event:keydown} and
* {@link module:engine/view/document~Document#event:keyup}.
*
* @class module:engine/view/observer/keyobserver~KeyEventData
* @extends module:engine/view/observer/domeventdata~DomEventData
Expand Down
1 change: 1 addition & 0 deletions tests/view/manual/keyobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { setData } from '../../../src/dev-utils/view';
const viewDocument = new Document();

viewDocument.on( 'keydown', ( evt, data ) => console.log( 'keydown', data ) );
viewDocument.on( 'keyup', ( evt, data ) => console.log( 'keyup', data ) );

viewDocument.createRoot( document.getElementById( 'editable' ), 'editable' );
setData( viewDocument, 'foo{}bar', { rootName: 'editable' } );
Expand Down
2 changes: 2 additions & 0 deletions tests/view/manual/keyobserver.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
* Expected initialization: `foo{}bar`.
* Press some keys - nothing should be added to editor's contents.
* When press some key - event `keydown` should be logged. When the key is released - event `keyup` should be logged.
* You can hold the key in order to check whether `keydown` event is fired multiple times. After releasing the key, `keyup` event should be fired once.
* Check whether key events are logged to the console with proper data:
* `keyCode`,
* `altKey`,
Expand Down
54 changes: 50 additions & 4 deletions tests/view/observer/keyobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe( 'KeyObserver', () => {
} );

it( 'should define domEventType', () => {
expect( observer.domEventType ).to.equal( 'keydown' );
expect( observer.domEventType ).to.contains( 'keydown' );
expect( observer.domEventType ).to.contains( 'keyup' );
} );

describe( 'onDomEvent', () => {
Expand All @@ -31,7 +32,15 @@ describe( 'KeyObserver', () => {

viewDocument.on( 'keydown', spy );

observer.onDomEvent( { target: document.body, keyCode: 111, altKey: false, ctrlKey: false, metaKey: false, shiftKey: false } );
observer.onDomEvent( {
type: 'keydown',
target: document.body,
keyCode: 111,
altKey: false,
ctrlKey: false,
metaKey: false,
shiftKey: false
} );

expect( spy.calledOnce ).to.be.true;

Expand All @@ -52,7 +61,15 @@ describe( 'KeyObserver', () => {

viewDocument.on( 'keydown', spy );

observer.onDomEvent( { target: document.body, keyCode: 111, altKey: true, ctrlKey: true, metaKey: false, shiftKey: true } );
observer.onDomEvent( {
type: 'keydown',
target: document.body,
keyCode: 111,
altKey: true,
ctrlKey: true,
metaKey: false,
shiftKey: true
} );

const data = spy.args[ 0 ][ 1 ];
expect( data ).to.have.property( 'keyCode', 111 );
Expand All @@ -70,10 +87,39 @@ describe( 'KeyObserver', () => {

viewDocument.on( 'keydown', spy );

observer.onDomEvent( { target: document.body, keyCode: 111, metaKey: true } );
observer.onDomEvent( { type: 'keydown', target: document.body, keyCode: 111, metaKey: true } );

const data = spy.args[ 0 ][ 1 ];
expect( data ).to.have.property( 'ctrlKey', true );
} );

it( 'should fire keyup with the target and key info', () => {
const spy = sinon.spy();

viewDocument.on( 'keyup', spy );

observer.onDomEvent( {
type: 'keyup',
target: document.body,
keyCode: 111,
altKey: false,
ctrlKey: false,
metaKey: false,
shiftKey: false
} );

expect( spy.calledOnce ).to.be.true;

const data = spy.args[ 0 ][ 1 ];
expect( data ).to.have.property( 'domTarget', document.body );
expect( data ).to.have.property( 'keyCode', 111 );
expect( data ).to.have.property( 'altKey', false );
expect( data ).to.have.property( 'ctrlKey', false );
expect( data ).to.have.property( 'shiftKey', false );
expect( data ).to.have.property( 'keystroke', getCode( data ) );

// Just to be sure.
expect( getCode( data ) ).to.equal( 111 );
} );
} );
} );

0 comments on commit cc766ab

Please sign in to comment.