mirror of https://github.com/nocodb/nocodb
Raju Udava
2 years ago
committed by
GitHub
15 changed files with 192 additions and 89 deletions
@ -1,21 +1,31 @@ |
|||||||
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( |
||||||
// bind listener when `selected` is truthy
|
selected, |
||||||
if (nextVal) { |
(nextVal: boolean, _: boolean, cleanup) => { |
||||||
document.addEventListener('keydown', handler, true) |
// bind listener when `selected` is truthy
|
||||||
// if `selected` is falsy then remove the event handler
|
if (nextVal) { |
||||||
} else { |
document.addEventListener('keydown', handler, true) |
||||||
document.removeEventListener('keydown', handler, true) |
// if `selected` is falsy then remove the event handler
|
||||||
} |
} else { |
||||||
|
document.removeEventListener('keydown', handler, true) |
||||||
|
} |
||||||
|
|
||||||
// cleanup is called whenever the watcher is re-evaluated or stopped
|
// cleanup is called whenever the watcher is re-evaluated or stopped
|
||||||
cleanup(() => { |
cleanup(() => { |
||||||
document.removeEventListener('keydown', handler, true) |
document.removeEventListener('keydown', handler, true) |
||||||
}) |
}) |
||||||
}) |
}, |
||||||
|
{ immediate }, |
||||||
|
) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
export { useSelectedCellKeyupListener, useSelectedCellKeyupListener as useActiveKeyupListener } |
||||||
|
Loading…
Reference in new issue