Преглед на файлове

Merge branch 'master' into t/19

Piotrek Koszuliński преди 9 години
родител
ревизия
09d52007c4

+ 1 - 1
packages/ckeditor5-utils/gulpfile.js

@@ -19,7 +19,7 @@ const config = {
 	]
 };
 
-const ckeditor5Lint = require( 'ckeditor5-dev-lint' )( config );
+const ckeditor5Lint = require( '@ckeditor/ckeditor5-dev-lint' )( config );
 
 require( './dev/tasks/lodash/tasks' )();
 

+ 1 - 1
packages/ckeditor5-utils/package.json

@@ -7,7 +7,7 @@
   ],
   "dependencies": {},
   "devDependencies": {
-    "ckeditor5-dev-lint": "github:ckeditor/ckeditor5-dev-lint",
+    "@ckeditor/ckeditor5-dev-lint": "^1.0.1",
     "del": "^2.2.0",
     "gulp": "^3.9.0",
     "guppy-pre-commit": "^0.3.0",

+ 1 - 1
packages/ckeditor5-utils/src/emittermixin.js

@@ -296,7 +296,7 @@ const EmitterMixin = {
 	 *		emitterA.fire( 'eventY', data );
 	 *
 	 * @method utils.EmitterMixin#delegate
-	 * @param {String...} events Event names that will be delegated to another emitter.
+	 * @param {...String} events Event names that will be delegated to another emitter.
 	 * @returns {utils.EmitterMixin.delegate#to}
 	 */
 	delegate( ...events ) {

+ 33 - 0
packages/ckeditor5-utils/src/env.js

@@ -0,0 +1,33 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals navigator:false */
+
+const userAgent = navigator.userAgent.toLowerCase();
+
+/**
+ * A namespace containing environment and browser information.
+ *
+ * @namespace utils.env
+ */
+export default {
+	/**
+	 * Indicates that application is running on Macintosh.
+	 *
+	 * @member {Boolean} utils.env.mac
+	 */
+	mac: isMac( userAgent )
+};
+
+/**
+ * Checks if User Agent represented by the string is running on Macintosh.
+ *
+ * @function utils.env.isMac
+ * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
+ * @returns {Boolean} Whether User Agent is running on Macintosh or not.
+ */
+export function isMac( userAgent ) {
+	return userAgent.indexOf( 'macintosh' ) > -1;
+}

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

@@ -4,6 +4,7 @@
  */
 
 import CKEditorError from './ckeditorerror.js';
+import env from './env.js';
 
 /**
  * Set of utils related to keyboard support.
@@ -82,7 +83,7 @@ export function getCode( key ) {
  */
 export function parseKeystroke( keystroke ) {
 	if ( typeof keystroke == 'string' ) {
-		keystroke = keystroke.split( /\s*\+\s*/ );
+		keystroke = splitKeystrokeText( keystroke );
 	}
 
 	return keystroke
@@ -90,6 +91,26 @@ export function parseKeystroke( keystroke ) {
 		.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() {
 	const keyCodes = {
 		arrowleft: 37,
@@ -125,6 +146,10 @@ function generateKnownKeyCodes() {
 	return keyCodes;
 }
 
+function splitKeystrokeText( keystroke ) {
+	return keystroke.split( /\s*\+\s*/ );
+}
+
 /**
  * Information about a keystroke.
  *

+ 2 - 2
packages/ckeditor5-utils/src/observablemixin.js

@@ -107,7 +107,7 @@ const ObservableMixin = {
 	 *		A.bind( 'a' ).to( B, 'b', C, 'd', ( b, d ) => b + d );
 	 *
 	 * @method utils.ObservableMixin#bind
-	 * @param {String...} bindAttrs Observable attributes that will be bound to another observable(s).
+	 * @param {...String} bindAttrs Observable attributes that will be bound to another observable(s).
 	 * @returns {utils.BindChain}
 	 */
 	bind( ...bindAttrs ) {
@@ -188,7 +188,7 @@ const ObservableMixin = {
 	 *		A.unbind();
 	 *
 	 * @method utils.ObservableMixin#unbind
-	 * @param {String} [unbindAttrs] Observable attributes to be unbound. All the bindings will
+	 * @param {...String} [unbindAttrs] Observable attributes to be unbound. All the bindings will
 	 * be released if no attributes provided.
 	 */
 	unbind( ...unbindAttrs ) {

+ 34 - 0
packages/ckeditor5-utils/tests/env.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import env, { isMac } from '/ckeditor5/utils/env.js';
+
+describe( 'Env', () => {
+	beforeEach( () => {
+	} );
+
+	it( 'is an object', () => {
+		expect( env ).to.be.an( 'object' );
+	} );
+
+	describe( 'mac', () => {
+		it( 'is a boolean', () => {
+			expect( env.mac ).to.be.a( 'boolean' );
+		} );
+	} );
+
+	describe( 'isMac', () => {
+		it( 'returns true for macintosh UA strings', () => {
+			expect( isMac( 'macintosh' ) ).to.be.true;
+			expect( isMac( 'foo macintosh bar' ) ).to.be.true;
+		} );
+
+		it( 'returns false for non–macintosh UA strings', () => {
+			expect( isMac( '' ) ).to.be.false;
+			expect( isMac( 'mac' ) ).to.be.false;
+			expect( isMac( 'foo' ) ).to.be.false;
+		} );
+	} );
+} );

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

@@ -3,7 +3,8 @@
  * 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';
 
 describe( 'Keyboard', () => {
@@ -87,4 +88,42 @@ describe( 'Keyboard', () => {
 			} ).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' );
+			} );
+		} );
+	} );
 } );