快捷键,防抖

This commit is contained in:
zilong
2021-11-06 17:45:09 +08:00
parent d70bf6738e
commit f59b43cbbe
6 changed files with 84 additions and 30 deletions

View File

@@ -4,7 +4,7 @@ import { useStore } from "vuex";
//设置
let config = {
elName: "mainContent", //#元素名称
log: true, //是否显示log
log: false, //是否显示log
};
const backSnaps = []; //返回快照数组

39
src/lib/useHotkey.js Normal file
View File

@@ -0,0 +1,39 @@
import { debounce, throttle } from "lodash";
import pubsub from "pubsub-js";
import hotkeys from "hotkeys-js";
export default function useHotkey(){
hotkeys(
[
"space,ctrl+p,⌘+p",
"ctrl+left,⌘+left,ctrl+right,⌘+right",
"enter,ctrl+enter,⌘+enter",
].join(","),
throttle((e, h) => {
switch (h.key) {
// space,ctrl+p,⌘+p
case "space":
case "ctrl+p":
case "⌘+p":
pubsub.publish("zp.pause");
break;
// ctrl+left,⌘+left,ctrl+right,⌘+right
case "ctrl+left":
case "⌘+left":
pubsub.publish("zp.previous");
break;
case "ctrl+right":
case "⌘+right":
pubsub.publish("zp.next");
break;
// enter,ctrl+enter,⌘+enter
case "enter":
case "ctrl+enter":
case "⌘+enter":
pubsub.publish("zp.toggleSongDetail");
break;
}
e.preventDefault();
}, 500)
);
};