快捷键,防抖

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

@ -22,6 +22,7 @@
"core-js": "^3.6.5", "core-js": "^3.6.5",
"dayjs": "^1.10.7", "dayjs": "^1.10.7",
"element-plus": "^1.1.0-beta.19", "element-plus": "^1.1.0-beta.19",
"hotkeys-js": "^3.8.7",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"pubsub-js": "^1.9.3", "pubsub-js": "^1.9.3",
"unplugin-vue-components": "^0.15.6", "unplugin-vue-components": "^0.15.6",

View File

@ -22,9 +22,9 @@ import {
NMessageProvider, NMessageProvider,
} from "naive-ui"; } from "naive-ui";
import router from "./router"; import router from "./router";
import useHotkey from '@/lib/useHotkey.js'
// import { CloudCircleSharp } from "@vicons/ionicons5"; useHotkey() //
const store = useStore(); const store = useStore();
/** /**
@ -62,15 +62,14 @@ const showSongDetail = ref(false); //是否显示歌曲详细信息
const showSearch = ref(false); // const showSearch = ref(false); //
watch( watch(
()=> store.state.showSongDetail, () => store.state.showSongDetail,
()=>{ () => {
showSongDetail.value = toRaw(store.state.showSongDetail) showSongDetail.value = toRaw(store.state.showSongDetail);
}, },
{ {
immediate: true, immediate: true,
} }
) );
//route //route
const routeToken = pubsub.subscribe("router", (msg, data) => { const routeToken = pubsub.subscribe("router", (msg, data) => {
@ -90,13 +89,14 @@ const token = pubsub.subscribe("zp", (msg, data) => {
showPlaying.value = !showPlaying.value; showPlaying.value = !showPlaying.value;
break; break;
case "zp.toggleSongDetail": case "zp.toggleSongDetail":
showSongDetail.value = store.state.showSongDetail = !showSongDetail.value; showSongDetail.value = store.state.showSongDetail =
!showSongDetail.value;
break; break;
case "zp.showSearch": case "zp.showSearch":
showSearch.value = true; showSearch.value = true;
break; break;
case "zp.toggleSearch": case "zp.toggleSearch":
showSearch.value = !showSearch.value ; showSearch.value = !showSearch.value;
break; break;
} }
}); });
@ -107,25 +107,26 @@ onUnmounted(() => {
pubsub.unsubscribe(token); pubsub.unsubscribe(token);
}); });
const wpEl = ref("");
const wpEl = ref('')
const hideWins = (e) => { const hideWins = (e) => {
// console.log(e.clientX > 0,e, wpEl.value.clientWidth); // console.log(e.clientX > 0,e, wpEl.value.clientWidth);
// //
if (showPlaying.value if (
&& e.clientX > 0 showPlaying.value &&
&& e.clientX < wpEl.value.clientWidth - 500 e.clientX > 0 &&
&& e.clientY > 40 e.clientX < wpEl.value.clientWidth - 500 &&
&& e.clientY < wpEl.value.clientHeight - 64 e.clientY > 40 &&
e.clientY < wpEl.value.clientHeight - 64
) { ) {
showPlaying.value = false; showPlaying.value = false;
} }
// //
if (showSearch.value if (
&& e.clientX > 0 showSearch.value &&
&& e.clientX < wpEl.value.clientWidth - 360 e.clientX > 0 &&
&& e.clientY > 40 e.clientX < wpEl.value.clientWidth - 360 &&
&& e.clientY < wpEl.value.clientHeight - 64 e.clientY > 40 &&
e.clientY < wpEl.value.clientHeight - 64
) { ) {
showSearch.value = false; showSearch.value = false;
} }
@ -133,8 +134,7 @@ const hideWins = (e) => {
const keyup = (e) => { const keyup = (e) => {
console.log(e); console.log(e);
};
}
</script> </script>
<template> <template>
@ -172,7 +172,7 @@ const keyup = (e) => {
</div> </div>
</div> </div>
<div id="wpSongDetail" v-show="showSongDetail"> <div id="wpSongDetail" v-show="showSongDetail">
<SongDetail/> <SongDetail />
</div> </div>
<div id="wpPlayingList" v-show="showPlaying"> <div id="wpPlayingList" v-show="showPlaying">
<div id="playingList"> <div id="playingList">
@ -294,8 +294,8 @@ body {
} }
} }
#wpSearch{ #wpSearch {
#search{ #search {
position: absolute; position: absolute;
top: 40px; top: 40px;
right: 0; right: 0;

View File

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

View File

@ -165,7 +165,9 @@ const favorite = () => {
}; };
const removeCurrentSong = () => { const removeCurrentSong = () => {
pubsub.publish('zp.removeCurrPlayingSong', { id: store.state.settings.songId }) pubsub.publish("zp.removeCurrPlayingSong", {
id: store.state.settings.songId,
});
}; };
let interval; let interval;
@ -197,9 +199,16 @@ const psToken = pubsub.subscribe("zp", (msg, data) => {
case "zp.next": case "zp.next":
forward(); forward();
break; break;
case "zp.previous":
previous();
break;
case "zp.stop": case "zp.stop":
stop(); stop();
break; break;
case "zp.pause":
if (playing.value) pause();
else resume();
break;
case "zp.setProgressScale": case "zp.setProgressScale":
setProgressScale(data.scale); setProgressScale(data.scale);
break; break;

View File

@ -5691,6 +5691,11 @@ hosted-git-info@^4.0.2:
dependencies: dependencies:
lru-cache "^6.0.0" lru-cache "^6.0.0"
hotkeys-js@^3.8.7:
version "3.8.7"
resolved "https://registry.yarnpkg.com/hotkeys-js/-/hotkeys-js-3.8.7.tgz#c16cab978b53d7242f860ca3932e976b92399981"
integrity sha512-ckAx3EkUr5XjDwjEHDorHxRO2Kb7z6Z2Sxul4MbBkN8Nho7XDslQsgMJT+CiJ5Z4TgRxxvKHEpuLE3imzqy4Lg==
hpack.js@^2.1.6: hpack.js@^2.1.6:
version "2.1.6" version "2.1.6"
resolved "https://registry.yarnpkg.com/hpack.js/-/hpack.js-2.1.6.tgz#87774c0949e513f42e84575b3c45681fade2a0b2" resolved "https://registry.yarnpkg.com/hpack.js/-/hpack.js-2.1.6.tgz#87774c0949e513f42e84575b3c45681fade2a0b2"