@solidtv/solid 1.7.1 → 1.7.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidtv/solid",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -73,6 +73,17 @@ export interface Config {
73
73
  lockStyles?: boolean;
74
74
  fontWeightAlias?: Record<string, number | string>;
75
75
  throttleInput?: number;
76
+ /**
77
+ * For devices that never set `KeyboardEvent.repeat`: while a key is held
78
+ * they send a stream of `repeat=false` key-downs, and one key-up on release.
79
+ * With this set (in ms), a key-down of a key that has had no key-up since
80
+ * its previous key-down, arriving within this many ms of it, is marked as a
81
+ * repeat (`repeat` reads `true`) before anything handles it. It must cover
82
+ * the device's initial auto-repeat delay. A key-up that never arrives stops
83
+ * mattering once the window passes. Off by default: on a key that swallows
84
+ * key-up (webOS OK), a second tap inside the window would read as a repeat.
85
+ */
86
+ inferKeyRepeat?: number;
76
87
  /**
77
88
  * Call `preventDefault()` on every key event the app consumed: a handler
78
89
  * returned `true`, or the focus manager dropped the press itself (a
@@ -488,18 +488,62 @@ type Suppression = {
488
488
  // key differently still finds it. Entries for one key share a Suppression.
489
489
  const suppressedKeys = new Map<KeyNameOrKeyCode, Suppression>();
490
490
 
491
- // The suppression of an event's key: looked up under its name, then its
492
- // keyCode (keyNameId, keyCodeId), either of which may be undefined.
491
+ // An event's entry in a map keyed by key identity: looked up under its name,
492
+ // then its keyCode (keyNameId, keyCodeId), either of which may be undefined.
493
+ const findByIdentity = <T>(
494
+ map: Map<KeyNameOrKeyCode, T>,
495
+ name: KeyNameOrKeyCode | undefined,
496
+ code: KeyNameOrKeyCode | undefined,
497
+ ): T | undefined => {
498
+ let found: T | undefined;
499
+ if (name !== undefined) found = map.get(name);
500
+ if (found === undefined && code !== undefined) found = map.get(code);
501
+ return found;
502
+ };
503
+
493
504
  const findSuppression = (
494
505
  name: KeyNameOrKeyCode | undefined,
495
506
  code: KeyNameOrKeyCode | undefined,
496
- ): Suppression | undefined => {
497
- let found: Suppression | undefined;
498
- if (name !== undefined) found = suppressedKeys.get(name);
499
- if (found === undefined && code !== undefined) {
500
- found = suppressedKeys.get(code);
507
+ ): Suppression | undefined => findByIdentity(suppressedKeys, name, code);
508
+
509
+ // Keys down with no key-up yet, for Config.inferKeyRepeat: when each last sent
510
+ // a key-down. Indexed under every identity like suppressedKeys, so a key-up
511
+ // naming the key differently (webOS Back) still ends the press.
512
+ type HeldKey = { ids: KeyNameOrKeyCode[]; time: number };
513
+ const heldKeys = new Map<KeyNameOrKeyCode, HeldKey>();
514
+
515
+ // Marks a key-down as a repeat when it follows a key-down of the same key,
516
+ // with no key-up between, by less than `windowMs`. KeyboardEvent.repeat is a
517
+ // read-only getter; an own property shadows it, so handlers, useHold and the
518
+ // suppression check below all read the same value.
519
+ const inferRepeat = (
520
+ e: KeyboardEvent,
521
+ name: KeyNameOrKeyCode | undefined,
522
+ code: KeyNameOrKeyCode | undefined,
523
+ windowMs: number,
524
+ ): void => {
525
+ const now = performance.now();
526
+ const held = findByIdentity(heldKeys, name, code);
527
+ if (held === undefined) {
528
+ const ids = keyIdentities(e);
529
+ const entry: HeldKey = { ids, time: now };
530
+ for (let i = 0; i < ids.length; i++) heldKeys.set(ids[i]!, entry);
531
+ return;
501
532
  }
502
- return found;
533
+ if (!e.repeat && now - held.time < windowMs) {
534
+ Object.defineProperty(e, 'repeat', { value: true });
535
+ }
536
+ held.time = now;
537
+ };
538
+
539
+ const releaseHeldKey = (
540
+ name: KeyNameOrKeyCode | undefined,
541
+ code: KeyNameOrKeyCode | undefined,
542
+ ): void => {
543
+ const held = findByIdentity(heldKeys, name, code);
544
+ if (!held) return;
545
+ const ids = held.ids;
546
+ for (let i = 0; i < ids.length; i++) heldKeys.delete(ids[i]!);
503
547
  };
504
548
 
505
549
  const liftSuppression = (found: Suppression | undefined): void => {
@@ -559,6 +603,9 @@ const handleKeyEvents = (
559
603
  if (keydown) {
560
604
  const name = keyNameId(keydown);
561
605
  const code = keyCodeId(keydown);
606
+ if (Config.inferKeyRepeat) {
607
+ inferRepeat(keydown, name, code, Config.inferKeyRepeat);
608
+ }
562
609
  if (keydown.repeat) {
563
610
  if (findSuppression(name, code)) return true;
564
611
  } else {
@@ -576,7 +623,10 @@ const handleKeyEvents = (
576
623
  // The key is up: whatever was suppressing its repeats is done. Settle it
577
624
  // before propagating, so a suppressor that is still in the focus path sees
578
625
  // its own release callback rather than a second one via the key-up below.
579
- liftSuppression(findSuppression(keyNameId(keyup), keyCodeId(keyup)));
626
+ const name = keyNameId(keyup);
627
+ const code = keyCodeId(keyup);
628
+ releaseHeldKey(name, code);
629
+ liftSuppression(findSuppression(name, code));
580
630
 
581
631
  return propagateKeyPress(
582
632
  keyup,
@@ -696,5 +746,6 @@ export const useFocusManager = (
696
746
  eventTarget.removeEventListener('keydown', keyPressHandler);
697
747
  eventTarget.removeEventListener('keyup', keyUpHandler);
698
748
  suppressedKeys.clear();
749
+ heldKeys.clear();
699
750
  });
700
751
  };
@@ -254,9 +254,12 @@ function createVirtual<T>(
254
254
  } else {
255
255
  // Initial setup
256
256
  if (c > 0) {
257
+ // Near the end, the window navigation reaches there: it starts
258
+ // at total - displaySize. Subtracting bufferSize as well put
259
+ // the focused item past the last visible slot.
257
260
  start = Math.min(
258
261
  c - (scrollIndex() || 1),
259
- total - props.displaySize - bufferSize(),
262
+ total - props.displaySize,
260
263
  );
261
264
  selected = Math.max(scrollIndex() || 1, c - start);
262
265
  shiftBy = total - c < 3 ? c - total : -1;
@@ -313,7 +316,8 @@ function createVirtual<T>(
313
316
  if (delta === 0 && c > 0) {
314
317
  //initial setup
315
318
  selected = c > startScrolling ? startScrolling : c;
316
- start = Math.max(0, c - startScrolling + 1);
319
+ // The window slot `selected` holds item c (start was one more).
320
+ start = c - selected;
317
321
  shiftBy = c > startScrolling ? -1 : 0;
318
322
  atStart = c < startScrolling;
319
323
  } else if (delta > 0) {
@@ -492,6 +496,7 @@ function createVirtual<T>(
492
496
  } else {
493
497
  view.lng[axis] =
494
498
  shiftPrevChildPos - active[axis] + childSize * slice.shiftBy;
499
+ targetPosition = view.lng[axis];
495
500
  }
496
501
  }
497
502
 
@@ -573,14 +578,20 @@ function createVirtual<T>(
573
578
  viewRef.children[activeIndex]?.setFocus();
574
579
  }
575
580
 
576
- if (shiftBy === 0) return;
577
-
578
- const childSize = computeSize(slice.selected);
579
581
  // Original Position is offset to support scrollToIndex
580
- originalPosition = originalPosition ?? viewRef.lng[axis];
581
- targetPosition = targetPosition ?? viewRef.lng[axis];
582
-
583
- viewRef.lng[axis] = (viewRef.lng[axis] || 0) + childSize * -1;
582
+ originalPosition = originalPosition ?? viewRef.lng[axis] ?? 0;
583
+ // The window is laid out from scratch (delta 0), so the row goes to
584
+ // its original position, one slot further with a shift. Shifting from
585
+ // the current position moved an already scrolled row a second slot.
586
+ let position: number = originalPosition;
587
+ if (shiftBy !== 0) position -= computeSize(slice.selected);
588
+ if (cachedAnimationController?.state === 'running') {
589
+ cachedAnimationController.stop();
590
+ }
591
+ viewRef.lng[axis] = position;
592
+ // The next press shifts from targetPosition: left at the position
593
+ // before the shift, it moved the row one slot too far.
594
+ targetPosition = position;
584
595
  });
585
596
  };
586
597
 
@@ -617,6 +628,7 @@ function createVirtual<T>(
617
628
  c = Math.max(0, itemCount() - 1);
618
629
  setCursor(c);
619
630
  }
631
+ slice.slice = noItems;
620
632
  viewRef.selected = computeSlice(c, 0).selected;
621
633
  }),
622
634
  );