Skip to content

Commit c870bc2

Browse files
terapyonterapyon
authored andcommitted
bugfix for download html
1 parent e0d3cac commit c870bc2

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/htmlExport.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,62 @@ export interface ExportConfig {
2424
};
2525
}
2626

27+
/**
28+
* Normalize graph data for standalone HTML export.
29+
*
30+
* After D3.js simulation runs, link.source and link.target become
31+
* object references instead of IDs. This function converts them back
32+
* to IDs so the standalone HTML can create its own simulation.
33+
*
34+
* @param graphData - Graph data potentially containing object references
35+
* @returns Normalized graph data with IDs for source/target
36+
*/
37+
function normalizeGraphData(graphData: {
38+
nodes: any[];
39+
links: any[];
40+
}): { nodes: any[]; links: any[] } {
41+
// Normalize links: convert source/target objects back to IDs
42+
const normalizedLinks = graphData.links.map((link) => {
43+
const source =
44+
typeof link.source === 'object' && link.source !== null
45+
? link.source.id
46+
: link.source;
47+
const target =
48+
typeof link.target === 'object' && link.target !== null
49+
? link.target.id
50+
: link.target;
51+
52+
// Keep other link properties (weight, etc.) but exclude D3 simulation props
53+
const { index: _index, ...rest } = link;
54+
55+
return {
56+
...rest,
57+
source,
58+
target,
59+
};
60+
});
61+
62+
// Normalize nodes: remove D3 simulation properties
63+
const normalizedNodes = graphData.nodes.map((node) => {
64+
const {
65+
x: _x,
66+
y: _y,
67+
vx: _vx,
68+
vy: _vy,
69+
fx: _fx,
70+
fy: _fy,
71+
index: _index,
72+
...rest
73+
} = node;
74+
return rest;
75+
});
76+
77+
return {
78+
nodes: normalizedNodes,
79+
links: normalizedLinks,
80+
};
81+
}
82+
2783
/**
2884
* Default export configuration values.
2985
*/
@@ -54,7 +110,9 @@ export function generateStandaloneHtml(config: ExportConfig): string {
54110
const title = config.title || DEFAULT_EXPORT_CONFIG.title!;
55111
const width = config.width || DEFAULT_EXPORT_CONFIG.width!;
56112
const height = config.height || DEFAULT_EXPORT_CONFIG.height!;
57-
const graphData = config.graphData;
113+
114+
// Normalize graph data to ensure source/target are IDs, not object references
115+
const graphData = normalizeGraphData(config.graphData);
58116

59117
// Serialize graph data as JSON
60118
const jsonData = JSON.stringify(graphData);

0 commit comments

Comments
 (0)