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 #207 from ckeditor/t/206
Browse files Browse the repository at this point in the history
Fix: Long keystrokes should be handled properly by getEnvKeystrokeText on Mac. Added support for ⇧ and ⌥ modifiers. Closes #206.
  • Loading branch information
szymonkups authored Nov 23, 2017
2 parents 6a5848a + 5fda7a7 commit d8443e2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
import CKEditorError from './ckeditorerror';
import env from './env';

const macGlyphsToModifiers = {
'⌘': 'ctrl',
'⇧': 'shift',
'⌥': 'alt'
};

const modifiersToMacGlyphs = {
'ctrl': '⌘',
'shift': '⇧',
'alt': '⌥'
};

/**
* Object with `keyName => keyCode` pairs for a set of known keys.
*
Expand Down Expand Up @@ -96,15 +108,22 @@ export function parseKeystroke( keystroke ) {
* @returns {String} Keystroke text specific for the environment.
*/
export function getEnvKeystrokeText( keystroke ) {
const split = splitKeystrokeText( keystroke );

if ( env.mac ) {
if ( split[ 0 ].toLowerCase() == 'ctrl' ) {
return '⌘' + ( split[ 1 ] || '' );
}
if ( !env.mac ) {
return keystroke;
}

return keystroke;
return splitKeystrokeText( keystroke )
// Replace modifiers (e.g. "ctrl") with Mac glyphs (e.g. "⌘") first.
.map( key => modifiersToMacGlyphs[ key.toLowerCase() ] || key )

// Decide whether to put "+" between keys in the keystroke or not.
.reduce( ( value, key ) => {
if ( value.slice( -1 ) in macGlyphsToModifiers ) {
return value + key;
} else {
return value + '+' + key;
}
} );
}

function generateKnownKeyCodes() {
Expand Down
23 changes: 22 additions & 1 deletion tests/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,28 @@ describe( 'Keyboard', () => {
expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
} );

it( 'replaces SHIFT with ⇧', () => {
expect( getEnvKeystrokeText( 'SHIFT' ) ).to.equal( '⇧' );
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( '⇧A' );
expect( getEnvKeystrokeText( 'shift+A' ) ).to.equal( '⇧A' );
} );

it( 'replaces ALT with ⌥', () => {
expect( getEnvKeystrokeText( 'ALT' ) ).to.equal( '⌥' );
expect( getEnvKeystrokeText( 'ALT+A' ) ).to.equal( '⌥A' );
expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( '⌥A' );
} );

it( 'work for multiple modifiers', () => {
expect( getEnvKeystrokeText( 'CTRL+SHIFT+X' ) ).to.equal( '⌘⇧X' );
expect( getEnvKeystrokeText( 'ALT+SHIFT+X' ) ).to.equal( '⌥⇧X' );
} );

it( 'does not touch other keys', () => {
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
expect( getEnvKeystrokeText( 'ESC+A' ) ).to.equal( 'ESC+A' );
expect( getEnvKeystrokeText( 'TAB' ) ).to.equal( 'TAB' );
expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
expect( getEnvKeystrokeText( 'A+CTRL+B' ) ).to.equal( 'A+⌘B' );
} );
} );

Expand All @@ -135,6 +154,8 @@ describe( 'Keyboard', () => {
expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( 'alt+A' );
expect( getEnvKeystrokeText( 'CTRL+SHIFT+A' ) ).to.equal( 'CTRL+SHIFT+A' );
expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
} );
} );
Expand Down

0 comments on commit d8443e2

Please sign in to comment.