foss-design 0.4.2 → 0.6.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.
@@ -467,6 +467,77 @@ var FrameInspector = class {
467
467
  };
468
468
  var SWALLOWED = ["pointerdown", "mousedown", "pointerup", "mouseup", "dblclick", "submit"];
469
469
 
470
+ // src/runtime/public.ts
471
+ var ATTRS = ["src", "srcset", "poster", "href", "xlink:href"];
472
+ var LOADS_HREF = /* @__PURE__ */ new Set(["link", "image", "use", "feimage"]);
473
+ function servePublicFolder(folder) {
474
+ if (!folder?.files.length) return;
475
+ const files = new Set(folder.files);
476
+ const base = new URL(folder.base, location.href);
477
+ const resolve = (value) => {
478
+ if (!value.startsWith("/") || value.startsWith("//")) return null;
479
+ const [, pathname = "", suffix = ""] = /^([^?#]*)(.*)$/.exec(value) ?? [];
480
+ let rel;
481
+ try {
482
+ rel = decodeURIComponent(pathname.slice(1));
483
+ } catch {
484
+ return null;
485
+ }
486
+ return files.has(rel) ? new URL(pathname.slice(1), base).href + suffix : null;
487
+ };
488
+ const rewrite = (name, value) => {
489
+ if (name !== "srcset") return resolve(value);
490
+ let changed = false;
491
+ const next = value.split(",").map((candidate) => {
492
+ const [url = "", ...rest] = candidate.trim().split(/\s+/);
493
+ const resolved = resolve(url);
494
+ if (!resolved) return candidate;
495
+ changed = true;
496
+ return [resolved, ...rest].join(" ");
497
+ }).join(", ");
498
+ return changed ? next : null;
499
+ };
500
+ const applies = (element, name) => name.endsWith("href") ? LOADS_HREF.has(element.localName.toLowerCase()) : true;
501
+ const setAttribute = Element.prototype.setAttribute;
502
+ Element.prototype.setAttribute = function(name, value) {
503
+ const lower = name.toLowerCase();
504
+ const next = ATTRS.includes(lower) && applies(this, lower) ? rewrite(lower, String(value)) : null;
505
+ setAttribute.call(this, name, next ?? value);
506
+ };
507
+ const fix = (element) => {
508
+ for (const name of ATTRS) {
509
+ const value = element.getAttribute(name);
510
+ if (value === null || !applies(element, name)) continue;
511
+ const next = rewrite(name, value);
512
+ if (next !== null) setAttribute.call(element, name, next);
513
+ }
514
+ };
515
+ const fixTree = (root) => {
516
+ fix(root);
517
+ for (const element of root.querySelectorAll("[src], [srcset], [poster], [href]")) fix(element);
518
+ };
519
+ new MutationObserver((records) => {
520
+ for (const record of records) {
521
+ if (record.type === "attributes") fix(record.target);
522
+ for (const node of record.addedNodes) if (node.nodeType === Node.ELEMENT_NODE) fixTree(node);
523
+ }
524
+ }).observe(document, {
525
+ subtree: true,
526
+ childList: true,
527
+ attributes: true,
528
+ attributeFilter: ["src", "srcset", "poster", "href"]
529
+ });
530
+ if (document.documentElement) fixTree(document.documentElement);
531
+ const fetch2 = window.fetch.bind(window);
532
+ window.fetch = (input, init) => {
533
+ const raw = typeof input === "string" ? input : input instanceof URL ? input.href : null;
534
+ let path = raw;
535
+ if (raw && input instanceof URL && input.origin === location.origin) path = input.pathname + input.search;
536
+ const resolved = path ? resolve(path) : null;
537
+ return fetch2(resolved ?? input, init);
538
+ };
539
+ }
540
+
470
541
  // src/runtime/typography.tsx
471
542
  import { useEffect, useSyncExternalStore } from "react";
472
543
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -566,6 +637,7 @@ var config = window.__DESIGN__ ?? { key: "", canvas: "", id: "", props: {} };
566
637
  var embedded = window.parent !== window;
567
638
  var query = new URLSearchParams(window.location.search);
568
639
  var capture = query.has("capture");
640
+ servePublicFolder(config.public);
569
641
  var theme = config.theme ?? (query.get("theme") === "dark" ? "dark" : "light");
570
642
  var themeListeners = /* @__PURE__ */ new Set();
571
643
  function applyTheme(next) {
@@ -594,8 +666,9 @@ function useTheme() {
594
666
  );
595
667
  }
596
668
  function go(target) {
597
- if (embedded) post({ type: "go", target });
598
- else console.info(`[design] go("${target}") works inside the canvas viewer`);
669
+ const message = `go("${target}") is no longer supported: link to the screen instead (<a href="/\u2026">, or history.pushState in code) and give it a "route" in canvas.json`;
670
+ recordError(message);
671
+ throw new Error(message);
599
672
  }
600
673
  function useScreen() {
601
674
  return { canvas: config.canvas, id: config.id, props: config.props };
@@ -701,11 +774,13 @@ async function takeSnapshot() {
701
774
  }
702
775
  }
703
776
  function keepLinksInFrame() {
704
- const report = (target, form) => post({
777
+ const report = (target, form, raw, pushed = false) => post({
705
778
  type: "link",
706
779
  href: target.href,
707
780
  path: target.origin === location.origin ? target.pathname + target.search + target.hash : null,
708
- form
781
+ form,
782
+ raw,
783
+ pushed
709
784
  });
710
785
  const onClick = (event) => {
711
786
  if (event.defaultPrevented || event.button > 1) return;
@@ -716,7 +791,7 @@ function keepLinksInFrame() {
716
791
  const samePage = target.origin === location.origin && target.pathname === location.pathname && target.search === location.search;
717
792
  if (samePage && target.hash) return;
718
793
  event.preventDefault();
719
- report(target, false);
794
+ report(target, false, link.getAttribute("href") ?? "");
720
795
  };
721
796
  window.addEventListener("click", onClick);
722
797
  window.addEventListener("auxclick", onClick);
@@ -724,8 +799,32 @@ function keepLinksInFrame() {
724
799
  const form = event.target;
725
800
  if (event.defaultPrevented || form.method === "dialog") return;
726
801
  event.preventDefault();
727
- report(new URL(form.getAttribute("action") || location.href, location.href), true);
802
+ const action = form.getAttribute("action") ?? "";
803
+ report(new URL(action || location.href, location.href), true, action);
804
+ });
805
+ const navigation = window.navigation;
806
+ navigation?.addEventListener("navigate", (event) => {
807
+ const nav = event;
808
+ if (!nav.cancelable || nav.destination.sameDocument || nav.downloadRequest !== null) return;
809
+ if (nav.navigationType === "reload") return;
810
+ const target = new URL(nav.destination.url);
811
+ if (target.pathname === location.pathname && target.search === location.search) return;
812
+ event.preventDefault();
813
+ report(
814
+ target,
815
+ false,
816
+ target.origin === location.origin ? target.pathname + target.search + target.hash : target.href
817
+ );
728
818
  });
819
+ for (const name of ["pushState", "replaceState"]) {
820
+ const original = history[name].bind(history);
821
+ history[name] = (data, unused, url) => {
822
+ original(data, unused, url);
823
+ if (url === void 0 || url === null) return;
824
+ const raw = String(url);
825
+ report(new URL(raw, location.href), false, raw, true);
826
+ };
827
+ }
729
828
  }
730
829
  var booted = false;
731
830
  var hmrBound = false;