diff --git a/src/index.js b/src/index.js index 384ba5f..7524583 100644 --- a/src/index.js +++ b/src/index.js @@ -30,8 +30,8 @@ const bindValue = (value, el, binding, vnode) => { } const unbindValue = (value, el) => { - for (let item in value) { - const k = value[item].join('') + for (let key in value) { + const k = ShortKey.encodeKey(value[key]) const idxElm = mapFunctions[k].el.indexOf(el) if (mapFunctions[k].el.length > 1 && idxElm > -1) { mapFunctions[k].el.splice(idxElm, 1) @@ -63,7 +63,20 @@ ShortKey.install = (Vue, options) => { }) } -ShortKey.decodeKey = (pKey) => { +ShortKey.decodeKey = (pKey) => createShortcutIndex(pKey) +ShortKey.encodeKey = (pKey) => { + const shortKey = {} + shortKey.shiftKey = pKey.includes('shift') + shortKey.ctrlKey = pKey.includes('ctrl') + shortKey.metaKey = pKey.includes('meta') + shortKey.altKey = pKey.includes('alt') + let indexedKeys = createShortcutIndex(shortKey) + const vKey = pKey.filter((item) => !['shift', 'ctrl', 'meta', 'alt'].includes(item)) + indexedKeys += vKey.join('') + return indexedKeys +} + +const createShortcutIndex = (pKey) => { let k = '' if (pKey.key === 'Shift' || pKey.shiftKey) { k += 'shift' } if (pKey.key === 'Control' || pKey.ctrlKey) { k += 'ctrl' } @@ -143,7 +156,7 @@ if (process && process.env && process.env.NODE_ENV !== 'test') { const mappingFunctions = ({b, push, once, focus, el}) => { for (let key in b) { - const k = b[key].join('') + const k = ShortKey.encodeKey(b[key]) const elm = mapFunctions[k] && mapFunctions[k].el ? mapFunctions[k].el : [] elm.push(el) mapFunctions[k] = { @@ -159,7 +172,6 @@ const mappingFunctions = ({b, push, once, focus, el}) => { const availableElement = (decodedKey) => { const objectIsAvoided = !!objAvoided.find(r => r === document.activeElement) const filterAvoided = !!(elementAvoided.find(selector => document.activeElement && document.activeElement.matches(selector))) - return !!mapFunctions[decodedKey] && !(objectIsAvoided || filterAvoided) } diff --git a/test/Pollyfills.js b/test/Pollyfills.js new file mode 100644 index 0000000..93de8d4 --- /dev/null +++ b/test/Pollyfills.js @@ -0,0 +1,80 @@ +// https://tc39.github.io/ecma262/#sec-array.prototype.includes +if (!Array.prototype.includes) { + Object.defineProperty(Array.prototype, 'includes', { + value: function(searchElement, fromIndex) { + + // 1. Let O be ? ToObject(this value). + if (this == null) { + throw new TypeError('"this" is null or not defined'); + } + + var o = Object(this); + + // 2. Let len be ? ToLength(? Get(O, "length")). + var len = o.length >>> 0; + + // 3. If len is 0, return false. + if (len === 0) { + return false; + } + + // 4. Let n be ? ToInteger(fromIndex). + // (If fromIndex is undefined, this step produces the value 0.) + var n = fromIndex | 0; + + // 5. If n ≥ 0, then + // a. Let k be n. + // 6. Else n < 0, + // a. Let k be len + n. + // b. If k < 0, let k be 0. + var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); + + // 7. Repeat, while k < len + while (k < len) { + // a. Let elementK be the result of ? Get(O, ! ToString(k)). + // b. If SameValueZero(searchElement, elementK) is true, return true. + // c. Increase k by 1. + // NOTE: === provides the correct "SameValueZero" comparison needed here. + if (o[k] === searchElement) { + return true; + } + k++; + } + + // 8. Return false + return false; + } + }); +} + +if (!Object.assign) { + Object.defineProperty(Object, 'assign', { + enumerable: false, + configurable: true, + writable: true, + value: function(target, firstSource) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert first argument to object'); + } + + var to = Object(target); + for (var i = 1; i < arguments.length; i++) { + var nextSource = arguments[i]; + if (nextSource === undefined || nextSource === null) { + continue; + } + + var keysArray = Object.keys(Object(nextSource)); + for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { + var nextKey = keysArray[nextIndex]; + var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); + if (desc !== undefined && desc.enumerable) { + to[nextKey] = nextSource[nextKey]; + } + } + } + return to; + } + }); +} diff --git a/test/index.test.js b/test/index.test.js index cc7d5fd..4b0091d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,4 @@ +import './Pollyfills' import find from 'array.prototype.find' import {expect} from 'chai' import Vue from 'vue'