@diagrammo/dgmo 0.14.1 → 0.15.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.
@@ -34,3 +34,42 @@ export { tokyoNightPalette } from './tokyo-night';
34
34
 
35
35
  export { draculaPalette } from './dracula';
36
36
  export { monokaiPalette } from './monokai';
37
+
38
+ // ============================================================
39
+ // Public namespace — `palettes` for use with render()
40
+ // ============================================================
41
+
42
+ import { boldPalette } from './bold';
43
+ import { catppuccinPalette } from './catppuccin';
44
+ import { draculaPalette } from './dracula';
45
+ import { gruvboxPalette } from './gruvbox';
46
+ import { monokaiPalette } from './monokai';
47
+ import { nordPalette } from './nord';
48
+ import { oneDarkPalette } from './one-dark';
49
+ import { rosePinePalette } from './rose-pine';
50
+ import { solarizedPalette } from './solarized';
51
+ import { tokyoNightPalette } from './tokyo-night';
52
+
53
+ import type { PaletteConfig } from './types';
54
+
55
+ /**
56
+ * All built-in palettes, keyed by camelCase id. Use directly with render():
57
+ *
58
+ * await render(text, { palette: palettes.catppuccin });
59
+ *
60
+ * For preference/settings storage, the `.id` field of each entry is the
61
+ * canonical string (e.g. `'tokyo-night'`, `'nord'`) — that's the wire format
62
+ * used by share URLs and the CLI `--palette` flag.
63
+ */
64
+ export const palettes = {
65
+ nord: nordPalette,
66
+ catppuccin: catppuccinPalette,
67
+ solarized: solarizedPalette,
68
+ gruvbox: gruvboxPalette,
69
+ tokyoNight: tokyoNightPalette,
70
+ oneDark: oneDarkPalette,
71
+ rosePine: rosePinePalette,
72
+ dracula: draculaPalette,
73
+ monokai: monokaiPalette,
74
+ bold: boldPalette,
75
+ } as const satisfies Record<string, PaletteConfig>;
package/src/render.ts CHANGED
@@ -13,7 +13,7 @@ import type { CompactViewState } from './sharing';
13
13
  async function ensureDom(): Promise<void> {
14
14
  if (typeof document !== 'undefined') return;
15
15
 
16
- const { JSDOM } = await import('jsdom');
16
+ const { JSDOM } = await loadJsdom();
17
17
  const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
18
18
  const win = dom.window;
19
19
 
@@ -39,6 +39,22 @@ async function ensureDom(): Promise<void> {
39
39
  });
40
40
  }
41
41
 
