Skip to content

Commit 17bd92d

Browse files
committed
0.2.0 - Added csv support
1 parent 98dad52 commit 17bd92d

File tree

8 files changed

+118
-6
lines changed

8 files changed

+118
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.2.0
2+
- Added `csv` parameter for CSV loading support
3+
14
# 0.1.9
25
- Added `embed=1` URL parameter, as documented by draw.io
36

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-drawio",
3-
"version": "0.1.9",
3+
"version": "0.2.0",
44
"type": "module",
55
"description": "React component for integrating the Diagrams (draw.io) embed iframe",
66
"main": "index.js",

public/mydrawio.csv

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## From https://github.com/jgraph/drawio-diagrams/blob/dev/examples/csv/car-partial-supply-chain.txt
2+
## Supply chain tracking example
3+
# label: %name%
4+
# stylename: shapeType
5+
# styles: {"raw": "shape=parallelogram;fillColor=#f5f5f5;strokeColor=#666666;perimeter=parallelogramPerimeter;", \
6+
# "well": "shape=cylinder;fillColor=#f5f5f5;strokeColor=#666666;", \
7+
# "foundry": "shape=trapezoid;fillColor=#dae8fc;strokeColor=#6c8ebf;perimeter=trapezoidPerimeter;", \
8+
# "manufacturer":"shape=hexagon;fillColor=#d5e8d4;strokeColor=#82b366;perimeter=hexagonPerimeter;", \
9+
# "chemicals":"rounded=0;fillColor=#f8cecc;strokeColor=#b85450", \
10+
# "electronics":"rounded=1;fillColor=#fff2cc;strokeColor=#d6b656", \
11+
# "assembly":"shape=triangle;fillColor=#ffe6cc;strokeColor=#d79b00;perimeter=trianglePerimeter;", \
12+
# "component":"shape=ellipse;fillColor=#ffffff;strokeColor=#000000;perimeter=ellipsePerimeter;"}
13+
# namespace: csvimport-
14+
# connect: {"from":"supplier", "to":"id", "invert":true, "style":"curved=1;endArrow=blockThin;startArrow=none;strokeColor=#999999;endFill=1;"}
15+
# width: auto
16+
# height: auto
17+
# padding: 40
18+
# ignore: id,shapeType,supplier
19+
# nodespacing: 40
20+
# levelspacing: 40
21+
# edgespacing: 40
22+
# layout: horizontalflow
23+
## CSV data starts below this line
24+
id,name,supplier,shapeType
25+
mb,Mine B,,raw
26+
ma,Mine A,,raw
27+
mc,Mine C,,raw
28+
md,Mine D,,raw
29+
w1,Well 1,,well
30+
w2,Well 2,,well
31+
w3,Well 3,,well
32+
w4,Well 4,,well
33+
fa,Foundry A,"mb,mc,ma",foundry
34+
fb,Foundry B,"mc,md",foundry
35+
fc,Foundry C,"ma,md",foundry
36+
o1,Oil and Gas 1,w2,raw
37+
o2,Oil and Gas 2,"w4,w3",raw
38+
o3,Oil and Gas 3,w1,raw
39+
man1,Manufacturer 1,fb,manufacturer
40+
man2,Manufacturer 2,fc,manufacturer
41+
man3,Manufacturer 3,fa,manufacturer
42+
man4,Manufacturer 4,fc,manufacturer
43+
man5,Manufacturer 5,ca,manufacturer
44+
ca,Chemicals A,o3,chemicals
45+
cb,Chemicals B,o1,chemicals
46+
cc,Chemicals C,o2,chemicals
47+
ea,Electronics A,cc,electronics
48+
eb,Electronics B,cc,electronics
49+
a1,Assembly 1,"3,4,5",assembly
50+
a2,Assembly 2,2,assembly
51+
a3,Assembly 3,"1,2,6,8,10,11",assembly
52+
a4,Assembly 4,"9,12,13,7,15,14",assembly
53+
a5,Assembly 5,16,assembly
54+
1,gearbox housing,fb,component
55+
2,gears,man3,component
56+
3,turbine,man1,component
57+
4,stator,man1,component
58+
5,impellor,man1,component
59+
6,bearings,man2,component
60+
7,transmission fluid,cb,component
61+
8,sealant,cc,component
62+
9,transmission control unit,ea,component
63+
10,gasket,man5,component
64+
11,seals,man5,component
65+
12,planetary gear train,a2,component
66+
13,torque converter,a1,component
67+
14,hydraulic controls,man4,component
68+
15,gearbox,a3,component
69+
16,transmission,a4,component
70+
17,car,a5,component

