addn stackable addKeyUpListener
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
|
||||||
|
type ApplicationStoreEventKeyUpCallback = (event: KeyboardEvent) => boolean;
|
||||||
|
|
||||||
export interface ApplicationStore {
|
export interface ApplicationStore {
|
||||||
dynamicTitle: string;
|
dynamicTitle: string;
|
||||||
|
eventKeyUpStack: ApplicationStoreEventKeyUpCallback[];
|
||||||
|
_addedListener: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useApplicationStore = defineStore({
|
export const useApplicationStore = defineStore({
|
||||||
id: "application",
|
id: "application",
|
||||||
state: (): ApplicationStore => ({
|
state: (): ApplicationStore => ({
|
||||||
dynamicTitle: "",
|
dynamicTitle: "",
|
||||||
|
eventKeyUpStack: [],
|
||||||
|
_addedListener: false,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
@@ -20,8 +26,29 @@ export const useApplicationStore = defineStore({
|
|||||||
this.$state.dynamicTitle = "";
|
this.$state.dynamicTitle = "";
|
||||||
},
|
},
|
||||||
|
|
||||||
setRouterLoading(loading: boolean) {
|
addKeyUpListener(callback: ApplicationStoreEventKeyUpCallback) {
|
||||||
this.$state.routerLoading = loading;
|
this.eventKeyUpStack.push(callback);
|
||||||
|
|
||||||
|
if (!this._addedListener) {
|
||||||
|
document.addEventListener("keyup", (event: KeyboardEvent) => {
|
||||||
|
this.eventKeyUpStack.every(
|
||||||
|
(item: ApplicationStoreEventKeyUpCallback) => {
|
||||||
|
const result = item(event);
|
||||||
|
if (result) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
removeKeyUpListener(callback: ApplicationStoreEventKeyUpCallback) {
|
||||||
|
this.eventKeyUpStack = this.eventKeyUpStack.filter(
|
||||||
|
(item: ApplicationStoreEventKeyUpCallback) => item !== callback
|
||||||
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user