From a530b98fe2c898d02efcf6c7ca3d19f919382839 Mon Sep 17 00:00:00 2001 From: James Collins Date: Mon, 27 Feb 2023 08:27:20 +1000 Subject: [PATCH] addn stackable addKeyUpListener --- resources/js/store/ApplicationStore.ts | 31 ++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/resources/js/store/ApplicationStore.ts b/resources/js/store/ApplicationStore.ts index 5b16563..2875179 100644 --- a/resources/js/store/ApplicationStore.ts +++ b/resources/js/store/ApplicationStore.ts @@ -1,13 +1,19 @@ import { defineStore } from "pinia"; +type ApplicationStoreEventKeyUpCallback = (event: KeyboardEvent) => boolean; + export interface ApplicationStore { dynamicTitle: string; + eventKeyUpStack: ApplicationStoreEventKeyUpCallback[]; + _addedListener: boolean; } export const useApplicationStore = defineStore({ id: "application", state: (): ApplicationStore => ({ dynamicTitle: "", + eventKeyUpStack: [], + _addedListener: false, }), actions: { @@ -20,8 +26,29 @@ export const useApplicationStore = defineStore({ this.$state.dynamicTitle = ""; }, - setRouterLoading(loading: boolean) { - this.$state.routerLoading = loading; + addKeyUpListener(callback: ApplicationStoreEventKeyUpCallback) { + 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 + ); }, }, });