@constela/compiler 0.14.2 → 0.14.3
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/dist/index.js +97 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2430,6 +2430,23 @@ function transformActionStep2(step) {
|
|
|
2430
2430
|
};
|
|
2431
2431
|
}
|
|
2432
2432
|
}
|
|
2433
|
+
function transformLocalState2(localState) {
|
|
2434
|
+
const result = {};
|
|
2435
|
+
for (const [name, field] of Object.entries(localState)) {
|
|
2436
|
+
result[name] = { type: field.type, initial: field.initial };
|
|
2437
|
+
}
|
|
2438
|
+
return result;
|
|
2439
|
+
}
|
|
2440
|
+
function transformLocalActions2(localActions) {
|
|
2441
|
+
const result = {};
|
|
2442
|
+
for (const action of localActions) {
|
|
2443
|
+
result[action.name] = {
|
|
2444
|
+
name: action.name,
|
|
2445
|
+
steps: action.steps.map(transformActionStep2)
|
|
2446
|
+
};
|
|
2447
|
+
}
|
|
2448
|
+
return result;
|
|
2449
|
+
}
|
|
2433
2450
|
function transformActions2(actions) {
|
|
2434
2451
|
if (!actions) return [];
|
|
2435
2452
|
return actions.map((action) => ({
|
|
@@ -2493,11 +2510,38 @@ function transformViewNode2(node, ctx) {
|
|
|
2493
2510
|
name: node.name
|
|
2494
2511
|
};
|
|
2495
2512
|
case "component": {
|
|
2496
|
-
const
|
|
2497
|
-
|
|
2498
|
-
|
|
2513
|
+
const componentNode = node;
|
|
2514
|
+
const def = ctx.components[componentNode.name];
|
|
2515
|
+
if (!def) {
|
|
2516
|
+
return { kind: "element", tag: "div" };
|
|
2517
|
+
}
|
|
2518
|
+
const params = {};
|
|
2519
|
+
if (componentNode.props) {
|
|
2520
|
+
for (const [name, expr] of Object.entries(componentNode.props)) {
|
|
2521
|
+
params[name] = transformExpression2(expr);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
const children = [];
|
|
2525
|
+
if (componentNode.children && componentNode.children.length > 0) {
|
|
2526
|
+
for (const child of componentNode.children) {
|
|
2527
|
+
children.push(transformViewNode2(child, ctx));
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
const newCtx = {
|
|
2531
|
+
...ctx,
|
|
2532
|
+
currentParams: params,
|
|
2533
|
+
currentChildren: children
|
|
2534
|
+
};
|
|
2535
|
+
const expandedView = transformViewNode2(def.view, newCtx);
|
|
2536
|
+
if (def.localState && Object.keys(def.localState).length > 0) {
|
|
2537
|
+
return {
|
|
2538
|
+
kind: "localState",
|
|
2539
|
+
state: transformLocalState2(def.localState),
|
|
2540
|
+
actions: transformLocalActions2(def.localActions ?? []),
|
|
2541
|
+
child: expandedView
|
|
2542
|
+
};
|
|
2499
2543
|
}
|
|
2500
|
-
return
|
|
2544
|
+
return expandedView;
|
|
2501
2545
|
}
|
|
2502
2546
|
case "markdown":
|
|
2503
2547
|
return {
|
|
@@ -2676,7 +2720,43 @@ function processNamedSlotsOnly(node, namedContent) {
|
|
|
2676
2720
|
}
|
|
2677
2721
|
return node;
|
|
2678
2722
|
}
|
|
2679
|
-
function
|
|
2723
|
+
function expandComponentNode(node, components, defaultContent, namedContent) {
|
|
2724
|
+
const componentNode = node;
|
|
2725
|
+
const def = components[componentNode.name];
|
|
2726
|
+
if (!def) {
|
|
2727
|
+
return { kind: "element", tag: "div" };
|
|
2728
|
+
}
|
|
2729
|
+
const params = {};
|
|
2730
|
+
if (componentNode.props) {
|
|
2731
|
+
for (const [name, expr] of Object.entries(componentNode.props)) {
|
|
2732
|
+
params[name] = transformExpression2(expr);
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
const children = [];
|
|
2736
|
+
if (componentNode.children && componentNode.children.length > 0) {
|
|
2737
|
+
for (const child of componentNode.children) {
|
|
2738
|
+
const transformedChild = transformViewNode2(child, { components });
|
|
2739
|
+
children.push(replaceSlots(transformedChild, defaultContent, namedContent, components));
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2742
|
+
const newCtx = {
|
|
2743
|
+
components,
|
|
2744
|
+
currentParams: params,
|
|
2745
|
+
currentChildren: children
|
|
2746
|
+
};
|
|
2747
|
+
const expandedView = transformViewNode2(def.view, newCtx);
|
|
2748
|
+
const processedView = replaceSlots(expandedView, defaultContent, namedContent, components);
|
|
2749
|
+
if (def.localState && Object.keys(def.localState).length > 0) {
|
|
2750
|
+
return {
|
|
2751
|
+
kind: "localState",
|
|
2752
|
+
state: transformLocalState2(def.localState),
|
|
2753
|
+
actions: transformLocalActions2(def.localActions ?? []),
|
|
2754
|
+
child: processedView
|
|
2755
|
+
};
|
|
2756
|
+
}
|
|
2757
|
+
return processedView;
|
|
2758
|
+
}
|
|
2759
|
+
function replaceSlots(node, defaultContent, namedContent, components) {
|
|
2680
2760
|
if (node.kind === "slot") {
|
|
2681
2761
|
const slotName = node.name;
|
|
2682
2762
|
if (slotName && namedContent?.[slotName]) {
|
|
@@ -2688,10 +2768,13 @@ function replaceSlots(node, defaultContent, namedContent) {
|
|
|
2688
2768
|
}
|
|
2689
2769
|
return clonedDefault;
|
|
2690
2770
|
}
|
|
2771
|
+
if (node.kind === "component" && components) {
|
|
2772
|
+
return expandComponentNode(node, components, defaultContent, namedContent);
|
|
2773
|
+
}
|
|
2691
2774
|
if (node.kind === "element") {
|
|
2692
2775
|
const children = node.children;
|
|
2693
2776
|
if (children && children.length > 0) {
|
|
2694
|
-
const newChildren = children.map((child) => replaceSlots(child, defaultContent, namedContent));
|
|
2777
|
+
const newChildren = children.map((child) => replaceSlots(child, defaultContent, namedContent, components));
|
|
2695
2778
|
return {
|
|
2696
2779
|
...node,
|
|
2697
2780
|
children: newChildren
|
|
@@ -2703,10 +2786,10 @@ function replaceSlots(node, defaultContent, namedContent) {
|
|
|
2703
2786
|
const ifNode = node;
|
|
2704
2787
|
const result = {
|
|
2705
2788
|
...node,
|
|
2706
|
-
then: replaceSlots(ifNode.then, defaultContent, namedContent)
|
|
2789
|
+
then: replaceSlots(ifNode.then, defaultContent, namedContent, components)
|
|
2707
2790
|
};
|
|
2708
2791
|
if (ifNode.else) {
|
|
2709
|
-
result.else = replaceSlots(ifNode.else, defaultContent, namedContent);
|
|
2792
|
+
result.else = replaceSlots(ifNode.else, defaultContent, namedContent, components);
|
|
2710
2793
|
}
|
|
2711
2794
|
return result;
|
|
2712
2795
|
}
|
|
@@ -2714,7 +2797,7 @@ function replaceSlots(node, defaultContent, namedContent) {
|
|
|
2714
2797
|
const eachNode = node;
|
|
2715
2798
|
return {
|
|
2716
2799
|
...node,
|
|
2717
|
-
body: replaceSlots(eachNode.body, defaultContent, namedContent)
|
|
2800
|
+
body: replaceSlots(eachNode.body, defaultContent, namedContent, components)
|
|
2718
2801
|
};
|
|
2719
2802
|
}
|
|
2720
2803
|
return node;
|
|
@@ -2744,7 +2827,11 @@ function composeLayoutWithPage(layout, page, layoutParams, slots) {
|
|
|
2744
2827
|
} else {
|
|
2745
2828
|
namedContent = extractMdxSlotsFromImportData(page.importData);
|
|
2746
2829
|
}
|
|
2747
|
-
const
|
|
2830
|
+
const mergedComponents = {
|
|
2831
|
+
...layout.components || {},
|
|
2832
|
+
...page.components || {}
|
|
2833
|
+
};
|
|
2834
|
+
const composedView = replaceSlots(layoutView, page.view, namedContent, mergedComponents);
|
|
2748
2835
|
const mergedState = {};
|
|
2749
2836
|
for (const [name, field] of Object.entries(page.state)) {
|
|
2750
2837
|
mergedState[name] = field;
|
|
@@ -2778,10 +2865,6 @@ function composeLayoutWithPage(layout, page, layoutParams, slots) {
|
|
|
2778
2865
|
mergedActions[action.name] = action;
|
|
2779
2866
|
}
|
|
2780
2867
|
}
|
|
2781
|
-
const mergedComponents = {
|
|
2782
|
-
...layout.components || {},
|
|
2783
|
-
...page.components || {}
|
|
2784
|
-
};
|
|
2785
2868
|
const result = {
|
|
2786
2869
|
version: "1.0",
|
|
2787
2870
|
state: mergedState,
|