42
+ /**
43
+ * Load jsdom server-side. The specifier is constructed at runtime so
44
+ * downstream bundlers (Vite, Rollup, esbuild, webpack) cannot statically
45
+ * resolve it. Without this indirection, every browser bundle of
46
+ * @diagrammo/dgmo emits a 5+ MB jsdom chunk even though `ensureDom()`
47
+ * guards execution with a `typeof document` check — the guard prevents
48
+ * runtime evaluation, but the static dependency edge still pulls jsdom
49
+ * into the bundle.
50
+ */
51
+ async function loadJsdom(): Promise<typeof import('jsdom')> {
52
+ const spec = ['js', 'dom'].join('');
53
+ return import(/* @vite-ignore */ /* webpackIgnore: true */ spec) as Promise<
54
+ typeof import('jsdom')
55
+ >;
56
+ }
57
+
42
58
  /**
43
59
  * Render DGMO source to an SVG string.
44
60
  *
package/src/themes.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Theme — render mode flag. Selects which palette variant the renderer uses
3
+ * for background and text:
4
+ * - 'light' → palette.light colors
5
+ * - 'dark' → palette.dark colors
6
+ * - 'transparent' → no background fill (for embedding in colored containers)
7
+ */
8
+ export type Theme = 'light' | 'dark' | 'transparent';
9
+
10
+ /**
11
+ * `themes` namespace — use with render() options for a typed handle:
12
+ *
13
+ * await render(text, { theme: themes.dark });
14
+ *
15
+ * Passing the raw string `'dark'` also works (the underlying type is the
16
+ * string-literal union); the namespace is the conventional path.
17
+ */
18
+ export const themes = {
19
+ light: 'light',
20
+ dark: 'dark',
21
+ transparent: 'transparent',
22
+ } as const satisfies Record<string, Theme>;
package/dist/pert.cjs DELETED
@@ -1,325 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/pert/index.ts
21
- var pert_exports = {};
22
- __export(pert_exports, {
23
- buildSimulationContext: () => buildSimulationContext,
24
- mulberry32: () => mulberry32,
25
- sampleBetaPert: () => sampleBetaPert,
26
- simulateCanonical: () => simulateCanonical,
27
- simulateFast: () => simulateFast
28
- });
29
- module.exports = __toCommonJS(pert_exports);
30
-
31
- // src/pert/internal.ts
32
- var UNIT_TO_DAYS_LOCAL = {
33
- min: 1 / (60 * 24),
34
- h: 1 / 24,
35
- d: 1,
36
- bd: 1,
37
- // PERT has no calendar; bd ≈ d for analytical purposes
38
- w: 7,
39
- m: 30,
40
- q: 90,
41
- y: 365,
42
- s: 14
43
- // sprint (14 calendar days) — not seconds
44
- };
45
- function unitToDays(unit) {
46
- return UNIT_TO_DAYS_LOCAL[unit];
47
- }
48
-
49
- // src/pert/monte-carlo.ts
50
- function lagDays(lag) {
51
- return lag === null ? 0 : lag.amount * unitToDays(lag.unit);
52
- }
53
- function mulberry32(seed) {
54
- let s = seed >>> 0;
55
- return () => {
56
- s = s + 1831565813 >>> 0;
57
- let t = s;
58
- t = Math.imul(t ^ t >>> 15, t | 1);
59
- t ^= t + Math.imul(t ^ t >>> 7, t | 61);
60
- return ((t ^ t >>> 14) >>> 0) / 4294967296;
61
- };
62
- }
63
- function standardNormal(rng) {
64
- let u = 0;
65
- let v = 0;
66
- while (u === 0) u = rng();
67
- while (v === 0) v = rng();
68
- return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
69
- }
70
- function gamma(shape, rng) {
71
- if (shape < 1) {
72
- const u = rng();
73
- return gamma(shape + 1, rng) * Math.pow(u, 1 / shape);
74
- }
75
- const d = shape - 1 / 3;
76
- const c = 1 / Math.sqrt(9 * d);
77
- while (true) {
78
- let x;
79
- let v;
80
- do {
81
- x = standardNormal(rng);
82
- v = 1 + c * x;
83
- } while (v <= 0);
84
- v = v * v * v;
85
- const u = rng();
86
- if (u < 1 - 0.0331 * x * x * x * x) return d * v;
87
- if (Math.log(u) < x * x / 2 + d * (1 - v + Math.log(v))) return d * v;
88
- }
89
- }
90
- function sampleBeta(alpha, beta, rng) {
91
- const x = gamma(alpha, rng);
92
- const y = gamma(beta, rng);
93
- return x / (x + y);
94
- }
95
- function sampleBetaPert(o, m, p, rng) {
96
- if (o === p) return m;
97
- const range = p - o;
98
- const alpha = 1 + 4 * (m - o) / range;
99
- const beta = 1 + 4 * (p - m) / range;
100
- return o + sampleBeta(alpha, beta, rng) * range;
101
- }
102
- function simulate(resolved, expanded, predecessors, successors, topo, terminals, poisoned, opts) {
103
- const rng = mulberry32(opts.seed);
104
- const expById = /* @__PURE__ */ new Map();
105
- for (const e of expanded) expById.set(e.id, e);
106
- const completionTimes = new Array(opts.trials);
107
- const criticalityCount = /* @__PURE__ */ new Map();
108
- for (const id of topo) criticalityCount.set(id, 0);
109
- const pathCount = /* @__PURE__ */ new Map();
110
- const pathById = /* @__PURE__ */ new Map();
111
- const incomingEdges = /* @__PURE__ */ new Map();
112
- for (const id of topo) incomingEdges.set(id, []);
113
- for (const e of resolved.edges) incomingEdges.get(e.target)?.push(e);
114
- const es = /* @__PURE__ */ new Map();
115
- const ef = /* @__PURE__ */ new Map();
116
- const predOnLongestPath = /* @__PURE__ */ new Map();
117
- for (let trial = 0; trial < opts.trials; trial++) {
118
- for (const id of topo) {
119
- const exp = expById.get(id);
120
- let duration;
121
- if (!exp || poisoned.has(id)) {
122
- duration = 0;
123
- } else if (exp.o === exp.p) {
124
- duration = exp.m;
125
- } else {
126
- duration = sampleBetaPert(exp.o, exp.m, exp.p, rng);
127
- }
128
- let esLower = 0;
129
- let efLower = -Infinity;
130
- let bestPred = null;
131
- let bestPredVal = -Infinity;
132
- for (const edge of incomingEdges.get(id) ?? []) {
133
- const aEs = es.get(edge.source) ?? 0;
134
- const aEf = ef.get(edge.source) ?? 0;
135
- const lag = lagDays(edge.lag);
136
- let candES = -Infinity;
137
- let candEF = -Infinity;
138
- switch (edge.type) {
139
- case "FS":
140
- candES = aEf + lag;
141
- break;
142
- case "SS":
143
- candES = aEs + lag;
144
- break;
145
- case "FF":
146
- candEF = aEf + lag;
147
- break;
148
- case "SF":
149
- candEF = aEs + lag;
150
- break;
151
- }
152
- if (candES > esLower) esLower = candES;
153
- if (candEF > efLower) efLower = candEF;
154
- const contribution = Math.max(candES, candEF - duration);
155
- if (contribution > bestPredVal) {
156
- bestPredVal = contribution;
157
- bestPred = edge.source;
158
- }
159
- }
160
- let esVal = esLower;
161
- let efVal = esVal + duration;
162
- if (efLower > efVal) {
163
- efVal = efLower;
164
- esVal = efVal - duration;
165
- }
166
- es.set(id, esVal);
167
- ef.set(id, efVal);
168
- predOnLongestPath.set(id, bestPred);
169
- }
170
- let endEf = 0;
171
- let endId = terminals[0] ?? null;
172
- for (const id of topo) {
173
- const e = ef.get(id) ?? 0;
174
- if (e > endEf) {
175
- endEf = e;
176
- endId = id;
177
- }
178
- }
179
- completionTimes[trial] = endEf;
180
- if (endId !== null) {
181
- const path = [];
182
- let cur = endId;
183
- while (cur !== null) {
184
- path.unshift(cur);
185
- criticalityCount.set(cur, (criticalityCount.get(cur) ?? 0) + 1);
186
- cur = predOnLongestPath.get(cur) ?? null;
187
- }
188
- const key = path.join("\0");
189
- pathCount.set(key, (pathCount.get(key) ?? 0) + 1);
190
- if (!pathById.has(key)) pathById.set(key, path);
191
- }
192
- }
193
- completionTimes.sort((a, b) => a - b);
194
- const pct = (frac) => {
195
- const idx = Math.min(
196
- completionTimes.length - 1,
197
- Math.max(0, Math.floor(frac * completionTimes.length))
198
- );
199
- return completionTimes[idx] ?? 0;
200
- };
201
- const minDurationDays = completionTimes[0] ?? 0;
202
- const maxDurationDays = completionTimes[completionTimes.length - 1] ?? 0;
203
- let modalKey = "";
204
- let modalCount = -1;
205
- for (const [key, count] of pathCount) {
206
- if (count > modalCount) {
207
- modalCount = count;
208
- modalKey = key;
209
- } else if (count === modalCount) {
210
- const a = pathById.get(modalKey) ?? [];
211
- const b = pathById.get(key) ?? [];
212
- const va = pathVariance(a, expById);
213
- const vb = pathVariance(b, expById);
214
- if (vb > va) {
215
- modalKey = key;
216
- } else if (vb === va && key.localeCompare(modalKey) < 0) {
217
- modalKey = key;
218
- }
219
- }
220
- }
221
- const criticalityByActivity = {};
222
- for (const id of topo) {
223
- criticalityByActivity[id] = (criticalityCount.get(id) ?? 0) / opts.trials;
224
- }
225
- return {
226
- trials: opts.trials,
227
- seed: opts.seed,
228
- p50: pct(0.5),
229
- p80: pct(0.8),
230
- p95: pct(0.95),
231
- p16: pct(0.16),
232
- p84: pct(0.84),
233
- criticalityByActivity,
234
- modalCriticalPath: pathById.get(modalKey) ?? [],
235
- // Analyzer populates this after MC returns by running the
236
- // deterministic per-activity tornado pass. Empty here so the
237
- // type contract is satisfied even in scrubber/fast paths that
238
- // never compute swings.
239
- tornadoSwings: [],
240
- minDurationDays,
241
- maxDurationDays
242
- };
243
- }
244
- function pathVariance(path, expById) {
245
- let sum = 0;
246
- for (const id of path) {
247
- const e = expById.get(id);
248
- if (!e) continue;
249
- const sigma = (e.p - e.o) / 6;
250
- sum += sigma * sigma;
251
- }
252
- return sum;
253
- }
254
- function buildSimulationContext(resolved) {
255
- const predecessors = /* @__PURE__ */ new Map();
256
- const successors = /* @__PURE__ */ new Map();
257
- for (const r of resolved.activities) {
258
- predecessors.set(r.activity.id, []);
259
- successors.set(r.activity.id, []);
260
- }
261
- for (const e of resolved.edges) {
262
- successors.get(e.source)?.push(e.target);
263
- predecessors.get(e.target)?.push(e.source);
264
- }
265
- const inDegree = /* @__PURE__ */ new Map();
266
- for (const r of resolved.activities) {
267
- inDegree.set(r.activity.id, predecessors.get(r.activity.id).length);
268
- }
269
- const queue = [];
270
- for (const [id, deg] of inDegree) if (deg === 0) queue.push(id);
271
- const topo = [];
272
- while (queue.length > 0) {
273
- const id = queue.shift();
274
- topo.push(id);
275
- for (const next of successors.get(id) ?? []) {
276
- const deg = (inDegree.get(next) ?? 0) - 1;
277
- inDegree.set(next, deg);
278
- if (deg === 0) queue.push(next);
279
- }
280
- }
281
- const terminals = [];
282
- for (const r of resolved.activities) {
283
- if ((successors.get(r.activity.id) ?? []).length === 0) {
284
- terminals.push(r.activity.id);
285
- }
286
- }
287
- const poisoned = /* @__PURE__ */ new Set();
288
- for (const r of resolved.activities) {
289
- if (r.activity.duration === null) {
290
- const stack = [r.activity.id];
291
- while (stack.length > 0) {
292
- const id = stack.pop();
293
- if (poisoned.has(id)) continue;
294
- poisoned.add(id);
295
- for (const next of successors.get(id) ?? []) stack.push(next);
296
- }
297
- }
298
- }
299
- return { predecessors, successors, topo, terminals, poisoned };
300
- }
301
- function simulateCanonical(resolved, expanded, opts) {
302
- const ctx = buildSimulationContext(resolved);
303
- return simulate(
304
- resolved,
305
- expanded,
306
- ctx.predecessors,
307
- ctx.successors,
308
- ctx.topo,
309
- ctx.terminals,
310
- ctx.poisoned,
311
- { trials: opts.trials, seed: opts.seed }
312
- );
313
- }
314
- function simulateFast(resolved, expanded, opts) {
315
- return simulateCanonical(resolved, expanded, opts);
316
- }
317
- // Annotate the CommonJS export names for ESM import in node:
318
- 0 && (module.exports = {
319
- buildSimulationContext,
320
- mulberry32,
321
- sampleBetaPert,
322
- simulateCanonical,
323
- simulateFast
324
- });
325
- //# sourceMappingURL=pert.cjs.map