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

t/206: Long keystrokes should be handled properly by getEnvKeystrokeText #207

Merged
merged 1 commit into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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