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 #138 from ckeditor/t/92
Browse files Browse the repository at this point in the history
Fix: Properly discover delete-word keyboard modifier on mac and non-mac computers. Closes #92.
  • Loading branch information
Reinmar authored Feb 20, 2018
2 parents 9aabd1b + 20b1d84 commit 81f5b76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/deleteobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import Observer from '@ckeditor/ckeditor5-engine/src/view/observer/observer';
import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import env from '@ckeditor/ckeditor5-utils/src/env';

/**
* Delete observer introduces the {@link module:engine/view/document~Document#event:delete} event.
Expand Down Expand Up @@ -42,7 +43,8 @@ export default class DeleteObserver extends Observer {
return;
}

deleteData.unit = data.altKey ? 'word' : deleteData.unit;
const hasWordModifier = env.isMac ? data.altKey : data.ctrlKey;
deleteData.unit = hasWordModifier ? 'word' : deleteData.unit;
deleteData.sequence = ++sequence;

// Save the event object to check later if it was stopped or not.
Expand Down
29 changes: 28 additions & 1 deletion tests/deleteobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import View from '@ckeditor/ckeditor5-engine/src/view/view';
import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
import createViewRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot';
import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
import env from '@ckeditor/ckeditor5-utils/src/env';

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

testUtils.createSinonSandbox();

describe( 'DeleteObserver', () => {
let view, viewDocument;
Expand Down Expand Up @@ -50,9 +55,11 @@ describe( 'DeleteObserver', () => {
expect( data ).to.have.property( 'sequence', 1 );
} );

it( 'is fired with a proper direction and unit', () => {
it( 'is fired with a proper direction and unit (on Mac)', () => {
const spy = sinon.spy();

testUtils.sinon.stub( env, 'isMac' ).value( true );

viewDocument.on( 'delete', spy );

viewDocument.fire( 'keydown', new DomEventData( viewDocument, getDomEvent(), {
Expand All @@ -68,6 +75,26 @@ describe( 'DeleteObserver', () => {
expect( data ).to.have.property( 'sequence', 1 );
} );

it( 'is fired with a proper direction and unit (on non-Mac)', () => {
const spy = sinon.spy();

testUtils.sinon.stub( env, 'isMac' ).value( false );

viewDocument.on( 'delete', spy );

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

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

const data = spy.args[ 0 ][ 1 ];
expect( data ).to.have.property( 'direction', 'backward' );
expect( data ).to.have.property( 'unit', 'word' );
expect( data ).to.have.property( 'sequence', 1 );
} );

it( 'is not fired on keydown when keyCode does not match backspace or delete', () => {
const spy = sinon.spy();

Expand Down

0 comments on commit 81f5b76

Please sign in to comment.