@vanduo-oss/vd3-flowchart 1.6.0 → 1.6.1

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.
package/dist/index.js CHANGED
@@ -528,7 +528,7 @@ var EDGE_STROKE_PRESETS = [
528
528
  { id: "medium", label: "Medium", width: DEFAULT_EDGE_STROKE_WIDTH },
529
529
  { id: "bold", label: "Bold", width: 3.5 }
530
530
  ];
531
- var VD_FLOWCHART_VERSION = "1.6.0";
531
+ var VD_FLOWCHART_VERSION = "1.6.1";
532
532
  var FLOWCHART_DOCUMENT_VERSION = "1.3.0";
533
533
  var FLOWCHART_NODE_TYPES = [
534
534
  "rounded-rect",
@@ -588,6 +588,19 @@ var FLOWCHART_COLOR_PRESET_FALLBACKS = {
588
588
  pink: { fill: "#fce7f3", stroke: "#db2777", text: "#831843", edge: "#db2777" },
589
589
  neutral: { fill: "#f1f5f9", stroke: "#64748b", text: "#1e293b", edge: "#64748b" }
590
590
  };
591
+ var FLOWCHART_COLOR_PRESET_DARK_FALLBACKS = {
592
+ primary: { fill: "#1e3a8a", stroke: "#60a5fa", text: "#dbeafe", edge: "#60a5fa" },
593
+ secondary: { fill: "#312e81", stroke: "#818cf8", text: "#e0e7ff", edge: "#818cf8" },
594
+ success: { fill: "#14532d", stroke: "#4ade80", text: "#dcfce7", edge: "#4ade80" },
595
+ warning: { fill: "#78350f", stroke: "#fbbf24", text: "#fef3c7", edge: "#fbbf24" },
596
+ danger: { fill: "#7f1d1d", stroke: "#f87171", text: "#fee2e2", edge: "#f87171" },
597
+ info: { fill: "#0c4a6e", stroke: "#38bdf8", text: "#e0f2fe", edge: "#38bdf8" },
598
+ violet: { fill: "#581c87", stroke: "#c084fc", text: "#f3e8ff", edge: "#c084fc" },
599
+ teal: { fill: "#134e4a", stroke: "#2dd4bf", text: "#ccfbf1", edge: "#2dd4bf" },
600
+ orange: { fill: "#7c2d12", stroke: "#fb923c", text: "#ffedd5", edge: "#fb923c" },
601
+ pink: { fill: "#831843", stroke: "#f472b6", text: "#fce7f3", edge: "#f472b6" },
602
+ neutral: { fill: "#1e293b", stroke: "#94a3b8", text: "#f1f5f9", edge: "#94a3b8" }
603
+ };
591
604
  var FLOWCHART_EDGE_STROKE_STYLE_LABELS = {
592
605
  solid: "Solid",
593
606
  dashed: "Dashed",
@@ -880,8 +893,8 @@ function normalizeViewport(viewport, scaleBounds) {
880
893
  const min = scaleBounds?.min ?? DEFAULT_MIN_SCALE;
881
894
  const max = scaleBounds?.max ?? DEFAULT_MAX_SCALE;
882
895
  return {
883
- x: toFiniteNumber(viewport?.x, 0),
884
- y: toFiniteNumber(viewport?.y, 0),
896
+ x: formatNumber(toFiniteNumber(viewport?.x, 0)),
897
+ y: formatNumber(toFiniteNumber(viewport?.y, 0)),
885
898
  scale: clamp(formatScale(toFiniteNumber(viewport?.scale, 1)), min, max)
886
899
  };
887
900
  }
@@ -891,10 +904,10 @@ function normalizeNode(rawNode, index, usedIds, sizeBounds) {
891
904
  const node = {
892
905
  id: ensureUniqueId(rawNode?.id, "node", usedIds),
893
906
  type,
894
- x: toFiniteNumber(rawNode?.x, index * 28),
895
- y: toFiniteNumber(rawNode?.y, index * 18),
896
- width: clampNodeWidth(type, rawNode?.width, spec.width, sizeBounds),
897
- height: clampNodeHeight(type, rawNode?.height, spec.height, sizeBounds),
907
+ x: formatNumber(toFiniteNumber(rawNode?.x, index * 28)),
908
+ y: formatNumber(toFiniteNumber(rawNode?.y, index * 18)),
909
+ width: formatNumber(clampNodeWidth(type, rawNode?.width, spec.width, sizeBounds)),
910
+ height: formatNumber(clampNodeHeight(type, rawNode?.height, spec.height, sizeBounds)),
898
911
  text: rawNode?.text == null ? spec.text : String(rawNode.text),
899
912
  data: isPlainObject(rawNode?.data) ? deepClone(rawNode.data) : {}
900
913
  };
@@ -1896,8 +1909,19 @@ var VdFlowchart = class {
1896
1909
  });
1897
1910
  this.resizeObserver.observe(this.canvasEl);
1898
1911
  }
1899
- const raf = typeof requestAnimationFrame === "function" ? requestAnimationFrame : (cb) => setTimeout(cb, 16);
1900
- raf(() => tryEmit());
1912
+ if (typeof requestAnimationFrame === "function") {
1913
+ this.readyFrameIsTimeout = false;
1914
+ this.readyFrame = requestAnimationFrame(() => {
1915
+ this.readyFrame = 0;
1916
+ tryEmit();
1917
+ });
1918
+ } else {
1919
+ this.readyFrameIsTimeout = true;
1920
+ this.readyFrame = setTimeout(() => {
1921
+ this.readyFrame = 0;
1922
+ tryEmit();
1923
+ }, 16);
1924
+ }
1901
1925
  };
