@lightningtv/solid 3.0.0-2 → 3.0.0-20
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/LICENSE +1 -1
- package/README.md +6 -0
- package/dist/src/jsx-runtime.d.ts +1 -3
- package/dist/src/primitives/Column.jsx +9 -10
- package/dist/src/primitives/Column.jsx.map +1 -1
- package/dist/src/primitives/Grid.d.ts +15 -6
- package/dist/src/primitives/Grid.jsx +35 -22
- package/dist/src/primitives/Grid.jsx.map +1 -1
- package/dist/src/primitives/Image.d.ts +8 -0
- package/dist/src/primitives/Image.jsx +24 -0
- package/dist/src/primitives/Image.jsx.map +1 -0
- package/dist/src/primitives/KeepAlive.d.ts +30 -0
- package/dist/src/primitives/KeepAlive.jsx +77 -0
- package/dist/src/primitives/KeepAlive.jsx.map +1 -0
- package/dist/src/primitives/Lazy.d.ts +8 -7
- package/dist/src/primitives/Lazy.jsx +49 -23
- package/dist/src/primitives/Lazy.jsx.map +1 -1
- package/dist/src/primitives/Marquee.d.ts +64 -0
- package/dist/src/primitives/Marquee.jsx +86 -0
- package/dist/src/primitives/Marquee.jsx.map +1 -0
- package/dist/src/primitives/Preserve.d.ts +4 -0
- package/dist/src/primitives/Preserve.jsx +11 -0
- package/dist/src/primitives/Preserve.jsx.map +1 -0
- package/dist/src/primitives/Row.jsx +9 -10
- package/dist/src/primitives/Row.jsx.map +1 -1
- package/dist/src/primitives/Suspense.d.ts +22 -0
- package/dist/src/primitives/Suspense.jsx +33 -0
- package/dist/src/primitives/Suspense.jsx.map +1 -0
- package/dist/src/primitives/Virtual.d.ts +18 -0
- package/dist/src/primitives/Virtual.jsx +434 -0
- package/dist/src/primitives/Virtual.jsx.map +1 -0
- package/dist/src/primitives/VirtualGrid.d.ts +13 -0
- package/dist/src/primitives/VirtualGrid.jsx +139 -0
- package/dist/src/primitives/VirtualGrid.jsx.map +1 -0
- package/dist/src/primitives/VirtualList.d.ts +11 -0
- package/dist/src/primitives/VirtualList.jsx +96 -0
- package/dist/src/primitives/VirtualList.jsx.map +1 -0
- package/dist/src/primitives/VirtualRow.d.ts +13 -0
- package/dist/src/primitives/VirtualRow.jsx +97 -0
- package/dist/src/primitives/VirtualRow.jsx.map +1 -0
- package/dist/src/primitives/Visible.d.ts +0 -1
- package/dist/src/primitives/Visible.jsx +1 -1
- package/dist/src/primitives/Visible.jsx.map +1 -1
- package/dist/src/primitives/announcer/announcer.d.ts +2 -0
- package/dist/src/primitives/announcer/announcer.js +7 -5
- package/dist/src/primitives/announcer/announcer.js.map +1 -1
- package/dist/src/primitives/announcer/index.d.ts +5 -1
- package/dist/src/primitives/announcer/index.js +8 -2
- package/dist/src/primitives/announcer/index.js.map +1 -1
- package/dist/src/primitives/announcer/speech.d.ts +2 -2
- package/dist/src/primitives/announcer/speech.js +157 -28
- package/dist/src/primitives/announcer/speech.js.map +1 -1
- package/dist/src/primitives/createFocusStack.d.ts +4 -4
- package/dist/src/primitives/createFocusStack.jsx +15 -6
- package/dist/src/primitives/createFocusStack.jsx.map +1 -1
- package/dist/src/primitives/createTag.d.ts +8 -0
- package/dist/src/primitives/createTag.jsx +20 -0
- package/dist/src/primitives/createTag.jsx.map +1 -0
- package/dist/src/primitives/index.d.ts +13 -3
- package/dist/src/primitives/index.js +13 -3
- package/dist/src/primitives/index.js.map +1 -1
- package/dist/src/primitives/types.d.ts +3 -0
- package/dist/src/primitives/useHold.d.ts +27 -0
- package/dist/src/primitives/useHold.js +54 -0
- package/dist/src/primitives/useHold.js.map +1 -0
- package/dist/src/primitives/useMouse.d.ts +24 -1
- package/dist/src/primitives/useMouse.js +153 -47
- package/dist/src/primitives/useMouse.js.map +1 -1
- package/dist/src/primitives/utils/chainFunctions.d.ts +30 -4
- package/dist/src/primitives/utils/chainFunctions.js +14 -3
- package/dist/src/primitives/utils/chainFunctions.js.map +1 -1
- package/dist/src/primitives/utils/createBlurredImage.d.ts +56 -0
- package/dist/src/primitives/utils/createBlurredImage.js +223 -0
- package/dist/src/primitives/utils/createBlurredImage.js.map +1 -0
- package/dist/src/primitives/utils/createSpriteMap.d.ts +2 -2
- package/dist/src/primitives/utils/createSpriteMap.js.map +1 -1
- package/dist/src/primitives/utils/handleNavigation.d.ts +85 -5
- package/dist/src/primitives/utils/handleNavigation.js +242 -69
- package/dist/src/primitives/utils/handleNavigation.js.map +1 -1
- package/dist/src/primitives/utils/withScrolling.d.ts +8 -1
- package/dist/src/primitives/utils/withScrolling.js +25 -6
- package/dist/src/primitives/utils/withScrolling.js.map +1 -1
- package/dist/src/render.d.ts +6 -5
- package/dist/src/render.js +4 -0
- package/dist/src/render.js.map +1 -1
- package/dist/src/solidOpts.d.ts +3 -2
- package/dist/src/solidOpts.js +31 -15
- package/dist/src/solidOpts.js.map +1 -1
- package/dist/src/universal.d.ts +25 -0
- package/dist/src/universal.js +232 -0
- package/dist/src/universal.js.map +1 -0
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.js +8 -0
- package/dist/src/utils.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/jsx-runtime.d.ts +2 -4
- package/package.json +19 -10
- package/src/primitives/Column.tsx +10 -12
- package/src/primitives/Grid.tsx +57 -33
- package/src/primitives/Image.tsx +36 -0
- package/src/primitives/KeepAlive.tsx +124 -0
- package/src/primitives/Lazy.tsx +60 -37
- package/src/primitives/Marquee.tsx +149 -0
- package/src/primitives/Preserve.tsx +18 -0
- package/src/primitives/Row.tsx +11 -12
- package/src/primitives/Suspense.tsx +39 -0
- package/src/primitives/Virtual.tsx +478 -0
- package/src/primitives/VirtualGrid.tsx +199 -0
- package/src/primitives/Visible.tsx +1 -2
- package/src/primitives/announcer/announcer.ts +16 -10
- package/src/primitives/announcer/index.ts +12 -2
- package/src/primitives/announcer/speech.ts +188 -27
- package/src/primitives/createFocusStack.tsx +18 -7
- package/src/primitives/createTag.tsx +31 -0
- package/src/primitives/index.ts +17 -3
- package/src/primitives/types.ts +10 -0
- package/src/primitives/useHold.ts +69 -0
- package/src/primitives/useMouse.ts +283 -66
- package/src/primitives/utils/chainFunctions.ts +40 -9
- package/src/primitives/utils/createBlurredImage.ts +366 -0
- package/src/primitives/utils/createSpriteMap.ts +6 -4
- package/src/primitives/utils/handleNavigation.ts +307 -84
- package/src/primitives/utils/withScrolling.ts +47 -16
- package/src/render.ts +9 -7
- package/src/solidOpts.ts +34 -19
- package/src/utils.ts +10 -0
|
@@ -1,101 +1,324 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import * as s from 'solid-js';
|
|
2
|
+
import * as lng from '@lightningtv/solid';
|
|
3
|
+
import * as lngp from '@lightningtv/solid/primitives';
|
|
4
|
+
|
|
5
|
+
declare module '@lightningtv/core' {
|
|
6
|
+
interface ElementNode {
|
|
7
|
+
/** For children of {@link lngp.NavigableElement}, set to `true` to prevent being selected */
|
|
8
|
+
skipFocus?: boolean;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function idxInArray(idx: number, arr: readonly any[]): boolean {
|
|
13
|
+
return idx === 0 || (idx >= 0 && idx < arr.length);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function findFirstFocusableChildIdx(
|
|
17
|
+
el: lngp.NavigableElement,
|
|
18
|
+
from = 0,
|
|
19
|
+
delta = 1,
|
|
20
|
+
): number {
|
|
21
|
+
for (let i = from; ; i += delta) {
|
|
22
|
+
if (!idxInArray(i, el.children)) {
|
|
23
|
+
if (el.wrap) {
|
|
24
|
+
i = (i + el.children.length) % el.children.length;
|
|
25
|
+
} else break;
|
|
26
|
+
}
|
|
27
|
+
if (!el.children[i]?.skipFocus) {
|
|
28
|
+
return i;
|
|
17
29
|
}
|
|
18
|
-
|
|
30
|
+
}
|
|
31
|
+
return -1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function selectChild(el: lngp.NavigableElement, index: number): boolean {
|
|
35
|
+
const child = el.children[index];
|
|
36
|
+
|
|
37
|
+
if (child == null || child.skipFocus) {
|
|
38
|
+
el.selected = -1;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const lastSelected = el.selected;
|
|
43
|
+
el.selected = index;
|
|
44
|
+
|
|
45
|
+
if (!lng.isFocused(child)) {
|
|
19
46
|
child.setFocus();
|
|
47
|
+
}
|
|
20
48
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
49
|
+
// Always call onSelectedChanged on first focus for clients
|
|
50
|
+
el.onSelectedChanged?.(index, el, child as lng.ElementNode, lastSelected);
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @deprecated Use {@link navigableForwardFocus} instead */
|
|
56
|
+
export function onGridFocus(
|
|
57
|
+
_?: lngp.OnSelectedChanged,
|
|
58
|
+
): lng.ForwardFocusHandler {
|
|
59
|
+
return function () {
|
|
60
|
+
return navigableForwardFocus.call(this, this);
|
|
26
61
|
};
|
|
27
62
|
}
|
|
28
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Forwards focus to the first focusable child of a {@link lngp.NavigableElement} and
|
|
66
|
+
* selects it.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* <view
|
|
71
|
+
* selected={0}
|
|
72
|
+
* forwardFocus={navigableForwardFocus}
|
|
73
|
+
* onSelectedChanged={(idx, el, child, lastIdx) => {...}}
|
|
74
|
+
* >
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export const navigableForwardFocus: lng.ForwardFocusHandler = function () {
|
|
78
|
+
const navigable = this as lngp.NavigableElement;
|
|
79
|
+
|
|
80
|
+
let selected = navigable.selected;
|
|
81
|
+
|
|
82
|
+
if (selected !== 0) {
|
|
83
|
+
selected = lng.clamp(selected, 0, this.children.length - 1);
|
|
84
|
+
while (!idxInArray(selected, this.children)) {
|
|
85
|
+
selected--;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
selected = findFirstFocusableChildIdx(navigable, selected);
|
|
90
|
+
// update selected as firstfocusable maybe different if first element has skipFocus
|
|
91
|
+
navigable.selected = selected;
|
|
92
|
+
return selectChild(navigable, selected);
|
|
93
|
+
};
|
|
94
|
+
|
|
29
95
|
export function handleNavigation(
|
|
30
96
|
direction: 'up' | 'right' | 'down' | 'left',
|
|
31
|
-
): KeyHandler {
|
|
97
|
+
): lng.KeyHandler {
|
|
32
98
|
return function () {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
99
|
+
return moveSelection(
|
|
100
|
+
this as lngp.NavigableElement,
|
|
101
|
+
direction === 'up' || direction === 'left' ? -1 : 1,
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Handles navigation key events for navigable elements, \
|
|
108
|
+
* such as {@link lngp.Row} and {@link lngp.Column}.
|
|
109
|
+
*
|
|
110
|
+
* Uses {@link moveSelection} to select the next or previous child based on the key pressed.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```tsx
|
|
114
|
+
* <view
|
|
115
|
+
* selected={0}
|
|
116
|
+
* onUp={navigableHandleNavigation}
|
|
117
|
+
* onDown={navigableHandleNavigation}
|
|
118
|
+
* onSelectedChanged={(idx, el, child, lastIdx) => {...}}
|
|
119
|
+
* >
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export const navigableHandleNavigation: lng.KeyHandler = function (e) {
|
|
123
|
+
return moveSelection(
|
|
124
|
+
this as lngp.NavigableElement,
|
|
125
|
+
e.key === 'ArrowUp' || e.key === 'ArrowLeft' ? -1 : 1,
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Moves the selection within a {@link lngp.NavigableElement}.
|
|
131
|
+
*/
|
|
132
|
+
export function moveSelection(
|
|
133
|
+
el: lngp.NavigableElement,
|
|
134
|
+
delta: number,
|
|
135
|
+
): boolean {
|
|
136
|
+
let selected = findFirstFocusableChildIdx(el, el.selected + delta, delta);
|
|
36
137
|
|
|
37
|
-
|
|
138
|
+
if (selected === -1) {
|
|
139
|
+
if (
|
|
140
|
+
!idxInArray(el.selected, el.children) ||
|
|
141
|
+
el.children[el.selected]!.skipFocus ||
|
|
142
|
+
lng.isFocused(el.children[el.selected]!)
|
|
143
|
+
) {
|
|
38
144
|
return false;
|
|
39
145
|
}
|
|
146
|
+
selected = el.selected;
|
|
147
|
+
}
|
|
40
148
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
do {
|
|
54
|
-
this.selected = ((this.selected || 0) % numChildren) - 1;
|
|
55
|
-
if (this.selected < 0) {
|
|
56
|
-
if (!wrap) {
|
|
57
|
-
this.selected = -1;
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
this.selected = numChildren - 1;
|
|
61
|
-
}
|
|
62
|
-
} while (this.children[this.selected]?.skipFocus);
|
|
63
|
-
}
|
|
149
|
+
const active = el.children[selected]!;
|
|
150
|
+
|
|
151
|
+
if (el.plinko) {
|
|
152
|
+
// Set the next item to have the same selected index
|
|
153
|
+
// so we move up / down directly
|
|
154
|
+
const lastSelectedChild = el.children[el.selected];
|
|
155
|
+
lng.assertTruthy(lastSelectedChild instanceof lng.ElementNode);
|
|
156
|
+
|
|
157
|
+
const num = lastSelectedChild.selected || 0;
|
|
158
|
+
active.selected =
|
|
159
|
+
num < active.children.length ? num : active.children.length - 1;
|
|
160
|
+
}
|
|
64
161
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
162
|
+
return selectChild(el, selected);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function distanceBetweenRectCenters(a: lng.Rect, b: lng.Rect): number {
|
|
166
|
+
const dx = Math.abs(a.x + a.width / 2 - (b.x + b.width / 2)) / 2;
|
|
167
|
+
const dy = Math.abs(a.y + a.height / 2 - (b.y + b.height / 2)) / 2;
|
|
168
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function findClosestFocusableChildIdx(
|
|
172
|
+
el: lng.ElementNode,
|
|
173
|
+
prevEl: lng.ElementNode,
|
|
174
|
+
): number {
|
|
175
|
+
// select child closest to the previous active element
|
|
176
|
+
const prevRect = lng.getElementScreenRect(prevEl);
|
|
177
|
+
const elRect = lng.getElementScreenRect(el);
|
|
178
|
+
const childRect: lng.Rect = { x: 0, y: 0, width: 0, height: 0 };
|
|
179
|
+
|
|
180
|
+
let closestIdx = -1;
|
|
181
|
+
let closestDist = Infinity;
|
|
182
|
+
|
|
183
|
+
for (const [idx, child] of el.children.entries()) {
|
|
184
|
+
if (!child.skipFocus) {
|
|
185
|
+
lng.getElementScreenRect(child, el, childRect);
|
|
186
|
+
childRect.x += elRect.x;
|
|
187
|
+
childRect.y += elRect.y;
|
|
188
|
+
const distance = distanceBetweenRectCenters(prevRect, childRect);
|
|
189
|
+
if (distance < closestDist) {
|
|
190
|
+
closestDist = distance;
|
|
191
|
+
closestIdx = idx;
|
|
74
192
|
}
|
|
75
193
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
navigableThis.onSelectedChanged &&
|
|
81
|
-
navigableThis.onSelectedChanged.call(
|
|
82
|
-
navigableThis,
|
|
83
|
-
navigableThis.selected,
|
|
84
|
-
navigableThis,
|
|
85
|
-
active,
|
|
86
|
-
lastSelected,
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
if (this.plinko) {
|
|
90
|
-
// Set the next item to have the same selected index
|
|
91
|
-
// so we move up / down directly
|
|
92
|
-
const lastSelectedChild = this.children[lastSelected];
|
|
93
|
-
assertTruthy(lastSelectedChild instanceof ElementNode);
|
|
94
|
-
const num = lastSelectedChild.selected || 0;
|
|
95
|
-
active.selected =
|
|
96
|
-
num < active.children.length ? num : active.children.length - 1;
|
|
97
|
-
}
|
|
98
|
-
active.setFocus();
|
|
99
|
-
return true;
|
|
100
|
-
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return closestIdx;
|
|
101
197
|
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Forwards focus to the closest or first focusable child of a {@link lngp.NavigableElement} and
|
|
201
|
+
* selects it.
|
|
202
|
+
*
|
|
203
|
+
* To determine the closest child, it uses the distance between the center of the previous focused element
|
|
204
|
+
* and the center of each child element.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```tsx
|
|
208
|
+
* <view
|
|
209
|
+
* selected={0}
|
|
210
|
+
* forwardFocus={spatialForwardFocus}
|
|
211
|
+
* onSelectedChanged={(idx, el, child, lastIdx) => {...}}
|
|
212
|
+
* >
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export const spatialForwardFocus: lng.ForwardFocusHandler = function () {
|
|
216
|
+
const prevEl = s.untrack(lng.activeElement);
|
|
217
|
+
if (prevEl) {
|
|
218
|
+
const idx = findClosestFocusableChildIdx(this, prevEl);
|
|
219
|
+
const selected = selectChild(this as lngp.NavigableElement, idx);
|
|
220
|
+
if (selected) return true;
|
|
221
|
+
}
|
|
222
|
+
const idx = findFirstFocusableChildIdx(this as lngp.NavigableElement);
|
|
223
|
+
return selectChild(this as lngp.NavigableElement, idx);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Handles spatial navigation within a {@link lngp.NavigableElement} by moving focus
|
|
228
|
+
* based on the arrow keys pressed.
|
|
229
|
+
*
|
|
230
|
+
* This function allows for navigation in a grid-like manner for flex-wrap containers, \
|
|
231
|
+
* where pressing the arrow keys will either:
|
|
232
|
+
* - move focus to the next/prev child in the same row/column
|
|
233
|
+
* - or find the closest child in the next/prev row/column.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```tsx
|
|
237
|
+
* <view
|
|
238
|
+
* selected={0}
|
|
239
|
+
* display="flex"
|
|
240
|
+
* flexWrap="wrap"
|
|
241
|
+
* onUp={spatialHandleNavigation}
|
|
242
|
+
* onDown={spatialHandleNavigation}
|
|
243
|
+
* onSelectedChanged={(idx, el, child, lastIdx) => {...}}
|
|
244
|
+
* >
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export const spatialHandleNavigation: lng.KeyHandler = function (e) {
|
|
248
|
+
let selected = this.selected;
|
|
249
|
+
|
|
250
|
+
if (typeof selected !== 'number' || !idxInArray(selected, this.children)) {
|
|
251
|
+
selected = findFirstFocusableChildIdx(this as lngp.NavigableElement);
|
|
252
|
+
return selectChild(this as lngp.NavigableElement, selected);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const prevChild = this.children[selected]!;
|
|
256
|
+
|
|
257
|
+
const move = { x: 0, y: 0 };
|
|
258
|
+
switch (e.key) {
|
|
259
|
+
case 'ArrowLeft':
|
|
260
|
+
move.x = -1;
|
|
261
|
+
break;
|
|
262
|
+
case 'ArrowRight':
|
|
263
|
+
move.x = 1;
|
|
264
|
+
break;
|
|
265
|
+
case 'ArrowUp':
|
|
266
|
+
move.y = -1;
|
|
267
|
+
break;
|
|
268
|
+
case 'ArrowDown':
|
|
269
|
+
move.y = 1;
|
|
270
|
+
break;
|
|
271
|
+
default:
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const flexDir = this.flexDirection === 'column' ? 'y' : 'x';
|
|
276
|
+
const crossDir = flexDir === 'x' ? 'y' : 'x';
|
|
277
|
+
const flexDelta = move[flexDir];
|
|
278
|
+
const crossDelta = move[crossDir];
|
|
279
|
+
|
|
280
|
+
// Select next/prev child in the current column/row
|
|
281
|
+
if (flexDelta !== 0) {
|
|
282
|
+
for (
|
|
283
|
+
let i = selected + flexDelta;
|
|
284
|
+
idxInArray(i, this.children);
|
|
285
|
+
i += flexDelta
|
|
286
|
+
) {
|
|
287
|
+
const child = this.children[i]!;
|
|
288
|
+
if (child.skipFocus) continue;
|
|
289
|
+
|
|
290
|
+
// Different column/row
|
|
291
|
+
if (child[crossDir] !== prevChild[crossDir]) break;
|
|
292
|
+
|
|
293
|
+
return selectChild(this as lngp.NavigableElement, i);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// Find child in next/prev column/row
|
|
297
|
+
else {
|
|
298
|
+
let closestIdx = -1;
|
|
299
|
+
let closestDist = Infinity;
|
|
300
|
+
|
|
301
|
+
for (
|
|
302
|
+
let i = selected + crossDelta;
|
|
303
|
+
idxInArray(i, this.children);
|
|
304
|
+
i += crossDelta
|
|
305
|
+
) {
|
|
306
|
+
const child = this.children[i]!;
|
|
307
|
+
if (child.skipFocus) continue;
|
|
308
|
+
|
|
309
|
+
// Same column/row, skip
|
|
310
|
+
if (child[crossDir] === prevChild[crossDir]) continue;
|
|
311
|
+
|
|
312
|
+
// Different column/row, check distance
|
|
313
|
+
const distance = Math.abs(child[flexDir] - prevChild[flexDir]);
|
|
314
|
+
if (distance >= closestDist) break; // getting further away
|
|
315
|
+
|
|
316
|
+
closestDist = distance;
|
|
317
|
+
closestIdx = i;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return selectChild(this as lngp.NavigableElement, closestIdx);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return false;
|
|
324
|
+
};
|
|
@@ -5,14 +5,28 @@ import type {
|
|
|
5
5
|
Styles,
|
|
6
6
|
} from '@lightningtv/core';
|
|
7
7
|
|
|
8
|
+
export type Scroller = (
|
|
9
|
+
selected: number | ElementNode,
|
|
10
|
+
component?: ElementNode,
|
|
11
|
+
selectedElement?: ElementNode | ElementText,
|
|
12
|
+
lastSelected?: number,
|
|
13
|
+
) => void;
|
|
14
|
+
|
|
8
15
|
// Adds properties expected by withScrolling
|
|
9
16
|
export interface ScrollableElement extends ElementNode {
|
|
10
17
|
scrollIndex?: number;
|
|
18
|
+
scroll?: 'always' | 'none' | 'edge' | 'auto' | 'center';
|
|
11
19
|
selected: number;
|
|
12
20
|
offset?: number;
|
|
13
21
|
endOffset?: number;
|
|
22
|
+
onScrolled?: (
|
|
23
|
+
elm: ScrollableElement,
|
|
24
|
+
offset: number,
|
|
25
|
+
isInitial: boolean,
|
|
26
|
+
) => void;
|
|
14
27
|
_targetPosition?: number;
|
|
15
28
|
_screenOffset?: number;
|
|
29
|
+
_initialPosition?: number;
|
|
16
30
|
}
|
|
17
31
|
|
|
18
32
|
// From the renderer, not exported
|
|
@@ -26,16 +40,12 @@ const isNotShown = (node: ElementNode | ElementText) => {
|
|
|
26
40
|
Always scroll moves the list every time
|
|
27
41
|
*/
|
|
28
42
|
|
|
29
|
-
|
|
43
|
+
/** Use {@link scrollRow} or {@link scrollColumn} */
|
|
44
|
+
export function withScrolling(isRow: boolean): Scroller {
|
|
30
45
|
const dimension = isRow ? 'width' : 'height';
|
|
31
46
|
const axis = isRow ? 'x' : 'y';
|
|
32
47
|
|
|
33
|
-
return (
|
|
34
|
-
selected: number | ElementNode,
|
|
35
|
-
component?: ElementNode,
|
|
36
|
-
selectedElement?: ElementNode | ElementText,
|
|
37
|
-
lastSelected?: number,
|
|
38
|
-
) => {
|
|
48
|
+
return (selected, component, selectedElement, lastSelected) => {
|
|
39
49
|
let componentRef = component as ScrollableElement;
|
|
40
50
|
if (typeof selected !== 'number') {
|
|
41
51
|
componentRef = selected as ScrollableElement;
|
|
@@ -44,12 +54,17 @@ export function withScrolling(isRow: boolean) {
|
|
|
44
54
|
if (
|
|
45
55
|
!componentRef ||
|
|
46
56
|
componentRef.scroll === 'none' ||
|
|
57
|
+
selected === lastSelected ||
|
|
47
58
|
!componentRef.children.length
|
|
48
59
|
)
|
|
49
60
|
return;
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
if (componentRef._initialPosition === undefined) {
|
|
63
|
+
componentRef._initialPosition = componentRef[axis];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const lng = componentRef.lng as unknown as INode;
|
|
67
|
+
const screenSize = isRow ? lng.stage.root.w : lng.stage.root.h;
|
|
53
68
|
// Determine if movement is incremental or decremental
|
|
54
69
|
const isIncrementing =
|
|
55
70
|
lastSelected === undefined || lastSelected - 1 !== selected;
|
|
@@ -58,6 +73,7 @@ export function withScrolling(isRow: boolean) {
|
|
|
58
73
|
if (componentRef.parent!.clipping) {
|
|
59
74
|
const p = componentRef.parent!;
|
|
60
75
|
componentRef.endOffset =
|
|
76
|
+
componentRef.endOffset ??
|
|
61
77
|
screenSize - ((isRow ? p.absX : p.absY) || 0) - p[dimension];
|
|
62
78
|
}
|
|
63
79
|
|
|
@@ -68,14 +84,20 @@ export function withScrolling(isRow: boolean) {
|
|
|
68
84
|
|
|
69
85
|
const screenOffset = componentRef._screenOffset;
|
|
70
86
|
const gap = componentRef.gap || 0;
|
|
71
|
-
|
|
87
|
+
// when creating we set scroll to always so we setup the right location for selected and scrollIndex
|
|
88
|
+
const scroll =
|
|
89
|
+
componentRef.scroll ||
|
|
90
|
+
(lastSelected === undefined
|
|
91
|
+
? componentRef.scrollIndex
|
|
92
|
+
? 'center'
|
|
93
|
+
: 'always'
|
|
94
|
+
: 'auto');
|
|
72
95
|
|
|
73
96
|
// Allows manual position control
|
|
74
97
|
const targetPosition = componentRef._targetPosition ?? componentRef[axis];
|
|
75
|
-
const rootPosition =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
: Math.max(targetPosition, componentRef[axis]);
|
|
98
|
+
const rootPosition = isIncrementing
|
|
99
|
+
? Math.min(targetPosition, componentRef[axis])
|
|
100
|
+
: Math.max(targetPosition, componentRef[axis]);
|
|
79
101
|
componentRef.offset = componentRef.offset ?? rootPosition;
|
|
80
102
|
const offset = componentRef.offset;
|
|
81
103
|
selectedElement =
|
|
@@ -97,7 +119,7 @@ export function withScrolling(isRow: boolean) {
|
|
|
97
119
|
screenSize -
|
|
98
120
|
containerSize -
|
|
99
121
|
screenOffset -
|
|
100
|
-
(componentRef.endOffset
|
|
122
|
+
(componentRef.endOffset ?? 2 * gap),
|
|
101
123
|
offset,
|
|
102
124
|
);
|
|
103
125
|
|
|
@@ -138,7 +160,8 @@ export function withScrolling(isRow: boolean) {
|
|
|
138
160
|
nextPosition = rootPosition + selectedSize + gap;
|
|
139
161
|
}
|
|
140
162
|
} else if (isIncrementing) {
|
|
141
|
-
nextPosition = -selectedPosition + offset;
|
|
163
|
+
//nextPosition = -selectedPosition + offset;
|
|
164
|
+
nextPosition = rootPosition - selectedSize - gap;
|
|
142
165
|
} else {
|
|
143
166
|
nextPosition = rootPosition + selectedSize + gap;
|
|
144
167
|
}
|
|
@@ -157,9 +180,17 @@ export function withScrolling(isRow: boolean) {
|
|
|
157
180
|
|
|
158
181
|
// Update position if it has changed
|
|
159
182
|
if (componentRef[axis] !== nextPosition) {
|
|
183
|
+
if (componentRef.onScrolled) {
|
|
184
|
+
const isInitial = nextPosition === componentRef._initialPosition;
|
|
185
|
+
componentRef.onScrolled(componentRef, nextPosition, isInitial);
|
|
186
|
+
}
|
|
187
|
+
|
|
160
188
|
componentRef[axis] = nextPosition;
|
|
161
189
|
// Store the new position to keep track during animations
|
|
162
190
|
componentRef._targetPosition = nextPosition;
|
|
163
191
|
}
|
|
164
192
|
};
|
|
165
193
|
}
|
|
194
|
+
|
|
195
|
+
export const scrollRow = /* @__PURE__ */ withScrolling(true);
|
|
196
|
+
export const scrollColumn = /* @__PURE__ */ withScrolling(false);
|
package/src/render.ts
CHANGED
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
type TextProps,
|
|
6
6
|
startLightningRenderer,
|
|
7
7
|
type RendererMainSettings,
|
|
8
|
-
type IRendererMain,
|
|
9
8
|
} from '@lightningtv/core';
|
|
10
9
|
import nodeOpts from './solidOpts.js';
|
|
11
10
|
import {
|
|
@@ -14,15 +13,15 @@ import {
|
|
|
14
13
|
createRenderEffect,
|
|
15
14
|
untrack,
|
|
16
15
|
type JSXElement,
|
|
17
|
-
type ValidComponent,
|
|
18
16
|
createRoot,
|
|
17
|
+
type Component,
|
|
19
18
|
} from 'solid-js';
|
|
20
19
|
import type { SolidNode } from './types.js';
|
|
21
20
|
import { activeElement, setActiveElement } from './activeElement.js';
|
|
22
21
|
|
|
23
22
|
const solidRenderer = solidCreateRenderer<SolidNode>(nodeOpts);
|
|
24
23
|
|
|
25
|
-
let renderer
|
|
24
|
+
let renderer;
|
|
26
25
|
export const rootNode = nodeOpts.createElement('App');
|
|
27
26
|
|
|
28
27
|
const render = function (code: () => JSXElement) {
|
|
@@ -120,10 +119,8 @@ function processTasks(): void {
|
|
|
120
119
|
* ```
|
|
121
120
|
* @description https://www.solidjs.com/docs/latest/api#dynamic
|
|
122
121
|
*/
|
|
123
|
-
export function Dynamic<T
|
|
124
|
-
props: T & {
|
|
125
|
-
component?: ValidComponent;
|
|
126
|
-
},
|
|
122
|
+
export function Dynamic<T extends Record<string, any>>(
|
|
123
|
+
props: T & { component?: Component<T> | undefined | null },
|
|
127
124
|
): JSXElement {
|
|
128
125
|
const [p, others] = splitProps(props, ['component']);
|
|
129
126
|
|
|
@@ -159,3 +156,8 @@ export const Text = (props: TextProps) => {
|
|
|
159
156
|
spread(el, props, false);
|
|
160
157
|
return el as unknown as JSXElement;
|
|
161
158
|
};
|
|
159
|
+
|
|
160
|
+
export function registerDefaultShader(name: string, shader: any) {
|
|
161
|
+
// noop for v2
|
|
162
|
+
// renderer.stage.shManager.registerShaderType('rounded', Rounded);
|
|
163
|
+
}
|
package/src/solidOpts.ts
CHANGED
|
@@ -11,30 +11,41 @@ import type { SolidNode, SolidRendererOptions } from './types.js';
|
|
|
11
11
|
|
|
12
12
|
declare module '@lightningtv/core' {
|
|
13
13
|
interface ElementNode {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/** @internal for managing series of insertions and deletions */
|
|
15
|
+
_queueDelete?: number;
|
|
16
|
+
preserve?: boolean;
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Object.defineProperty(ElementNode.prototype, 'preserve', {
|
|
21
|
+
get(): boolean | undefined {
|
|
22
|
+
return this._queueDelete === 0;
|
|
23
|
+
},
|
|
24
|
+
set(v: boolean) {
|
|
25
|
+
this._queueDelete = v ? 0 : undefined;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
let elementDeleteQueue: ElementNode[] = [];
|
|
20
30
|
|
|
21
|
-
function
|
|
22
|
-
for (let el of
|
|
23
|
-
if (el.
|
|
31
|
+
function flushDeleteQueue(): void {
|
|
32
|
+
for (let el of elementDeleteQueue) {
|
|
33
|
+
if (Number(el._queueDelete) < 0) {
|
|
24
34
|
el.destroy();
|
|
25
35
|
}
|
|
26
|
-
el.
|
|
36
|
+
el._queueDelete = undefined;
|
|
27
37
|
}
|
|
28
|
-
|
|
38
|
+
elementDeleteQueue.length = 0;
|
|
29
39
|
}
|
|
30
40
|
|
|
31
|
-
function
|
|
32
|
-
if (node.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
function pushDeleteQueue(node: ElementNode, n: number): void {
|
|
42
|
+
if (node._queueDelete === undefined) {
|
|
43
|
+
node._queueDelete = n;
|
|
44
|
+
if (elementDeleteQueue.push(node) === 1) {
|
|
45
|
+
queueMicrotask(flushDeleteQueue);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
node._queueDelete += n;
|
|
38
49
|
}
|
|
39
50
|
}
|
|
40
51
|
|
|
@@ -59,12 +70,14 @@ export default {
|
|
|
59
70
|
insertNode(parent: ElementNode, node: SolidNode, anchor: SolidNode): void {
|
|
60
71
|
log('INSERT: ', parent, node, anchor);
|
|
61
72
|
|
|
73
|
+
let prevParent = node.parent;
|
|
62
74
|
parent.insertChild(node, anchor);
|
|
63
75
|
|
|
64
76
|
if (node instanceof ElementNode) {
|
|
65
|
-
node._queueInsert = true;
|
|
66
77
|
node.parent!.rendered && node.render(true);
|
|
67
|
-
|
|
78
|
+
if (prevParent !== undefined) {
|
|
79
|
+
pushDeleteQueue(node, 1);
|
|
80
|
+
}
|
|
68
81
|
} else if (isElementText(parent)) {
|
|
69
82
|
// TextNodes can be placed outside of <text> nodes when <Show> is used as placeholder
|
|
70
83
|
parent.text = parent.getText();
|
|
@@ -79,8 +92,10 @@ export default {
|
|
|
79
92
|
parent.removeChild(node);
|
|
80
93
|
|
|
81
94
|
if (node instanceof ElementNode) {
|
|
82
|
-
node
|
|
83
|
-
|
|
95
|
+
pushDeleteQueue(node, -1);
|
|
96
|
+
} else if (isElementText(parent)) {
|
|
97
|
+
// TextNodes can be placed outside of <text> nodes when <Show> is used as placeholder
|
|
98
|
+
parent.text = parent.getText();
|
|
84
99
|
}
|
|
85
100
|
},
|
|
86
101
|
getParentNode(node: SolidNode): ElementNode | ElementText | undefined {
|
package/src/utils.ts
CHANGED
|
@@ -61,3 +61,13 @@ export function combineStylesMemo<T extends Styles>(
|
|
|
61
61
|
...style1,
|
|
62
62
|
}));
|
|
63
63
|
}
|
|
64
|
+
|
|
65
|
+
export const clamp = (value: number, min: number, max: number) =>
|
|
66
|
+
min < max
|
|
67
|
+
? Math.min(Math.max(value, min), max)
|
|
68
|
+
: Math.min(Math.max(value, max), min);
|
|
69
|
+
|
|
70
|
+
export function mod(n: number, m: number): number {
|
|
71
|
+
if (m === 0) return 0;
|
|
72
|
+
return ((n % m) + m) % m;
|
|
73
|
+
}
|