From 5fda7a7e75396412dbc2545bea632f5b1e3a053a Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Wed, 22 Nov 2017 17:45:50 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20Long=20keystrokes=20should=20be=20handle?= =?UTF-8?q?d=20properly=20by=20getEnvKeystrokeText=20on=20Mac.=20Added=20s?= =?UTF-8?q?upport=20for=20=E2=87=A7=20and=20=E2=8C=A5=20modifiers.=20Close?= =?UTF-8?q?s=20#206.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/keyboard.js | 33 ++++++++++++++++++++++++++------- tests/keyboard.js | 23 ++++++++++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/keyboard.js b/src/keyboard.js index 4bd954f..9153f27 100644 --- a/src/keyboard.js +++ b/src/keyboard.js @@ -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. * @@ -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() { diff --git a/tests/keyboard.js b/tests/keyboard.js index 82c337a..1a96fa4 100644 --- a/tests/keyboard.js +++ b/tests/keyboard.js @@ -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' ); } ); } ); @@ -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' ); } ); } );