Skip to content

Commit

Permalink
Fixing order of keys #57
Browse files Browse the repository at this point in the history
  • Loading branch information
fgr-araujo committed Jun 24, 2018
1 parent 7f5d5d4 commit 06dba0c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -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] = {
Expand All @@ -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)
}

Expand Down
80 changes: 80 additions & 0 deletions test/Pollyfills.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
}
1 change: 1 addition & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './Pollyfills'
import find from 'array.prototype.find'
import {expect} from 'chai'
import Vue from 'vue'
Expand Down

0 comments on commit 06dba0c

Please sign in to comment.