@solidtv/solid 1.4.1-1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/focusKeyTypes.d.ts +0 -10
- package/dist/src/core/focusManager.d.ts +6 -16
- package/dist/src/core/focusManager.js +64 -103
- package/dist/src/core/focusManager.js.map +1 -1
- package/dist/src/core/intrinsicTypes.d.ts +2 -2
- package/dist/src/primitives/useFocusManager.d.ts +1 -1
- package/dist/src/primitives/useFocusManager.js +1 -1
- package/dist/src/primitives/useFocusManager.js.map +1 -1
- package/dist/src/primitives/useHold.d.ts +14 -23
- package/dist/src/primitives/useHold.js +8 -27
- package/dist/src/primitives/useHold.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/core/focusKeyTypes.ts +0 -18
- package/src/core/focusManager.ts +72 -123
- package/src/core/intrinsicTypes.ts +1 -7
- package/src/primitives/useFocusManager.ts +3 -3
- package/src/primitives/useHold.ts +17 -46
package/package.json
CHANGED
|
@@ -28,13 +28,6 @@ export interface FocusNode {
|
|
|
28
28
|
handlerElm: ElementNode,
|
|
29
29
|
currentFocusedElm: ElementNode,
|
|
30
30
|
) => KeyHandlerReturn;
|
|
31
|
-
onKeyHold?: (
|
|
32
|
-
this: ElementNode,
|
|
33
|
-
e: KeyboardEvent,
|
|
34
|
-
mappedKeyEvent: string | undefined,
|
|
35
|
-
handlerElm: ElementNode,
|
|
36
|
-
currentFocusedElm: ElementNode,
|
|
37
|
-
) => KeyHandlerReturn;
|
|
38
31
|
}
|
|
39
32
|
|
|
40
33
|
export type KeyNameOrKeyCode = string | number;
|
|
@@ -52,10 +45,6 @@ export interface KeyMap extends DefaultKeyMap {
|
|
|
52
45
|
[key: string]: KeyNameOrKeyCode | KeyNameOrKeyCode[] | null;
|
|
53
46
|
}
|
|
54
47
|
|
|
55
|
-
export interface DefaultKeyHoldMap {
|
|
56
|
-
EnterHold: KeyNameOrKeyCode | KeyNameOrKeyCode[] | null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
48
|
export type EventHandlers<Map> = {
|
|
60
49
|
[K in keyof Map as `on${Capitalize<string & K>}`]?: KeyHandler;
|
|
61
50
|
} & {
|
|
@@ -67,8 +56,6 @@ export type EventHandlers<Map> = {
|
|
|
67
56
|
onCaptureKeyRelease?: KeyHandler;
|
|
68
57
|
};
|
|
69
58
|
|
|
70
|
-
export interface KeyHoldMap extends DefaultKeyHoldMap {}
|
|
71
|
-
|
|
72
59
|
export type KeyHandlerReturn = boolean | void;
|
|
73
60
|
|
|
74
61
|
export type KeyHandler = (
|
|
@@ -83,8 +70,3 @@ export type ForwardFocusHandler = (
|
|
|
83
70
|
this: ElementNode,
|
|
84
71
|
elm: ElementNode,
|
|
85
72
|
) => boolean | void;
|
|
86
|
-
|
|
87
|
-
export type KeyHoldOptions = {
|
|
88
|
-
userKeyHoldMap: Partial<KeyHoldMap>;
|
|
89
|
-
holdThreshold?: number;
|
|
90
|
-
};
|
package/src/core/focusManager.ts
CHANGED
|
@@ -3,11 +3,7 @@ import { Config, isDev } from './config.js';
|
|
|
3
3
|
import { IRendererNode } from './dom-renderer/domRendererTypes.js';
|
|
4
4
|
export type * from './focusKeyTypes.js';
|
|
5
5
|
import { ElementNode } from './elementNode.js';
|
|
6
|
-
import type {
|
|
7
|
-
KeyNameOrKeyCode,
|
|
8
|
-
KeyHoldOptions,
|
|
9
|
-
KeyMap,
|
|
10
|
-
} from './focusKeyTypes.js';
|
|
6
|
+
import type { KeyNameOrKeyCode, KeyMap } from './focusKeyTypes.js';
|
|
11
7
|
import { isFunction } from './utils.js';
|
|
12
8
|
import {
|
|
13
9
|
activeElement,
|
|
@@ -30,9 +26,7 @@ const keyMapEntries: KeyMapEntries = {
|
|
|
30
26
|
Escape: 'Escape',
|
|
31
27
|
};
|
|
32
28
|
|
|
33
|
-
const
|
|
34
|
-
// Enter: 'EnterHold',
|
|
35
|
-
};
|
|
29
|
+
const keyOf = (e: KeyboardEvent): KeyNameOrKeyCode => e.key || e.keyCode;
|
|
36
30
|
|
|
37
31
|
const flattenKeyMap = (
|
|
38
32
|
keyMap: Partial<KeyMap>,
|
|
@@ -333,7 +327,6 @@ const runBubblePhase = (
|
|
|
333
327
|
fp: ElementNode[],
|
|
334
328
|
e: KeyboardEvent,
|
|
335
329
|
mappedEvent: string | undefined,
|
|
336
|
-
isHold: boolean,
|
|
337
330
|
isUp: boolean,
|
|
338
331
|
sameKey: boolean,
|
|
339
332
|
currentTime: number,
|
|
@@ -344,11 +337,9 @@ const runBubblePhase = (
|
|
|
344
337
|
? `on${mappedEvent}Release`
|
|
345
338
|
: `on${mappedEvent}`
|
|
346
339
|
: undefined;
|
|
347
|
-
const fallbackHandlerKey: '
|
|
340
|
+
const fallbackHandlerKey: 'onKeyPress' | undefined = isUp
|
|
348
341
|
? undefined
|
|
349
|
-
:
|
|
350
|
-
? 'onKeyHold'
|
|
351
|
-
: 'onKeyPress';
|
|
342
|
+
: 'onKeyPress';
|
|
352
343
|
|
|
353
344
|
let lastHandlerSeen: ElementNode | undefined;
|
|
354
345
|
|
|
@@ -387,11 +378,10 @@ const runBubblePhase = (
|
|
|
387
378
|
const propagateKeyPress = (
|
|
388
379
|
e: KeyboardEvent,
|
|
389
380
|
mappedEvent?: string,
|
|
390
|
-
isHold: boolean = false,
|
|
391
381
|
isUp: boolean = false,
|
|
392
382
|
): boolean => {
|
|
393
383
|
const currentTime = performance.now();
|
|
394
|
-
const key = e
|
|
384
|
+
const key = keyOf(e);
|
|
395
385
|
const sameKey = lastInputKey === key;
|
|
396
386
|
lastInputKey = key;
|
|
397
387
|
|
|
@@ -426,7 +416,6 @@ const propagateKeyPress = (
|
|
|
426
416
|
fp,
|
|
427
417
|
e,
|
|
428
418
|
mappedEvent,
|
|
429
|
-
isHold,
|
|
430
419
|
isUp,
|
|
431
420
|
sameKey,
|
|
432
421
|
currentTime,
|
|
@@ -434,7 +423,7 @@ const propagateKeyPress = (
|
|
|
434
423
|
if (handled) return true;
|
|
435
424
|
|
|
436
425
|
if (isDev && Config.keyDebug && !isUp) {
|
|
437
|
-
const detail = `key="${e.key}", mappedEvent=${mappedEvent},
|
|
426
|
+
const detail = `key="${e.key}", mappedEvent=${mappedEvent}, isUp=${isUp}`;
|
|
438
427
|
if (lastHandlerSeen) {
|
|
439
428
|
console.log(`Keypress bubbled, ${detail}`, lastHandlerSeen);
|
|
440
429
|
} else {
|
|
@@ -445,52 +434,27 @@ const propagateKeyPress = (
|
|
|
445
434
|
return false;
|
|
446
435
|
};
|
|
447
436
|
|
|
448
|
-
|
|
449
|
-
|
|
437
|
+
// `key` is not a stable identity for a physical key across key-down and key-up.
|
|
438
|
+
// webOS reports Back's key-down as { key: 'GoBack', keyCode: 461 } and its
|
|
439
|
+
// key-up as { key: 'Unidentified', keyCode: 461 } — same key, two names, with
|
|
440
|
+
// only the keyCode shared. So a key is identified by *every* name its event
|
|
441
|
+
// carries, and two events are the same key if any identity matches.
|
|
442
|
+
const UNIDENTIFIED = 'Unidentified';
|
|
450
443
|
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
// Keys this platform has actually been seen delivering a key-up for.
|
|
454
|
-
//
|
|
455
|
-
// Whether a hold can be detected by timer alone comes down to this. If a key
|
|
456
|
-
// delivers key-up, reaching the hold threshold without one means the key is
|
|
457
|
-
// genuinely still down, and no auto-repeat is needed to confirm the hold. If it
|
|
458
|
-
// does not (webOS OK), a press that is already over is indistinguishable from
|
|
459
|
-
// one still held, so only an auto-repeat can confirm it.
|
|
460
|
-
//
|
|
461
|
-
// This is per-key, not per-platform: webOS emits auto-repeat for OK but not for
|
|
462
|
-
// Back, and swallows key-up for OK but not for Back.
|
|
463
|
-
const keysSeenReleasing = new Set<KeyNameOrKeyCode>();
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Record that `keyOrEvent` delivered a key-up. The focus manager calls this for
|
|
467
|
-
* every key-up it sees; call it directly only when driving key events without
|
|
468
|
-
* {@link useFocusManager}.
|
|
469
|
-
*/
|
|
470
|
-
export const noteKeyUpDelivered = (
|
|
444
|
+
const keyIdentities = (
|
|
471
445
|
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
472
|
-
):
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
446
|
+
): KeyNameOrKeyCode[] => {
|
|
447
|
+
if (typeof keyOrEvent !== 'object') return [keyOrEvent];
|
|
448
|
+
const ids: KeyNameOrKeyCode[] = [];
|
|
449
|
+
// 'Unidentified' names no particular key. Treating it as an identity would
|
|
450
|
+
// conflate every key that reports it.
|
|
451
|
+
if (keyOrEvent.key && keyOrEvent.key !== UNIDENTIFIED)
|
|
452
|
+
ids.push(keyOrEvent.key);
|
|
453
|
+
if (keyOrEvent.keyCode) ids.push(keyOrEvent.keyCode);
|
|
454
|
+
return ids;
|
|
476
455
|
};
|
|
477
456
|
|
|
478
|
-
|
|
479
|
-
* Whether this key has been observed delivering a key-up in this session.
|
|
480
|
-
*
|
|
481
|
-
* `false` for a key not yet pressed — absence of evidence, not evidence that the
|
|
482
|
-
* key never releases. Callers that need a decision before any press has been
|
|
483
|
-
* observed should be told the answer explicitly rather than inferring it.
|
|
484
|
-
*/
|
|
485
|
-
export const keyDeliversKeyUp = (
|
|
486
|
-
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
487
|
-
): boolean =>
|
|
488
|
-
keysSeenReleasing.has(
|
|
489
|
-
typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
|
|
490
|
-
);
|
|
491
|
-
|
|
492
|
-
// Keys whose auto-repeat key-downs are dropped before propagation, mapped to an
|
|
493
|
-
// optional callback run when the key is finally released.
|
|
457
|
+
// Keys whose auto-repeat key-downs are dropped before propagation.
|
|
494
458
|
//
|
|
495
459
|
// A hold action typically moves focus (opening a context menu, say) while the
|
|
496
460
|
// key is still physically down. Two things then go wrong, and neither can be
|
|
@@ -502,12 +466,29 @@ export const keyDeliversKeyUp = (
|
|
|
502
466
|
// Both are propagation concerns, so the latch lives here. The release callback
|
|
503
467
|
// is what lets a suppressor still learn about a key-up it can no longer receive
|
|
504
468
|
// through the focus path.
|
|
505
|
-
|
|
469
|
+
type Suppression = {
|
|
470
|
+
ids: KeyNameOrKeyCode[];
|
|
471
|
+
onRelease?: () => void;
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// Indexed under every identity of the suppressed key, so a key-up naming the
|
|
475
|
+
// key differently still finds it. Entries for one key share a Suppression.
|
|
476
|
+
const suppressedKeys = new Map<KeyNameOrKeyCode, Suppression>();
|
|
477
|
+
|
|
478
|
+
const findSuppression = (ids: KeyNameOrKeyCode[]): Suppression | undefined => {
|
|
479
|
+
for (const id of ids) {
|
|
480
|
+
const found = suppressedKeys.get(id);
|
|
481
|
+
if (found) return found;
|
|
482
|
+
}
|
|
483
|
+
return undefined;
|
|
484
|
+
};
|
|
506
485
|
|
|
507
|
-
const liftSuppression = (
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
486
|
+
const liftSuppression = (ids: KeyNameOrKeyCode[]): void => {
|
|
487
|
+
const found = findSuppression(ids);
|
|
488
|
+
if (!found) return;
|
|
489
|
+
// Drop every alias, not just the one that matched.
|
|
490
|
+
for (const id of found.ids) suppressedKeys.delete(id);
|
|
491
|
+
found.onRelease?.();
|
|
511
492
|
};
|
|
512
493
|
|
|
513
494
|
/**
|
|
@@ -519,6 +500,10 @@ const liftSuppression = (key: KeyNameOrKeyCode): void => {
|
|
|
519
500
|
* when suppression lifts, whichever way it lifts, and is delivered regardless of
|
|
520
501
|
* where focus has moved in the meantime.
|
|
521
502
|
*
|
|
503
|
+
* Pass the `KeyboardEvent` where you have one: the key is then matched on both
|
|
504
|
+
* its name and its keyCode, which is what lets a key-up reporting a different
|
|
505
|
+
* `key` for the same physical key (webOS Back) still lift the suppression.
|
|
506
|
+
*
|
|
522
507
|
* `useHold` calls this itself when a hold fires; call it directly only when
|
|
523
508
|
* implementing hold behavior outside that primitive.
|
|
524
509
|
*/
|
|
@@ -526,10 +511,10 @@ export const suppressKeyUntilRelease = (
|
|
|
526
511
|
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
527
512
|
onRelease?: () => void,
|
|
528
513
|
): void => {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
);
|
|
514
|
+
const ids = keyIdentities(keyOrEvent);
|
|
515
|
+
if (ids.length === 0) return;
|
|
516
|
+
const suppression: Suppression = { ids, onRelease };
|
|
517
|
+
for (const id of ids) suppressedKeys.set(id, suppression);
|
|
533
518
|
};
|
|
534
519
|
|
|
535
520
|
/**
|
|
@@ -539,72 +524,42 @@ export const suppressKeyUntilRelease = (
|
|
|
539
524
|
export const releaseKeySuppression = (
|
|
540
525
|
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
541
526
|
): void => {
|
|
542
|
-
liftSuppression(
|
|
543
|
-
typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
|
|
544
|
-
);
|
|
527
|
+
liftSuppression(keyIdentities(keyOrEvent));
|
|
545
528
|
};
|
|
546
529
|
|
|
547
|
-
const handleKeyEvents = (
|
|
548
|
-
delay: number,
|
|
549
|
-
keydown?: KeyboardEvent,
|
|
550
|
-
keyup?: KeyboardEvent,
|
|
551
|
-
) => {
|
|
530
|
+
const handleKeyEvents = (keydown?: KeyboardEvent, keyup?: KeyboardEvent) => {
|
|
552
531
|
if (keydown) {
|
|
553
|
-
const
|
|
532
|
+
const ids = keyIdentities(keydown);
|
|
554
533
|
if (keydown.repeat) {
|
|
555
|
-
if (
|
|
556
|
-
} else
|
|
534
|
+
if (findSuppression(ids)) return;
|
|
535
|
+
} else {
|
|
557
536
|
// A fresh press starts a new gesture, so the previous one is over even
|
|
558
537
|
// though its key-up never arrived. Settle it before handling this press.
|
|
559
|
-
liftSuppression(
|
|
560
|
-
}
|
|
561
|
-
const mappedKeyHoldEvent =
|
|
562
|
-
keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode];
|
|
563
|
-
const mappedKeyEvent =
|
|
564
|
-
keyMapEntries[keydown.key] || keyMapEntries[keydown.keyCode];
|
|
565
|
-
if (mappedKeyHoldEvent) {
|
|
566
|
-
if (!keyHoldTimeouts[key]) {
|
|
567
|
-
keyHoldTimeouts[key] = window.setTimeout(() => {
|
|
568
|
-
keyHoldTimeouts[key] = true;
|
|
569
|
-
propagateKeyPress(keydown, mappedKeyHoldEvent, true);
|
|
570
|
-
}, delay);
|
|
571
|
-
}
|
|
572
|
-
return;
|
|
538
|
+
liftSuppression(ids);
|
|
573
539
|
}
|
|
574
540
|
|
|
575
|
-
propagateKeyPress(
|
|
541
|
+
propagateKeyPress(
|
|
542
|
+
keydown,
|
|
543
|
+
keyMapEntries[keydown.key] || keyMapEntries[keydown.keyCode],
|
|
544
|
+
);
|
|
576
545
|
} else if (keyup) {
|
|
577
|
-
const key: KeyNameOrKeyCode = keyOf(keyup);
|
|
578
|
-
keysSeenReleasing.add(key);
|
|
579
546
|
// The key is up: whatever was suppressing its repeats is done. Settle it
|
|
580
547
|
// before propagating, so a suppressor that is still in the focus path sees
|
|
581
548
|
// its own release callback rather than a second one via the key-up below.
|
|
582
|
-
|
|
583
|
-
const mappedKeyEvent =
|
|
584
|
-
keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode];
|
|
585
|
-
if (keyHoldTimeouts[key] === true) {
|
|
586
|
-
delete keyHoldTimeouts[key];
|
|
587
|
-
} else if (keyHoldTimeouts[key]) {
|
|
588
|
-
clearTimeout(keyHoldTimeouts[key]);
|
|
589
|
-
delete keyHoldTimeouts[key];
|
|
590
|
-
// trigger key down event when hold didn't finish
|
|
591
|
-
propagateKeyPress(keyup, mappedKeyEvent, false);
|
|
592
|
-
}
|
|
549
|
+
liftSuppression(keyIdentities(keyup));
|
|
593
550
|
|
|
594
|
-
propagateKeyPress(
|
|
551
|
+
propagateKeyPress(
|
|
552
|
+
keyup,
|
|
553
|
+
keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode],
|
|
554
|
+
true,
|
|
555
|
+
);
|
|
595
556
|
}
|
|
596
557
|
};
|
|
597
558
|
|
|
598
|
-
export const useFocusManager = (
|
|
599
|
-
userKeyMap?: Partial<KeyMap>,
|
|
600
|
-
keyHoldOptions?: KeyHoldOptions,
|
|
601
|
-
) => {
|
|
559
|
+
export const useFocusManager = (userKeyMap?: Partial<KeyMap>) => {
|
|
602
560
|
if (userKeyMap) {
|
|
603
561
|
flattenKeyMap(userKeyMap, keyMapEntries);
|
|
604
562
|
}
|
|
605
|
-
if (keyHoldOptions?.userKeyHoldMap) {
|
|
606
|
-
flattenKeyMap(keyHoldOptions.userKeyHoldMap, keyHoldMapEntries);
|
|
607
|
-
}
|
|
608
563
|
|
|
609
564
|
// Capture the calling owner so signal updates and key-event reactions
|
|
610
565
|
// can run inside it — needed for programmatic .setFocus(), post-mutation
|
|
@@ -620,13 +575,10 @@ export const useFocusManager = (
|
|
|
620
575
|
Config.setActiveElement = (elm) =>
|
|
621
576
|
ownerContext(() => setActiveElementSignal(elm));
|
|
622
577
|
|
|
623
|
-
const delay = keyHoldOptions?.holdThreshold || DEFAULT_KEY_HOLD_THRESHOLD;
|
|
624
|
-
const runKeyEvent = handleKeyEvents.bind(null, delay);
|
|
625
|
-
|
|
626
578
|
const keyPressHandler = (event: KeyboardEvent) =>
|
|
627
|
-
ownerContext(() =>
|
|
579
|
+
ownerContext(() => handleKeyEvents(event, undefined));
|
|
628
580
|
const keyUpHandler = (event: KeyboardEvent) =>
|
|
629
|
-
ownerContext(() =>
|
|
581
|
+
ownerContext(() => handleKeyEvents(undefined, event));
|
|
630
582
|
|
|
631
583
|
document.addEventListener('keydown', keyPressHandler);
|
|
632
584
|
document.addEventListener('keyup', keyUpHandler);
|
|
@@ -634,9 +586,6 @@ export const useFocusManager = (
|
|
|
634
586
|
onCleanup(() => {
|
|
635
587
|
document.removeEventListener('keydown', keyPressHandler);
|
|
636
588
|
document.removeEventListener('keyup', keyUpHandler);
|
|
637
|
-
for (const timeout of Object.values(keyHoldTimeouts)) {
|
|
638
|
-
if (timeout && timeout !== true) clearTimeout(timeout);
|
|
639
|
-
}
|
|
640
589
|
suppressedKeys.clear();
|
|
641
590
|
});
|
|
642
591
|
};
|
|
@@ -10,12 +10,7 @@ import {
|
|
|
10
10
|
ShaderRoundedProps,
|
|
11
11
|
ShaderShadowProps,
|
|
12
12
|
} from './shaders.js';
|
|
13
|
-
import {
|
|
14
|
-
EventHandlers,
|
|
15
|
-
DefaultKeyMap,
|
|
16
|
-
KeyHoldMap,
|
|
17
|
-
FocusNode,
|
|
18
|
-
} from './focusKeyTypes.js';
|
|
13
|
+
import { EventHandlers, DefaultKeyMap, FocusNode } from './focusKeyTypes.js';
|
|
19
14
|
import type { JSXElement } from 'solid-js';
|
|
20
15
|
|
|
21
16
|
export type AnimationSettings = Partial<lngr.AnimationSettings>;
|
|
@@ -123,7 +118,6 @@ export interface NodeProps
|
|
|
123
118
|
extends
|
|
124
119
|
RendererNode,
|
|
125
120
|
EventHandlers<DefaultKeyMap>,
|
|
126
|
-
EventHandlers<KeyHoldMap>,
|
|
127
121
|
FocusNode,
|
|
128
122
|
Partial<
|
|
129
123
|
NewOmit<
|
|
@@ -4,9 +4,9 @@ export {
|
|
|
4
4
|
setActiveElementCore,
|
|
5
5
|
suppressKeyUntilRelease,
|
|
6
6
|
releaseKeySuppression,
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
printFocusHistory,
|
|
8
|
+
getFocusHistory,
|
|
9
|
+
type FocusHistoryEntry,
|
|
9
10
|
type KeyMap,
|
|
10
|
-
type KeyHoldOptions,
|
|
11
11
|
} from '../core/focusManager.js';
|
|
12
12
|
export { activeElement, setActiveElement } from '../core/activeElement.js';
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { createMemo } from 'solid-js';
|
|
2
|
-
import {
|
|
3
|
-
suppressKeyUntilRelease,
|
|
4
|
-
keyDeliversKeyUp,
|
|
5
|
-
noteKeyUpDelivered,
|
|
6
|
-
} from '../core/focusManager.js';
|
|
2
|
+
import { suppressKeyUntilRelease } from '../core/focusManager.js';
|
|
7
3
|
import type { ElementNode } from '../core/elementNode.js';
|
|
8
4
|
|
|
9
5
|
/**
|
|
@@ -33,21 +29,17 @@ export type UseHoldProps = {
|
|
|
33
29
|
/**
|
|
34
30
|
* Whether a hold must be confirmed by an auto-repeat key-down.
|
|
35
31
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* swallows key-up does need one, since a finished press is otherwise
|
|
40
|
-
* indistinguishable from one still held.
|
|
41
|
-
* - `false`: always resolve by timer, matching the legacy `keyHoldOptions`
|
|
42
|
-
* behavior. Deterministic from the very first press.
|
|
43
|
-
* - `true`: require an auto-repeat, never resolving a repeat-less press as a
|
|
44
|
-
* hold.
|
|
32
|
+
* `true` (default): a press with neither key-up nor auto-repeat by the
|
|
33
|
+
* threshold resolves as a tap. `false`: it resolves as a hold, matching the
|
|
34
|
+
* behavior of the removed `keyHoldOptions`.
|
|
45
35
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
36
|
+
* Set this to `false` for a key that emits no auto-repeat but does emit
|
|
37
|
+
* key-up on real release: the timer alone can then resolve the hold. It does
|
|
38
|
+
* *not* rescue a key that emits key-up immediately at press time (LG's Back
|
|
39
|
+
* button does this) — that key-up cancels the timer before it fires, and no
|
|
40
|
+
* setting can distinguish such a tap from a hold.
|
|
49
41
|
*/
|
|
50
|
-
holdRequiresRepeat?: boolean
|
|
42
|
+
holdRequiresRepeat?: boolean;
|
|
51
43
|
};
|
|
52
44
|
|
|
53
45
|
/**
|
|
@@ -62,16 +54,11 @@ export type UseHoldProps = {
|
|
|
62
54
|
* when the timer fires it resolves to a hold → `onHold`.
|
|
63
55
|
* - if key-up arrives before the timer, it's a tap → `onEnter` (fires
|
|
64
56
|
* immediately, no latency, on platforms that deliver key-up).
|
|
65
|
-
* - if
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* cost of ~`holdThreshold` ms latency.
|
|
71
|
-
*
|
|
72
|
-
* Auto-repeat is therefore used where it exists and not required where it does
|
|
73
|
-
* not — webOS emits it for OK but not for Back, so Back holds resolve by timer
|
|
74
|
-
* while OK holds are confirmed by repeat.
|
|
57
|
+
* - if neither key-up nor auto-repeat arrives, the timer resolves to a tap →
|
|
58
|
+
* `onEnter` after `holdThreshold` ms. This is the key-up-independent path that
|
|
59
|
+
* keeps taps working on webOS, at the cost of ~`holdThreshold` ms latency.
|
|
60
|
+
* Set `holdRequiresRepeat: false` to resolve this ambiguous case as a hold
|
|
61
|
+
* instead, on platforms that deliver no auto-repeat at all.
|
|
75
62
|
*
|
|
76
63
|
* Once `onHold` fires the key is still physically down, so the focus manager
|
|
77
64
|
* drops its remaining auto-repeats until it is released. Without that, a hold
|
|
@@ -113,18 +100,7 @@ export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
|
|
|
113
100
|
const performOnEnterImmediately = createMemo(
|
|
114
101
|
() => props.performOnEnterImmediately ?? false,
|
|
115
102
|
);
|
|
116
|
-
const holdRequiresRepeat = createMemo(
|
|
117
|
-
() => props.holdRequiresRepeat ?? 'auto',
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
// At the threshold with no auto-repeat seen, is this press still held (a hold)
|
|
121
|
-
// or already over with its key-up swallowed (a tap)? Only a key that delivers
|
|
122
|
-
// key-up can answer that on its own.
|
|
123
|
-
const requiresRepeat = (e: KeyboardEvent | undefined) => {
|
|
124
|
-
const mode = holdRequiresRepeat();
|
|
125
|
-
if (mode !== 'auto') return mode;
|
|
126
|
-
return !(e !== undefined && keyDeliversKeyUp(e));
|
|
127
|
-
};
|
|
103
|
+
const holdRequiresRepeat = createMemo(() => props.holdRequiresRepeat ?? true);
|
|
128
104
|
|
|
129
105
|
let holdTimeout = -1;
|
|
130
106
|
let enterFired = false; // onEnter already fired for this press
|
|
@@ -174,7 +150,7 @@ export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
|
|
|
174
150
|
|
|
175
151
|
holdTimeout = setTimeout(() => {
|
|
176
152
|
holdTimeout = -1;
|
|
177
|
-
if (repeated || !
|
|
153
|
+
if (repeated || !holdRequiresRepeat()) {
|
|
178
154
|
// Held past the threshold → hold gesture.
|
|
179
155
|
holdFired = true;
|
|
180
156
|
// The key is still down and will keep repeating. Drop those repeats so
|
|
@@ -194,11 +170,6 @@ export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
|
|
|
194
170
|
};
|
|
195
171
|
|
|
196
172
|
const releaseHold: HoldHandler = (e, target, handlerElm) => {
|
|
197
|
-
// Reaching here at all is proof this key delivers key-up, which is what
|
|
198
|
-
// `'auto'` reads. The focus manager records this too; doing it here as well
|
|
199
|
-
// keeps `'auto'` working behind a custom focus manager.
|
|
200
|
-
if (e) noteKeyUpDelivered(e);
|
|
201
|
-
|
|
202
173
|
if (holdTimeout !== -1) {
|
|
203
174
|
// Released before the threshold → tap. Fires immediately where key-up is
|
|
204
175
|
// delivered, avoiding the timer latency.
|