@treasure-ai/treasure-deck 2026.10.11 → 2026.10.12

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.
@@ -21,6 +21,8 @@
21
21
  // written from them where the page has no URL to reopen (Apps Script's sandboxed iframe).
22
22
  var pristineDeck = deck.outerHTML;
23
23
  var runtimeSource = document.currentScript ? document.currentScript.textContent : '';
24
+ // The deck's source, which `deck build` puts in the page (unless --no-source), for the share panel's download.
25
+ var deckSource = document.getElementById('deck-source');
24
26
 
25
27
  // Host adapter: Apps Script web apps run in a sandboxed iframe, so the top-level URL
26
28
  // hash is reachable only through google.script.history / google.script.url.
@@ -65,6 +67,7 @@
65
67
  '<button data-act="embed">Copy embed snippet</button>' +
66
68
  '<button data-act="print">Print / save as PDF</button>' +
67
69
  '<button data-act="presenter">Open presenter window</button>' +
70
+ (deckSource ? '<button data-act="download">Download source (.zip)</button>' : '') +
68
71
  '<div class="deck-status"></div>');
69
72
  share.hidden = true;
70
73
  var notesPanel = el('div', 'deck-panel deck-notes-panel');
@@ -224,6 +227,101 @@
224
227
  ta.remove();
225
228
  }
226
229
 
