wirepulse 0.0.0-stage → 0.1.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.
@@ -0,0 +1,330 @@
1
+ import { getIconMarkup } from "./icons.js";
2
+ import { layoutDiagram } from "./layout.js";
3
+ import { ACCENT_NAMES, buildThemeCss } from "./themes.js";
4
+ function escXml(str) {
5
+ return str
6
+ .replace(/&/g, "&")
7
+ .replace(/</g, "&lt;")
8
+ .replace(/>/g, "&gt;")
9
+ .replace(/"/g, "&quot;")
10
+ .replace(/'/g, "&apos;");
11
+ }
12
+ function speedToSeconds(speed, multiplier = 1) {
13
+ const base = speed === "fast" ? 1.5 : speed === "slow" ? 3.8 : 2.5;
14
+ return Number((base / Math.max(0.25, Math.min(4, multiplier))).toFixed(2));
15
+ }
16
+ function estimateTextWidth(text, charPx) {
17
+ return Math.ceil(text.length * charPx);
18
+ }
19
+ /**
20
+ * Compile a DiagramSpec into a self-contained, zero-JS, GitHub-README-safe
21
+ * animated SVG string.
22
+ */
23
+ export function renderSvg(spec, options = {}) {
24
+ const layout = layoutDiagram(spec);
25
+ const theme = options.theme ?? spec.theme ?? "auto";
26
+ const speedMul = options.speedMultiplier ?? 1;
27
+ const paused = Boolean(options.paused);
28
+ const showGrid = options.showGrid !== false;
29
+ const includeWatermark = options.includeWatermark !== false;
30
+ const animPlayState = paused ? "paused" : "running";
31
+ // Build arrow markers for every accent color
32
+ const markerDefs = ACCENT_NAMES.map((col) => `
33
+ <marker id="wp-arrow-${col}" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6.5" markerHeight="6.5" orient="auto-start-reverse">
34
+ <path d="M 1 1.5 L 9 5 L 1 8.5 z" fill="var(--wp-${col})" />
35
+ </marker>`).join("");
36
+ // Soft neon glow filter for moving packets and active LEDs
37
+ const defs = `
38
+ <defs>
39
+ <pattern id="wp-grid-pattern" width="24" height="24" patternUnits="userSpaceOnUse">
40
+ <circle cx="1.5" cy="1.5" r="1.1" fill="var(--wp-grid)" />
41
+ </pattern>
42
+ <filter id="wp-card-shadow" x="-10%" y="-12%" width="120%" height="130%">
43
+ <feDropShadow dx="0" dy="6" stdDeviation="7" flood-color="var(--wp-shadow)" flood-opacity="1" />
44
+ </filter>
45
+ <filter id="wp-glow" x="-60%" y="-60%" width="220%" height="220%">
46
+ <feGaussianBlur stdDeviation="2.6" result="blur" />
47
+ <feMerge>
48
+ <feMergeNode in="blur" />
49
+ <feMergeNode in="SourceGraphic" />
50
+ </feMerge>
51
+ </filter>
52
+ ${markerDefs}
53
+ </defs>`;
54
+ const styleBlock = `
55
+ <style>
56
+ ${buildThemeCss(theme)}
57
+
58
+ .wp-root {
59
+ font-family: var(--wp-font);
60
+ }
61
+ .wp-title {
62
+ font-size: 18px;
63
+ font-weight: 700;
64
+ fill: var(--wp-text);
65
+ letter-spacing: -0.02em;
66
+ }
67
+ .wp-subtitle {
68
+ font-size: 12.5px;
69
+ font-weight: 400;
70
+ fill: var(--wp-sub);
71
+ }
72
+ .wp-group-rect {
73
+ fill: var(--wp-group-bg);
74
+ stroke-width: 1.25;
75
+ stroke-dasharray: 5 5;
76
+ }
77
+ .wp-group-label {
78
+ font-size: 10.5px;
79
+ font-weight: 700;
80
+ letter-spacing: 0.08em;
81
+ text-transform: uppercase;
82
+ }
83
+ .wp-node-card {
84
+ fill: var(--wp-card);
85
+ stroke: var(--wp-border);
86
+ stroke-width: 1.25;
87
+ filter: url(#wp-card-shadow);
88
+ }
89
+ .wp-node-title {
90
+ font-size: 13.5px;
91
+ font-weight: 650;
92
+ fill: var(--wp-text);
93
+ letter-spacing: -0.01em;
94
+ }
95
+ .wp-node-sub {
96
+ font-size: 11px;
97
+ font-weight: 400;
98
+ fill: var(--wp-sub);
99
+ }
100
+ .wp-node-badge-text {
101
+ font-family: var(--wp-mono);
102
+ font-size: 9.5px;
103
+ font-weight: 600;
104
+ }
105
+ .wp-edge-base {
106
+ fill: none;
107
+ stroke: var(--wp-edge);
108
+ stroke-width: 1.75;
109
+ stroke-linecap: round;
110
+ opacity: 0.65;
111
+ }
112
+ .wp-edge-flow {
113
+ fill: none;
114
+ stroke-width: 2;
115
+ stroke-linecap: round;
116
+ animation-play-state: ${animPlayState};
117
+ }
118
+ .wp-edge-dash {
119
+ stroke-dasharray: 7 7;
120
+ animation-name: wp-dash-move;
121
+ animation-timing-function: linear;
122
+ animation-iteration-count: infinite;
123
+ }
124
+ .wp-edge-pulse {
125
+ stroke-dasharray: 28 180;
126
+ stroke-width: 2.6;
127
+ opacity: 0.92;
128
+ animation-name: wp-pulse-travel;
129
+ animation-timing-function: linear;
130
+ animation-iteration-count: infinite;
131
+ }
132
+ .wp-edge-label-bg {
133
+ fill: var(--wp-label-bg);
134
+ stroke: var(--wp-label-border);
135
+ stroke-width: 1;
136
+ }
137
+ .wp-edge-label-text {
138
+ font-family: var(--wp-mono);
139
+ font-size: 10px;
140
+ font-weight: 500;
141
+ fill: var(--wp-sub);
142
+ }
143
+ .wp-led-ring {
144
+ transform-box: fill-box;
145
+ transform-origin: center;
146
+ animation: wp-ping 2.2s cubic-bezier(0, 0, 0.2, 1) infinite;
147
+ animation-play-state: ${animPlayState};
148
+ }
149
+ .wp-live-dot {
150
+ animation: wp-breathe 2s ease-in-out infinite;
151
+ animation-play-state: ${animPlayState};
152
+ }
153
+ .wp-watermark {
154
+ font-family: var(--wp-mono);
155
+ font-size: 10px;
156
+ fill: var(--wp-muted);
157
+ opacity: 0.7;
158
+ }
159
+
160
+ @keyframes wp-dash-move {
161
+ from { stroke-dashoffset: 28; }
162
+ to { stroke-dashoffset: 0; }
163
+ }
164
+ @keyframes wp-pulse-travel {
165
+ from { stroke-dashoffset: 208; }
166
+ to { stroke-dashoffset: 0; }
167
+ }
168
+ @keyframes wp-ping {
169
+ 0% { transform: scale(1); opacity: 0.75; }
170
+ 75%, 100% { transform: scale(2.4); opacity: 0; }
171
+ }
172
+ @keyframes wp-breathe {
173
+ 0%, 100% { opacity: 1; }
174
+ 50% { opacity: 0.38; }
175
+ }
176
+
177
+ @media (prefers-reduced-motion: reduce) {
178
+ .wp-edge-flow, .wp-led-ring, .wp-live-dot {
179
+ animation: none !important;
180
+ }
181
+ }
182
+ </style>`;
183
+ // Header
184
+ const headerMarkup = renderHeader(spec, layout.width);
185
+ // Groups
186
+ const groupsMarkup = layout.groups.map(renderGroup).join("\n");
187
+ // Edges (drawn below nodes)
188
+ const edgesMarkup = layout.edges
189
+ .map((le, idx) => renderEdge(le, idx, spec.speed, speedMul, paused))
190
+ .join("\n");
191
+ // Nodes
192
+ const nodesMarkup = layout.nodes.map(renderNode).join("\n");
193
+ // Subtle footer watermark
194
+ const watermarkMarkup = includeWatermark
195
+ ? `<text x="${layout.width - 18}" y="${layout.height - 14}" text-anchor="end" class="wp-watermark">wirepulse</text>`
196
+ : "";
197
+ const ariaLabel = escXml(spec.title || "Animated Architecture Diagram");
198
+ return [
199
+ `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${layout.width} ${layout.height}" width="${layout.width}" height="${layout.height}" role="img" aria-label="${ariaLabel}" class="wp-root">`,
200
+ ` <title>${ariaLabel}</title>`,
201
+ defs,
202
+ styleBlock,
203
+ ` <!-- Background -->`,
204
+ ` <rect x="0.75" y="0.75" width="${layout.width - 1.5}" height="${layout.height - 1.5}" rx="16" fill="var(--wp-bg)" stroke="var(--wp-border)" stroke-width="1.25" />`,
205
+ showGrid
206
+ ? ` <rect x="1" y="1" width="${layout.width - 2}" height="${layout.height - 2}" rx="16" fill="url(#wp-grid-pattern)" />`
207
+ : "",
208
+ headerMarkup,
209
+ ` <!-- Groups -->`,
210
+ groupsMarkup,
211
+ ` <!-- Edges & Animated Packets -->`,
212
+ edgesMarkup,
213
+ ` <!-- Nodes -->`,
214
+ nodesMarkup,
215
+ watermarkMarkup,
216
+ `</svg>`,
217
+ ]
218
+ .filter(Boolean)
219
+ .join("\n");
220
+ }
221
+ function renderHeader(spec, width) {
222
+ if (!spec.title && !spec.subtitle)
223
+ return "";
224
+ const titleY = spec.subtitle ? 38 : 44;
225
+ return ` <!-- Header -->
226
+ <g class="wp-header">
227
+ ${spec.title ? `<text x="28" y="${titleY}" class="wp-title">${escXml(spec.title)}</text>` : ""}
228
+ ${spec.subtitle ? `<text x="28" y="57" class="wp-subtitle">${escXml(spec.subtitle)}</text>` : ""}
229
+ <g transform="translate(${width - 96}, 24)">
230
+ <rect x="0" y="0" width="68" height="22" rx="11" fill="var(--wp-emerald-soft)" stroke="var(--wp-emerald)" stroke-width="0.9" stroke-opacity="0.5" />
231
+ <circle cx="14" cy="11" r="3.5" fill="var(--wp-emerald)" class="wp-live-dot" />
232
+ <text x="24" y="14.5" font-family="var(--wp-mono)" font-size="9.5" font-weight="700" fill="var(--wp-emerald-text)" letter-spacing="0.06em">LIVE</text>
233
+ </g>
234
+ <line x1="24" y1="72" x2="${width - 24}" y2="72" stroke="var(--wp-border)" stroke-width="1" opacity="0.8" />
235
+ </g>`;
236
+ }
237
+ function renderGroup(lg) {
238
+ const col = lg.group.color;
239
+ const labelW = Math.max(64, estimateTextWidth(lg.group.label, 7.2) + 20);
240
+ return ` <g class="wp-group" data-group-id="${escXml(lg.group.id)}">
241
+ <rect x="${lg.x}" y="${lg.y}" width="${lg.width}" height="${lg.height}" rx="14" class="wp-group-rect" stroke="var(--wp-${col})" stroke-opacity="0.42" />
242
+ <rect x="${lg.x + 14}" y="${lg.y - 10}" width="${labelW}" height="20" rx="6" fill="var(--wp-bg)" stroke="var(--wp-${col})" stroke-width="1" stroke-opacity="0.5" />
243
+ <text x="${lg.x + 14 + labelW / 2}" y="${lg.y + 3.5}" text-anchor="middle" class="wp-group-label" fill="var(--wp-${col}-text)">${escXml(lg.group.label)}</text>
244
+ </g>`;
245
+ }
246
+ function renderEdge(le, index, defaultSpeed, speedMul, paused) {
247
+ const { edge, pathD, reversePathD, labelX, labelY, color } = le;
248
+ const dur = speedToSeconds(edge.speed ?? defaultSpeed, speedMul);
249
+ const pathId = `wp-path-${index}`;
250
+ const revPathId = `wp-rpath-${index}`;
251
+ const markerEnd = `marker-end="url(#wp-arrow-${color})"`;
252
+ const markerStart = edge.bidir ? `marker-start="url(#wp-arrow-${color})"` : "";
253
+ const layers = [
254
+ ` <g class="wp-edge" data-edge-id="${escXml(edge.id)}">`,
255
+ ` <path id="${pathId}" d="${pathD}" class="wp-edge-base" ${markerEnd} ${markerStart} />`,
256
+ ];
257
+ if (edge.flow === "dash") {
258
+ layers.push(` <path d="${pathD}" class="wp-edge-flow wp-edge-dash" stroke="var(--wp-${color})" style="animation-duration:${(dur * 0.65).toFixed(2)}s" opacity="0.88" />`);
259
+ }
260
+ else if (edge.flow === "pulse") {
261
+ layers.push(` <path d="${pathD}" class="wp-edge-flow wp-edge-pulse" stroke="var(--wp-${color})" filter="url(#wp-glow)" style="animation-duration:${dur}s" />`);
262
+ }
263
+ else if (edge.flow === "solid") {
264
+ layers.push(` <path d="${pathD}" fill="none" stroke="var(--wp-${color})" stroke-width="2" opacity="0.85" ${markerEnd} ${markerStart} />`);
265
+ }
266
+ // Animated glowing packets (default for `packet` flow, also subtle accent for `dash` when bidir)
267
+ if (edge.flow === "packet") {
268
+ // Subtle colored overlay line so frame-0 & reduced-motion look vibrant
269
+ layers.push(` <path d="${pathD}" fill="none" stroke="var(--wp-${color})" stroke-width="1.6" opacity="0.42" />`);
270
+ if (!paused) {
271
+ const packetCount = edge.packets ?? 1;
272
+ for (let p = 0; p < packetCount; p++) {
273
+ const beginOffset = ((dur / packetCount) * p + (index % 4) * 0.22).toFixed(2);
274
+ layers.push(` <g filter="url(#wp-glow)">`, ` <circle r="4.2" fill="var(--wp-${color})">`, ` <animateMotion dur="${dur}s" begin="-${beginOffset}s" repeatCount="indefinite" path="${pathD}" />`, ` </circle>`, ` <circle r="1.9" fill="#ffffff" opacity="0.9">`, ` <animateMotion dur="${dur}s" begin="-${beginOffset}s" repeatCount="indefinite" path="${pathD}" />`, ` </circle>`, ` </g>`);
275
+ }
276
+ if (edge.bidir) {
277
+ const revBegin = (dur * 0.5).toFixed(2);
278
+ layers.push(` <path id="${revPathId}" d="${reversePathD}" fill="none" stroke="none" />`, ` <g filter="url(#wp-glow)">`, ` <circle r="3.4" fill="var(--wp-${color})" opacity="0.85">`, ` <animateMotion dur="${(dur * 1.15).toFixed(2)}s" begin="-${revBegin}s" repeatCount="indefinite" path="${reversePathD}" />`, ` </circle>`, ` </g>`);
279
+ }
280
+ }
281
+ }
282
+ // Edge pill label
283
+ if (edge.label) {
284
+ const labelW = Math.max(44, estimateTextWidth(edge.label, 6.4) + 16);
285
+ const labelH = 20;
286
+ layers.push(` <rect x="${Math.round(labelX - labelW / 2)}" y="${Math.round(labelY - labelH / 2)}" width="${labelW}" height="${labelH}" rx="6" class="wp-edge-label-bg" />`, ` <text x="${labelX}" y="${labelY + 3.5}" text-anchor="middle" class="wp-edge-label-text">${escXml(edge.label)}</text>`);
287
+ }
288
+ layers.push(` </g>`);
289
+ return layers.join("\n");
290
+ }
291
+ function renderNode(ln) {
292
+ const { node, x, y, width, height } = ln;
293
+ const col = node.color;
294
+ const iconSvg = getIconMarkup(node.icon);
295
+ const badgeMarkup = node.badge
296
+ ? (() => {
297
+ const bw = Math.max(38, estimateTextWidth(node.badge, 6.2) + 14);
298
+ const bx = x + width - bw - 12;
299
+ const by = y - 9;
300
+ return `
301
+ <rect x="${bx}" y="${by}" width="${bw}" height="18" rx="6" fill="var(--wp-bg)" stroke="var(--wp-${col})" stroke-width="0.9" stroke-opacity="0.6" />
302
+ <text x="${bx + bw / 2}" y="${by + 12.2}" text-anchor="middle" class="wp-node-badge-text" fill="var(--wp-${col}-text)">${escXml(node.badge)}</text>`;
303
+ })()
304
+ : "";
305
+ const pulseMarkup = node.pulse
306
+ ? `
307
+ <circle cx="${x + width - 14}" cy="${y + height - 14}" r="3.2" fill="var(--wp-${col})" opacity="0.35" class="wp-led-ring" />
308
+ <circle cx="${x + width - 14}" cy="${y + height - 14}" r="3.2" fill="var(--wp-${col})" filter="url(#wp-glow)" />`
309
+ : "";
310
+ const titleY = node.sublabel ? y + 34 : y + 43;
311
+ const maxTitleChars = 19;
312
+ const displayTitle = node.label.length > maxTitleChars
313
+ ? `${node.label.slice(0, maxTitleChars - 1)}…`
314
+ : node.label;
315
+ const displaySub = node.sublabel && node.sublabel.length > 22
316
+ ? `${node.sublabel.slice(0, 21)}…`
317
+ : node.sublabel;
318
+ return ` <g class="wp-node" data-node-id="${escXml(node.id)}" data-col="${ln.col}" data-row="${ln.row}">
319
+ <rect x="${x}" y="${y}" width="${width}" height="${height}" rx="12" class="wp-node-card" />
320
+ <rect x="${x}" y="${y + 12}" width="3.5" height="${height - 24}" rx="1.75" fill="var(--wp-${col})" />
321
+ <rect x="${x + 14}" y="${y + 17}" width="40" height="40" rx="10" fill="var(--wp-${col}-soft)" stroke="var(--wp-${col})" stroke-width="1" stroke-opacity="0.38" />
322
+ <g transform="translate(${x + 22}, ${y + 25})" fill="none" stroke="var(--wp-${col})" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
323
+ ${iconSvg}
324
+ </g>
325
+ <text x="${x + 64}" y="${titleY}" class="wp-node-title">${escXml(displayTitle)}</text>
326
+ ${displaySub ? `<text x="${x + 64}" y="${y + 52}" class="wp-node-sub">${escXml(displaySub)}</text>` : ""}
327
+ ${badgeMarkup}
328
+ ${pulseMarkup}
329
+ </g>`;
330
+ }
@@ -0,0 +1,7 @@
1
+ export interface StudioHtmlOptions {
2
+ initialSource: string;
3
+ fileName?: string;
4
+ canSaveToDisk: boolean;
5
+ version: string;
6
+ }
7
+ export declare function buildStudioHtml(opts: StudioHtmlOptions): string;