UNPKG

juejin-hotkeys-js

Version:

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.

4 lines (2 loc) 3.94 kB
/*! hotkeys-js v2.0.1 | (c) 2017 | https://github.com/jaywcjlove/hotkeys.git */ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.hotkeys=e()}}(function(){function n(e){_scope=e||"all"}function o(){return _scope||"all"}function t(e,n,o){e.addEventListener?e.addEventListener(n,o,!1):e.attachEvent&&e.attachEvent("on"+n,function(){o(window.event)})}function r(e){return"string"==typeof e&&(e=code(e)),_downKeys.indexOf(e)!==-1}function i(e){return _downKeys.slice(0)}function d(e){var n,t=e.keyCode||d.which||d.charCode,r=_handlers["*"];if(_downKeys.indexOf(t)===-1&&_downKeys.push(t),93!==t&&224!==t||(t=91),t in _mods){_mods[t]=!0;for(var i in _modifier)_modifier[i]===t&&(_[i]=!0);if(!r)return}for(var d in _mods)_mods[d]=e[modifierMap[d]];if(_.filter.call(this,e)){if(n=o(),r)for(f=0;f<r.length;f++)r[f].scope===n&&s(e,r[f],n);if(t in _handlers)for(var f=0;f<_handlers[t].length;f++)s(e,_handlers[t][f],n)}}function s(e,n,o){var t;if(n.scope===o||"all"===n.scope){t=n.mods.length>0;for(var r in _mods)(!_mods[r]&&n.mods.indexOf(+r)>-1||_mods[r]&&n.mods.indexOf(+r)===-1)&&(t=!1);(0!==n.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!t&&"*"!==n.shortcut||n.method(e,n)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function f(e,n){for(var t,r,i=h(e),d=[],s=0;s<i.length;s++){if(t=i[s].split("+"),t.length>1&&(d=u(t)),e=t[t.length-1],e=code(e),void 0===n&&(n=o()),!_handlers[e])return;for(var f=0;f<_handlers[e].length;f++)r=_handlers[e][f],r.scope===n&&l(r.mods,d)&&(_handlers[e][f]={})}}function a(e){var n,o,t;for(n in _handlers)for(o=_handlers[n],t=0;t<o.length;)o[t].scope===e?o.splice(t,1):t++}function l(e,n){if(e.length!==n.length)return!1;for(var o=0;o<e.length;o++)if(e[o]!==n[o])return!1;return!0}function c(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)}function u(e){for(var n=e.slice(0,e.length-1),o=0;o<n.length;o++)n[o]=_modifier[n[o]];return n}function h(e){var n;return e=e.replace(/\s/g,""),n=e.split(","),""===n[n.length-1]&&(n[n.length-2]+=","),n}function p(n){var o=n.keyCode||e.which||e.charCode,t=_downKeys.indexOf(o);if(t>=0&&_downKeys.splice(t,1),93!==o&&224!==o||(o=91),o in _mods){_mods[o]=!1;for(var r in _modifier)_modifier[r]===o&&(_[r]=!1)}}function _(e,n,o){var t=h(e),r=[],i=0;for(void 0===o&&(o=n,n="all");i<t.length;i++)e=t[i].split("+"),r=[],e.length>1&&(r=u(e),e=[e[e.length-1]]),e=e[0],e="*"===e?"*":code(e),e in _handlers||(_handlers[e]=[]),_handlers[e].push({shortcut:t[i],scope:n,method:o,key:t[i],mods:r})}Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;n++)if(this[n]===e)return n;return-1});var m,y=!1;_keyMap={backspace:8,tab:9,clear:12,enter:13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,home:36,end:35,pageup:33,pagedown:34,",":188,".":190,"/":191,"`":192,"-":y?173:189,"=":y?61:187,";":y?59:186,"'":222,"[":219,"]":221,"\\":220},_scope="all",_modifier={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":y?224:91,command:y?224:91},_downKeys=[],modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey"},_mods={16:!1,18:!1,17:!1},code=function(e){return _keyMap[e]||e.toUpperCase().charCodeAt(0)},_handlers={};for(var g=1;g<20;g++)_keyMap["f"+g]=111+g;modifierMap[y?224:91]="metaKey",_mods[y?224:91]=!1,t(document,"keydown",function(e){d(e)}),t(document,"keyup",function(e){p(e)}),m={setScope:n,getScope:o,deleteScope:a,getPressedKeyCodes:i,isPressed:r,filter:c,unbind:f};for(var v in m)_[v]=m[v];var w=window.hotkeys;return _.noConflict=function(e){return e&&window.hotkeys===_&&(window.hotkeys=w),_},window.hotkeys=_,_});