File size: 4,500 Bytes
9ee0efc
79cd0aa
 
 
 
 
 
c4e53ef
 
9ee0efc
 
 
 
 
 
 
 
 
79cd0aa
 
 
 
 
 
9ee0efc
79cd0aa
 
 
 
 
c4e53ef
79cd0aa
 
 
 
 
 
9ee0efc
 
 
79cd0aa
 
 
 
 
 
9ee0efc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cab6fcb
 
 
 
 
 
 
79cd0aa
9ee0efc
 
 
79cd0aa
 
 
 
 
 
 
 
9ee0efc
 
79cd0aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ee0efc
 
 
cab6fcb
 
79cd0aa
 
c4e53ef
79cd0aa
 
 
 
 
 
 
 
 
 
 
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
import { useRef, useState } from "react";
import { AlertCircle, Camera, Check, Loader2 } from "lucide-react";
import type { SharePayload } from "../lib/share";
import { exportFigure } from "../lib/figureExport";

/**
 * Small camera button that exports its host view as a paper figure
 * (one .bundle.zip: SVG + PNG + .provenance.json sidecar — see
 * lib/figureExport.ts).
 * Placed on the canvas toolbar, the Multiverse board header, the
 * Evidence pane header and the Evidence theater. The button itself
 * never appears in exports (figureExport strips all <button>
 * elements from the clone).
 *
 * Raster scale: a normal click exports the hybrid PNG at 2.5×;
 * alt/⌥-click OR press-and-hold (≥550 ms) exports at 4× — the
 * print-resolution option for large composite figures. (True-SVG
 * targets are resolution-independent; scale is a no-op there.)
 */
export function FigureExportButton({
  getTarget,
  name,
  view,
  getGraph,
  getTracePosition,
  className = "",
  labelBreakpoint,
}: {
  /** Resolve the DOM subtree to export at click time. */
  getTarget: () => HTMLElement | SVGSVGElement | null;
  /** Download basename (names the bundle zip and its contents). */
  name: string;
  /** Provenance view key: canvas | multiverse | evidence-<tab>. */
  view: string;
  /** Canvas graph to embed in the provenance, when the host knows it. */
  getGraph?: () => SharePayload | null;
  className?: string;
  /** Evidence-theater trace scrubber position at click time (batch
   *  k); embedded as provenance.trace_position + filename suffix. */
  getTracePosition?: () => number | null;
  /** Show a text label at/above this breakpoint (icon-only otherwise). */
  labelBreakpoint?: "sm" | "md" | "lg";
}) {
  const [state, setState] = useState<"idle" | "busy" | "done" | "error">(
    "idle",
  );
  // Long-press detection (the touch counterpart to alt-click for the
  // 4× print-resolution raster).
  const longPress = useRef(false);
  const pressTimer = useRef<number | null>(null);
  const pressStart = () => {
    longPress.current = false;
    if (pressTimer.current != null) window.clearTimeout(pressTimer.current);
    pressTimer.current = window.setTimeout(() => {
      longPress.current = true;
    }, 550);
  };
  const pressEnd = () => {
    if (pressTimer.current != null) {
      window.clearTimeout(pressTimer.current);
      pressTimer.current = null;
    }
  };
  // Abandoned press (pointer leaves without a click): the 4× flag must
  // not leak into the NEXT normal click (audit S3). pointerup keeps
  // the flag — the click event that consumes it follows immediately.
  const pressCancel = () => {
    pressEnd();
    longPress.current = false;
  };

  const onClick = async (e: React.MouseEvent) => {
    const hires = e.altKey || longPress.current;
    longPress.current = false;
    const target = getTarget();
    if (!target || state === "busy") return;
    setState("busy");
    try {
      await exportFigure(target, {
        name,
        view,
        graph: getGraph ? getGraph() : null,
        scale: hires ? 4 : 2.5,
        tracePosition: getTracePosition ? getTracePosition() : null,
      });
      setState("done");
    } catch {
      setState("error");
    } finally {
      window.setTimeout(() => setState("idle"), 2000);
    }
  };

  const labelVisibility = labelBreakpoint
    ? { sm: "hidden sm:inline", md: "hidden md:inline", lg: "hidden lg:inline" }[
        labelBreakpoint
      ]
    : "hidden";

  const Icon =
    state === "busy"
      ? Loader2
      : state === "done"
        ? Check
        : state === "error"
          ? AlertCircle
          : Camera;

  return (
    <button
      type="button"
      onClick={(e) => void onClick(e)}
      onPointerDown={pressStart}
      onPointerUp={pressEnd}
      onPointerLeave={pressCancel}
      onPointerCancel={pressCancel}
      data-marker="figure-export"
      className={`btn ${className}`}
      title="Export this view as a paper figure: one .bundle.zip holding the SVG + PNG with embedded provenance (run ids, seeds, config hashes, graph) + a .provenance.json sidecar. Alt/⌥-click or press-and-hold for the 4× print-resolution PNG (default 2.5×)."
      aria-label="Export figure"
    >
      <Icon
        className={`w-3.5 h-3.5 ${state === "busy" ? "animate-spin" : ""} ${
          state === "done" ? "text-ok" : state === "error" ? "text-danger" : ""
        }`}
      />
      <span className={labelVisibility}>Figure</span>
    </button>
  );
}