-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathinit.svelte.ts
More file actions
897 lines (824 loc) · 24.3 KB
/
init.svelte.ts
File metadata and controls
897 lines (824 loc) · 24.3 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
import {
determine_interactivity,
get_component,
get_inputs_outputs
} from "./init_utils";
import { translate_if_needed } from "./i18n";
import { tick } from "svelte";
import { dequal } from "dequal";
import type {
ComponentMeta,
ProcessedComponentMeta,
LayoutNode,
Dependency,
LoadingComponent,
AppConfig,
ServerFunctions
} from "./types";
import { type SharedProps } from "@gradio/utils";
import { allowed_shared_props } from "@gradio/utils";
import { Client } from "@gradio/client";
type client_return = Awaited<ReturnType<typeof Client.connect>>;
type set_data_type = (data: Record<string, unknown>) => void;
type get_data_type = () => Promise<Record<string, unknown>>;
type visitor<T> = (node: T) => ProcessedComponentMeta;
type Tab = {
label: string;
id: string;
visible: boolean;
interactive: boolean;
elem_id: string | undefined;
scale: number | null;
order?: number;
component_id: number;
};
const type_map = {
walkthrough: "tabs",
walkthroughstep: "tabitem"
};
function get_api_url(config: Omit<AppConfig, "api_url">): string {
// Handle api_prefix correctly when app is mounted at a subpath
// Since get_root_url() returns root without trailing slash, we need to manually
// append api_prefix to root's pathname to ensure correct URL construction
const rootUrl = new URL(config.root);
const rootPath = rootUrl.pathname.endsWith("/")
? rootUrl.pathname
: rootUrl.pathname + "/";
const apiPrefix = config.api_prefix.startsWith("/")
? config.api_prefix
: "/" + config.api_prefix;
return new URL(rootPath.slice(0, -1) + apiPrefix, rootUrl.origin).toString();
}
export class AppTree {
/** the raw component structure received from the backend */
#component_payload: ComponentMeta[];
/** the raw layout node structure received from the backend */
#layout_payload: LayoutNode;
/** the raw dependency structure received from the backend */
#dependency_payload: Dependency[];
/** Need this to set i18n in re-render */
reactive_formatter: (str: string) => string = (str: string) => str;
/** the config for the app */
#config: AppConfig;
client: client_return;
/** the root node of the processed layout tree */
root = $state<ProcessedComponentMeta>();
root_untracked: ProcessedComponentMeta;
/** a set of all component IDs that are inputs to dependencies */
#input_ids: Set<number> = new Set();
/** a set of all component IDs that are outputs of dependencies */
#output_ids: Set<number> = new Set();
/** A list of components that are currently loading */
#pending_components: Array<LoadingComponent> = [];
#get_callbacks = new Map<number, get_data_type>();
#set_callbacks = new Map<number, set_data_type>();
#event_dispatcher: (id: number, event: string, data: unknown) => void;
component_ids: number[];
initial_tabs: Record<number, Tab[]> = {};
components_to_register: Set<number> = new Set();
ready: Promise<void>;
ready_resolve!: () => void;
resolved: boolean = false;
#hidden_on_startup: Set<number> = new Set();
constructor(
components: ComponentMeta[],
layout: LayoutNode,
dependencies: Dependency[],
config: Omit<AppConfig, "api_url">,
app: client_return,
reactive_formatter: (str: string) => string,
event_dispatcher: (id: number, event: string, data: unknown) => void
) {
this.ready = new Promise<void>((resolve) => {
this.ready_resolve = resolve;
});
this.reactive_formatter = reactive_formatter;
const api_url = get_api_url(config);
this.#config = {
...config,
api_url
};
this.#component_payload = components;
this.#layout_payload = layout;
this.#dependency_payload = dependencies;
this.root = this.create_node(
{ id: layout.id, children: [] },
new Map(),
true
);
for (const comp of components) {
if (comp.props.visible != false) this.components_to_register.add(comp.id);
}
this.client = app;
this.prepare();
const component_map = components.reduce((map, comp) => {
map.set(comp.id, comp);
return map;
}, new Map<number, ComponentMeta>());
this.root!.children = this.#layout_payload.children.map((node) =>
this.traverse(node, (node) => {
const new_node = this.create_node(
node,
component_map,
false,
this.reactive_formatter
);
return new_node;
})
);
this.component_ids = components.map((c) => c.id);
this.initial_tabs = {};
gather_initial_tabs(this.root!, this.initial_tabs);
this.postprocess(this.root!);
this.#event_dispatcher = event_dispatcher;
this.root_untracked = this.root;
}
reload(
components: ComponentMeta[],
layout: LayoutNode,
dependencies: Dependency[],
config: Omit<AppConfig, "api_url">
) {
this.#layout_payload = layout;
this.#component_payload = components;
const api_url = get_api_url(config);
this.#config = {
...config,
api_url
};
this.#dependency_payload = dependencies;
this.root = this.create_node(
{ id: layout.id, children: [] },
new Map(),
true
);
for (const comp of components) {
if (comp.props.visible != false) this.components_to_register.add(comp.id);
}
this.prepare();
const component_map = components.reduce((map, comp) => {
map.set(comp.id, comp);
return map;
}, new Map<number, ComponentMeta>());
this.root!.children = this.#layout_payload.children.map((node) =>
this.traverse(node, (node) => {
const new_node = this.create_node(
node,
component_map,
false,
this.reactive_formatter
);
return new_node;
})
);
this.component_ids = components.map((c) => c.id);
this.initial_tabs = {};
gather_initial_tabs(this.root!, this.initial_tabs);
this.postprocess(this.root!);
}
/**
* Registers a component with its ID and data callbacks
* @param id the ID of the component
* @param _set_data the set data callback
* @param _get_data the get data callback
*/
register_component(
id: number,
_set_data: set_data_type,
_get_data: get_data_type
): void {
this.#set_callbacks.set(id, _set_data);
this.#get_callbacks.set(id, _get_data);
this.components_to_register.delete(id);
if (this.components_to_register.size === 0 && !this.resolved) {
this.resolved = true;
this.ready_resolve();
}
}
/**
* Preprocess the payloads to get the correct state read to build the tree
*/
prepare() {
const [inputs, outputs] = get_inputs_outputs(this.#dependency_payload);
this.#input_ids = inputs;
this.#output_ids = outputs;
}
/** Processes the layout payload into a tree of components */
process() {}
postprocess(tree: ProcessedComponentMeta) {
this.root = this.traverse(tree, [
(node) => handle_visibility(node, this.#config.api_url),
(node) =>
untrack_children_of_invisible_parents(
node,
this.components_to_register
),
(node) => handle_empty_forms(node, this.components_to_register),
(node) => translate_props(node),
(node) => apply_initial_tabs(node, this.initial_tabs),
(node) => this.find_attached_events(node, this.#dependency_payload),
(node) =>
untrack_children_of_closed_accordions_or_inactive_tabs(
node,
this.components_to_register,
this.#hidden_on_startup
)
]);
}
find_attached_events(
node: ProcessedComponentMeta,
dependencies: Dependency[]
): ProcessedComponentMeta {
const attached_events = dependencies
.filter((dep) => dep.targets.find(([id]) => id === node.id))
.map((dep) => {
const target = dep.targets.find(([id]) => id === node.id);
return target ? target[1] : null;
})
.filter(Boolean) as string[];
node.props.shared_props.attached_events = attached_events;
return node;
}
/**
* Traverses the layout tree and applies a callback to each node
* @param node the current layout node
* @param visit the callback to apply to each node
* @returns the return value of the callback, with a `children` property added for any child nodes
*/
traverse<T extends LayoutNode | ProcessedComponentMeta>(
node: T,
visit: visitor<T> | visitor<T>[]
): ProcessedComponentMeta {
function single_visit<U extends T>(
node: U,
visit: visitor<U>,
traverse_fn: any
): ProcessedComponentMeta {
const result = visit(node);
if ("children" in node && node.children.length > 0) {
result.children =
node.children?.map((child) => traverse_fn(child, visit)) || [];
}
return result;
}
if (Array.isArray(visit)) {
let result: ProcessedComponentMeta = node as ProcessedComponentMeta;
for (const v of visit) {
result = single_visit(result as T, v, this.traverse.bind(this));
}
return result;
} else {
return single_visit(node, visit, this.traverse.bind(this));
}
}
/**
* Creates a processed component node from a layout node
* @param opts the layout node options
* @param root whether this is the root node
* @returns the processed component node
*/
create_node(
opts: LayoutNode,
component_map: Map<number, ComponentMeta>,
root = false,
reactive_formatter?: (str: string) => string
): ProcessedComponentMeta {
let component: ComponentMeta | undefined;
if (!root) {
component = component_map.get(opts.id);
} else {
component = {
type: "column",
id: opts.id,
// @ts-ignore
props: {
visible: true,
root: "",
theme_mode: "light"
},
component_class_id: "column",
key: null
};
}
if (!component) {
throw new Error(`Component with ID ${opts.id} not found`);
}
if (reactive_formatter) {
component.props.i18n = reactive_formatter;
}
const processed_props = gather_props(
opts.id,
component.props,
[this.#input_ids, this.#output_ids],
this.client,
this.#config.api_url,
{ ...this.#config }
);
const type =
type_map[component.type as keyof typeof type_map] || component.type;
const node = {
id: opts.id,
type: type,
props: processed_props,
children: [],
show_progress_on: null,
component_class_id: component.component_class_id || component.type,
component:
processed_props.shared_props.visible !== false
? get_component(
component.type,
component.component_class_id,
this.#config.api_url || ""
)
: null,
key: component.key,
rendered_in: component.rendered_in,
documentation: component.documentation,
original_visibility: processed_props.shared_props.visible
};
return node;
}
rerender(components: ComponentMeta[], layout: LayoutNode) {
const component_map = components.reduce((map, comp) => {
map.set(comp.id, comp);
return map;
}, new Map<number, ComponentMeta>());
const _subtree = this.traverse(layout, (node) => {
const new_node = this.create_node(
node,
component_map,
false,
this.reactive_formatter
);
return new_node;
});
gather_initial_tabs(_subtree, this.initial_tabs);
const subtree = this.traverse(_subtree, (node) =>
apply_initial_tabs(node, this.initial_tabs)
);
const n = find_node_by_id(this.root!, subtree.id);
if (!n) {
throw new Error("Rerender failed: root node not found in current tree");
}
n.children = subtree.children;
}
async update_visibility(
node: ProcessedComponentMeta,
new_state: any
): Promise<void> {
node.children.forEach((child) => {
const _set_data = this.#set_callbacks.get(child.id);
if (_set_data) {
_set_data(new_state);
}
this.update_visibility(child, new_state);
});
}
/*
* Updates the state of a component by its ID
* @param id the ID of the component to update
* @param new_state the new state to set
* */
async update_state(
id: number,
new_state: Partial<SharedProps> & Record<string, unknown>,
check_visibility: boolean = true
) {
// Visibility is tricky 😅
// If the component is not visible, it has not been rendered
// and so it has no _set_data callback
// Therefore, we need to traverse the tree and set the visible prop to true
// and then render it and its children. After that, we can call the _set_data callback
const node = find_node_by_id(this.root!, id);
let already_updated_visibility = false;
if (check_visibility && !node?.component) {
await tick();
this.root = this.traverse(this.root!, [
//@ts-ignore
(n) => set_visibility_for_updated_node(n, id, new_state.visible),
//@ts-ignore
(n) => update_parent_visibility(n, id, new_state.visible),
(n) => handle_visibility(n, this.#config.api_url)
]);
await tick();
already_updated_visibility = true;
}
const _set_data = this.#set_callbacks.get(id);
if (!_set_data) {
const old_value = node?.props.props.value;
// @ts-ignore
const new_props = create_props_shared_props(new_state);
node!.props.shared_props = {
...node?.props.shared_props,
...new_props.shared_props
};
node!.props.props = { ...node?.props.props, ...new_props.props };
if ("value" in new_state && !dequal(old_value, new_state.value)) {
this.#event_dispatcher(id, "change", null);
}
} else if (_set_data) {
_set_data(new_state);
}
if (!check_visibility || already_updated_visibility) return;
// need to let the UI settle before traversing again
// otherwise there could be
await tick();
// Update the visibility in a way that does not
// re-render the root/tree. Doing that would nuke
// any values currently in the UI.
// @ts-ignore
await this.update_visibility(node, new_state);
const parent_node = find_parent(this.root!, id);
if (parent_node)
// @ts-ignore
update_parent_visibility(parent_node, id, new_state.visible);
}
/**
* Gets the current state of a component by its ID
* @param id the ID of the component to get the state of
* @returns the current state of the component, or null if not found
*/
async get_state(id: number): Promise<Record<string, unknown> | null> {
const _get_data = this.#get_callbacks.get(id);
const component = find_node_by_id(this.root!, id);
if (!_get_data && !component) return null;
if (_get_data) return await _get_data();
if (component)
return Promise.resolve({ value: component.props.props.value });
return null;
}
async render_previously_invisible_children(id: number) {
this.root = this.traverse(this.root!, [
(node) => {
if (node.id === id) {
make_visible_if_not_rendered(node, this.#hidden_on_startup);
}
return node;
},
(node) => handle_visibility(node, this.#config.api_url)
]);
}
}
function make_visible_if_not_rendered(
node: ProcessedComponentMeta,
hidden_on_startup: Set<number>
): void {
node.props.shared_props.visible = hidden_on_startup.has(node.id)
? true
: node.props.shared_props.visible;
node.children.forEach((child) => {
make_visible_if_not_rendered(child, hidden_on_startup);
});
}
/**
* Process the server function names and return a dictionary of functions
* @param id the component id
* @param server_fns the server function names
* @param app the client instance
* @returns the actual server functions
*/
export function process_server_fn(
id: number,
server_fns: string[] | undefined,
app: client_return
): ServerFunctions {
if (!server_fns) {
return {};
}
return server_fns.reduce((acc, fn: string) => {
acc[fn] = async (...args: any[]) => {
if (args.length === 1) {
args = args[0];
}
const result = await app.component_server(id, fn, args);
return result;
};
return acc;
}, {} as ServerFunctions);
}
function create_props_shared_props(props: ComponentMeta["props"]): {
shared_props: SharedProps;
props: Record<string, unknown>;
} {
const _shared_props: Partial<SharedProps> = {};
const _props: Record<string, unknown> = {};
for (const key in props) {
// For Tabs (or any component that already has an id prop)
// Set the id to the props so that it doesn't get overwritten
if (key === "id" || key === "autoscroll") {
_props[key] = props[key];
} else if (allowed_shared_props.includes(key as keyof SharedProps)) {
const _key = key as keyof SharedProps;
_shared_props[_key] = props[key];
} else {
_props[key] = props[key];
}
}
return { shared_props: _shared_props as SharedProps, props: _props };
}
/**
* Gathers the props for a component
* @param id the ID of the component
* @param props the props of the component
* @param dependencies the component's dependencies
* @param additional any additional props to include
* @returns the gathered props as an object with `shared_props` and `props` keys
*/
function gather_props(
id: number,
props: ComponentMeta["props"],
dependencies: [Set<number>, Set<number>],
client: client_return,
api_url: string,
additional: Record<string, unknown> = {}
): {
shared_props: SharedProps;
props: Record<string, unknown>;
} {
const { shared_props: _shared_props, props: _props } =
create_props_shared_props(props);
_shared_props.server = process_server_fn(id, props.server_fns, client);
for (const key in additional) {
if (allowed_shared_props.includes(key as keyof SharedProps)) {
const _key = key as keyof SharedProps;
_shared_props[_key] = additional[key];
} else {
_props[key] = additional[key];
}
}
_shared_props.client = client;
_shared_props.id = id;
_shared_props.interactive = determine_interactivity(
id,
_shared_props.interactive,
_props.value,
dependencies
);
_shared_props.load_component = (
name: string,
variant: "base" | "component" | "example"
) => get_component(name, "", api_url, variant) as LoadingComponent;
_shared_props.visible =
_shared_props.visible === undefined ? true : _shared_props.visible;
_shared_props.loading_status = {};
return { shared_props: _shared_props as SharedProps, props: _props };
}
function handle_visibility(
node: ProcessedComponentMeta,
api_url: string
): ProcessedComponentMeta {
// Check if the node is visible
if (node.props.shared_props.visible && !node.component) {
const result: ProcessedComponentMeta = {
...node,
component: get_component(node.type, node.component_class_id, api_url),
children: []
};
if (node.children) {
result.children = node.children.map((child) =>
handle_visibility(child, api_url)
);
}
return result;
} else {
return node;
}
}
function set_visibility_for_updated_node(
node: ProcessedComponentMeta,
id: number,
visible: boolean
): ProcessedComponentMeta {
if (node.id == id) {
node.props.shared_props.visible = visible;
}
return node;
}
function _untrack(
node: ProcessedComponentMeta,
components_to_register: Set<number>
): void {
components_to_register.delete(node.id);
if (node.children) {
node.children.forEach((child) => _untrack(child, components_to_register));
}
return;
}
function untrack_children_of_invisible_parents(
node: ProcessedComponentMeta,
components_to_register: Set<number>
): ProcessedComponentMeta {
// Check if the node is visible
if (node.props.shared_props.visible !== true) {
_untrack(node, components_to_register);
}
return node;
}
function mark_component_invisible_if_visible(
node: ProcessedComponentMeta,
hidden_on_startup: Set<number>
): ProcessedComponentMeta {
if (node.props.shared_props.visible === true) {
hidden_on_startup.add(node.id);
node.props.shared_props.visible = false;
}
node.children.forEach((child) => {
mark_component_invisible_if_visible(child, hidden_on_startup);
});
return node;
}
function untrack_children_of_closed_accordions_or_inactive_tabs(
node: ProcessedComponentMeta,
components_to_register: Set<number>,
hidden_on_startup: Set<number>
): ProcessedComponentMeta {
// Check if the node is an accordion or tabs
if (node.type === "accordion" && node.props.props.open === false) {
_untrack(node, components_to_register);
if (node.children) {
node.children.forEach((child) => {
mark_component_invisible_if_visible(child, hidden_on_startup);
});
}
}
if (node.type === "tabs") {
node.children.forEach((child) => {
if (
child.type === "tabitem" &&
child.props.props.id !==
(node.props.props.selected || node.props.props.initial_tabs[0].id)
) {
_untrack(child, components_to_register);
mark_component_invisible_if_visible(child, hidden_on_startup);
}
});
}
return node;
}
function handle_empty_forms(
node: ProcessedComponentMeta,
components_to_register: Set<number>
): ProcessedComponentMeta {
// Check if the node is visible
if (node.type === "form") {
const all_children_invisible = node.children.every(
(child) => child.props.shared_props.visible === false
);
if (all_children_invisible) {
node.props.shared_props.visible = false;
components_to_register.delete(node.id);
return node;
}
}
return node;
}
function update_parent_visibility(
node: ProcessedComponentMeta,
child_made_visible: number,
visibility_state: boolean | "hidden"
): ProcessedComponentMeta {
// This function was added to address a tricky situation:
// Form components are wrapped in a Form component automatically.
// If all the children of the Form are invisible, the Form itself is marked invisible.
// in AppTree.postprocess -> handle_empty_forms
// This is to avoid rendering empty forms in the UI. They look ugly.
// So what happens when a child inside the Form is made visible again?
// The Form needs to become visible again too.
// If the child is made invisible, the form should be too if all other children are invisible.
// However, we are not doing this now since what we want to do is fetch the latest visibility of all
// the children from the UI. However, get_data only returns the props, not the shared props.
if (
node.type === "form" &&
node.children.length &&
node.children.some((child) => child.id === child_made_visible)
) {
if (visibility_state === true) node.props.shared_props.visible = true;
else if (!visibility_state && node.children.length === 1)
node.props.shared_props.visible = "hidden";
}
return node;
}
function translate_props(node: ProcessedComponentMeta): ProcessedComponentMeta {
const supported_props = [
"description",
"info",
"title",
"placeholder",
"value",
"label"
];
for (const attr of Object.keys(node.props.shared_props)) {
if (supported_props.includes(attr as string)) {
// @ts-ignore
node.props.shared_props[attr] = translate_if_needed(
node.props.shared_props[attr as keyof SharedProps]
);
}
}
for (const attr of Object.keys(node.props.props)) {
if (supported_props.includes(attr as string)) {
node.props.props[attr] = translate_if_needed(node.props.props[attr]);
}
}
return node;
}
function apply_initial_tabs(
node: ProcessedComponentMeta,
initial_tabs: Record<number, Tab[]>
): ProcessedComponentMeta {
if (node.type === "tabs" && node.id in initial_tabs) {
const tabs = initial_tabs[node.id].sort((a, b) => a.order! - b.order!);
node.props.props.initial_tabs = tabs;
} else if (node.type === "tabitem") {
node.props.props.component_id = node.id;
}
return node;
}
function _gather_initial_tabs(
node: ProcessedComponentMeta,
initial_tabs: Record<number, Tab[]>,
parent_tab_id: number | null,
order: number | null
): void {
if (parent_tab_id !== null && node.type === "tabitem") {
if (!(parent_tab_id in initial_tabs)) {
initial_tabs[parent_tab_id] = [];
}
if (!("id" in node.props.props)) {
node.props.props.id = node.id;
}
initial_tabs[parent_tab_id].push({
label: node.props.shared_props.label as string,
id: node.props.props.id as string,
elem_id: node.props.shared_props.elem_id,
visible: node.props.shared_props.visible as boolean,
interactive: node.props.shared_props.interactive,
scale: node.props.shared_props.scale || null,
component_id: node.id
});
node.props.props.order = order;
}
if (node.children) {
node.children.forEach((child, i) => {
_gather_initial_tabs(
child,
initial_tabs,
node.type === "tabs" ? node.id : null,
node.type === "tabs" ? i : null
);
});
}
return;
}
function gather_initial_tabs(
node: ProcessedComponentMeta,
initial_tabs: Record<number, Tab[]>
): void {
function single_visit<U extends ProcessedComponentMeta>(node: U): void {
if ("children" in node && node.children.length > 0) {
node.children?.forEach((child) =>
_gather_initial_tabs(
child,
initial_tabs,
node.type === "tabs" ? node.id : null,
null
)
);
}
}
return single_visit(node);
}
function find_node_by_id(
tree: ProcessedComponentMeta,
id: number
): ProcessedComponentMeta | null {
if (tree.id === id) {
return tree;
}
if (tree.children) {
for (const child of tree.children) {
const result = find_node_by_id(child, id);
if (result) {
return result;
}
}
}
return null;
}
function find_parent(
tree: ProcessedComponentMeta,
id: number
): ProcessedComponentMeta | null {
if (tree.children) {
for (const child of tree.children) {
if (child.id === id) {
return tree;
}
const result = find_parent(child, id);
if (result) {
return result;
}
}
}
return null;
}