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

Commit d8443e2

Browse files
authored
Merge pull request #207 from ckeditor/t/206
Fix: Long keystrokes should be handled properly by getEnvKeystrokeText on Mac. Added support for ⇧ and ⌥ modifiers. Closes #206.
2 parents 6a5848a + 5fda7a7 commit d8443e2

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/keyboard.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
import CKEditorError from './ckeditorerror';
1313
import env from './env';
1414

15+
const macGlyphsToModifiers = {
16+
'⌘': 'ctrl',
17+
'⇧': 'shift',
18+
'⌥': 'alt'
19+
};
20+
21+
const modifiersToMacGlyphs = {
22+
'ctrl': '⌘',
23+
'shift': '⇧',
24+
'alt': '⌥'
25+
};
26+
1527
/**
1628
* Object with `keyName => keyCode` pairs for a set of known keys.
1729
*
@@ -96,15 +108,22 @@ export function parseKeystroke( keystroke ) {
96108
* @returns {String} Keystroke text specific for the environment.
97109
*/
98110
export function getEnvKeystrokeText( keystroke ) {
99-
const split = splitKeystrokeText( keystroke );
100-
101-
if ( env.mac ) {
102-
if ( split[ 0 ].toLowerCase() == 'ctrl' ) {
103-
return '⌘' + ( split[ 1 ] || '' );
104-
}
111+
if ( !env.mac ) {
112+
return keystroke;
105113
}
106114

107-
return keystroke;
115+
return splitKeystrokeText( keystroke )
116+
// Replace modifiers (e.g. "ctrl") with Mac glyphs (e.g. "⌘") first.
117+
.map( key => modifiersToMacGlyphs[ key.toLowerCase() ] || key )
118+
119+
// Decide whether to put "+" between keys in the keystroke or not.
120+
.reduce( ( value, key ) => {
121+
if ( value.slice( -1 ) in macGlyphsToModifiers ) {
122+
return value + key;
123+
} else {
124+
return value + '+' + key;
125+
}
126+
} );
108127
}
109128

110129
function generateKnownKeyCodes() {

tests/keyboard.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,28 @@ describe( 'Keyboard', () => {
120120
expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
121121
} );
122122

123+
it( 'replaces SHIFT with ⇧', () => {
124+
expect( getEnvKeystrokeText( 'SHIFT' ) ).to.equal( '⇧' );
125+
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( '⇧A' );
126+
expect( getEnvKeystrokeText( 'shift+A' ) ).to.equal( '⇧A' );
127+
} );
128+
129+
it( 'replaces ALT with ⌥', () => {
130+
expect( getEnvKeystrokeText( 'ALT' ) ).to.equal( '⌥' );
131+
expect( getEnvKeystrokeText( 'ALT+A' ) ).to.equal( '⌥A' );
132+
expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( '⌥A' );
133+
} );
134+
135+
it( 'work for multiple modifiers', () => {
136+
expect( getEnvKeystrokeText( 'CTRL+SHIFT+X' ) ).to.equal( '⌘⇧X' );
137+
expect( getEnvKeystrokeText( 'ALT+SHIFT+X' ) ).to.equal( '⌥⇧X' );
138+
} );
139+
123140
it( 'does not touch other keys', () => {
124-
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
141+
expect( getEnvKeystrokeText( 'ESC+A' ) ).to.equal( 'ESC+A' );
142+
expect( getEnvKeystrokeText( 'TAB' ) ).to.equal( 'TAB' );
125143
expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
144+
expect( getEnvKeystrokeText( 'A+CTRL+B' ) ).to.equal( 'A+⌘B' );
126145
} );
127146
} );
128147

@@ -135,6 +154,8 @@ describe( 'Keyboard', () => {
135154
expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
136155
expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
137156
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
157+
expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( 'alt+A' );
158+
expect( getEnvKeystrokeText( 'CTRL+SHIFT+A' ) ).to.equal( 'CTRL+SHIFT+A' );
138159
expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
139160
} );
140161
} );

0 commit comments

Comments
 (0)