@lightningtv/core
Advanced tools
Comparing version 2.5.10 to 2.6.0
@@ -416,4 +416,4 @@ import { renderer, createShader } from './lightningInit.js'; | ||
set autofocus(val) { | ||
this._autofocus = val ? true : false; | ||
this._autofocus && this.setFocus(); | ||
this._autofocus = val; | ||
val && this.setFocus(); | ||
} | ||
@@ -420,0 +420,0 @@ get autofocus() { |
@@ -14,5 +14,6 @@ export type * from './focusKeyTypes.js'; | ||
keyHoldOptions?: KeyHoldOptions; | ||
propagatedUpKeys?: string[]; | ||
ownerContext?: (cb: () => void) => void; | ||
} | ||
export declare const useFocusManager: ({ userKeyMap, keyHoldOptions, ownerContext, }?: FocusManagerOptions) => { | ||
export declare const useFocusManager: ({ userKeyMap, propagatedUpKeys, keyHoldOptions, ownerContext, }?: FocusManagerOptions) => { | ||
cleanup: () => void; | ||
@@ -19,0 +20,0 @@ focusPath: () => ElementNode[]; |
@@ -106,8 +106,11 @@ import { Config } from './config.js'; | ||
}; | ||
const propagateKeyDown = (e, mappedEvent, isHold) => { | ||
const propagateKeyPress = (e, mappedEvent, isHold) => { | ||
let finalFocusElm = undefined; | ||
const isUp = e.type === 'keyup'; | ||
for (const elm of focusPath) { | ||
finalFocusElm = finalFocusElm || elm; | ||
if (mappedEvent) { | ||
const onKeyHandler = elm[`on${mappedEvent}`]; | ||
const onKeyHandler = isUp | ||
? elm[`on${mappedEvent}Up`] | ||
: elm[`on${mappedEvent}`] || elm[`on${mappedEvent}Down`]; | ||
if (isFunction(onKeyHandler) && | ||
@@ -121,2 +124,5 @@ onKeyHandler.call(elm, e, elm, finalFocusElm) === true) { | ||
} | ||
if (isUp && !isHold) { | ||
continue; | ||
} | ||
const fallbackFunction = isHold ? elm.onKeyHold : elm.onKeyPress; | ||
@@ -134,30 +140,31 @@ if (isFunction(fallbackFunction) && | ||
delete keyHoldTimeouts[e.key || e.keyCode]; | ||
propagateKeyDown(e, mappedKeyHoldEvent, true); | ||
propagateKeyPress(e, mappedKeyHoldEvent, true); | ||
}; | ||
const handleKeyEvents = (delay, keypress, keyup) => { | ||
if (keypress) { | ||
const key = keypress.key || keypress.keyCode; | ||
const mappedKeyHoldEvent = keyHoldMapEntries[keypress.key] || keyHoldMapEntries[keypress.keyCode]; | ||
const mappedKeyEvent = keyMapEntries[keypress.key] || keyMapEntries[keypress.keyCode]; | ||
if (!mappedKeyHoldEvent) { | ||
propagateKeyDown(keypress, mappedKeyEvent, false); | ||
} | ||
else { | ||
const handleKeyEvents = (delay, propagatedUpKeys, keydown, keyup) => { | ||
if (keydown) { | ||
const key = keydown.key || keydown.keyCode; | ||
const mappedKeyHoldEvent = keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode]; | ||
const mappedKeyEvent = keyMapEntries[keydown.key] || keyMapEntries[keydown.keyCode]; | ||
if (mappedKeyHoldEvent) { | ||
if (keyHoldTimeouts[key]) { | ||
clearTimeout(keyHoldTimeouts[key]); | ||
} | ||
keyHoldTimeouts[key] = window.setTimeout(() => keyHoldCallback(keypress, mappedKeyHoldEvent), delay); | ||
keyHoldTimeouts[key] = window.setTimeout(() => keyHoldCallback(keydown, mappedKeyHoldEvent), delay); | ||
} | ||
else { | ||
propagateKeyPress(keydown, mappedKeyEvent, false); | ||
} | ||
} | ||
if (keyup) { | ||
else if (keyup) { | ||
const key = keyup.key || keyup.keyCode; | ||
const mappedKeyEvent = keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode]; | ||
if (keyHoldTimeouts[key]) { | ||
if (keyHoldTimeouts[key] || | ||
(mappedKeyEvent && propagatedUpKeys.includes(mappedKeyEvent))) { | ||
clearTimeout(keyHoldTimeouts[key]); | ||
delete keyHoldTimeouts[key]; | ||
propagateKeyDown(keyup, mappedKeyEvent, false); | ||
propagateKeyPress(keyup, mappedKeyEvent, false); | ||
} | ||
} | ||
}; | ||
export const useFocusManager = ({ userKeyMap, keyHoldOptions, ownerContext = (cb) => { | ||
export const useFocusManager = ({ userKeyMap, propagatedUpKeys = [], keyHoldOptions, ownerContext = (cb) => { | ||
cb(); | ||
@@ -172,3 +179,3 @@ }, } = {}) => { | ||
const delay = keyHoldOptions?.holdThreshold || DEFAULT_KEY_HOLD_THRESHOLD; | ||
const runKeyEvent = handleKeyEvents.bind(null, delay); | ||
const runKeyEvent = handleKeyEvents.bind(null, delay, propagatedUpKeys); | ||
// Owner context is for frameworks that need effects | ||
@@ -175,0 +182,0 @@ const keyPressHandler = (event) => ownerContext(() => { |
{ | ||
"name": "@lightningtv/core", | ||
"version": "2.5.10", | ||
"version": "2.6.0", | ||
"description": "Lightning TV Core for Universal Renderers", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -669,4 +669,4 @@ import { renderer, createShader } from './lightningInit.js'; | ||
set autofocus(val: any) { | ||
this._autofocus = val ? true : false; | ||
this._autofocus && this.setFocus(); | ||
this._autofocus = val; | ||
val && this.setFocus(); | ||
} | ||
@@ -673,0 +673,0 @@ |
@@ -142,3 +142,3 @@ import { Config } from './config.js'; | ||
const propagateKeyDown = ( | ||
const propagateKeyPress = ( | ||
e: KeyboardEvent, | ||
@@ -149,6 +149,9 @@ mappedEvent: string | undefined, | ||
let finalFocusElm: ElementNode | undefined = undefined; | ||
const isUp = e.type === 'keyup'; | ||
for (const elm of focusPath) { | ||
finalFocusElm = finalFocusElm || elm; | ||
if (mappedEvent) { | ||
const onKeyHandler = elm[`on${mappedEvent}`]; | ||
const onKeyHandler = isUp | ||
? elm[`on${mappedEvent}Up`] | ||
: elm[`on${mappedEvent}`] || elm[`on${mappedEvent}Down`]; | ||
if ( | ||
@@ -163,2 +166,7 @@ isFunction(onKeyHandler) && | ||
} | ||
if (isUp && !isHold) { | ||
continue; | ||
} | ||
const fallbackFunction = isHold ? elm.onKeyHold : elm.onKeyPress; | ||
@@ -183,3 +191,3 @@ if ( | ||
delete keyHoldTimeouts[e.key || e.keyCode]; | ||
propagateKeyDown(e, mappedKeyHoldEvent, true); | ||
propagateKeyPress(e, mappedKeyHoldEvent, true); | ||
}; | ||
@@ -189,14 +197,13 @@ | ||
delay: number, | ||
keypress?: KeyboardEvent, | ||
propagatedUpKeys: string[], | ||
keydown?: KeyboardEvent, | ||
keyup?: KeyboardEvent, | ||
) => { | ||
if (keypress) { | ||
const key: KeyNameOrKeyCode = keypress.key || keypress.keyCode; | ||
if (keydown) { | ||
const key: KeyNameOrKeyCode = keydown.key || keydown.keyCode; | ||
const mappedKeyHoldEvent = | ||
keyHoldMapEntries[keypress.key] || keyHoldMapEntries[keypress.keyCode]; | ||
keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode]; | ||
const mappedKeyEvent = | ||
keyMapEntries[keypress.key] || keyMapEntries[keypress.keyCode]; | ||
if (!mappedKeyHoldEvent) { | ||
propagateKeyDown(keypress, mappedKeyEvent, false); | ||
} else { | ||
keyMapEntries[keydown.key] || keyMapEntries[keydown.keyCode]; | ||
if (mappedKeyHoldEvent) { | ||
if (keyHoldTimeouts[key]) { | ||
@@ -206,16 +213,19 @@ clearTimeout(keyHoldTimeouts[key]); | ||
keyHoldTimeouts[key] = window.setTimeout( | ||
() => keyHoldCallback(keypress, mappedKeyHoldEvent), | ||
() => keyHoldCallback(keydown, mappedKeyHoldEvent), | ||
delay, | ||
); | ||
} else { | ||
propagateKeyPress(keydown, mappedKeyEvent, false); | ||
} | ||
} | ||
if (keyup) { | ||
} else if (keyup) { | ||
const key: KeyNameOrKeyCode = keyup.key || keyup.keyCode; | ||
const mappedKeyEvent = | ||
keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode]; | ||
if (keyHoldTimeouts[key]) { | ||
if ( | ||
keyHoldTimeouts[key] || | ||
(mappedKeyEvent && propagatedUpKeys.includes(mappedKeyEvent)) | ||
) { | ||
clearTimeout(keyHoldTimeouts[key]); | ||
delete keyHoldTimeouts[key]; | ||
propagateKeyDown(keyup, mappedKeyEvent, false); | ||
propagateKeyPress(keyup, mappedKeyEvent, false); | ||
} | ||
@@ -228,2 +238,3 @@ } | ||
keyHoldOptions?: KeyHoldOptions; | ||
propagatedUpKeys?: string[]; | ||
ownerContext?: (cb: () => void) => void; | ||
@@ -234,2 +245,3 @@ } | ||
userKeyMap, | ||
propagatedUpKeys = [], | ||
keyHoldOptions, | ||
@@ -249,3 +261,3 @@ ownerContext = (cb) => { | ||
const delay = keyHoldOptions?.holdThreshold || DEFAULT_KEY_HOLD_THRESHOLD; | ||
const runKeyEvent = handleKeyEvents.bind(null, delay); | ||
const runKeyEvent = handleKeyEvents.bind(null, delay, propagatedUpKeys); | ||
@@ -252,0 +264,0 @@ // Owner context is for frameworks that need effects |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
231942
3603