@pluginjs/keyboard
Advanced tools
Comparing version 0.2.19 to 0.6.1
@@ -1,363 +0,6 @@ | ||
let Emitter = class Emitter { | ||
constructor() { | ||
this.listeners = {}; | ||
this.sortedListeners = {}; | ||
} | ||
import SimpleEmitter from '@pluginjs/simple-emitter'; | ||
import { each } from '@pluginjs/utils'; | ||
emit(event, ...args) { | ||
const listeners = this.getListeners(event); | ||
for (let i = 0; i < listeners.length; i++) { | ||
let context = null; | ||
/* eslint object-property-newline: 'off' */ | ||
if (listeners[i].context !== null) { | ||
context = listeners[i].context; | ||
} else { | ||
context = { type: event }; | ||
} | ||
const result = listeners[i].listener.apply(context, args); | ||
if (result === false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
on(event, listener, context, priority) { | ||
return this.addListener(event, listener, context, priority); | ||
} | ||
once(event, listener, context, priority) { | ||
return this.addOneTimeListener(event, listener, context, priority); | ||
} | ||
off(event, listener) { | ||
if (typeof listener === 'undefined') { | ||
return this.removeAllListeners(event); | ||
} | ||
return this.removeListener(event, listener); | ||
} | ||
/* Lower numbers correspond with earlier execution, | ||
/* and functions with the same priority are executed | ||
/* in the order in which they were added to the action. */ | ||
addListener(event, listener, context = null, priority = 10) { | ||
this.ensureListener(listener); | ||
if (!this.listeners[event]) { | ||
this.listeners[event] = {}; | ||
} | ||
if (!this.listeners[event][priority]) { | ||
this.listeners[event][priority] = []; | ||
} | ||
this.listeners[event][priority].push({ | ||
context, | ||
listener | ||
}); | ||
this.clearSortedListeners(event); | ||
return this; | ||
} | ||
addOneTimeListener(event, listener, context, priority = 10) { | ||
const that = this; | ||
function wrapper(...args) { | ||
that.removeListener(event, wrapper); | ||
return listener(...args); | ||
} | ||
this.addListener(event, wrapper, context, priority); | ||
return this; | ||
} | ||
removeListener(event, listener) { | ||
this.clearSortedListeners(event); | ||
const listeners = this.hasListeners(event) ? this.listeners[event] : []; | ||
for (const priority in listeners) { | ||
if (Object.prototype.hasOwnProperty.call(listeners, priority)) { | ||
listeners[priority] = listeners[priority].filter(value => value.listener !== listener); | ||
if (listeners[priority].length === 0) { | ||
delete listeners[priority]; | ||
} | ||
} | ||
} | ||
this.listeners[event] = listeners; | ||
return this; | ||
} | ||
removeAllListeners(event) { | ||
this.clearSortedListeners(event); | ||
if (this.hasListeners(event)) { | ||
delete this.listeners[event]; | ||
} | ||
return this; | ||
} | ||
ensureListener(listener) { | ||
const type = typeof listener; | ||
if (type === 'function') { | ||
return listener; | ||
} | ||
throw new TypeError(`Listeners should be function or closure. Received type: ${type}`); | ||
} | ||
hasListeners(event) { | ||
if (!this.listeners[event] || Object.keys(this.listeners[event]).length === 0) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
getListeners(event) { | ||
if (!this.sortedListeners.hasOwnProperty(event)) { | ||
this.sortedListeners[event] = this.getSortedListeners(event); | ||
} | ||
return this.sortedListeners[event]; | ||
} | ||
getSortedListeners(event) { | ||
if (!this.hasListeners(event)) { | ||
return []; | ||
} | ||
const listeners = this.listeners[event]; | ||
const priorities = Object.keys(listeners); | ||
priorities.sort((a, b) => a - b); | ||
let sortedlisteners = []; | ||
for (let i = 0; i < priorities.length; i++) { | ||
sortedlisteners = sortedlisteners.concat(listeners[priorities[i]]); | ||
} | ||
return sortedlisteners; | ||
} | ||
clearSortedListeners(event) { | ||
delete this.sortedListeners[event]; | ||
} | ||
}; | ||
const curry = (fn, args = []) => (...subArgs) => { | ||
const currylen = fn.currylen || fn.length; | ||
const collect = args.concat(subArgs); | ||
if (collect.length >= currylen) { | ||
return fn(...collect); | ||
} | ||
return curry(fn, collect); | ||
}; | ||
/*eslint-disable */ | ||
/* Credit to http://is.js.org MIT */ | ||
const toString = Object.prototype.toString; | ||
const is = { | ||
// Type checks | ||
/* -------------------------------------------------------------------------- */ | ||
// is a given value Arguments? | ||
arguments: function (value) { | ||
// fallback check is for IE | ||
return toString.call(value) === '[object Arguments]' || value != null && typeof value === 'object' && 'callee' in value; | ||
}, | ||
// is a given value Array? | ||
array: function (val) { | ||
if (Array.isArray) { | ||
return Array.isArray(val); | ||
} | ||
return toString.call(val) === '[object Array]'; | ||
}, | ||
// is a given value Boolean? | ||
boolean: function (val) { | ||
return val === true || val === false || toString.call(val) === '[object Boolean]'; | ||
}, | ||
// is a given value Char? | ||
char: function (val) { | ||
return this.string(val) && val.length === 1; | ||
}, | ||
// is a given value Date Object? | ||
date: function (value) { | ||
return toString.call(value) === '[object Date]'; | ||
}, | ||
// is a given object a DOM node? | ||
domNode: function (object) { | ||
return this.object(object) && object.nodeType > 0; | ||
}, | ||
// is a given value Error object? | ||
error: function (val) { | ||
return toString.call(val) === '[object Error]'; | ||
}, | ||
// is a given value function? | ||
function: function (val) { | ||
// fallback check is for IE | ||
return toString.call(val) === '[object Function]' || typeof val === 'function'; | ||
}, | ||
// is given value a pure JSON object? | ||
json: function (value) { | ||
return toString.call(value) === '[object Object]'; | ||
}, | ||
// is a given value NaN? | ||
nan: function (val) { | ||
// NaN is number :) Also it is the only value which does not equal itself | ||
return val !== val; | ||
}, | ||
// is a given value null? | ||
null: function (val) { | ||
return val === null; | ||
}, | ||
// is a given value number? | ||
number: function (val) { | ||
return !this.nan(val) && toString.call(val) === '[object Number]'; | ||
}, | ||
// is a given value object? | ||
object: function (val) { | ||
return Object(val) === val; | ||
}, | ||
// is a given value empty object? | ||
emptyObject: function (val) { | ||
return this.object(val) && Object.getOwnPropertyNames(val).length == 0; | ||
}, | ||
// is a given value RegExp? | ||
regexp: function (val) { | ||
return toString.call(val) === '[object RegExp]'; | ||
}, | ||
// is a given value String? | ||
string: function (val) { | ||
return typeof val === 'string' || toString.call(val) === '[object String]'; | ||
}, | ||
// is a given value undefined? | ||
undefined: function (val) { | ||
return val === void 0; | ||
}, | ||
// Arithmetic checks | ||
/* -------------------------------------------------------------------------- */ | ||
// is a given value numeric? | ||
numeric: function (n) { | ||
return (this.number(n) || this.string(n)) && !this.nan(n - parseFloat(n)); | ||
}, | ||
// is a given number percentage? | ||
percentage: function (n) { | ||
return typeof n === 'string' && n.indexOf('%') !== -1; | ||
}, | ||
// is a given number decimal? | ||
decimal: function (n) { | ||
return this.number(n) && n % 1 !== 0; | ||
}, | ||
// is a given number finite? | ||
finite: function (n) { | ||
if (isFinite) { | ||
return isFinite(n); | ||
} | ||
return !this.infinite(n) && !this.nan(n); | ||
}, | ||
// is a given number infinite? | ||
infinite: function (n) { | ||
return n === Infinity || n === -Infinity; | ||
}, | ||
integer: function (n) { | ||
return this.number(n) && n % 1 === 0; | ||
}, | ||
// is a given number negative? | ||
negative: function (n) { | ||
return this.number(n) && n < 0; | ||
}, | ||
// is a given number positive? | ||
positive: function (n) { | ||
return this.number(n) && n > 0; | ||
} | ||
}; | ||
const Each = (obj, callback) => { | ||
let i = 0, | ||
length; | ||
if (is.array(obj)) { | ||
length = obj.length; | ||
for (; i < length; i++) { | ||
callback(obj[i], i); | ||
} | ||
} else { | ||
Object.entries(obj).map(([k, v]) => callback(k, v)); | ||
} | ||
return obj; | ||
}; | ||
// == animation == // | ||
const fade = curry((type, { duration, callback }, element) => { | ||
const isIn = type === 'in'; | ||
let opacity = isIn ? 0 : 1; | ||
let start = null; | ||
if (isIn) { | ||
if (element.style.display === 'none') { | ||
element.style.display = 'inline'; | ||
} | ||
element.style.opacity = opacity; | ||
} | ||
function step(timestamp) { | ||
if (!start) { | ||
start = timestamp; | ||
} | ||
const progress = timestamp - start; | ||
const percent = progress / duration; | ||
opacity = isIn ? opacity + percent : opacity - percent; | ||
element.style.opacity = opacity; | ||
if (opacity <= 0) { | ||
element.style.display = 'none'; | ||
} | ||
if (progress < duration) { | ||
window.requestAnimationFrame(step); | ||
} else if (callback) { | ||
callback(); | ||
} | ||
} | ||
window.requestAnimationFrame(step); | ||
}); | ||
const fadeOut = fade('out'); | ||
const fadeIn = fade('in'); | ||
/* eslint object-property-newline: 'off' */ | ||
const MAP_BY_CODE = { | ||
@@ -434,3 +77,2 @@ 8: 'backspace', | ||
}; | ||
const MAP_BY_NAME = {}; | ||
@@ -451,7 +93,6 @@ | ||
let Keyboard = class Keyboard { | ||
class Keyboard { | ||
constructor(element) { | ||
this.element = element || window.document; | ||
this.emitter = new Emitter(); | ||
this.emitter = new SimpleEmitter(); | ||
this.initialize(); | ||
@@ -463,5 +104,4 @@ this.registerEvent(); | ||
this.status = {}; | ||
Each(MODIFIERS, (keyCode, keyName) => { | ||
each(MODIFIERS, (keyCode, keyName) => { | ||
this.status[keyName] = false; | ||
this.emitter.on(`${keyCode}down`, () => { | ||
@@ -471,2 +111,3 @@ if (this.status[keyName]) { | ||
} | ||
this.status[keyName] = true; | ||
@@ -478,2 +119,3 @@ }); | ||
} | ||
this.status[keyName] = false; | ||
@@ -486,6 +128,5 @@ }); | ||
const handler = e => this.handler(e); | ||
this.element.addEventListener('keydown', handler); | ||
this.element.addEventListener('keyup', handler); | ||
// bindEvent({ type: 'keyup', handler }, this.element) | ||
// bindEvent({ type: 'keydown', handler }, this.element) | ||
} | ||
@@ -501,8 +142,8 @@ | ||
keyCode = 91; | ||
} | ||
} // if (!this.filter(e)) return; | ||
// if (!this.filter(e)) return; | ||
if (keyCode in MODIFIERS) { | ||
const result = this.emitter.emit(keyCode + action); | ||
if (result === false) { | ||
@@ -513,3 +154,3 @@ return false; | ||
Each(this.status, (keyName, status) => { | ||
each(this.status, (keyName, status) => { | ||
if (status) { | ||
@@ -519,3 +160,2 @@ prefix += keyName; | ||
}); | ||
const eventName = prefix + keyCode + action; | ||
@@ -527,3 +167,10 @@ | ||
return this.emitter.emit(eventName); | ||
const result = this.emitter.emit(eventName); | ||
if (result === false) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
return; | ||
} | ||
@@ -558,3 +205,2 @@ | ||
}); | ||
return this; | ||
@@ -568,3 +214,2 @@ } | ||
let modifiers = null; | ||
key = key.split('+'); | ||
@@ -579,3 +224,2 @@ const length = key.length; | ||
key = this.getKeyCode(key[0]); | ||
newKey.modifiers = modifiers; | ||
@@ -586,3 +230,2 @@ newKey.keyCode = key; | ||
}); | ||
return newKeys; | ||
@@ -594,5 +237,7 @@ } | ||
const keys = key.split(','); | ||
if (keys[keys.length - 1] === '') { | ||
keys[keys.length - 2] += ','; | ||
} | ||
return keys; | ||
@@ -609,3 +254,2 @@ } | ||
modifiers.sort(); | ||
return modifiers; | ||
@@ -615,3 +259,3 @@ } | ||
distribute(action, key, func) { | ||
return func === null || func === undefined ? this.off(action, key, func) : this.on(action, key, func); | ||
return func === null || typeof func === 'undefined' ? this.off(action, key, func) : this.on(action, key, func); | ||
} | ||
@@ -634,11 +278,11 @@ | ||
} | ||
}; | ||
const keyboard = { | ||
init(element) { | ||
return new Keyboard(element); | ||
static of(...args) { | ||
return new this(...args); | ||
} | ||
}; | ||
} | ||
const keyboard = (...args) => Keyboard.of(...args); | ||
export default keyboard; |
{ | ||
"name": "@pluginjs/keyboard", | ||
"title": "Plugin", | ||
"version": "0.2.19", | ||
"description": "A workflow for modern frontend development.", | ||
"author": "Creation Studio Limited", | ||
"homepage": "https://github.com/amazingSurge/plugin.js", | ||
"license": "GPL-v3", | ||
"main": "dist/keyboard.js", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
"start": "gulp plugin", | ||
"build": "npm run prestart && gulp build", | ||
"deploy": "gulp deploy", | ||
"deploy:prepare": "gulp deploy:prepare", | ||
"test": "gulp test", | ||
"new": "cp -r ./plugins/sample ./plugins/$Component" | ||
}, | ||
"repository": { | ||
"url": "git@github.com:amazingSurge/plugin.js.git", | ||
"type": "git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/amazingSurge/plugin.js/issues" | ||
}, | ||
"engines": { | ||
"node": ">= 8", | ||
"npm": ">= 5" | ||
}, | ||
"category": "core", | ||
"min": "dist/keyboard.min.js", | ||
"standalone": "dist/keyboard.standalone.js", | ||
"module": "dist/keyboard.esm.js", | ||
"dev-main": "src/main.js" | ||
"name": "@pluginjs/keyboard", | ||
"description": "A workflow for modern frontend development.", | ||
"license": "GPL-3.0", | ||
"author": "Creation Studio Limited", | ||
"homepage": "https://github.com/pluginjs/pluginjs", | ||
"repository": { | ||
"url": "git@github.com:pluginjs/pluginjs.git", | ||
"type": "git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/pluginjs/pluginjs/issues" | ||
}, | ||
"version": "0.6.1", | ||
"category": "core", | ||
"main": "dist/keyboard.umd.js", | ||
"module": "dist/keyboard.esm.js", | ||
"source": "src/main.js", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "npm run build:js", | ||
"build:js": "plugin script build-js", | ||
"build:md": "plugin script build-md", | ||
"lint": "npm run lint:js", | ||
"lint:js": "eslint ./src/**/*.js --fix", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@pluginjs/dom": "^0.6.1", | ||
"@pluginjs/simple-emitter": "^0.6.1", | ||
"@pluginjs/utils": "^0.6.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.1.2", | ||
"@pluginjs/browserslist-config": "^1.1.1", | ||
"@pluginjs/cli": "^0.6.1", | ||
"babel-jest": "*", | ||
"jest": "*", | ||
"jest-extended": "*", | ||
"rollup": "*", | ||
"rollup-plugin-babel": "*", | ||
"rollup-plugin-commonjs": "*", | ||
"rollup-plugin-node-resolve": "*" | ||
}, | ||
"engines": { | ||
"node": ">= 8", | ||
"npm": ">= 5" | ||
}, | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "jest-extended", | ||
"verbose": true, | ||
"testURL": "http://localhost", | ||
"testPathIgnorePatterns": [ | ||
"fixtures" | ||
] | ||
}, | ||
"browserslist": [ | ||
"extends @pluginjs/browserslist-config" | ||
], | ||
"title": "Plugin", | ||
"gitHead": "353b9f7a27eb7306aecd38b475a479616e7db55d" | ||
} |
@@ -1,3 +0,3 @@ | ||
import Emitter from '@pluginjs/emitter' | ||
import { Each } from '@pluginjs/dom' | ||
import SimpleEmitter from '@pluginjs/simple-emitter' | ||
import { each } from '@pluginjs/utils' | ||
@@ -95,3 +95,3 @@ /* eslint object-property-newline: 'off' */ | ||
this.element = element || window.document | ||
this.emitter = new Emitter() | ||
this.emitter = new SimpleEmitter() | ||
@@ -104,3 +104,3 @@ this.initialize() | ||
this.status = {} | ||
Each(MODIFIERS, (keyCode, keyName) => { | ||
each(MODIFIERS, (keyCode, keyName) => { | ||
this.status[keyName] = false | ||
@@ -127,4 +127,2 @@ | ||
this.element.addEventListener('keyup', handler) | ||
// bindEvent({ type: 'keyup', handler }, this.element) | ||
// bindEvent({ type: 'keydown', handler }, this.element) | ||
} | ||
@@ -151,3 +149,3 @@ | ||
Each(this.status, (keyName, status) => { | ||
each(this.status, (keyName, status) => { | ||
if (status) { | ||
@@ -164,3 +162,10 @@ prefix += keyName | ||
return this.emitter.emit(eventName) | ||
const result = this.emitter.emit(eventName) | ||
if (result === false) { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
} | ||
return | ||
} | ||
@@ -178,2 +183,3 @@ | ||
const keys = this.parseKeys(this.processKey(key)) | ||
keys.forEach(key => { | ||
@@ -247,3 +253,3 @@ const modifiers = key.modifiers | ||
distribute(action, key, func) { | ||
return func === null || func === undefined | ||
return func === null || typeof func === 'undefined' | ||
? this.off(action, key, func) | ||
@@ -268,10 +274,10 @@ : this.on(action, key, func) | ||
} | ||
} | ||
const keyboard = { | ||
init(element) { | ||
return new Keyboard(element) | ||
static of(...args) { | ||
return new this(...args) | ||
} | ||
} | ||
const keyboard = (...args) => Keyboard.of(...args) | ||
export default keyboard |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
1
236
25676
3
10
5
787
2
+ Added@pluginjs/dom@^0.6.1
+ Added@pluginjs/utils@^0.6.1
+ Added@pluginjs/dom@0.6.5(transitive)
+ Added@pluginjs/is@0.6.5(transitive)
+ Added@pluginjs/simple-emitter@0.6.5(transitive)
+ Added@pluginjs/utils@0.6.5(transitive)