Home Reference Source

application/components/common/rich-editor2/util/modifiers.js

/*
    This file contains a few methods borrowed from:
    https://github.com/applitools/modifier-keys

    We had to copy them here because including the library caused an issue
    with uglify on deployment. The ua-parser-js dependency wasn't transpiling properly so
    it needed to be removed.
*/

/**
 * Determines if it's a keyboard event
 * @see https://github.com/applitools/modifier-keys/blob/master/src/modifier.js
 * @param {Event} event
 */
function isKeyboardEvent(event) {
    return (event instanceof KeyboardEvent);
}

/**
 * Determines if it is a keyboard event
 * @see https://github.com/applitools/modifier-keys/blob/master/src/modifier.js
 * @param {Event} event
 */
function isSimilarToKeyboardEvent(event) {
    return (event && event.hasOwnProperty('metaKey') && event.hasOwnProperty('ctrlKey') && event.hasOwnProperty('altKey'));
}

/**
 * Are we using a meta key
 * @see https://github.com/applitools/modifier-keys/blob/master/src/modifier.js
 * @param {Event} event
 */
function modifier(event) {
    if (!(isKeyboardEvent(event) || isSimilarToKeyboardEvent(event))) {
        throw new Error(`Expected to receive KeyboardEvent instead received ${event ? event.constructor.name : event}`);
    }

    const isMac = (navigator.userAgent.search(/(Mac OS X|MacPPC|MacIntel|Mac_PowerPC|Macintosh)/ig) !== -1) ? true : false;
    event.primaryKey = ((isMac && event.metaKey) || (!isMac && event.ctrlKey));
    event.secondaryKey = event.altKey;
    return event;
}

export function isPrimaryModifier(e) {
    const m = modifier(e);
    return m.primaryKey;
}

export function isSecondaryModifier(e) {
    const m = modifier(e);
    return m.secondaryKey;
}

export function isPrimaryPlus(e, key) {
    return (isPrimaryModifier(e) && e.key == key)
}

export function isSecondaryPlus(e, key) {
    return (isSecondaryModifier(e) && e.key == key)
}

export function isMonkeyClawPlus(e, key) {
    return (isPrimaryModifier(e) && isSecondaryModifier(e) && e.key == key)
}