|
60 | 60 | border: none; |
61 | 61 | border-radius: 4px; |
62 | 62 | cursor: pointer; |
| 63 | + margin-right: 10px; |
63 | 64 | } |
64 | 65 | button:hover { |
65 | 66 | background-color: #45a049; |
66 | 67 | } |
| 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 | + } |
67 | 78 | .error-message { |
68 | 79 | color: #d32f2f; |
69 | 80 | background-color: #ffebee; |
|
170 | 181 | go.run(result.instance); |
171 | 182 | console.log("WASM loaded, RenderTemplate function available"); |
172 | 183 | document.getElementById("renderButton").disabled = false; |
| 184 | + document.getElementById("downloadButton").disabled = false; |
173 | 185 |
|
174 | 186 | // Check if we have parameters in the URL hash |
175 | 187 | const hashParamsLoaded = decodeHashToParams(); |
|
214 | 226 | return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; |
215 | 227 | } |
216 | 228 |
|
| 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 | + |
217 | 276 | function renderTemplate() { |
218 | 277 | // Update URL hash with current parameters |
219 | 278 | encodeParamsToHash(); |
@@ -404,7 +463,10 @@ <h3>Common Parameters:</h3> |
404 | 463 | <textarea id="helpersInput"></textarea> |
405 | 464 | </div> |
406 | 465 |
|
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> |
408 | 470 | <div id="output"></div> |
409 | 471 | <!-- Add Prism.js scripts at the end of body --> |
410 | 472 | <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script> |
|
0 commit comments