|
|
@ -1,9 +1,15 @@ |
|
|
|
import { isClient } from '@vueuse/core' |
|
|
|
import { isClient } from '@vueuse/core' |
|
|
|
import type { Ref } from 'vue' |
|
|
|
import type { Ref } from 'vue' |
|
|
|
|
|
|
|
|
|
|
|
export function useSelectedCellKeyupListener(selected: Ref<boolean>, handler: (e: KeyboardEvent) => void) { |
|
|
|
function useSelectedCellKeyupListener( |
|
|
|
|
|
|
|
selected: Ref<boolean>, |
|
|
|
|
|
|
|
handler: (e: KeyboardEvent) => void, |
|
|
|
|
|
|
|
{ immediate = false }: { immediate?: boolean } = {}, |
|
|
|
|
|
|
|
) { |
|
|
|
if (isClient) { |
|
|
|
if (isClient) { |
|
|
|
watch(selected, (nextVal, _, cleanup) => { |
|
|
|
watch( |
|
|
|
|
|
|
|
selected, |
|
|
|
|
|
|
|
(nextVal: boolean, _: boolean, cleanup) => { |
|
|
|
// bind listener when `selected` is truthy
|
|
|
|
// bind listener when `selected` is truthy
|
|
|
|
if (nextVal) { |
|
|
|
if (nextVal) { |
|
|
|
document.addEventListener('keydown', handler, true) |
|
|
|
document.addEventListener('keydown', handler, true) |
|
|
@ -16,6 +22,10 @@ export function useSelectedCellKeyupListener(selected: Ref<boolean>, handler: (e |
|
|
|
cleanup(() => { |
|
|
|
cleanup(() => { |
|
|
|
document.removeEventListener('keydown', handler, true) |
|
|
|
document.removeEventListener('keydown', handler, true) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
{ immediate }, |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export { useSelectedCellKeyupListener, useSelectedCellKeyupListener as useActiveKeyupListener } |
|
|
|