230
+ // The deck folder as a .zip: the Markdown and theme files from the page's source, and its images and theme assets
231
+ // from the page's data: URIs, found by the hash the source lists them by. Under Apps Script, the one copy of a deck
232
+ // a viewer can reach.
233
+ function downloadDeck() {
234
+ var bundle = JSON.parse(deckSource.textContent);
235
+ var folder = bundle.deck.replace(/\.md$/, '');
236
+ var enc = new TextEncoder(), entries = [], missing = [];
237
+ function add(rel, data) {
238
+ if (/^\//.test(rel) || /(^|\/)\.\.(\/|$)/.test(rel)) { missing.push(rel); return; }
239
+ entries.push({ name: folder + '/' + rel, data: typeof data === 'string' ? enc.encode(data) : data });
240
+ }
241
+ function addInlined(media, base, found) {
242
+ Object.keys(media || {}).forEach(function (rel) {
243
+ var bytes = found[media[rel].sha256] || (media[rel].data && bytesOf(media[rel].data));
244
+ if (bytes) add(base + rel, bytes); else missing.push(base + rel);
245
+ });
246
+ }
247
+ status('Preparing the download…');
248
+ // In the chain, so an error hashing (no crypto.subtle off https and localhost) reaches the catch below.
249
+ Promise.resolve().then(inlinedFiles).then(function (found) {
250
+ Object.keys(bundle.files).forEach(function (rel) { add(rel, bundle.files[rel]); });
251
+ addInlined(bundle.media, '', found);
252
+ if (bundle.themeFolder) {
253
+ var base = bundle.theme.replace(/^\.\//, '').replace(/\/?$/, '/');
254
+ Object.keys(bundle.themeFolder.files).forEach(function (rel) { add(base + rel, bundle.themeFolder.files[rel]); });
255
+ addInlined(bundle.themeFolder.media, base, found);
256
+ }
257
+ // Videos kept out of the page (--media-dir) aren't in it to save.
258
+ missing = missing.concat(Object.keys(bundle.linked || {}));
259
+ var name = folder + '.zip';
260
+ var url = URL.createObjectURL(zip(entries));
261
+ var a = el('a');
262
+ a.href = url;
263
+ a.download = name;
264
+ document.body.appendChild(a);
265
+ a.click();
266
+ a.remove();
267
+ setTimeout(function () { URL.revokeObjectURL(url); }, 60000);
268
+ status('Saved ' + name + (missing.length ? ', without ' + missing.join(', ') : '') + '. Unzip it and edit it with: deck editor ' + folder);
269
+ }).catch(function (e) { status('The download failed: ' + e.message); });
270
+ }
271
+ // Each data: URI's bytes in the page as built (the deck, and the stylesheets with the theme's assets), by SHA-256.
272
+ function inlinedFiles() {
273
+ var text = pristineDeck + Array.prototype.map.call(document.querySelectorAll('style'), function (e) { return e.textContent; }).join('\n');
274
+ var re = /data:[\w.+-]+\/[\w.+-]+;base64,([A-Za-z0-9+\/]+=*)/g, m, seen = {}, found = {}, work = [];
275
+ while ((m = re.exec(text))) {
276
+ if (seen[m[1]]) continue;
277
+ seen[m[1]] = true;
278
+ work.push(bytesOf(m[1]));
279
+ }
280
+ return Promise.all(work.map(function (bytes) {
281
+ return crypto.subtle.digest('SHA-256', bytes).then(function (h) {
282
+ found[Array.prototype.map.call(new Uint8Array(h), function (b) { return (b < 16 ? '0' : '') + b.toString(16); }).join('')] = bytes;
283
+ });
284
+ })).then(function () { return found; });
285
+ }
286
+ function bytesOf(base64) {
287
+ var bin = atob(base64), bytes = new Uint8Array(bin.length);
288
+ for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
289
+ return bytes;
290
+ }
291
+ var CRC_TABLE = (function () {
292
+ var t = [];
293
+ for (var n = 0; n < 256; n++) {
294
+ var c = n;
295
+ for (var k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
296
+ t[n] = c >>> 0;
297
+ }
298
+ return t;
299
+ })();
300
+ function crc32(bytes) {
301
+ var c = -1;
302
+ for (var i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 255] ^ (c >>> 8);
303
+ return (c ^ -1) >>> 0;
304
+ }
305
+ // A zip of stored (uncompressed) entries, {name, data}: a deck's images are compressed already and its text is small.
306
+ function zip(entries) {
307
+ var enc = new TextEncoder(), parts = [], central = [], offset = 0, size = 0;
308
+ function u16(v) { return [v & 255, (v >>> 8) & 255]; }
309
+ function u32(v) { return u16(v & 0xffff).concat(u16(v >>> 16)); }
310
+ entries.forEach(function (e) {
311
+ var name = enc.encode(e.name);
312
+ // Version 2.0, UTF-8 names, stored, dated 1980-01-01, the CRC and sizes, the name's length, no extra field.
313
+ var fields = [].concat(u16(20), u16(0x0800), u16(0), u16(0), u16(0x21), u32(crc32(e.data)), u32(e.data.length), u32(e.data.length), u16(name.length), u16(0));
314
+ var local = new Uint8Array(u32(0x04034b50).concat(fields));
315
+ var entry = new Uint8Array(u32(0x02014b50).concat(u16(20), fields, u16(0), u16(0), u16(0), u32(0), u32(offset)));
316
+ parts.push(local, name, e.data);
317
+ central.push(entry, name);
318
+ offset += local.length + name.length + e.data.length;
319
+ size += entry.length + name.length;
320
+ });
321
+ var end = new Uint8Array(u32(0x06054b50).concat(u16(0), u16(0), u16(entries.length), u16(entries.length), u32(size), u32(offset), u16(0)));
322
+ return new Blob(parts.concat(central, [end]), { type: 'application/zip' });
323
+ }
324
+
227
325
  share.addEventListener('click', function (e) {
228
326
  var act = e.target && e.target.getAttribute('data-act');
229
327
  var base = host.baseUrl();
@@ -233,6 +331,7 @@
233
331
  else if (act === 'embed') copy(base && '<iframe src="' + base + '" width="960" height="540" frameborder="0" allowfullscreen></iframe>', 'Embed snippet');
234
332
  else if (act === 'print') { share.hidden = true; window.print(); }
235
333
  else if (act === 'presenter') { share.hidden = true; openPresenter(); }
334
+ else if (act === 'download') { used('download'); downloadDeck(); }
236
335
  });
237
336
 
238
337
  var typed = '';
@@ -319,6 +319,10 @@ Treasure Work publishes a built deck as an Apps Script web page through the `sha
319
319
  4. Give the user the URL. Keep the artifact id from the result. To update the same page after edits, rebuild and pass that id as `artifact_id`; without it, the publish creates a second page.
320
320
  5. Publishing needs the user's Google account connected in Settings ▸ Connectors. If the tool reports that it is missing, tell the user to connect it.
321
321
 
322
+ A built page carries the deck's source, so whoever can open it can get the deck folder back (share panel ▸ Download source, or `deck import`), hidden slides included. Build with `--no-source` when the user wants the page shared but not its source.
323
+
324
+ To start from a deck someone shared, run `deck import <page.html | url> [dir]` and edit the folder it writes. For an Apps Script deck that isn't public, it needs the user's Google sign-in (`gcloud auth login --enable-gdrive-access`); otherwise have the user download the `.zip` from the deck's share panel.
325
+
322
326
  Don't publish the deck `.md` as a `note`: it would appear as a plain document, not slides. Don't use `mechanism: "drive"` for a deck either, since Drive doesn't serve the HTML as a page. A published page is a snapshot, so rebuild and republish after each edit.
323
327
 
324
328
  ## When the user wants a different look