1902
1926
  if (typeof queueMicrotask === "function") {
1903
1927
  queueMicrotask(startObserving);
@@ -2278,10 +2302,11 @@ var VdFlowchart = class {
2278
2302
  this.graphEditButton.disabled = this.readonly || nodes.length === 0;
2279
2303
  }
2280
2304
  bindEvents() {
2305
+ this.handleDeleteClick = () => this.deleteSelection();
2281
2306
  this.toolbar.addEventListener("click", this.handleToolbarClick);
2282
2307
  this.arrangeSelect.addEventListener("change", this.handleArrangeChange);
2283
2308
  this.paletteGrid.addEventListener("click", this.handlePaletteClick);
2284
- this.deleteButton.addEventListener("click", () => this.deleteSelection());
2309
+ this.deleteButton.addEventListener("click", this.handleDeleteClick);
2285
2310
  this.selectionFields.addEventListener("input", this.handleSelectionFieldInput);
2286
2311
  this.selectionFields.addEventListener("change", this.handleSelectionFieldChange);
2287
2312
  this.selectionFields.addEventListener("click", this.handleSelectionActionClick);
@@ -2303,6 +2328,14 @@ var VdFlowchart = class {
2303
2328
  this.toolbar.removeEventListener("click", this.handleToolbarClick);
2304
2329
  this.arrangeSelect.removeEventListener("change", this.handleArrangeChange);
2305
2330
  this.paletteGrid.removeEventListener("click", this.handlePaletteClick);
2331
+ if (this.handleDeleteClick) {
2332
+ this.deleteButton.removeEventListener("click", this.handleDeleteClick);
2333
+ this.handleDeleteClick = null;
2334
+ }
2335
+ if (this.handleDeleteClick) {
2336
+ this.deleteButton.removeEventListener("click", this.handleDeleteClick);
2337
+ this.handleDeleteClick = null;
2338
+ }
2306
2339
  this.selectionFields.removeEventListener("input", this.handleSelectionFieldInput);
2307
2340
  this.selectionFields.removeEventListener("change", this.handleSelectionFieldChange);
2308
2341
  this.selectionFields.removeEventListener("click", this.handleSelectionActionClick);
@@ -3129,6 +3162,23 @@ var VdFlowchart = class {
3129
3162
  return snap?.node || snap?.edge || null;
3130
3163
  }
3131
3164
  cutSelection() {
3165
+ if (!this.selection || this.readonly) return false;
3166
+ if (this.hasMultiSelection()) {
3167
+ const unlockedIds = this.getSelectedNodeIds().filter((id) => {
3168
+ const node = this.findNode(id);
3169
+ return node && !node.locked;
3170
+ });
3171
+ if (!unlockedIds.length) return false;
3172
+ const unlockedSet = new Set(unlockedIds);
3173
+ const nodes = unlockedIds.map((id) => deepClone(this.findNode(id)));
3174
+ const edges = this.documentData.edges.filter((edge) => unlockedSet.has(edge.from.nodeId) && unlockedSet.has(edge.to.nodeId)).map((edge) => deepClone(edge));
3175
+ this.clipboard = { kind: "nodes", data: { nodes, edges } };
3176
+ return this.deleteSelection();
3177
+ }
3178
+ if (this.selection.kind === "node") {
3179
+ const node = this.findNode(this.selection.id);
3180
+ if (!node || node.locked) return false;
3181
+ }
3132
3182
  if (!this.copySelection()) return false;
3133
3183
  return this.deleteSelection();
3134
3184
  }
@@ -6160,7 +6210,11 @@ var VdFlowchart = class {
6160
6210
  const gap = Math.max(0, (totalSpan - totalSize) / (sorted.length - 1));
6161
6211
  let cursor = first.x;
6162
6212
  sorted.forEach((node, index) => {
6163
- if (index > 0 && index < sorted.length - 1 && !node.locked) {
6213
+ if (index > 0 && index < sorted.length - 1) {
6214
+ if (node.locked) {
6215
+ cursor = node.x + node.width + gap;
6216
+ return;
6217
+ }
6164
6218
  const nextX = formatNumber(cursor);
6165
6219
  if (nextX !== node.x) {
6166
6220
  node.x = nextX;
@@ -6175,7 +6229,11 @@ var VdFlowchart = class {
6175
6229
  const gap = Math.max(0, (totalSpan - totalSize) / (sorted.length - 1));
6176
6230
  let cursor = first.y;
6177
6231
  sorted.forEach((node, index) => {
6178
- if (index > 0 && index < sorted.length - 1 && !node.locked) {
6232
+ if (index > 0 && index < sorted.length - 1) {
6233
+ if (node.locked) {
6234
+ cursor = node.y + node.height + gap;
6235
+ return;
6236
+ }
6179
6237
  const nextY = formatNumber(cursor);
6180
6238
  if (nextY !== node.y) {
6181
6239
  node.y = nextY;
@@ -6258,16 +6316,62 @@ var VdFlowchart = class {
6258
6316
  height: viewHeight,
6259
6317
  class: "vd-flowchart-export"
6260
6318
  });
6319
+ const isDarkTheme = (() => {
6320
+ const explicitTheme = this.element?.closest?.("[data-theme]")?.getAttribute("data-theme") || document.documentElement?.getAttribute("data-theme");
6321
+ if (explicitTheme === "dark") return true;
6322
+ if (explicitTheme === "light") return false;
6323
+ return Boolean(window.matchMedia?.("(prefers-color-scheme: dark)")?.matches);
6324
+ })();
6325
+ const presetFallbacks = isDarkTheme ? FLOWCHART_COLOR_PRESET_DARK_FALLBACKS : FLOWCHART_COLOR_PRESET_FALLBACKS;
6326
+ const computedStyle = typeof window.getComputedStyle === "function" ? window.getComputedStyle(this.root || this.element) : null;
6327
+ const readCssVar = (name, fallback) => {
6328
+ const val = computedStyle?.getPropertyValue(name)?.trim();
6329
+ return val && !val.includes("var(") ? val : fallback;
6330
+ };
6331
+ const defaultSurface = readCssVar(
6332
+ "--vd-flowchart-surface",
6333
+ readCssVar("--vd-bg-primary", isDarkTheme ? "#0f172a" : "#ffffff")
6334
+ );
6335
+ const defaultNodeFill = readCssVar(
6336
+ "--vd-flowchart-node-fill",
6337
+ isDarkTheme ? "#1e293b" : "#ffffff"
6338
+ );
6339
+ const defaultNodeStroke = readCssVar(
6340
+ "--vd-flowchart-node-stroke",
6341
+ isDarkTheme ? "#94a3b8" : "#475569"
6342
+ );
6343
+ const defaultNodeText = readCssVar(
6344
+ "--vd-flowchart-node-text",
6345
+ isDarkTheme ? "#f1f5f9" : "#0f172a"
6346
+ );
6347
+ const defaultEdgeStroke = readCssVar(
6348
+ "--vd-flowchart-edge-stroke",
6349
+ isDarkTheme ? "#94a3b8" : "#475569"
6350
+ );
6351
+ const resolvePresetColor = (presetName, slot) => {
6352
+ const fallback = presetFallbacks[presetName]?.[slot];
6353
+ if (!fallback) return null;
6354
+ return readCssVar(`--vd-flowchart-palette-${presetName}-${slot}`, fallback);
6355
+ };
6356
+ const hasAnimatedEdge = this.documentData.edges.some((edge) => edge.strokeStyle === "animated");
6261
6357
  const styleEl = svgEl("style");
6262
- styleEl.textContent = [
6358
+ const styleRules = [
6263
6359
  '.vd-flowchart-export { font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }',
6264
- ".vd-flowchart-node-shape { fill: #ffffff; stroke: #475569; stroke-width: 2px; }",
6360
+ `.vd-flowchart-node-shape { fill: ${defaultNodeFill}; stroke: ${defaultNodeStroke}; stroke-width: 2px; }`,
6265
6361
  ".vd-flowchart-node--textbox .vd-flowchart-node-shape { stroke-dasharray: 6 4; }",
6266
- ".vd-flowchart-node--junction .vd-flowchart-node-shape { fill: #2563eb; stroke: #ffffff; }",
6267
- ".vd-flowchart-node-text { fill: #0f172a; font-size: 14px; font-weight: 600; dominant-baseline: middle; text-anchor: middle; }",
6268
- ".vd-flowchart-edge-path { fill: none; stroke: #475569; stroke-linecap: round; stroke-linejoin: round; }",
6269
- ".vd-flowchart-edge-label { fill: #0f172a; font-size: 12px; font-weight: 600; paint-order: stroke; stroke: #ffffff; stroke-width: 5px; }"
6270
- ].join("\n");
6362
+ `.vd-flowchart-node--junction .vd-flowchart-node-shape { fill: #2563eb; stroke: ${defaultNodeFill}; }`,
6363
+ `.vd-flowchart-node-text { fill: ${defaultNodeText}; font-size: 14px; font-weight: 600; dominant-baseline: middle; text-anchor: middle; }`,
6364
+ `.vd-flowchart-edge-path { fill: none; stroke: ${defaultEdgeStroke}; stroke-linecap: round; stroke-linejoin: round; }`,
6365
+ `.vd-flowchart-edge-label { fill: ${defaultNodeText}; font-size: 12px; font-weight: 600; paint-order: stroke; stroke: ${defaultSurface}; stroke-width: 5px; }`
6366
+ ];
6367
+ if (hasAnimatedEdge) {
6368
+ styleRules.push(
6369
+ "@keyframes vd-flowchart-edge-flow { to { stroke-dashoffset: -26; } }",
6370
+ ".vd-flowchart-edge.is-stroke-animated .vd-flowchart-edge-path { animation: vd-flowchart-edge-flow 900ms linear infinite; }",
6371
+ "@media (prefers-reduced-motion: reduce) { .vd-flowchart-edge.is-stroke-animated .vd-flowchart-edge-path { animation: none; } }"
6372
+ );
6373
+ }
6374
+ styleEl.textContent = styleRules.join("\n");
6271
6375
  exportRoot.appendChild(styleEl);
6272
6376
  if (this.markerDefs) {
6273
6377
  const defsClone = this.markerDefs.cloneNode(true);
@@ -6275,18 +6379,18 @@ var VdFlowchart = class {
6275
6379
  const fill = shape.getAttribute("fill") || "";
6276
6380
  const preset = fill.match(/--vd-flowchart-palette-([a-z]+)-edge/);
6277
6381
  if (preset) {
6278
- const fallback = FLOWCHART_COLOR_PRESET_FALLBACKS[preset[1]]?.edge;
6279
- if (fallback) {
6280
- shape.setAttribute("fill", fallback);
6382
+ const resolved = resolvePresetColor(preset[1], "edge");
6383
+ if (resolved) {
6384
+ shape.setAttribute("fill", resolved);
6281
6385
  return;
6282
6386
  }
6283
6387
  }
6284
- if (fill.includes("var(")) shape.setAttribute("fill", "#475569");
6388
+ if (fill.includes("var(")) shape.setAttribute("fill", defaultEdgeStroke);
6285
6389
  });
6286
6390
  exportRoot.appendChild(defsClone);
6287
6391
  }
6288
6392
  if (typeof options.background === "string" && options.background.trim() && options.background.trim() !== "transparent") {
6289
- const bgColor = options.background.trim() === "theme" ? "#ffffff" : sanitizeCssColor(options.background.trim()) || "#ffffff";
6393
+ const bgColor = options.background.trim() === "theme" ? defaultSurface : sanitizeCssColor(options.background.trim()) || defaultSurface;
6290
6394
  exportRoot.appendChild(
6291
6395
  svgEl("rect", {
6292
6396
  x: viewX,
@@ -6306,7 +6410,7 @@ var VdFlowchart = class {
6306
6410
  const edge = this.findEdge(edgeId);
6307
6411
  const pathEl = edgeGroup.querySelector(".vd-flowchart-edge-path");
6308
6412
  if (edge && pathEl) {
6309
- const stroke = edge.color ? FLOWCHART_COLOR_PRESET_FALLBACKS[edge.color]?.edge || edge.color : "#475569";
6413
+ const stroke = edge.color ? resolvePresetColor(edge.color, "edge") || edge.color : defaultEdgeStroke;
6310
6414
  pathEl.setAttribute("stroke", stroke);
6311
6415
  pathEl.style.stroke = stroke;
6312
6416
  }
@@ -6322,17 +6426,19 @@ var VdFlowchart = class {
6322
6426
  if (!node) return;
6323
6427
  const shapeEl = nodeGroup.querySelector(".vd-flowchart-node-shape");
6324
6428
  const labelEl = nodeGroup.querySelector(".vd-flowchart-node-text");
6325
- const presetColors = node.color ? FLOWCHART_COLOR_PRESET_FALLBACKS[node.color] : null;
6429
+ const presetFill = node.color ? resolvePresetColor(node.color, "fill") : null;
6430
+ const presetStroke = node.color ? resolvePresetColor(node.color, "stroke") : null;
6431
+ const presetText = node.color ? resolvePresetColor(node.color, "text") : null;
6326
6432
  if (shapeEl) {
6327
- const fill = node.fill || presetColors?.fill || (node.type === "junction" ? "#2563eb" : "#ffffff");
6328
- const stroke = node.stroke || presetColors?.stroke || (node.type === "junction" ? "#ffffff" : "#475569");
6433
+ const fill = node.fill || presetFill || (node.type === "junction" ? "#2563eb" : defaultNodeFill);
6434
+ const stroke = node.stroke || presetStroke || (node.type === "junction" ? defaultNodeFill : defaultNodeStroke);
6329
6435
  shapeEl.setAttribute("fill", fill);
6330
6436
  shapeEl.setAttribute("stroke", stroke);
6331
6437
  shapeEl.style.fill = fill;
6332
6438
  shapeEl.style.stroke = stroke;
6333
6439
  }
6334
6440
  if (labelEl) {
6335
- const textColor = node.textColor || presetColors?.text || "#0f172a";
6441
+ const textColor = node.textColor || presetText || defaultNodeText;
6336
6442
  labelEl.setAttribute("fill", textColor);
6337
6443
  labelEl.style.fill = textColor;
6338
6444
  }
@@ -6482,7 +6588,35 @@ var VdFlowchart = class {
6482
6588
  if ("readonly" in options) {
6483
6589
  if (options.readonly && !this.readonly) {
6484
6590
  this.stopTextEdit({ commit: true });
6485
- this.interaction = null;
6591
+ if (this.interaction) {
6592
+ const active = this.interaction;
6593
+ if (active.kind === "drag-node" && active.origins) {
6594
+ active.origins.forEach((origin, id) => {
6595
+ const node = this.findNode(id);
6596
+ if (node) {
6597
+ node.x = origin.x;
6598
+ node.y = origin.y;
6599
+ }
6600
+ });
6601
+ } else if (active.kind === "resize-node" && active.original) {
6602
+ const node = this.findNode(active.nodeId);
6603
+ if (node) {
6604
+ node.x = active.original.x;
6605
+ node.y = active.original.y;
6606
+ node.width = active.original.width;
6607
+ node.height = active.original.height;
6608
+ }
6609
+ }
6610
+ if (active.pointerId != null && typeof this.canvasEl?.releasePointerCapture === "function") {
6611
+ try {
6612
+ this.canvasEl.releasePointerCapture(active.pointerId);
6613
+ } catch {
6614
+ }
6615
+ }
6616
+ this.renderGuides([]);
6617
+ this.interaction = null;
6618
+ this.render({ inspector: false, json: false });
6619
+ }
6486
6620
  this.activeTool = null;
6487
6621
  this.reconnectEdgeId = null;
6488
6622
  }
@@ -6595,6 +6729,14 @@ var VdFlowchart = class {
6595
6729
  cancelAnimationFrame(this.minimapFrame);
6596
6730
  }
6597
6731
  this.minimapFrame = 0;
6732
+ if (this.readyFrame) {
6733
+ if (this.readyFrameIsTimeout) {
6734
+ clearTimeout(this.readyFrame);
6735
+ } else if (typeof cancelAnimationFrame === "function") {
6736
+ cancelAnimationFrame(this.readyFrame);
6737
+ }
6738
+ this.readyFrame = 0;
6739
+ }
6598
6740
  this.activeTool = null;
6599
6741
  this.stopTextEdit({ commit: false });
6600
6742
  this.resizeObserver?.disconnect();
@@ -6610,7 +6752,7 @@ var VdFlowchart = class {
6610
6752
  };
6611
6753
 
6612
6754
  // src/vue.js
6613
- var FORWARDED_EVENTS = ["change", "select", "viewport", "connect", "ready"];
6755
+ var FORWARDED_EVENTS = ["change", "select", "viewport", "connect", "ready", "history"];
6614
6756
  var EDITOR_OPTION_KEYS = [
6615
6757
  "readonly",
6616
6758
  "gridSize",