Skip to content

Commit 0fefbce

Browse files
committed
template: Add zip download
1 parent 21b606d commit 0fefbce

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
65.5 KB
Binary file not shown.

experimental/template/index.html

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,21 @@
6060
border: none;
6161
border-radius: 4px;
6262
cursor: pointer;
63+
margin-right: 10px;
6364
}
6465
button:hover {
6566
background-color: #45a049;
6667
}
68+
.button-container {
69+
display: flex;
70+
margin-bottom: 20px;
71+
}
72+
#downloadButton {
73+
background-color: #2196F3;
74+
}
75+
#downloadButton:hover {
76+
background-color: #0b7dda;
77+
}
6778
.error-message {
6879
color: #d32f2f;
6980
background-color: #ffebee;
@@ -170,6 +181,7 @@
170181
go.run(result.instance);
171182
console.log("WASM loaded, RenderTemplate function available");
172183
document.getElementById("renderButton").disabled = false;
184+
document.getElementById("downloadButton").disabled = false;
173185

174186
// Check if we have parameters in the URL hash
175187
const hashParamsLoaded = decodeHashToParams();
@@ -214,6 +226,53 @@
214226
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
215227
}
216228

229+
function downloadTemplateZip() {
230+
// Update URL hash with current parameters
231+
encodeParamsToHash();
232+
233+
try {
234+
// Get template name
235+
const templateName = document.getElementById("templateName").value.trim();
236+
if (!templateName) {
237+
throw new Error("Template name cannot be empty");
238+
}
239+
240+
const paramsText = document.getElementById("paramsInput").value;
241+
const params = JSON.parse(paramsText);
242+
243+
const helpersText = document.getElementById("helpersInput").value;
244+
const helpers = JSON.parse(helpersText);
245+
246+
// Call the WASM function to get a ZIP file
247+
const result = RenderTemplateZip(templateName, JSON.stringify(params), JSON.stringify(helpers));
248+
249+
try {
250+
const zipData = JSON.parse(result);
251+
252+
if (zipData.error) {
253+
throw new Error(zipData.error);
254+
}
255+
256+
// Create a download link for the ZIP file
257+
const downloadLink = document.createElement("a");
258+
downloadLink.href = "data:" + zipData.type + ";base64," + zipData.data;
259+
downloadLink.download = templateName + ".zip";
260+
261+
// Append to body, click, and remove
262+
document.body.appendChild(downloadLink);
263+
downloadLink.click();
264+
document.body.removeChild(downloadLink);
265+
266+
} catch (parseError) {
267+
alert("Error creating ZIP file: " + parseError.message);
268+
console.error(parseError);
269+
}
270+
} catch (error) {
271+
alert("Error: " + error.message);
272+
console.error(error);
273+
}
274+
}
275+
217276
function renderTemplate() {
218277
// Update URL hash with current parameters
219278
encodeParamsToHash();
@@ -404,7 +463,10 @@ <h3>Common Parameters:</h3>
404463
<textarea id="helpersInput"></textarea>
405464
</div>
406465

407-
<button id="renderButton" onclick="renderTemplate()" disabled>Render Template</button>
466+
<div class="button-container">
467+
<button id="renderButton" onclick="renderTemplate()" disabled>Render Template</button>
468+
<button id="downloadButton" onclick="downloadTemplateZip()" disabled>Download ZIP</button>
469+
</div>
408470
<div id="output"></div>
409471
<!-- Add Prism.js scripts at the end of body -->
410472
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>

0 commit comments

Comments
 (0)