forked from lightning-tv/solid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementNode.ts
More file actions
1624 lines (1475 loc) · 44.1 KB
/
elementNode.ts
File metadata and controls
1624 lines (1475 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { renderer } from './lightningInit.js';
import {
type BorderRadius,
type BorderStyle,
type StyleEffects,
type AnimationSettings,
type ElementText,
type Styles,
type AnimationEvents,
type AnimationEventHandler,
AddColorString,
TextProps,
TextNode,
type OnEvent,
NewOmit,
SingleBorderStyle,
} from './intrinsicTypes.js';
import States, { type NodeStates } from './states.js';
import calculateFlexOld from './flex.js';
import calculateFlexNew from './flexLayout.js';
const calculateFlex = (import.meta as any).env?.VITE_USE_NEW_FLEX
? calculateFlexNew
: calculateFlexOld;
import {
log,
isArray,
isFunc,
keyExists,
isINode,
isElementNode,
isElementText,
logRenderTree,
isFunction,
spliceItem,
} from './utils.js';
import { Config, DOM_RENDERING, isDev, SHADERS_ENABLED } from './config.js';
import type {
RendererMain,
INode,
INodeAnimateProps,
IAnimationController,
LinearGradientProps,
RadialGradientProps,
ShadowProps,
CoreShaderNode,
ITextNodeProps,
INodeProps,
} from '@lightningjs/renderer';
import { assertTruthy } from '@lightningjs/renderer/utils';
import { NodeType } from './nodeTypes.js';
import {
ForwardFocusHandler,
setActiveElement,
FocusNode,
} from './focusManager.js';
import simpleAnimation, { SimpleAnimationSettings } from './animation.js';
import {
IRendererNode,
IRendererNodeProps,
IRendererShader,
IRendererShaderProps,
IRendererTextNode,
IRendererTextNodeProps,
} from './dom-renderer/domRendererTypes.js';
let nextActiveElement: ElementNode | null = null;
let focusQueued: boolean = false;
let layoutRunQueued = false;
const layoutQueue = new Set<ElementNode>();
function addToLayoutQueue(node: ElementNode) {
layoutQueue.add(node);
if (!layoutRunQueued) {
layoutRunQueued = true;
if (
'reprocessUpdates' in renderer.stage &&
renderer.stage.reprocessUpdates
) {
renderer.stage.reprocessUpdates(runLayout);
} else {
queueMicrotask(runLayout);
}
}
}
function runLayout() {
while (layoutQueue.size > 0) {
const queue = [...layoutQueue];
layoutQueue.clear();
for (let i = queue.length - 1; i >= 0; i--) {
const node = queue[i] as ElementNode;
node.updateLayout();
}
}
layoutRunQueued = false;
}
const parseAndAssignShaderProps = (
prefix: string,
obj: Record<string, any>,
props: Record<string, any> = {},
) => {
if (!obj) return;
// Handle individual border sides: transform width/w to bottom/left/right/top
const borderSideMap: Record<string, string> = {
borderBottom: 'bottom',
borderLeft: 'left',
borderRight: 'right',
borderTop: 'top',
};
const side = borderSideMap[prefix];
const actualPrefix = side ? 'border' : prefix;
props[actualPrefix] = obj;
Object.entries(obj).forEach(([key, value]) => {
let transformedKey = key === 'width' ? 'w' : key;
// If border side and key is width/w, transform to side (bottom/left/right/top)
if (side && transformedKey === 'w') {
transformedKey = side;
}
props[`${actualPrefix}-${transformedKey}`] = value;
});
};
export function convertToShader(
_node: ElementNode,
v: StyleEffects,
): IRendererShader {
let type = 'rounded';
if (v.border) type += 'WithBorder';
if (v.shadow) type += 'WithShadow';
return renderer.createShader(type, v);
}
function getPropertyAlias(name: string) {
if (name === 'w') return 'width';
if (name === 'h') return 'height';
return name;
}
export const LightningRendererNumberProps = [
'alpha',
'color',
'colorTop',
'colorRight',
'colorLeft',
'colorBottom',
'colorTl',
'colorTr',
'colorBl',
'colorBr',
'h',
'fontSize',
'lineHeight',
'mount',
'mountX',
'mountY',
'pivot',
'pivotX',
'pivotY',
'rotation',
'scale',
'scaleX',
'scaleY',
'w',
'worldX',
'worldY',
'x',
'y',
'zIndex',
'zIndexLocked',
];
const LightningRendererNonAnimatingProps = [
'absX',
'absY',
'autosize',
'clipping',
'contain',
'data',
'destroyed',
'fontStretch',
'fontStyle',
'imageType',
'letterSpacing',
'maxHeight',
'maxLines',
'maxWidth',
'offsetY',
'overflowSuffix',
'preventCleanup',
'rtt',
'scrollable',
'scrollY',
'srcHeight',
'srcWidth',
'srcX',
'srcY',
'strictBounds',
'text',
'textAlign',
'textBaseline',
'textOverflow',
'texture',
'textureOptions',
'verticalAlign',
'wordBreak',
'wordWrap',
];
declare global {
interface HTMLElement {
/** Assigned for development, to quickly get ElementNode from selected HTMLElement */
element?: ElementNode;
}
}
export type RendererNode = AddColorString<
Partial<
NewOmit<
INode,
'parent' | 'shader' | 'src' | 'children' | 'id' | 'removeChild'
>
>
>;
export interface ElementNode extends RendererNode, FocusNode {
[key: string]: unknown;
// Properties
/** @internal for managing series of insertions and deletions */
_queueDelete?: number;
preserve?: boolean;
_animationQueue?:
| Array<{
props: Partial<INodeAnimateProps<CoreShaderNode>>;
animationSettings?: AnimationSettings;
}>
| undefined;
_animationQueueSettings?: AnimationSettings;
_animationRunning?: boolean;
_animationSettings?: AnimationSettings;
_autofocus?: boolean;
_containsFlexGrow?: boolean | null;
_hasRenderedChildren?: boolean;
_effects?: Record<string, any>;
_fontFamily?: string;
_id: string | undefined;
_parent: ElementNode | undefined;
_rendererProps?: any;
_states?: States;
_style?: Styles;
_lastAnyKeyPressTime?: number;
_type: 'element' | 'textNode';
_undoStyles?: string[];
autosize?: boolean;
/**
* The distance from the bottom edge of the parent element.
* When `bottom` is set, `mountY` is automatically set to 1.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
bottom?: number;
/**
* An array of child `ElementNode` or `ElementText` nodes.
*/
children: Array<ElementNode | ElementText>;
/**
* Enable debug logging for this specific node.
*/
debug?: boolean;
/**
* Specifies how much a flex item should grow relative to the rest of the flex items.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex-grow
*/
flexGrow?: number;
/**
* Specifies whether flex items are forced onto one line or can wrap onto multiple lines.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
flexWrap?: 'nowrap' | 'wrap' | 'wrap-reverse';
/**
* Determines if an element is a flex item. If set to `false`, the element will be ignored by the flexbox layout.
* @default false
*/
flexItem?: boolean;
/**
* Specifies the order of a flex item relative to the rest of the flex items.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
flexOrder?: number;
/**
* Defines the ability for a flex item to shrink if necessary.
* Defaults to 0 since existing legacy implementations did not shrink layout boxes.
* Only available in NEW flex layout.
*/
flexShrink?: number;
/**
* Defines the default size of an element before the remaining space is distributed.
* Only available in NEW flex layout.
*/
flexBasis?: number | string;
/**
* Forwards focus to a child element. It can be a numeric index of the child or a handler function.
*
* @see https://lightning-tv.github.io/solid/#/essentials/focus?id=forwardfocus
*/
forwardFocus?: number | ForwardFocusHandler;
/**
* If `true`, the states of this node will be propagated to its children.
*
* @see https://lightning-tv.github.io/solid/#/essentials/states?id=forwardstates
*/
forwardStates?: boolean;
/**
* The underlying Lightning Renderer node object. This is where the properties are ultimately set for rendering.
*/
lng:
| INode
| IRendererNode
| Partial<ElementNode>
| (IRendererTextNode & { shader?: any });
/**
* A reference to the `ElementNode` instance. Can be an object or a callback function.
*/
ref?: ElementNode | ((node: ElementNode) => void) | undefined;
/**
* A boolean indicating whether the node has been rendered.
*/
rendered: boolean;
/**
* The main renderer instance.
*/
renderer?: RendererMain;
/**
* The distance from the right edge of the parent element.
* When `right` is set, `mountX` is automatically set to 1.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=layout-and-positioning-elements
*/
right?: number;
/**
* The index of the currently selected child element, used for focus management for Column and Row components.
*/
selected?: number;
/**
* The width of the element before flexbox layout is applied. Used internally for layout calculations.
*/
preFlexwidth?: number;
/**
* The height of the element before flexbox layout is applied. Used internally for layout calculations.
*/
preFlexheight?: number;
/**
* The text content of a text node.
*/
text?: string;
/**
* Aligns flex items along the cross axis of the current line of the flex container.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex-properties
*/
alignItems?: 'flexStart' | 'flexEnd' | 'center';
/**
* Aligns a flex item along the cross axis, overriding the `alignItems` value of the flex container.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex-properties
*/
alignSelf?: 'flexStart' | 'flexEnd' | 'center';
/**
* The border style for all sides of the element. Takes an object with width and color properties.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
border?: BorderStyle;
/**
* The border style for the bottom side of the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
borderBottom?: SingleBorderStyle;
/**
* The border style for the left side of the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
borderLeft?: SingleBorderStyle;
/**
* The radius of the element's corners.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
borderRadius?: BorderRadius;
/**
* The border style for the right side of the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
borderRight?: SingleBorderStyle;
/**
* The border style for the top side of the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects?id=border-and-borderradius
*/
borderTop?: SingleBorderStyle;
/**
* A shorthand to set both `centerX` and `centerY` to true.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=layout-and-positioning-elements
*/
center?: boolean;
/**
* If `true`, centers the element horizontally within its parent.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=layout-and-positioning-elements
*/
centerX?: boolean;
/**
* If `true`, centers the element vertically within its parent.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=layout-and-positioning-elements
*/
centerY?: boolean;
/**
* Specifies the direction of the flex items.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
direction?: 'ltr' | 'rtl';
/**
* Specifies the display behavior of an element. 'flex' enables flexbox layout.
*
* @default 'block'
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
display?: 'flex' | 'block';
/**
* Defines how the flex container's size is determined. 'contain' allows it to grow with its content, 'fixed' keeps it at its specified size.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
flexBoundary?: 'contain' | 'fixed';
/**
* Defines how the flex container's cross-axis size is determined. 'fixed' keeps it at its specified size. Default is 'contain'.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
flexCrossBoundary?: 'fixed'; // default is contain
/**
* Specifies the direction of the main axis for flex items.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
flexDirection?: 'row' | 'column' | 'row-reverse' | 'column-reverse';
/**
* The gap between flex items.
*
* @see @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
gap?: number;
/**
* The gap between flex rows.
*
* @see @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
rowGap?: number;
/**
* The gap between flex columns.
*
* @see @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
columnGap?: number;
/**
* Defines the alignment of flex items along the main axis.
*
* @see @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
justifyContent?:
| 'flexStart'
| 'flexEnd'
| 'center'
| 'spaceBetween'
| 'spaceAround'
| 'spaceEvenly';
/**
* Applies a linear gradient effect to the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects
*/
linearGradient?: LinearGradientProps;
/**
* Applies a radial gradient effect to the element.
*
* @see https://lightning-tv.github.io/solid/#/essentials/effects
*/
radialGradient?: RadialGradientProps;
/**
* The margin on the bottom side of the element for a flexItem.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
marginBottom?: number;
/**
* The margin on the left side of the element for a flexItem.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
marginLeft?: number;
/**
* The margin on the right side of the element for a flexItem.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
marginRight?: number;
/**
* The margin on the top side of the element for a flexItem.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
marginTop?: number;
/**
* The padding on all sides of the flex element, or an array defining [Top, Right, Bottom, Left] padding.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
padding?:
| number
| [number, number]
| [number, number, number]
| [number, number, number, number];
/**
* The margin on all sides of the flex element, or an array defining [Top, Right, Bottom, Left] margins.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
margin?:
| number
| [number, number]
| [number, number, number]
| [number, number, number, number];
/**
* The x-coordinate of the element's position.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
x: number;
/**
* The y-coordinate of the element's position.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
y: number;
/**
* Throttles key press events by the specified number of milliseconds.
*
* @see https://lightning-tv.github.io/solid/#/primitives/useFocusManager?id=input-throttling-available-core-212
*/
throttleInput?: number;
/**
* The width of the element.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
w: number;
/**
* The height of the element.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
h: number;
/**
* The maximum width of the element.
*/
maxWidth?: number;
/**
* The maximum height of the element.
*/
maxHeight?: number;
/**
* The minimum width of the element.
*/
minWidth?: number;
/**
* The minimum height of the element.
*/
minHeight?: number;
/**
* The z-index of the element, which affects its stacking order.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
zIndex?: number;
/**
* Defines transitions for animatable properties.
*
* @see https://lightning-tv.github.io/solid/#/essentials/transitions?id=transitions-animations
*/
transition?:
| Record<string, AnimationSettings | undefined | true | false>
| true
| false;
/**
* Optional handlers for animation events.
*
* Available animation events:
* - 'animating': Fired when the animation is in progress.
* - 'tick': Fired at each tick or frame update of the animation.
* - 'stopped': Fired when the animation stops.
*
* Each event handler is optional and maps to a corresponding event.
*
* @type {Partial<Record<AnimationEvents, AnimationEventHandler>>}
*
* @property {AnimationEventHandler} [animating] - Handler for the 'animating' event.
* @property {AnimationEventHandler} [tick] - Handler for the 'tick' event.
* @property {AnimationEventHandler} [stopped] - Handler for the 'stopped' event.
*
* @see https://lightning-tv.github.io/solid/#/essentials/transitions?id=animation-callbacks
*/
onAnimation?: Partial<Record<AnimationEvents, AnimationEventHandler>>;
/** Optional handler for when the element is created and rendered.
*
* @see https://lightning-tv.github.io/solid/#/flow/ondestroy
*/
onCreate?: (this: ElementNode, el: ElementNode) => void;
/**
* Optional handler for when the element is destroyed.
* It can return a promise to wait for the cleanup to finish before the element is destroyed.
*
* @see https://lightning-tv.github.io/solid/#/flow/ondestroy
*/
onDestroy?: (this: ElementNode, el: ElementNode) => Promise<any> | void;
/**
* Optional handlers for when the element is rendered—after creation and when switching parents.
*
* @see https://lightning-tv.github.io/solid/#/primitives/KeepAlive
*/
onRender?: (this: ElementNode, el: ElementNode) => void;
/**
* Optional handlers for when the element is removed from a parent element.
*
* @see https://lightning-tv.github.io/solid/#/primitives/KeepAlive
*/
onRemove?: (this: ElementNode, el: ElementNode) => void;
/**
* Listen to Events coming from the renderer
* @param NodeEvents
*
* Available events:
* - 'loaded'
* - 'failed'
* - 'freed'
* - 'inBounds'
* - 'outOfBounds'
* - 'inViewport'
* - 'outOfViewport'
*
* @typedef {'loaded' | 'failed' | 'freed' | 'inBounds' | 'outOfBounds' | 'inViewport' | 'outOfViewport'} NodeEvents
*
* @param {Partial<Record<NodeEvents, EventHandler>>} events - An object where the keys are event names from NodeEvents and the values are the respective event handlers.
* @returns {void}
*
* @see https://lightning-tv.github.io/solid/#/essentials/events
*/
onEvent?: OnEvent;
/**
* Callback run after flex layout is calculated on flex elements
*
* @see https://lightning-tv.github.io/solid/#/flow/layout
*/
onLayout?: (this: ElementNode, target: ElementNode) => void;
/**
* The individual padding on each side of an element, acting as an override to the `padding` array property.
* `paddingTop`, `paddingRight`, `paddingBottom`, `paddingLeft`.
* Only in the new flex engine.
*
* @see https://lightning-tv.github.io/solid/#/flow/layout?id=flex
*/
paddingTop?: number;
paddingRight?: number;
paddingBottom?: number;
paddingLeft?: number;
}
export class ElementNode extends Object {
constructor(name: string) {
super();
this._type = name === 'text' ? NodeType.TextNode : NodeType.Element;
this.rendered = false;
this.lng = {};
this.children = [];
}
get effects(): StyleEffects | undefined {
return this.lng.shader;
}
set effects(v: StyleEffects) {
if (!SHADERS_ENABLED) return;
let target = this.lng.shader || {};
if (this.lng.shader?.props) {
target = this.lng.shader.props;
}
if (v.rounded) target.radius = v.rounded.radius;
if (v.borderRadius) target.radius = v.borderRadius;
if (v.border) parseAndAssignShaderProps('border', v.border, target);
if (v.borderTop)
parseAndAssignShaderProps('borderTop', v.borderTop, target);
if (v.borderRight)
parseAndAssignShaderProps('borderRight', v.borderRight, target);
if (v.borderBottom)
parseAndAssignShaderProps('borderBottom', v.borderBottom, target);
if (v.borderLeft)
parseAndAssignShaderProps('borderLeft', v.borderLeft, target);
if (v.shadow) parseAndAssignShaderProps('shadow', v.shadow, target);
if (this.rendered) {
if (!this.lng.shader) {
this.lng.shader = Config.convertToShader(this, target);
} else if (DOM_RENDERING && Config.domRendererEnabled) {
this.lng.shader = this.lng.shader; // lng.shader is a setter, force style update
}
} else {
this.lng.shader = target;
}
}
set id(id: string) {
this._id = id;
if (
Config.rendererOptions &&
'inspector' in Config.rendererOptions &&
Config.rendererOptions.inspector
) {
this.data = { ...this.data, testId: id };
}
}
get id(): string | undefined {
return this._id;
}
get parent() {
return this._parent;
}
set parent(p) {
this._parent = p;
if (this.rendered && p?.rendered) {
this.lng.parent = (p.lng as IRendererNode) ?? null;
}
}
get height(): number {
return this.maxHeight || this.h;
}
set height(h: number) {
this.h = h;
}
get width(): number {
return this.maxWidth || this.w;
}
set width(w: number) {
this.w = w;
}
set fontWeight(v) {
if (this._fontWeight === v) {
return;
}
this._fontWeight = v;
const weight =
(Config.fontWeightAlias &&
(Config.fontWeightAlias[v as string] as number | string)) ??
v;
(this.lng as any).fontFamily =
`${this.fontFamily || Config.fontSettings?.fontFamily}${weight}`;
}
get fontWeight() {
return this._fontWeight;
}
set fontFamily(v) {
this._fontFamily = v;
(this.lng as any).fontFamily = v;
}
get fontFamily() {
return this._fontFamily;
}
insertChild(
node: ElementNode | ElementText | TextNode,
beforeNode?: ElementNode | ElementText | TextNode | null,
) {
// always remove nodes if they have a parent - for back swap of node
// this will then put the node at the end of the array when re-added
if (node.parent) {
node.parent.removeChild(node);
// We're inserting a node thats been rendered into a node that hasn't been
if (!this.rendered) {
this._hasRenderedChildren = true;
}
}
node.parent = this;
if (beforeNode) {
// SolidJS can move nodes around in the children array.
// We need to insert following DOM insertBefore which moves elements.
spliceItem(this.children, node as ElementNode, 1);
if (spliceItem(this.children, beforeNode as ElementNode, 0, node) > -1) {
return;
}
}
this.children.push(node as ElementNode);
}
removeChild(node: ElementNode | ElementText | TextNode) {
if (spliceItem(this.children, node, 1) > -1) {
node.onRemove?.call(node, node);
if (this.requiresLayout()) {
addToLayoutQueue(this);
}
}
}
get selectedNode(): ElementNode | undefined {
const selectedIndex = this.selected || 0;
for (let i = selectedIndex; i < this.children.length; i++) {
const element = this.children[i];
if (isElementNode(element)) {
this.selected = i;
return element;
}
}
return undefined;
}
set shader(
shaderProps: IRendererShader | [kind: string, props: IRendererShaderProps],
) {
this.lng.shader = isArray(shaderProps)
? renderer.createShader(...shaderProps)
: shaderProps;
}
_sendToLightningAnimatable(name: string, value: number) {
if (
this.transition &&
this.rendered &&
Config.animationsEnabled &&
(this.transition === true ||
this.transition[name] ||
this.transition[getPropertyAlias(name)])
) {
const animationSettings =
this.transition === true || this.transition[name] === true
? undefined
: this.transition[name] ||
(this.transition[getPropertyAlias(name)] as
| undefined
| AnimationSettings);
if (Config.simpleAnimationsEnabled) {
simpleAnimation.add(
this,
name,
value,
animationSettings ||
(this.animationSettings as SimpleAnimationSettings),
);
simpleAnimation.register(renderer.stage);
return;
} else {
const animationController = this.animate(
{ [name]: value },
animationSettings,
);
if (this.onAnimation) {
const animationEvents = Object.keys(
this.onAnimation,
) as AnimationEvents[];
for (const event of animationEvents) {
const handler = this.onAnimation[event];
animationController.on(
event,
(controller: IAnimationController, props?: any) => {
handler!.call(this, controller, name, value, props);
},
);
}
}
return animationController.start();
}
}
(this.lng[name as keyof (IRendererNode | INode)] as number | string) =
value;
}
animate(
props: Partial<INodeAnimateProps<CoreShaderNode>>,
animationSettings?: AnimationSettings,
): IAnimationController {
isDev &&
assertTruthy(this.rendered, 'Node must be rendered before animating');
return (this.lng as IRendererNode).animate(
props,
animationSettings || this.animationSettings || {},
);
}
chain(
props: Partial<INodeAnimateProps<CoreShaderNode>>,
animationSettings?: AnimationSettings,
) {
if (this._animationRunning) {
this._animationQueue = [];
this._animationRunning = false;
}
if (animationSettings) {
this._animationQueueSettings = animationSettings;
} else if (!this._animationQueueSettings) {
this._animationQueueSettings =
animationSettings || this.animationSettings;
}
animationSettings = animationSettings || this._animationQueueSettings;
this._animationQueue = this._animationQueue || [];
this._animationQueue.push({ props, animationSettings });
return this;
}
async start() {
let animation = this._animationQueue!.shift();
while (animation) {
this._animationRunning = true;
await this.animate(animation.props, animation.animationSettings)
.start()
.waitUntilStopped();
animation = this._animationQueue!.shift();
}
this._animationRunning = false;
this._animationQueueSettings = undefined;
}
emit(event: string, ...args: any[]): boolean {
let current = this as ElementNode;
const capitalizedEvent = `on${event.charAt(0).toUpperCase()}${event.slice(1)}`;
while (current) {
const handler = current[capitalizedEvent];
if (isFunction(handler)) {
if (handler.call(current, this, ...args) === true) {
return true;
}
}
current = current.parent!;
}
return false;
}
setFocus(): void {
if (this.rendered) {
// can be 0
if (this.forwardFocus !== undefined) {
if (isFunc(this.forwardFocus)) {
if (this.forwardFocus.call(this, this) !== false) {
return;
}
} else {
const focusedIndex =
typeof this.forwardFocus === 'number' ? this.forwardFocus : null;
const nodes = this.children;
if (focusedIndex !== null && focusedIndex < nodes.length) {
const child = nodes[focusedIndex];
isElementNode(child) && child.setFocus();
return;
}