Use a typescript is function to extract event target node

This commit is contained in:
Jackson Harper
2023-10-12 10:22:34 +08:00
parent 0a8d6515f9
commit c75c38b01b

View File

@ -419,15 +419,25 @@ export default function PdfArticleContainer(
}
)
function keyDownHandler(event: globalThis.KeyboardEvent) {
type PossibleInputEventTarget = KeyboardEvent & {
nodeName: string
}
function isPossibleInputEventTarget(
target: any
): target is PossibleInputEventTarget {
return (
'nodeName' in target &&
typeof target.nodeName == 'string' &&
target.nodeName
)
}
function keyDownHandler(event: KeyboardEvent) {
const inputs = ['input', 'select', 'button', 'textarea']
if (
event.target &&
'nodeName' in event.target &&
typeof event.target.nodeName == 'string'
) {
const nodeName = (event.target.nodeName as string).toLowerCase()
if (event.target && isPossibleInputEventTarget(event.target)) {
const nodeName = event.target.nodeName.toLowerCase()
if (inputs.indexOf(nodeName) != -1) {
return
}