@ckeditor/ckeditor5-utils
Advanced tools
Comparing version 25.0.0 to 26.0.0
{ | ||
"name": "@ckeditor/ckeditor5-utils", | ||
"version": "25.0.0", | ||
"version": "26.0.0", | ||
"description": "Miscellaneous utils used by CKEditor 5.", | ||
@@ -9,4 +9,6 @@ "keywords": [ | ||
"ckeditor 5", | ||
"ckeditor5-lib" | ||
"ckeditor5-lib", | ||
"ckeditor5-dll" | ||
], | ||
"main": "src/index.js", | ||
"dependencies": { | ||
@@ -16,6 +18,6 @@ "lodash-es": "^4.17.15" | ||
"devDependencies": { | ||
"@ckeditor/ckeditor5-build-classic": "^25.0.0", | ||
"@ckeditor/ckeditor5-editor-classic": "^25.0.0", | ||
"@ckeditor/ckeditor5-core": "^25.0.0", | ||
"@ckeditor/ckeditor5-engine": "^25.0.0", | ||
"@ckeditor/ckeditor5-build-classic": "^26.0.0", | ||
"@ckeditor/ckeditor5-editor-classic": "^26.0.0", | ||
"@ckeditor/ckeditor5-core": "^26.0.0", | ||
"@ckeditor/ckeditor5-engine": "^26.0.0", | ||
"assertion-error": "^1.1.0", | ||
@@ -22,0 +24,0 @@ "js-beautify": "^1.11.0" |
@@ -7,3 +7,3 @@ /** | ||
/** | ||
* Set of utils related to keyboard support. | ||
* A set of utilities related to keyboard support. | ||
* | ||
@@ -16,16 +16,17 @@ * @module utils/keyboard | ||
const macGlyphsToModifiers = { | ||
'⌘': 'ctrl', | ||
'⇧': 'shift', | ||
'⌥': 'alt' | ||
const modifiersToGlyphsMac = { | ||
ctrl: '⌃', | ||
cmd: '⌘', | ||
alt: '⌥', | ||
shift: '⇧' | ||
}; | ||
const modifiersToMacGlyphs = { | ||
'ctrl': '⌘', | ||
'shift': '⇧', | ||
'alt': '⌥' | ||
const modifiersToGlyphsNonMac = { | ||
ctrl: 'Ctrl+', | ||
alt: 'Alt+', | ||
shift: 'Shift+' | ||
}; | ||
/** | ||
* Object with `keyName => keyCode` pairs for a set of known keys. | ||
* An object with `keyName => keyCode` pairs for a set of known keys. | ||
* | ||
@@ -43,8 +44,12 @@ * Contains: | ||
const keyCodeNames = Object.fromEntries( | ||
Object.entries( keyCodes ).map( ( [ name, code ] ) => [ code, name.charAt( 0 ).toUpperCase() + name.slice( 1 ) ] ) | ||
); | ||
/** | ||
* Converts a key name or a {@link module:utils/keyboard~KeystrokeInfo keystroke info} into a key code. | ||
* Converts a key name or {@link module:utils/keyboard~KeystrokeInfo keystroke info} into a key code. | ||
* | ||
* Note: Key names are matched with {@link module:utils/keyboard~keyCodes} in a case-insensitive way. | ||
* | ||
* @param {String|module:utils/keyboard~KeystrokeInfo} Key name (see {@link module:utils/keyboard~keyCodes}) | ||
* @param {String|module:utils/keyboard~KeystrokeInfo} A key name (see {@link module:utils/keyboard~keyCodes}) | ||
* or a keystroke data object. | ||
@@ -61,3 +66,3 @@ * @returns {Number} Key or keystroke code. | ||
/** | ||
* Unknown key name. Only key names contained by the {@link module:utils/keyboard~keyCodes} can be used. | ||
* Unknown key name. Only key names included in the {@link module:utils/keyboard~keyCodes} can be used. | ||
* | ||
@@ -73,3 +78,4 @@ * @error keyboard-unknown-key | ||
( key.ctrlKey ? keyCodes.ctrl : 0 ) + | ||
( key.shiftKey ? keyCodes.shift : 0 ); | ||
( key.shiftKey ? keyCodes.shift : 0 ) + | ||
( key.metaKey ? keyCodes.cmd : 0 ); | ||
} | ||
@@ -81,4 +87,4 @@ | ||
/** | ||
* Parses keystroke and returns a keystroke code that will match the code returned by | ||
* link {@link module:utils/keyboard~getCode} for a corresponding {@link module:utils/keyboard~KeystrokeInfo keystroke info}. | ||
* Parses the keystroke and returns a keystroke code that will match the code returned by | ||
* {@link module:utils/keyboard~getCode} for the corresponding {@link module:utils/keyboard~KeystrokeInfo keystroke info}. | ||
* | ||
@@ -96,3 +102,7 @@ * The keystroke can be passed in two formats: | ||
* | ||
* @param {String|Array.<Number|String>} keystroke Keystroke definition. | ||
* Note: On macOS, keystroke handling is translating the `Ctrl` key to the `Cmd` key and handling only that keystroke. | ||
* For example, a registered keystroke `Ctrl+A` will be translated to `Cmd+A` on macOS. To disable the translation of some keystroke, | ||
* use the forced modifier: `Ctrl!+A` (note the exclamation mark). | ||
* | ||
* @param {String|Array.<Number|String>} keystroke The keystroke definition. | ||
* @returns {Number} Keystroke code. | ||
@@ -106,3 +116,3 @@ */ | ||
return keystroke | ||
.map( key => ( typeof key == 'string' ) ? getCode( key ) : key ) | ||
.map( key => ( typeof key == 'string' ) ? getEnvKeyCode( key ) : key ) | ||
.reduce( ( key, sum ) => sum + key, 0 ); | ||
@@ -112,25 +122,24 @@ } | ||
/** | ||
* It translates any keystroke string text like `"CTRL+A"` to an | ||
* environment–specific keystroke, i.e. `"⌘A"` on Mac OSX. | ||
* Translates any keystroke string text like `"Ctrl+A"` to an | ||
* environment–specific keystroke, i.e. `"⌘A"` on macOS. | ||
* | ||
* @param {String} keystroke Keystroke text. | ||
* @returns {String} Keystroke text specific for the environment. | ||
* @param {String} keystroke The keystroke text. | ||
* @returns {String} The keystroke text specific for the environment. | ||
*/ | ||
export function getEnvKeystrokeText( keystroke ) { | ||
if ( !env.isMac ) { | ||
return keystroke; | ||
} | ||
let keystrokeCode = parseKeystroke( keystroke ); | ||
return splitKeystrokeText( keystroke ) | ||
// Replace modifiers (e.g. "ctrl") with Mac glyphs (e.g. "⌘") first. | ||
.map( key => modifiersToMacGlyphs[ key.toLowerCase() ] || key ) | ||
const modifiersToGlyphs = Object.entries( env.isMac ? modifiersToGlyphsMac : modifiersToGlyphsNonMac ); | ||
// 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; | ||
} | ||
} ); | ||
const modifiers = modifiersToGlyphs.reduce( ( modifiers, [ name, glyph ] ) => { | ||
// Modifier keys are stored as a bit mask so extract those from the keystroke code. | ||
if ( ( keystrokeCode & keyCodes[ name ] ) != 0 ) { | ||
keystrokeCode &= ~keyCodes[ name ]; | ||
modifiers += glyph; | ||
} | ||
return modifiers; | ||
}, '' ); | ||
return modifiers + ( keystrokeCode ? keyCodeNames[ keystrokeCode ] : '' ); | ||
} | ||
@@ -153,5 +162,5 @@ | ||
* Returns the direction in which the {@link module:engine/model/documentselection~DocumentSelection selection} | ||
* will move when a provided arrow key code is pressed considering the language direction of the editor content. | ||
* will move when the provided arrow key code is pressed considering the language direction of the editor content. | ||
* | ||
* For instance, in right–to–left (RTL) content languages, pressing the left arrow means moving selection right (forward) | ||
* For instance, in right–to–left (RTL) content languages, pressing the left arrow means moving the selection right (forward) | ||
* in the model structure. Similarly, pressing the right arrow moves the selection left (backward). | ||
@@ -182,2 +191,19 @@ * | ||
// Converts a key name to the key code with mapping based on the env. | ||
// | ||
// See: {@link module:utils/keyboard~getCode}. | ||
// | ||
// @param {String} key The key name (see {@link module:utils/keyboard~keyCodes}). | ||
// @returns {Number} Key code. | ||
function getEnvKeyCode( key ) { | ||
// Don't remap modifier key for forced modifiers. | ||
if ( key.endsWith( '!' ) ) { | ||
return getCode( key.slice( 0, -1 ) ); | ||
} | ||
const code = getCode( key ); | ||
return env.isMac && code == keyCodes.ctrl ? keyCodes.cmd : code; | ||
} | ||
/** | ||
@@ -217,7 +243,5 @@ * Determines if the provided key code moves the {@link module:engine/model/documentselection~DocumentSelection selection} | ||
ctrl: 0x110000, | ||
// Has the same code as ctrl, because their behaviour should be unified across the editor. | ||
// See http://ckeditor.github.io/editor-recommendations/general-policies#ctrl-vs-cmd | ||
cmd: 0x110000, | ||
shift: 0x220000, | ||
alt: 0x440000 | ||
alt: 0x440000, | ||
cmd: 0x880000 | ||
}; | ||
@@ -250,3 +274,3 @@ | ||
/** | ||
* Information about a keystroke. | ||
* Information about the keystroke. | ||
* | ||
@@ -265,9 +289,9 @@ * @interface module:utils/keyboard~KeystrokeInfo | ||
* | ||
* @member {Bolean} module:utils/keyboard~KeystrokeInfo#altKey | ||
* @member {Boolean} module:utils/keyboard~KeystrokeInfo#altKey | ||
*/ | ||
/** | ||
* Whether the <kbd>Ctrl</kbd> or <kbd>Cmd</kbd> modifier was pressed. | ||
* Whether the <kbd>Ctrl</kbd> modifier was pressed. | ||
* | ||
* @member {Bolean} module:utils/keyboard~KeystrokeInfo#ctrlKey | ||
* @member {Boolean} module:utils/keyboard~KeystrokeInfo#ctrlKey | ||
*/ | ||
@@ -278,3 +302,9 @@ | ||
* | ||
* @member {Bolean} module:utils/keyboard~KeystrokeInfo#shiftKey | ||
* @member {Boolean} module:utils/keyboard~KeystrokeInfo#shiftKey | ||
*/ | ||
/** | ||
* Whether the <kbd>Cmd</kbd> modifier was pressed. | ||
* | ||
* @member {Boolean} module:utils/keyboard~KeystrokeInfo#metaKey | ||
*/ |
@@ -14,4 +14,6 @@ /** | ||
const version = '25.0.0'; | ||
const version = '26.0.0'; | ||
export default version; | ||
/* istanbul ignore next */ | ||
@@ -18,0 +20,0 @@ const windowOrGlobal = typeof window === 'object' ? window : global; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272984
58
6652