@solidtv/solid 1.3.9 → 1.4.1-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/README.md +4 -1
- package/dist/src/core/dom-renderer/domRenderer.js +16 -7
- package/dist/src/core/dom-renderer/domRenderer.js.map +1 -1
- package/dist/src/core/focusManager.d.ts +19 -1
- package/dist/src/core/focusManager.js +59 -2
- package/dist/src/core/focusManager.js.map +1 -1
- package/dist/src/core/lightningInit.d.ts +12 -0
- package/dist/src/core/lightningInit.js +76 -2
- package/dist/src/core/lightningInit.js.map +1 -1
- package/dist/src/mergeProps.d.ts +27 -0
- package/dist/src/mergeProps.js +41 -0
- package/dist/src/mergeProps.js.map +1 -0
- 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 +41 -5
- package/dist/src/primitives/useHold.js +51 -11
- package/dist/src/primitives/useHold.js.map +1 -1
- package/dist/src/render.d.ts +2 -2
- package/dist/src/render.js +5 -1
- package/dist/src/render.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/core/dom-renderer/domRenderer.ts +18 -7
- package/src/core/focusManager.ts +71 -2
- package/src/core/lightningInit.ts +99 -2
- package/src/mergeProps.ts +48 -0
- package/src/primitives/useFocusManager.ts +2 -0
- package/src/primitives/useHold.ts +88 -15
- package/src/render.ts +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solidtv/solid",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1-0",
|
|
4
4
|
"description": "SolidTV",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -109,7 +109,7 @@
|
|
|
109
109
|
},
|
|
110
110
|
"peerDependencies": {
|
|
111
111
|
"@solidjs/router": "^0.16.1",
|
|
112
|
-
"@solidtv/renderer": "^1.
|
|
112
|
+
"@solidtv/renderer": "^1.6.3",
|
|
113
113
|
"solid-js": "*"
|
|
114
114
|
},
|
|
115
115
|
"peerDependenciesMeta": {},
|
|
@@ -2279,7 +2279,7 @@ export class DOMRendererMain implements IRendererMain {
|
|
|
2279
2279
|
}
|
|
2280
2280
|
|
|
2281
2281
|
export function loadFontToDom(font: FontLoadOptions): void {
|
|
2282
|
-
// fontFamily: string;
|
|
2282
|
+
// fontFamily: string | string[];
|
|
2283
2283
|
// metrics?: FontMetrics;
|
|
2284
2284
|
// fontUrl?: string;
|
|
2285
2285
|
// atlasUrl?: string;
|
|
@@ -2294,16 +2294,27 @@ export function loadFontToDom(font: FontLoadOptions): void {
|
|
|
2294
2294
|
return;
|
|
2295
2295
|
}
|
|
2296
2296
|
|
|
2297
|
-
const fontFace = new FontFace(font.fontFamily, `url(${font.fontUrl})`);
|
|
2298
2297
|
const fontSet = document.fonts as FontFaceSet & {
|
|
2299
2298
|
add?: (font: FontFace) => FontFaceSet;
|
|
2300
2299
|
};
|
|
2301
|
-
fontSet.add?.(fontFace);
|
|
2302
2300
|
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2301
|
+
// One load may register the same file under several family names. Each name
|
|
2302
|
+
// needs its own FontFace — the browser dedupes the download by URL — so that
|
|
2303
|
+
// text referring to any of them resolves. Passing the array straight to
|
|
2304
|
+
// FontFace would stringify it into a single bogus family.
|
|
2305
|
+
const names = Array.isArray(font.fontFamily)
|
|
2306
|
+
? font.fontFamily
|
|
2307
|
+
: [font.fontFamily];
|
|
2308
|
+
|
|
2309
|
+
for (let i = 0; i < names.length; i++) {
|
|
2310
|
+
const fontFace = new FontFace(names[i]!, `url(${font.fontUrl})`);
|
|
2311
|
+
fontSet.add?.(fontFace);
|
|
2312
|
+
|
|
2313
|
+
fontFace
|
|
2314
|
+
.load()
|
|
2315
|
+
.then(scheduleContainTextNodesMeasurement)
|
|
2316
|
+
.catch(() => {});
|
|
2317
|
+
}
|
|
2307
2318
|
}
|
|
2308
2319
|
|
|
2309
2320
|
export function isDomRenderer(
|
package/src/core/focusManager.ts
CHANGED
|
@@ -448,13 +448,77 @@ const propagateKeyPress = (
|
|
|
448
448
|
const DEFAULT_KEY_HOLD_THRESHOLD = 500; // ms
|
|
449
449
|
const keyHoldTimeouts: { [key: KeyNameOrKeyCode]: number | true } = {};
|
|
450
450
|
|
|
451
|
+
const keyOf = (e: KeyboardEvent): KeyNameOrKeyCode => e.key || e.keyCode;
|
|
452
|
+
|
|
453
|
+
// Keys whose auto-repeat key-downs are dropped before propagation, mapped to an
|
|
454
|
+
// optional callback run when the key is finally released.
|
|
455
|
+
//
|
|
456
|
+
// A hold action typically moves focus (opening a context menu, say) while the
|
|
457
|
+
// key is still physically down. Two things then go wrong, and neither can be
|
|
458
|
+
// fixed by the element that owns the hold, because it is no longer in the focus
|
|
459
|
+
// path: the trailing auto-repeats propagate down the *new* focus path and fire
|
|
460
|
+
// whatever just took focus, and the key-up goes there too, so the owner's
|
|
461
|
+
// release handler never runs.
|
|
462
|
+
//
|
|
463
|
+
// Both are propagation concerns, so the latch lives here. The release callback
|
|
464
|
+
// is what lets a suppressor still learn about a key-up it can no longer receive
|
|
465
|
+
// through the focus path.
|
|
466
|
+
const suppressedKeys = new Map<KeyNameOrKeyCode, (() => void) | undefined>();
|
|
467
|
+
|
|
468
|
+
const liftSuppression = (key: KeyNameOrKeyCode): void => {
|
|
469
|
+
const onRelease = suppressedKeys.get(key);
|
|
470
|
+
suppressedKeys.delete(key);
|
|
471
|
+
onRelease?.();
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Drop auto-repeat key-downs for `keyOrEvent` until the key is released.
|
|
476
|
+
*
|
|
477
|
+
* Suppression is lifted by the key's key-up, or by the next fresh (non-repeat)
|
|
478
|
+
* key-down — the latter so platforms that swallow key-up (webOS) can't wedge a
|
|
479
|
+
* key permanently. Non-repeat key-downs are never suppressed. `onRelease` runs
|
|
480
|
+
* when suppression lifts, whichever way it lifts, and is delivered regardless of
|
|
481
|
+
* where focus has moved in the meantime.
|
|
482
|
+
*
|
|
483
|
+
* `useHold` calls this itself when a hold fires; call it directly only when
|
|
484
|
+
* implementing hold behavior outside that primitive.
|
|
485
|
+
*/
|
|
486
|
+
export const suppressKeyUntilRelease = (
|
|
487
|
+
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
488
|
+
onRelease?: () => void,
|
|
489
|
+
): void => {
|
|
490
|
+
suppressedKeys.set(
|
|
491
|
+
typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
|
|
492
|
+
onRelease,
|
|
493
|
+
);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Lift suppression added by {@link suppressKeyUntilRelease} early, running its
|
|
498
|
+
* release callback.
|
|
499
|
+
*/
|
|
500
|
+
export const releaseKeySuppression = (
|
|
501
|
+
keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
|
|
502
|
+
): void => {
|
|
503
|
+
liftSuppression(
|
|
504
|
+
typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
|
|
505
|
+
);
|
|
506
|
+
};
|
|
507
|
+
|
|
451
508
|
const handleKeyEvents = (
|
|
452
509
|
delay: number,
|
|
453
510
|
keydown?: KeyboardEvent,
|
|
454
511
|
keyup?: KeyboardEvent,
|
|
455
512
|
) => {
|
|
456
513
|
if (keydown) {
|
|
457
|
-
const key: KeyNameOrKeyCode = keydown
|
|
514
|
+
const key: KeyNameOrKeyCode = keyOf(keydown);
|
|
515
|
+
if (keydown.repeat) {
|
|
516
|
+
if (suppressedKeys.has(key)) return;
|
|
517
|
+
} else if (suppressedKeys.has(key)) {
|
|
518
|
+
// A fresh press starts a new gesture, so the previous one is over even
|
|
519
|
+
// though its key-up never arrived. Settle it before handling this press.
|
|
520
|
+
liftSuppression(key);
|
|
521
|
+
}
|
|
458
522
|
const mappedKeyHoldEvent =
|
|
459
523
|
keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode];
|
|
460
524
|
const mappedKeyEvent =
|
|
@@ -471,7 +535,11 @@ const handleKeyEvents = (
|
|
|
471
535
|
|
|
472
536
|
propagateKeyPress(keydown, mappedKeyEvent, false);
|
|
473
537
|
} else if (keyup) {
|
|
474
|
-
const key: KeyNameOrKeyCode = keyup
|
|
538
|
+
const key: KeyNameOrKeyCode = keyOf(keyup);
|
|
539
|
+
// The key is up: whatever was suppressing its repeats is done. Settle it
|
|
540
|
+
// before propagating, so a suppressor that is still in the focus path sees
|
|
541
|
+
// its own release callback rather than a second one via the key-up below.
|
|
542
|
+
if (suppressedKeys.has(key)) liftSuppression(key);
|
|
475
543
|
const mappedKeyEvent =
|
|
476
544
|
keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode];
|
|
477
545
|
if (keyHoldTimeouts[key] === true) {
|
|
@@ -529,5 +597,6 @@ export const useFocusManager = (
|
|
|
529
597
|
for (const timeout of Object.values(keyHoldTimeouts)) {
|
|
530
598
|
if (timeout && timeout !== true) clearTimeout(timeout);
|
|
531
599
|
}
|
|
600
|
+
suppressedKeys.clear();
|
|
532
601
|
});
|
|
533
602
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as lng from '@solidtv/renderer';
|
|
2
|
+
import { prefetchFont } from '@solidtv/renderer';
|
|
2
3
|
import { Config, DOM_RENDERING } from './config.js';
|
|
3
4
|
import { DOMRendererMain, loadFontToDom } from './dom-renderer/domRenderer.js';
|
|
4
5
|
import { DomRendererMainSettings } from './dom-renderer/domRendererTypes.js';
|
|
@@ -21,17 +22,36 @@ export function startLightningRenderer(
|
|
|
21
22
|
renderer = enableDomRenderer
|
|
22
23
|
? new DOMRendererMain(options, rootId)
|
|
23
24
|
: new lng.RendererMain(options, rootId);
|
|
25
|
+
|
|
26
|
+
// A stage now exists, so any fonts requested before this point can finish.
|
|
27
|
+
flushPendingFonts();
|
|
28
|
+
|
|
24
29
|
return renderer;
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
|
|
32
|
+
/**
|
|
33
|
+
* A `loadFonts()` call made before the renderer existed. The download is
|
|
34
|
+
* already in flight; only the stage-dependent half is still owed.
|
|
35
|
+
*/
|
|
36
|
+
interface PendingFontLoad {
|
|
37
|
+
fonts: FontLoadOptions[];
|
|
38
|
+
resolve: () => void;
|
|
39
|
+
reject: (reason?: unknown) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const pendingFontLoads: PendingFontLoad[] = [];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Hand fonts to the stage. Requires a renderer.
|
|
46
|
+
*/
|
|
47
|
+
function attachFonts(fonts: FontLoadOptions[]) {
|
|
28
48
|
// Inlined so the loadFontToDom branch + import tree-shake in WebGL builds.
|
|
29
49
|
const enableDomRenderer = DOM_RENDERING && Config.domRendererEnabled;
|
|
30
50
|
const hasCanvas =
|
|
31
51
|
!enableDomRenderer &&
|
|
32
52
|
'textRenderers' in renderer.stage &&
|
|
33
53
|
!!(renderer.stage as lng.Stage).textRenderers.canvas;
|
|
34
|
-
|
|
54
|
+
return Promise.all(
|
|
35
55
|
fonts.map((font) => {
|
|
36
56
|
// WebGL — SDF
|
|
37
57
|
if (
|
|
@@ -52,3 +72,80 @@ export async function loadFonts(fonts: FontLoadOptions[]) {
|
|
|
52
72
|
}),
|
|
53
73
|
);
|
|
54
74
|
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether the app will run an SDF text engine. Read from the renderer options
|
|
78
|
+
* rather than the stage, because this has to be answerable before the renderer
|
|
79
|
+
* is constructed. Unknown (no `fontEngines` configured yet) is treated as
|
|
80
|
+
* "maybe", matching the stage's own SDF-first preference order.
|
|
81
|
+
*/
|
|
82
|
+
function mayUseSdf() {
|
|
83
|
+
const engines = (Config.rendererOptions as lng.RendererMainSettings)
|
|
84
|
+
?.fontEngines;
|
|
85
|
+
if (engines === undefined || engines.length === 0) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
return engines.some((engine) => engine.type === 'sdf');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function flushPendingFonts() {
|
|
92
|
+
if (pendingFontLoads.length === 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const queued = pendingFontLoads.splice(0, pendingFontLoads.length);
|
|
97
|
+
for (let i = 0; i < queued.length; i++) {
|
|
98
|
+
const pending = queued[i]!;
|
|
99
|
+
attachFonts(pending.fonts).then(() => pending.resolve(), pending.reject);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Load fonts into the renderer.
|
|
105
|
+
*
|
|
106
|
+
* Can be called either side of `createRenderer()`. Calling it *first* is
|
|
107
|
+
* preferred: the downloads start immediately and overlap the renderer's boot
|
|
108
|
+
* (GL context, shaders, buffers) instead of queueing behind it. The fonts are
|
|
109
|
+
* then attached to the stage as soon as it exists.
|
|
110
|
+
*
|
|
111
|
+
* The returned promise resolves when the fonts are attached to the stage — so
|
|
112
|
+
* when called before `createRenderer()`, it cannot settle until
|
|
113
|
+
* `createRenderer()` has run. Do not `await` it before creating the renderer.
|
|
114
|
+
*/
|
|
115
|
+
export async function loadFonts(fonts: FontLoadOptions[]) {
|
|
116
|
+
if (renderer !== undefined) {
|
|
117
|
+
await attachFonts(fonts);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Inlined so the loadFontToDom branch + import tree-shake in WebGL builds.
|
|
122
|
+
const enableDomRenderer = DOM_RENDERING && Config.domRendererEnabled;
|
|
123
|
+
const preferSdf = mayUseSdf();
|
|
124
|
+
const deferred: FontLoadOptions[] = [];
|
|
125
|
+
|
|
126
|
+
for (let i = 0; i < fonts.length; i++) {
|
|
127
|
+
const font = fonts[i]!;
|
|
128
|
+
|
|
129
|
+
// The DOM path registers fonts with the document, never with a stage —
|
|
130
|
+
// there is nothing to wait for, so finish it here.
|
|
131
|
+
if (enableDomRenderer) {
|
|
132
|
+
if ('fontUrl' in font) {
|
|
133
|
+
loadFontToDom(font);
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Starts the download now; the stage-dependent half is owed until
|
|
139
|
+
// `createRenderer()` runs and `flushPendingFonts()` attaches it.
|
|
140
|
+
prefetchFont(preferSdf ? font : { ...font, type: 'canvas' });
|
|
141
|
+
deferred.push(font);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (deferred.length === 0) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
await new Promise<void>((resolve, reject) => {
|
|
149
|
+
pendingFontLoads.push({ fonts: deferred, resolve, reject });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mergeProps as solidMergeProps } from 'solid-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether the host engine implements ES2015 `Proxy`. Chrome < 49 — notably the
|
|
5
|
+
* Chrome 38 engine on LG webOS 3.x and older Tizen — does not, which changes how
|
|
6
|
+
* solid-js merges props (see {@link mergeProps}).
|
|
7
|
+
*/
|
|
8
|
+
export const SUPPORTS_PROXY = typeof Proxy === 'function';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Mirror of solid-js's internal `resolveSource`: a reactive spread source is
|
|
12
|
+
* handed to `mergeProps` as a thunk; calling it yields the object to merge.
|
|
13
|
+
*/
|
|
14
|
+
function resolveSource(source: unknown): unknown {
|
|
15
|
+
const value =
|
|
16
|
+
typeof source === 'function' ? (source as () => unknown)() : source;
|
|
17
|
+
return value == null ? {} : value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Proxy-free-safe `mergeProps`, re-exported by the renderer so the JSX compiler
|
|
22
|
+
* (`moduleName: '@lightningtv/solid'`) routes every compiled spread through it.
|
|
23
|
+
*
|
|
24
|
+
* With `Proxy` this is solid-js's `mergeProps` unchanged. Without `Proxy`
|
|
25
|
+
* (Chrome 38 / webOS 3), solid-js falls back to a path that enumerates each
|
|
26
|
+
* source via `Object.getOwnPropertyNames`. A reactive spread — e.g.
|
|
27
|
+
* `<Comp {...someFn()} />`, which the compiler passes as a *function* — reaches
|
|
28
|
+
* that fallback as the function itself, so enumeration yields `['length','name']`
|
|
29
|
+
* (or throws on `caller`/`callee`/`arguments` for strict-mode functions) instead
|
|
30
|
+
* of the object's props, and every spread-provided prop is silently dropped.
|
|
31
|
+
*
|
|
32
|
+
* Resolving function sources to their objects first lets the fallback enumerate
|
|
33
|
+
* real props. Getters are preserved, so values stay reactive; only a source
|
|
34
|
+
* whose object *identity* changes over time won't re-track — an accepted limit
|
|
35
|
+
* of running without `Proxy`.
|
|
36
|
+
*
|
|
37
|
+
* @see https://github.com/solidjs/solid/issues/2282
|
|
38
|
+
*/
|
|
39
|
+
export const mergeProps = ((...sources: unknown[]) => {
|
|
40
|
+
if (SUPPORTS_PROXY) {
|
|
41
|
+
return solidMergeProps(...(sources as Parameters<typeof solidMergeProps>));
|
|
42
|
+
}
|
|
43
|
+
return solidMergeProps(
|
|
44
|
+
...(sources.map((source) =>
|
|
45
|
+
typeof source === 'function' ? resolveSource(source) : source,
|
|
46
|
+
) as Parameters<typeof solidMergeProps>),
|
|
47
|
+
);
|
|
48
|
+
}) as typeof solidMergeProps;
|
|
@@ -1,11 +1,41 @@
|
|
|
1
1
|
import { createMemo } from 'solid-js';
|
|
2
|
+
import { suppressKeyUntilRelease } from '../core/focusManager.js';
|
|
3
|
+
import type { ElementNode } from '../core/elementNode.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A hold callback. Receives the `KeyboardEvent` that began the press, the
|
|
7
|
+
* element whose handler was invoked, and the focused (leaf) element at that
|
|
8
|
+
* moment — the same arguments a `KeyHandler` gets. All three are `undefined`
|
|
9
|
+
* when `startHold` was called without them.
|
|
10
|
+
*/
|
|
11
|
+
export type HoldCallback = (
|
|
12
|
+
e?: KeyboardEvent,
|
|
13
|
+
target?: ElementNode,
|
|
14
|
+
handlerElm?: ElementNode,
|
|
15
|
+
) => void;
|
|
16
|
+
|
|
17
|
+
export type HoldHandler = (
|
|
18
|
+
e?: KeyboardEvent,
|
|
19
|
+
target?: ElementNode,
|
|
20
|
+
handlerElm?: ElementNode,
|
|
21
|
+
) => boolean;
|
|
2
22
|
|
|
3
23
|
export type UseHoldProps = {
|
|
4
|
-
onHold:
|
|
5
|
-
onEnter:
|
|
6
|
-
onRelease?:
|
|
24
|
+
onHold: HoldCallback;
|
|
25
|
+
onEnter: HoldCallback;
|
|
26
|
+
onRelease?: HoldCallback;
|
|
7
27
|
holdThreshold?: number;
|
|
8
28
|
performOnEnterImmediately?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a hold must be confirmed by an auto-repeat key-down.
|
|
31
|
+
*
|
|
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
|
+
* legacy `keyHoldOptions` behavior. Set this to `false` on platforms whose
|
|
35
|
+
* remote input layer delivers no auto-repeat, where a hold gesture would
|
|
36
|
+
* otherwise be unreachable.
|
|
37
|
+
*/
|
|
38
|
+
holdRequiresRepeat?: boolean;
|
|
9
39
|
};
|
|
10
40
|
|
|
11
41
|
/**
|
|
@@ -23,10 +53,23 @@ export type UseHoldProps = {
|
|
|
23
53
|
* - if neither key-up nor auto-repeat arrives, the timer resolves to a tap →
|
|
24
54
|
* `onEnter` after `holdThreshold` ms. This is the key-up-independent path that
|
|
25
55
|
* keeps taps working on webOS, at the cost of ~`holdThreshold` ms latency.
|
|
56
|
+
* Set `holdRequiresRepeat: false` to resolve this ambiguous case as a hold
|
|
57
|
+
* instead, on platforms that deliver no auto-repeat at all.
|
|
58
|
+
*
|
|
59
|
+
* Once `onHold` fires the key is still physically down, so the focus manager
|
|
60
|
+
* drops its remaining auto-repeats until it is released. Without that, a hold
|
|
61
|
+
* that moves focus — the canonical case, opening a context menu — would have
|
|
62
|
+
* its trailing repeats fire whatever it just focused. The same latch delivers
|
|
63
|
+
* `onRelease` even though the key-up now propagates somewhere else entirely.
|
|
26
64
|
*
|
|
27
65
|
* `performOnEnterImmediately` keeps the legacy behavior of firing `onEnter` on
|
|
28
66
|
* key-down; a long-press then fires both `onEnter` and `onHold`.
|
|
29
67
|
*
|
|
68
|
+
* Note that `startHold` returns `true`, which ends the bubble phase: ancestor
|
|
69
|
+
* handlers for that key will not run, and whether the press was a tap isn't
|
|
70
|
+
* known until after propagation is over. If an ancestor performs navigation,
|
|
71
|
+
* invoke it from `onEnter` yourself. See the caveat in the docs.
|
|
72
|
+
*
|
|
30
73
|
* @example
|
|
31
74
|
* const [holdRight, releaseRight] = useHold({
|
|
32
75
|
* onHold: handleHoldRight,
|
|
@@ -40,21 +83,31 @@ export type UseHoldProps = {
|
|
|
40
83
|
* onRightRelease={releaseRight}
|
|
41
84
|
* />
|
|
42
85
|
*
|
|
86
|
+
* `startHold` reads `e.repeat` to detect a hold. Used directly as a `KeyHandler`
|
|
87
|
+
* (above) it gets the event; if you wrap it, forward all of the arguments, or
|
|
88
|
+
* hold detection silently never triggers.
|
|
89
|
+
*
|
|
43
90
|
* @param {UseHoldProps} props - The properties for configuring the hold behavior.
|
|
44
|
-
* @returns {[
|
|
91
|
+
* @returns {[HoldHandler, HoldHandler]} A tuple of `startHold` and `releaseHold`.
|
|
45
92
|
*/
|
|
46
93
|
|
|
47
|
-
export function useHold(props: UseHoldProps) {
|
|
94
|
+
export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
|
|
48
95
|
const holdThreshold = createMemo(() => props.holdThreshold ?? 500);
|
|
49
96
|
const performOnEnterImmediately = createMemo(
|
|
50
97
|
() => props.performOnEnterImmediately ?? false,
|
|
51
98
|
);
|
|
99
|
+
const holdRequiresRepeat = createMemo(() => props.holdRequiresRepeat ?? true);
|
|
52
100
|
|
|
53
101
|
let holdTimeout = -1;
|
|
54
102
|
let enterFired = false; // onEnter already fired for this press
|
|
55
103
|
let holdFired = false; // onHold already fired for this press
|
|
56
104
|
let repeated = false; // an auto-repeat key-down was seen (key still held)
|
|
57
105
|
|
|
106
|
+
// Context from the key-down that began the press, replayed to whichever
|
|
107
|
+
// callback resolves it — the timer path has no event of its own, and after a
|
|
108
|
+
// focus-moving hold neither does the release.
|
|
109
|
+
let press: Parameters<HoldCallback> = [];
|
|
110
|
+
|
|
58
111
|
const reset = () => {
|
|
59
112
|
if (holdTimeout !== -1) {
|
|
60
113
|
clearTimeout(holdTimeout);
|
|
@@ -63,9 +116,17 @@ export function useHold(props: UseHoldProps) {
|
|
|
63
116
|
enterFired = false;
|
|
64
117
|
holdFired = false;
|
|
65
118
|
repeated = false;
|
|
119
|
+
press = [];
|
|
66
120
|
};
|
|
67
121
|
|
|
68
|
-
const
|
|
122
|
+
const fireRelease = () => {
|
|
123
|
+
if (!holdFired) return;
|
|
124
|
+
const args = press;
|
|
125
|
+
reset(); // before the callback, so a re-entrant press isn't clobbered
|
|
126
|
+
props.onRelease?.(...args);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const startHold: HoldHandler = (e, target, handlerElm) => {
|
|
69
130
|
// Auto-repeat key-down: the key is still held. Record it so the timer
|
|
70
131
|
// resolves to a hold even if the key-up event never arrives (webOS).
|
|
71
132
|
if (e?.repeat) {
|
|
@@ -76,30 +137,35 @@ export function useHold(props: UseHoldProps) {
|
|
|
76
137
|
// Fresh key-down begins a new press. Reset first so a previous press whose
|
|
77
138
|
// key-up was never delivered doesn't leave us wedged for this one.
|
|
78
139
|
reset();
|
|
140
|
+
press = [e, target, handlerElm];
|
|
79
141
|
|
|
80
142
|
if (performOnEnterImmediately()) {
|
|
81
143
|
enterFired = true;
|
|
82
|
-
props.onEnter();
|
|
144
|
+
props.onEnter(...press);
|
|
83
145
|
}
|
|
84
146
|
|
|
85
147
|
holdTimeout = setTimeout(() => {
|
|
86
148
|
holdTimeout = -1;
|
|
87
|
-
if (repeated) {
|
|
149
|
+
if (repeated || !holdRequiresRepeat()) {
|
|
88
150
|
// Held past the threshold → hold gesture.
|
|
89
151
|
holdFired = true;
|
|
90
|
-
|
|
152
|
+
// The key is still down and will keep repeating. Drop those repeats so
|
|
153
|
+
// they can't reach whatever onHold focuses, and route the key-up back
|
|
154
|
+
// here even if this element is no longer in the focus path.
|
|
155
|
+
if (e) suppressKeyUntilRelease(e, fireRelease);
|
|
156
|
+
props.onHold(...press);
|
|
91
157
|
} else if (!enterFired) {
|
|
92
158
|
// No key-up and no auto-repeat arrived: resolve as a tap so the
|
|
93
159
|
// primary action still fires on remotes that swallow key-up.
|
|
94
160
|
enterFired = true;
|
|
95
|
-
props.onEnter();
|
|
161
|
+
props.onEnter(...press);
|
|
96
162
|
}
|
|
97
163
|
}, holdThreshold()) as unknown as number;
|
|
98
164
|
|
|
99
165
|
return true;
|
|
100
166
|
};
|
|
101
167
|
|
|
102
|
-
const releaseHold = () => {
|
|
168
|
+
const releaseHold: HoldHandler = (e, target, handlerElm) => {
|
|
103
169
|
if (holdTimeout !== -1) {
|
|
104
170
|
// Released before the threshold → tap. Fires immediately where key-up is
|
|
105
171
|
// delivered, avoiding the timer latency.
|
|
@@ -107,12 +173,19 @@ export function useHold(props: UseHoldProps) {
|
|
|
107
173
|
holdTimeout = -1;
|
|
108
174
|
if (!enterFired) {
|
|
109
175
|
enterFired = true;
|
|
110
|
-
props.onEnter(
|
|
176
|
+
props.onEnter(
|
|
177
|
+
e ?? press[0],
|
|
178
|
+
target ?? press[1],
|
|
179
|
+
handlerElm ?? press[2],
|
|
180
|
+
);
|
|
111
181
|
}
|
|
112
|
-
|
|
113
|
-
|
|
182
|
+
reset();
|
|
183
|
+
} else {
|
|
184
|
+
// After a hold, the suppression latch has usually already fired onRelease
|
|
185
|
+
// and reset — `holdFired` is false and this is a no-op.
|
|
186
|
+
fireRelease();
|
|
187
|
+
reset();
|
|
114
188
|
}
|
|
115
|
-
reset();
|
|
116
189
|
return true;
|
|
117
190
|
};
|
|
118
191
|
|
package/src/render.ts
CHANGED
|
@@ -61,10 +61,14 @@ export const {
|
|
|
61
61
|
insert,
|
|
62
62
|
spread,
|
|
63
63
|
setProp,
|
|
64
|
-
mergeProps,
|
|
65
64
|
use,
|
|
66
65
|
} = solidRenderer;
|
|
67
66
|
|
|
67
|
+
// Re-export a Proxy-free-safe mergeProps in place of the renderer's own so the
|
|
68
|
+
// JSX compiler routes compiled spreads through it — fixes dropped spread props
|
|
69
|
+
// on engines without Proxy (Chrome 38 / webOS 3). See ./mergeProps.ts.
|
|
70
|
+
export { mergeProps } from './mergeProps.js';
|
|
71
|
+
|
|
68
72
|
type Task = () => void;
|
|
69
73
|
const taskQueue: Task[] = [];
|
|
70
74
|
let tasksEnabled = false;
|