-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathexport-utils.ts
More file actions
174 lines (165 loc) · 4.87 KB
/
Copy pathexport-utils.ts
File metadata and controls
174 lines (165 loc) · 4.87 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
import { THEME_CSS, SVG_CLASSES_CSS, FORM_STYLES_CSS } from "./widget-renderer";
const CHART_COLORS = [
"#3b82f6",
"#8b5cf6",
"#ec4899",
"#f59e0b",
"#10b981",
"#06b6d4",
"#f97316",
];
// Import map matching widget-renderer's assembleShell — allows widgets that
// use bare specifiers (e.g. `import * as THREE from "three"`) to work standalone.
const IMPORT_MAP = `<script type="importmap">
{
"imports": {
"three": "https://esm.sh/three",
"three/": "https://esm.sh/three/",
"gsap": "https://esm.sh/gsap",
"gsap/": "https://esm.sh/gsap/",
"d3": "https://esm.sh/d3",
"d3/": "https://esm.sh/d3/",
"chart.js": "https://esm.sh/chart.js",
"chart.js/": "https://esm.sh/chart.js/"
}
}
</script>`;
/**
* Wrap a raw HTML fragment (the same string passed to WidgetRenderer)
* in a standalone document that works when opened in a browser.
*/
export function assembleStandaloneHtml(html: string, title: string): string {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${escapeHtml(title)}</title>
${IMPORT_MAP}
<style>
${THEME_CSS}
${SVG_CLASSES_CSS}
${FORM_STYLES_CSS}
</style>
</head>
<body>
<div id="content">
${html}
</div>
<script>
// Stub bridge functions so onclick="sendPrompt(...)" doesn't throw
window.sendPrompt = function() {};
window.openLink = function(url) { if (url) window.open(url, '_blank'); };
document.addEventListener('click', function(e) {
var a = e.target.closest('a[href]');
if (a && a.href.startsWith('http')) {
e.preventDefault();
window.open(a.href, '_blank');
}
});
</script>
</body>
</html>`;
}
/**
* Generate a standalone HTML file that renders a chart using Chart.js from CDN.
*/
export function chartToStandaloneHtml(
type: "bar" | "pie",
data: { title: string; description: string; data: Array<{ label: string; value: number }> }
): string {
const labels = JSON.stringify(data.data.map((d) => d.label));
const values = JSON.stringify(data.data.map((d) => d.value));
const colors = JSON.stringify(
data.data.map((_, i) => CHART_COLORS[i % CHART_COLORS.length])
);
const chartConfig =
type === "bar"
? `{
type: 'bar',
data: {
labels: ${labels},
datasets: [{
data: ${values},
backgroundColor: ${colors},
borderRadius: 4,
}]
},
options: {
responsive: true,
plugins: {
legend: { display: false },
tooltip: { backgroundColor: '#1f2937', titleColor: '#fff', bodyColor: '#fff', cornerRadius: 8, padding: 10 }
},
scales: {
x: { grid: { display: false } },
y: { grid: { color: 'rgba(0,0,0,0.06)' } }
}
}
}`
: `{
type: 'pie',
data: {
labels: ${labels},
datasets: [{
data: ${values},
backgroundColor: ${colors},
}]
},
options: {
responsive: true,
plugins: {
legend: { position: 'bottom', labels: { padding: 16, usePointStyle: true } },
tooltip: { backgroundColor: '#1f2937', titleColor: '#fff', bodyColor: '#fff', cornerRadius: 8, padding: 10 }
}
}
}`;
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${escapeHtml(data.title)}</title>
<style>
${THEME_CSS}
body { font-family: system-ui, -apple-system, sans-serif; padding: 24px; max-width: 640px; margin: 0 auto; }
h3 { font-size: 20px; font-weight: 700; margin: 0 0 4px; color: var(--color-text-primary); }
p { font-size: 14px; color: var(--color-text-secondary); margin: 0 0 20px; }
canvas { max-height: 360px; }
</style>
</head>
<body>
<h3>${escapeHtml(data.title)}</h3>
<p>${escapeHtml(data.description)}</p>
<canvas id="chart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js"></script>
<script>
new Chart(document.getElementById('chart'), ${chartConfig});
</script>
</body>
</html>`;
}
export function slugify(text: string): string {
return text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
export function triggerDownload(htmlString: string, filename: string): void {
const blob = new Blob([htmlString], { type: "text/html" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}