Procházet zdrojové kódy

Implemented utils.keyboard.getEnvKeystrokeText helper to translate keystroke strings depending on the environment.

Aleksander Nowodzinski před 9 roky
rodič
revize
38bd958a3f

+ 26 - 1
packages/ckeditor5-utils/src/keyboard.js

@@ -4,6 +4,7 @@
  */
  */
 
 
 import CKEditorError from './ckeditorerror.js';
 import CKEditorError from './ckeditorerror.js';
+import env from './env.js';
 
 
 /**
 /**
  * Set of utils related to keyboard support.
  * Set of utils related to keyboard support.
@@ -82,7 +83,7 @@ export function getCode( key ) {
  */
  */
 export function parseKeystroke( keystroke ) {
 export function parseKeystroke( keystroke ) {
 	if ( typeof keystroke == 'string' ) {
 	if ( typeof keystroke == 'string' ) {
-		keystroke = keystroke.split( /\s*\+\s*/ );
+		keystroke = splitKeystrokeText( keystroke );
 	}
 	}
 
 
 	return keystroke
 	return keystroke
@@ -90,6 +91,26 @@ export function parseKeystroke( keystroke ) {
 		.reduce( ( key, sum ) => sum + key, 0 );
 		.reduce( ( key, sum ) => sum + key, 0 );
 }
 }
 
 
+/**
+ * It translates any keystroke string text like `"CTRL+A"` to an
+ * environment–specific keystroke, i.e. `"⌘A"` on Mac OSX.
+ *
+ * @method utils.keyboard.getEnvKeystrokeText
+ * @param {String} keystroke Keystroke text.
+ * @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 ] || '' );
+		}
+	}
+
+	return keystroke;
+}
+
 function generateKnownKeyCodes() {
 function generateKnownKeyCodes() {
 	const keyCodes = {
 	const keyCodes = {
 		arrowleft: 37,
 		arrowleft: 37,
@@ -125,6 +146,10 @@ function generateKnownKeyCodes() {
 	return keyCodes;
 	return keyCodes;
 }
 }
 
 
+function splitKeystrokeText( keystroke ) {
+	return keystroke.split( /\s*\+\s*/ );
+}
+
 /**
 /**
  * Information about a keystroke.
  * Information about a keystroke.
  *
  *

+ 40 - 1
packages/ckeditor5-utils/tests/keyboard.js

@@ -3,7 +3,8 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
-import { keyCodes, getCode, parseKeystroke } from '/ckeditor5/utils/keyboard.js';
+import env from '/ckeditor5/utils/env.js';
+import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from '/ckeditor5/utils/keyboard.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 
 
 describe( 'Keyboard', () => {
 describe( 'Keyboard', () => {
@@ -87,4 +88,42 @@ describe( 'Keyboard', () => {
 			} ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
 			} ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
 		} );
 		} );
 	} );
 	} );
+
+	describe( 'getEnvKeystrokeText', () => {
+		let initialEnvMac = env.mac;
+
+		afterEach( () => {
+			env.mac = initialEnvMac;
+		} );
+
+		describe( 'on Macintosh', () => {
+			beforeEach( () => {
+				env.mac = true;
+			} );
+
+			it( 'replaces CTRL with ⌘', () => {
+				expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
+				expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
+				expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
+			} );
+
+			it( 'does not touch other keys', () => {
+				expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
+				expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
+			} );
+		} );
+
+		describe( 'on non–Macintosh', () => {
+			beforeEach( () => {
+				env.mac = false;
+			} );
+
+			it( 'does not touch anything', () => {
+				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( 'A' ) ).to.equal( 'A' );
+			} );
+		} );
+	} );
 } );
 } );