This commit is contained in:
zyronon
2023-09-21 23:26:05 +08:00
parent 9ed29f6827
commit 6e0d8a0185
13 changed files with 237 additions and 127 deletions

View File

@@ -4,84 +4,95 @@ import {useRuntimeStore} from "@/stores/runtime.ts";
import {$ref} from "vue/macros";
export function useWindowClick(cb: () => void) {
onMounted(() => {
emitter.on(EventKey.closeOther, cb)
window.addEventListener('click', cb)
})
onUnmounted(() => {
window.removeEventListener('click', cb)
})
onMounted(() => {
emitter.on(EventKey.closeOther, cb)
window.addEventListener('click', cb)
})
onUnmounted(() => {
window.removeEventListener('click', cb)
})
}
export function useEventListener(type: string, listener: EventListenerOrEventListenerObject) {
onMounted(() => window.addEventListener(type, listener))
onUnmounted(() => window.removeEventListener(type, listener))
onMounted(() => window.addEventListener(type, listener))
onUnmounted(() => window.removeEventListener(type, listener))
}
export function useStartKeyboardEventListener() {
const runtimeStore = useRuntimeStore()
const runtimeStore = useRuntimeStore()
useEventListener('keydown', (e: KeyboardEvent) => {
if (!runtimeStore.disableEventListener) {
emitter.emit(EventKey.keydown, e)
}
})
useEventListener('keyup', (e: KeyboardEvent) => {
if (!runtimeStore.disableEventListener) {
emitter.emit(EventKey.keyup, e)
}
})
useEventListener('keydown', (e: KeyboardEvent) => {
if (!runtimeStore.disableEventListener) {
emitter.emit(EventKey.keydown, e)
}
})
useEventListener('keyup', (e: KeyboardEvent) => {
if (!runtimeStore.disableEventListener) {
emitter.emit(EventKey.keyup, e)
}
})
}
export function useOnKeyboardEventListener(onKeyDown: (e: KeyboardEvent) => void, onKeyUp: (e: KeyboardEvent) => void) {
onMounted(() => {
emitter.on(EventKey.keydown, onKeyDown)
emitter.on(EventKey.keyup, onKeyUp)
})
onUnmounted(() => {
emitter.off(EventKey.keydown, onKeyDown)
emitter.off(EventKey.keyup, onKeyUp)
})
onMounted(() => {
emitter.on(EventKey.keydown, onKeyDown)
emitter.on(EventKey.keyup, onKeyUp)
})
onUnmounted(() => {
emitter.off(EventKey.keydown, onKeyDown)
emitter.off(EventKey.keyup, onKeyUp)
})
}
export function useDisableEventListener() {
const runtimeStore = useRuntimeStore()
onMounted(() => {
runtimeStore.disableEventListener = true
})
onUnmounted(() => {
runtimeStore.disableEventListener = false
})
export function useDisableEventListener(watchVal?: any) {
const runtimeStore = useRuntimeStore()
watch(watchVal, n => {
if (n) {
runtimeStore.disableEventListener = true
} else {
runtimeStore.disableEventListener = false
}
})
onMounted(() => {
if (!watchVal) {
runtimeStore.disableEventListener = true
}
})
onUnmounted(() => {
if (!watchVal) {
runtimeStore.disableEventListener = false
}
})
}
export function useEsc(close: () => void, watchVal?: any) {
const runtimeStore = useRuntimeStore()
const id = $ref(Date.now())
const runtimeStore = useRuntimeStore()
const id = $ref(Date.now())
watch(watchVal, n => {
if (n) {
runtimeStore.modalList.push({id, close})
} else {
let rIndex = runtimeStore.modalList.findIndex(item => item.id === id)
if (rIndex > 0) {
runtimeStore.modalList.splice(rIndex, 1)
}
}
})
watch(watchVal, n => {
if (n) {
runtimeStore.modalList.push({id, close})
} else {
let rIndex = runtimeStore.modalList.findIndex(item => item.id === id)
if (rIndex > 0) {
runtimeStore.modalList.splice(rIndex, 1)
}
}
})
onMounted(() => {
if (watchVal() === undefined) {
runtimeStore.modalList.push({id, close})
}
})
onMounted(() => {
if (watchVal() === undefined) {
runtimeStore.modalList.push({id, close})
}
})
onUnmounted(() => {
if (watchVal() === undefined) {
let rIndex = runtimeStore.modalList.findIndex(item => item.id === id)
if (rIndex > 0) {
runtimeStore.modalList.splice(rIndex, 1)
}
}
})
onUnmounted(() => {
if (watchVal() === undefined) {
let rIndex = runtimeStore.modalList.findIndex(item => item.id === id)
if (rIndex > 0) {
runtimeStore.modalList.splice(rIndex, 1)
}
}
})
}