src/DrawIoEmbed.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
1818
urlParameters,
1919
configuration,
2020
xml,
21+
csv,
2122
exportFormat,
2223
onSave,
2324
onClose,
@@ -132,11 +133,13 @@ export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
132133
} else {
133134
action.load({ xml });
134135
}
136+
} else if (csv) {
137+
action.load({ descriptor: { format: 'csv', data: csv } });
135138
} else {
136139
action.load({ xml: '' });
137140
}
138141
}
139-
}, [isInitialized, xml]);
142+
}, [isInitialized, xml, csv]);
140143

141144
// Initial load
142145
useEffect(() => {

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export type DrawIoEmbedProps = {
1313
* XML structure for prefilling the editor
1414
*/
1515
xml?: string;
16+
/**
17+
* CSV data for prefilling the editor
18+
*/
19+
csv?: string;
1620
/**
1721
* For configuration options, see https://www.drawio.com/doc/faq/configure-diagram-editor
1822
*/
@@ -177,6 +181,7 @@ export type ActionLoad = {
177181
action: 'load';
178182
xml?: string;
179183
xmlpng?: string;
184+
descriptor?: { format: 'csv'; data: string };
180185
};
181186

182187
export type ActionMerge = {

stories/DiagramsEmbed.stories.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ export const WithRemoteVsdx: Story = {
8282
args: {}
8383
};
8484

85+
export const WithRemoteCsv: Story = {
86+
decorators: [
87+
(Story) => {
88+
const { inputCsv, loadCsv } = useRemoteFile();
89+
90+
useEffect(() => {
91+
loadCsv('/mydrawio.csv');
92+
}, []);
93+
94+
return <Story args={{ csv: inputCsv }} />;
95+
}
96+
],
97+
args: {}
98+
};
99+
85100
export const WithConfigurations: Story = {
86101
args: {
87102
xml: '<mxfile host="embed.diagrams.net" modified="2023-08-27T13:33:23.800Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" version="21.6.9" etag="xthbfrarG6SmZwCYJPbt" type="embed"><diagram id="kaqXdMjmixOsNJA9w4jU" name="Page-1"><mxGraphModel dx="314" dy="361" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0"><root><mxCell id="0" /><mxCell id="1" parent="0" /><mxCell id="2" value="" style="shape=cube;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;darkOpacity=0.05;darkOpacity2=0.1;fillColor=#99FFFF;strokeColor=#0066CC;" vertex="1" parent="1"><mxGeometry x="110" y="150" width="120" height="80" as="geometry" /></mxCell></root></mxGraphModel></diagram></mxfile>',

stories/hooks/useRemoteFile.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const basePath = import.meta.env.VITE_GH_PAGE === 'true' ? '/react-drawio' : '';
88

99
export const useRemoteFile = () => {
1010
const [inputXml, setInputXml] = useState<string>('');
11+
const [inputCsv, setInputCsv] = useState<string>('');
1112

1213
const urlToBase64 = async (url: string, options?: UrlToBase64Options) => {
1314
const data = await fetch(`${basePath}${url}`);
@@ -21,16 +22,31 @@ export const useRemoteFile = () => {
2122
let base64data = (reader.result || '') as string;
2223

2324
if (options?.isVisio) {
24-
base64data = base64data.replace('data:application/octet-stream;base64', 'data:application/vnd.visio;base64');
25+
base64data = base64data.replace(
26+
'data:application/octet-stream;base64',
27+
'data:application/vnd.visio;base64'
28+
);
2529
}
2630

2731
setInputXml(base64data);
2832
};
2933
}
3034
};
3135

36+
const loadCsv = async (url: string) => {
37+
const data = await fetch(`${basePath}${url}`);
38+
39+
if (data) {
40+
const text = await data.text();
41+
42+
setInputCsv(text);
43+
}
44+
};
45+
3246
return {
3347
inputXml,
34-
urlToBase64
48+
inputCsv,
49+
urlToBase64,
50+
loadCsv
3551
};
3652
};

0 commit comments

Comments
 (0)