@cloudhal/astro-renderer 0.1.1 → 0.2.0
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/src/preview.js +1 -1
- package/dist/src/surface-client.d.ts +2 -2
- package/dist/src/surface-client.js +7 -6
- package/dist/src/surfaces/primitives/data.d.ts +4 -1
- package/dist/src/surfaces/primitives/data.js +9 -7
- package/dist/src/surfaces/primitives/inputs.js +3 -1
- package/dist/src/surfaces/tree.js +1 -1
- package/package.json +6 -6
package/dist/src/preview.js
CHANGED
|
@@ -508,7 +508,7 @@ const PREVIEW_CSS =
|
|
|
508
508
|
".ch-slider{display:flex;align-items:center;gap:.75rem}.ch-slider input{flex:1;max-width:24rem}.ch-slider__value{font-weight:600;min-width:3rem}" +
|
|
509
509
|
// RFC-0022 §4: tables (cards below 480 px).
|
|
510
510
|
".ch-table{width:100%;border-collapse:collapse;font-size:.9rem}.ch-table caption{text-align:left;font-weight:600;padding-bottom:.5rem}.ch-table th,.ch-table td{border-bottom:1px solid var(--line);padding:.4rem .6rem;text-align:left}.ch-table [data-align=end]{text-align:right}.ch-table[data-striped=yes] tbody tr:nth-child(even){background:color-mix(in srgb,var(--fg) 4%,transparent)}.ch-table tr[data-highlight=yes]{background:color-mix(in srgb,var(--fg) 8%,transparent)}" +
|
|
511
|
-
"@media (max-width:480px){.ch-table thead{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}.ch-table caption{display:block;width:100%}.ch-table,.ch-table tbody,.ch-table tr,.ch-table td{display:block;width:100%}.ch-table tr{margin-bottom:.75rem;border:1px solid var(--line);border-radius:8px;padding:.25rem .75rem}.ch-table td{display:flex;justify-content:space-between;gap:1rem;text-align:right}.ch-table td::before{content:attr(data-label);font-weight:500;color:var(--muted);text-align:left}}" +
|
|
511
|
+
"@media (max-width:480px){.ch-table thead{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}.ch-table caption{display:block;width:100%}.ch-table,.ch-table tbody,.ch-table tr,.ch-table td{display:block;width:100%}.ch-table tr{margin-bottom:.75rem;border:1px solid var(--line);border-radius:8px;padding:.25rem .75rem}.ch-table td{display:flex;justify-content:space-between;gap:1rem;text-align:right}.ch-table td::before{content:attr(data-label);font-weight:500;color:var(--muted);text-align:left}.ch-table td[data-phone=hide]{display:none}}" +
|
|
512
512
|
// RFC-0022 §3: charts.
|
|
513
513
|
":root{--ch-chart-1:#18181b;--ch-chart-2:#2563eb;--ch-chart-3:#d97706;--ch-chart-4:#059669;--ch-chart-5:#db2777;--ch-chart-6:#7c3aed}.ch-chart{margin:0;display:grid;gap:.75rem}.ch-chart__title{font-weight:600}.ch-chart__svg{display:block;width:100%;height:auto}.ch-chart__grid{stroke:var(--line)}.ch-chart__tick{fill:var(--muted);font-size:12px}.ch-chart__total{fill:currentColor;font-size:22px;font-weight:600}" +
|
|
514
514
|
".ch-chart__legend{display:flex;flex-wrap:wrap;gap:.25rem 1rem;margin:0;padding:0;list-style:none;font-size:.9rem}.ch-chart__legend li{display:flex;align-items:center;gap:.4rem}.ch-chart__swatch{display:inline-block;width:.75rem;height:.75rem;border-radius:2px}.ch-chart__caption,.ch-chart__empty,.ch-chart__data summary{color:var(--muted);font-size:.9rem}.ch-chart__data table{width:100%;border-collapse:collapse;font-size:.9rem}.ch-chart__data th,.ch-chart__data td{border-bottom:1px solid var(--line);padding:.25rem .5rem;text-align:left}" +
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
* with any change to the code; a test pins the code's hash to the version.
|
|
23
23
|
*/
|
|
24
24
|
export declare const SURFACE_SCRIPT: {
|
|
25
|
-
readonly path: "/_cloudhal/surface.
|
|
25
|
+
readonly path: "/_cloudhal/surface.11.js";
|
|
26
26
|
readonly code: string;
|
|
27
27
|
};
|
|
28
28
|
/**
|
|
29
29
|
* Earlier names of the runtime, served with the current code: pages cached with them keep
|
|
30
30
|
* working (each version only adds to the one before; RFC-0022 §1).
|
|
31
31
|
*/
|
|
32
|
-
export declare const SURFACE_SCRIPT_ALIASES: readonly ["/_cloudhal/surface.1.js", "/_cloudhal/surface.2.js", "/_cloudhal/surface.3.js", "/_cloudhal/surface.4.js", "/_cloudhal/surface.5.js", "/_cloudhal/surface.6.js", "/_cloudhal/surface.7.js", "/_cloudhal/surface.8.js", "/_cloudhal/surface.9.js"];
|
|
32
|
+
export declare const SURFACE_SCRIPT_ALIASES: readonly ["/_cloudhal/surface.1.js", "/_cloudhal/surface.2.js", "/_cloudhal/surface.3.js", "/_cloudhal/surface.4.js", "/_cloudhal/surface.5.js", "/_cloudhal/surface.6.js", "/_cloudhal/surface.7.js", "/_cloudhal/surface.8.js", "/_cloudhal/surface.9.js", "/_cloudhal/surface.10.js"];
|
|
33
33
|
/** JSON for a `<script type="application/json">`: nothing in it can end the element. */
|
|
34
34
|
export declare function scriptJson(value: unknown): string;
|
|
@@ -52,7 +52,7 @@ const ex=()=>{let v=te();for(let c=pk();c==="+"||c==="-";c=pk()){i++;const r=te(
|
|
|
52
52
|
try{if(!pk())return"";const v=ex();return pk()===undefined?fin(v):"";}catch{return"";}};
|
|
53
53
|
const fn=(name,a,loc)=>{const v=a.value;switch(name){
|
|
54
54
|
case"formatDate":{const d=new Date(txt(v));if(Number.isNaN(d.getTime()))return"";const st=["short","medium","long","full"].includes(txt(a.style))?txt(a.style):"medium";return new Intl.DateTimeFormat(loc,{dateStyle:st,timeZone:"UTC"}).format(d);}
|
|
55
|
-
case"formatNumber":{if(blank(v))return"";const n=num(v);if(!Number.isFinite(n))return"";const g=Math.min(Math.max(0,Math.trunc(num(a.digits===undefined?2:a.digits))||0),6);return new Intl.NumberFormat(loc,{maximumFractionDigits:g}).format(n);}
|
|
55
|
+
case"formatNumber":{if(blank(v))return"";const n=num(v);if(!Number.isFinite(n))return"";const g=Math.min(Math.max(0,Math.trunc(num(a.digits===undefined?2:a.digits))||0),6);const m=Math.min(Math.max(0,Math.trunc(num(a.minDigits===undefined?0:a.minDigits))||0),6);return new Intl.NumberFormat(loc,{minimumFractionDigits:m,maximumFractionDigits:Math.max(g,m)}).format(n);}
|
|
56
56
|
case"formatCurrency":{if(blank(v))return"";const n=num(v);const c=txt(a.currency).toUpperCase();if(!Number.isFinite(n)||!/^[A-Z]{3}$/.test(c))return"";try{return new Intl.NumberFormat(loc,{style:"currency",currency:c}).format(n);}catch{return"";}}
|
|
57
57
|
case"pluralize":{const n=num(a.count);return(n===1?txt(a.one):txt(a.other)).replace(/#/g,Number.isFinite(n)?String(n):"");}
|
|
58
58
|
case"concat":return(Array.isArray(a.parts)?a.parts:[]).map(txt).join("");
|
|
@@ -76,14 +76,14 @@ case"calculate":return calc(txt(a.expression),a.vars);}
|
|
|
76
76
|
return"";};
|
|
77
77
|
const nil=(o)=>o===undefined||o===null?"":o;
|
|
78
78
|
const val=(v,d,loc,z=0)=>{if(isPath(v))return get(d,v.path);if(isCall(v)){if(z>=8)return"";const r=obj(v.args)?v.args:{};const at=(x)=>val(x,d,loc,z+1);
|
|
79
|
-
if(v.call==="if")return nil(cond(r.cond,d)?at(r.ifTrue):at(r.ifFalse));
|
|
79
|
+
if(v.call==="if")return nil(cond(r.cond,d,loc,z+1)?at(r.ifTrue):at(r.ifFalse));
|
|
80
80
|
if(v.call==="calculate"&&obj(r.vars)){const vs={};for(const[k,x]of Object.entries(r.vars))vs[k]=at(x);return calc(txt(at(r.expression)),vs);}
|
|
81
81
|
if(v.call==="choose"){const k=txt(at(r.value));const c=obj(r.cases)?r.cases:{};return nil(own(c,k)?at(c[k]):at(r.default));}
|
|
82
82
|
const a={};for(const[k,x]of Object.entries(r))a[k]=Array.isArray(x)?x.map(at):at(x);return fn(v.call,a,loc);}return v;};
|
|
83
83
|
const truthy=(v)=>!(v===undefined||v===null||v===false||v===0||v===""||(Array.isArray(v)&&v.length===0));
|
|
84
|
-
const cond=(c,d)=>{if(c===undefined)return true;if(isPath(c))return truthy(get(d,c.path));if(!obj(c))return false;const[op,a]=Object.entries(c)[0]||[];const o=(x)=>isPath(x)?
|
|
84
|
+
const cond=(c,d,loc,z=0)=>{if(c===undefined)return true;if(isPath(c))return truthy(get(d,c.path));if(!obj(c))return false;const[op,a]=Object.entries(c)[0]||[];const o=(x)=>isPath(x)||isCall(x)?val(x,d,loc,z):x;const sub=(x)=>cond(x,d,loc,z);
|
|
85
85
|
if(["eq","ne","gt","lt","gte","lte"].includes(op)&&Array.isArray(a)){const x=o(a[0]);const y=o(a[1]);if(op==="eq")return x===y;if(op==="ne")return x!==y;if(typeof x!==typeof y||(typeof x!=="number"&&typeof x!=="string"))return false;return op==="gt"?x>y:op==="lt"?x<y:op==="gte"?x>=y:x<=y;}
|
|
86
|
-
if(op==="and"&&Array.isArray(a))return a.every(
|
|
86
|
+
if(op==="and"&&Array.isArray(a))return a.every(sub);if(op==="or"&&Array.isArray(a))return a.some(sub);if(op==="not")return!sub(a);return false;};
|
|
87
87
|
const safe=(h)=>typeof h==="string"&&/^(\/(?!\/)|#|https:\/\/)/.test(h)&&!/[\s<>"']/.test(h);
|
|
88
88
|
const json=(el,a)=>{try{return JSON.parse(el.getAttribute(a)||"null");}catch{return undefined;}};
|
|
89
89
|
let chartMod;
|
|
@@ -93,7 +93,7 @@ if(chartMod)draw(chartMod);else import("${CHART_SCRIPT.path}").then((m)=>{chartM
|
|
|
93
93
|
const boot=(root)=>{const s=root.querySelector("script[data-ch-state]");if(!s||root.hasAttribute("data-ch-live"))return;root.setAttribute("data-ch-live","");
|
|
94
94
|
let init;try{init=JSON.parse(s.textContent||"{}");}catch{return;}let d=structuredClone(init);const loc=document.documentElement.lang||"en";
|
|
95
95
|
const render=()=>{for(const el of root.querySelectorAll("[data-ch-text]"))el.textContent=txt(val(json(el,"data-ch-text"),d,loc));
|
|
96
|
-
for(const el of root.querySelectorAll("[data-ch-visible]"))el.hidden=!cond(json(el,"data-ch-visible"),d);
|
|
96
|
+
for(const el of root.querySelectorAll("[data-ch-visible]"))el.hidden=!cond(json(el,"data-ch-visible"),d,loc);
|
|
97
97
|
charts(root,d,loc);
|
|
98
98
|
for(const el of root.querySelectorAll("[data-ch-invalid]"))el.setAttribute("aria-invalid",truthy(val(json(el,"data-ch-invalid"),d,loc))?"true":"false");
|
|
99
99
|
for(const el of root.querySelectorAll("[data-ch-model]")){const v=get(d,el.getAttribute("data-ch-model"));if(el.type==="radio")el.checked=el.value===txt(v);else if(el.type==="checkbox")el.checked=truthy(v);else if(v!==undefined&&document.activeElement!==el)el.value=txt(v);}};
|
|
@@ -117,7 +117,7 @@ render();};
|
|
|
117
117
|
for(const root of document.querySelectorAll("[data-ch-surface]"))boot(root);
|
|
118
118
|
`;
|
|
119
119
|
|
|
120
|
-
export const SURFACE_SCRIPT = { path: "/_cloudhal/surface.
|
|
120
|
+
export const SURFACE_SCRIPT = { path: "/_cloudhal/surface.11.js", code } ;
|
|
121
121
|
|
|
122
122
|
/**
|
|
123
123
|
* Earlier names of the runtime, served with the current code: pages cached with them keep
|
|
@@ -133,6 +133,7 @@ export const SURFACE_SCRIPT_ALIASES = [
|
|
|
133
133
|
"/_cloudhal/surface.7.js",
|
|
134
134
|
"/_cloudhal/surface.8.js",
|
|
135
135
|
"/_cloudhal/surface.9.js",
|
|
136
|
+
"/_cloudhal/surface.10.js",
|
|
136
137
|
] ;
|
|
137
138
|
|
|
138
139
|
/** JSON for a `<script type="application/json">`: nothing in it can end the element. */
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { type PrimitiveRenderer } from "../markup.js";
|
|
2
2
|
/** RFC-0022 §3: drawn here as SVG; a live surface redraws it when its data changes. */
|
|
3
3
|
export declare const chartHtml: PrimitiveRenderer;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* RFC-0022 §4: a real table; `data-label` lets the theme stack rows into cards on phones, and
|
|
6
|
+
* `data-phone="hide"` leaves a column out of those cards (never the first, which names the row).
|
|
7
|
+
*/
|
|
5
8
|
export declare const tableHtml: PrimitiveRenderer;
|
|
@@ -53,25 +53,27 @@ function cellText(v , c , locale )
|
|
|
53
53
|
return str(v);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* RFC-0022 §4: a real table; `data-label` lets the theme stack rows into cards on phones, and
|
|
58
|
+
* `data-phone="hide"` leaves a column out of those cards (never the first, which names the row).
|
|
59
|
+
*/
|
|
57
60
|
export const tableHtml = ({ props: p }, { media }) => {
|
|
58
61
|
const locale = media?.locale ?? "en";
|
|
59
62
|
const rows = (Array.isArray(p.rows) ? p.rows : []).filter(isRow).slice(0, 200);
|
|
60
63
|
const columns = (Array.isArray(p.columns) ? p.columns : []).filter(isRow).slice(0, 8);
|
|
61
64
|
const hl = isRow(p.highlight) ? p.highlight : undefined;
|
|
65
|
+
const extra = (c , i ) =>
|
|
66
|
+
`${c.align === "end" ? ' data-align="end"' : ""}${i > 0 && c.hideOnPhone === true ? ' data-phone="hide"' : ""}`;
|
|
62
67
|
const head = columns
|
|
63
|
-
.map(
|
|
64
|
-
(c) =>
|
|
65
|
-
`<th scope="col"${c.align === "end" ? ' data-align="end"' : ""}>${escapeHtml(str(c.label))}</th>`,
|
|
66
|
-
)
|
|
68
|
+
.map((c, i) => `<th scope="col"${extra(c, i)}>${escapeHtml(str(c.label))}</th>`)
|
|
67
69
|
.join("");
|
|
68
70
|
const body = rows
|
|
69
71
|
.map((r) => {
|
|
70
72
|
const marked = hl && str(r[str(hl.field)]) === str(hl.equals);
|
|
71
73
|
return `<tr${marked ? ' data-highlight="yes"' : ""}>${columns
|
|
72
74
|
.map(
|
|
73
|
-
(c) =>
|
|
74
|
-
`<td data-label="${escapeHtml(str(c.label))}"${c
|
|
75
|
+
(c, i) =>
|
|
76
|
+
`<td data-label="${escapeHtml(str(c.label))}"${extra(c, i)}>${escapeHtml(cellText(r[str(c.field)], c, locale))}</td>`,
|
|
75
77
|
)
|
|
76
78
|
.join("")}</tr>`;
|
|
77
79
|
})
|
|
@@ -14,7 +14,9 @@ export const toggleHtml = (node, { id }) => {
|
|
|
14
14
|
`<label class="ch-toggle__option"><input type="radio" name="${id}" value="${escapeHtml(o)}"${model(node)}${o === str(p.value) ? " checked" : ""}><span>${escapeHtml(o)}</span></label>`,
|
|
15
15
|
)
|
|
16
16
|
.join("");
|
|
17
|
-
|
|
17
|
+
// A label shows as a legend, as RadioGroup's does (the fieldset names the group).
|
|
18
|
+
if (!str(p.label)) return `<div class="ch-toggle" role="radiogroup">${radios}</div>`;
|
|
19
|
+
return `<fieldset class="ch-radio ch-toggle-field"><legend class="ch-field__label">${escapeHtml(str(p.label))}</legend><div class="ch-toggle">${radios}</div></fieldset>`;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
export const selectHtml = (node) => {
|
|
@@ -202,7 +202,7 @@ export function surfaceTree(section , ctx = {})
|
|
|
202
202
|
const index = section.components.indexOf(c);
|
|
203
203
|
if (invalidProps.has(String(index))) return undefined;
|
|
204
204
|
// A live surface keeps hidden components (the runtime shows them when their condition holds).
|
|
205
|
-
const shown = evaluateCondition(c.visible, data);
|
|
205
|
+
const shown = evaluateCondition(c.visible, data, { locale });
|
|
206
206
|
if (!shown && !live) return undefined;
|
|
207
207
|
const entry = catalog.get(c.component);
|
|
208
208
|
if (!entry) return undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudhal/astro-renderer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Render a Cloudhal Site Graph with Astro: data sources (live/snapshot), section components and the cloudhal() integration",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://cloudhal.dev",
|
|
@@ -179,14 +179,14 @@
|
|
|
179
179
|
"README.md"
|
|
180
180
|
],
|
|
181
181
|
"dependencies": {
|
|
182
|
-
"@cloudhal/core-contracts": "0.
|
|
183
|
-
"@cloudhal/core-ts": "0.
|
|
184
|
-
"@cloudhal/storage": "0.
|
|
182
|
+
"@cloudhal/core-contracts": "0.2.0",
|
|
183
|
+
"@cloudhal/core-ts": "0.2.0",
|
|
184
|
+
"@cloudhal/storage": "0.2.0",
|
|
185
185
|
"marked": "^18.1.0",
|
|
186
186
|
"parse5": "8.0.1",
|
|
187
|
-
"@cloudhal/analytics": "0.
|
|
187
|
+
"@cloudhal/analytics": "0.2.0",
|
|
188
188
|
"vite": "^8.3.3",
|
|
189
|
-
"@cloudhal/comments": "0.
|
|
189
|
+
"@cloudhal/comments": "0.2.0"
|
|
190
190
|
},
|
|
191
191
|
"peerDependencies": {
|
|
192
192
|
"astro": "^7.3.0",
|