Skip to content

Commit a59ac6d

Browse files
committed
Dynamic save for graphML
1 parent b49f13a commit a59ac6d

11 files changed

Lines changed: 49 additions & 40 deletions

File tree

src/GraphWorkspace.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const GraphComp = (props) => {
7272
serverID={el.serverID}
7373
graphML={el.graphML}
7474
projectName={el.projectName}
75+
fileHandle={el.fileHandle}
76+
fileName={el.fileName}
7577
/>
7678
))}
7779
<ZoomComp dispatcher={dispatcher} superState={superState} />

src/component/TabBar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const TabBar = ({ superState, dispatcher }) => {
6060
id={`tab_${i}`}
6161
>
6262
<span className="tab-text">
63-
{el.projectName}
63+
{el.fileName || el.projectName}
6464
</span>
6565

6666
{superState.curGraphIndex === i ? (

src/component/fileBrowser.jsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,9 @@ const LocalFileBrowser = ({ superState, dispatcher }) => {
2121
}, []);
2222

2323
const [fileState, setFileState] = useState([]);
24-
// const [fileState, setFileState] = useState(() => {
25-
// let files = [];
26-
// files = [];
27-
// // if (window.localStorage.getItem('fileList')) {
28-
// // files = JSON.parse(window.localStorage.getItem('fileList'));
29-
// // console.log(files);
30-
// // return files;
31-
// // }
32-
// return { files };
33-
// });
3424

35-
// TODO
3625
useEffect(() => {
26+
// TODO - Loading file list from localStorage. Not supported by browsers.
3727
// if(window.localStorage.getItem('fileList')) {
3828
// const allFiles = window.localStorage.getItem('fileList');
3929
// setFileState({ files: allFiles });

src/graph-builder/graph-core/1-core.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,6 @@ class CoreGraph {
106106
this.cy.emit('graph-modified');
107107
}
108108

109-
setGraphFileHandle(fileHandle, shouldEmit = true) {
110-
this.fileHandle = fileHandle;
111-
if (shouldEmit) {
112-
this.dispatcher({
113-
type: T.SET_PROJECT_DETAILS,
114-
payload: {
115-
value: fileHandle,
116-
graphID: this.id,
117-
type: 'filehandle',
118-
},
119-
});
120-
}
121-
this.cy.emit('graph-modified');
122-
}
123-
124109
setServerID(serverID, shouldEmit = true) {
125110
this.serverID = serverID;
126111
if (shouldEmit) {

src/graph-builder/graph-core/5-load-save.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class GraphLoadSave extends GraphUndoRedo {
5656
projectName: this.projectName,
5757
id: this.id,
5858
serverID: this.serverID,
59+
fileName: null,
60+
fileHandle: null,
5961
};
6062
this.cy.nodes().forEach((node) => {
6163
if (this.shouldNodeBeSaved(node.id())) {
@@ -102,7 +104,14 @@ class GraphLoadSave extends GraphUndoRedo {
102104
const str = graphmlBuilder(this.jsonifyGraph());
103105
const bytes = new TextEncoder().encode(str);
104106
const blob = new Blob([bytes], { type: 'application/json;charset=utf-8' });
105-
saveAs(blob, `${fileName || `${this.getName()}-DHGWorkflow`}.graphml`);
107+
saveAs(blob, `${fileName || `${this.getName()}-concore`}.graphml`);
108+
}
109+
110+
saveToFolder() {
111+
const str = graphmlBuilder(this.jsonifyGraph());
112+
const bytes = new TextEncoder().encode(str);
113+
const blob = new Blob([bytes], { type: 'application/json;charset=utf-8' });
114+
return blob;
106115
}
107116

108117
getGraphML() {
@@ -111,7 +120,7 @@ class GraphLoadSave extends GraphUndoRedo {
111120

112121
loadJson(content) {
113122
content.nodes.forEach((node) => {
114-
this.addNode(node.label, node.style, 'ordin', node.position, { }, node.id, 0);
123+
this.addNode(node.label, node.style, 'ordin', node.position, {}, node.id, 0);
115124
});
116125
content.edges.forEach((edge) => {
117126
this.addEdge({ ...edge, sourceID: edge.source, targetID: edge.target }, 0);

src/graph-builder/graphml/builder/graphML.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const graphML = ({
2-
nodes, edges, id, projectName, actionHistory, serverID,
2+
nodes, edges, id, projectName, actionHistory, serverID, fileHandle, fileName,
33
}) => ({
44
graphml: {
55
$: {
@@ -31,6 +31,8 @@ const graphML = ({
3131
id,
3232
projectName,
3333
serverID,
34+
fileHandle,
35+
fileName,
3436
},
3537
node: nodes,
3638
edge: edges,

src/graph-builder/graphml/builder/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const builder = (G) => {
2828
id: G.id,
2929
actionHistory: G.actionHistory,
3030
serverID: G.serverID,
31+
fileHandle: G.fileHandle,
32+
fileName: G.fileName,
3133
});
3234
const xml = new xml2js.Builder().buildObject(X);
3335
return xml;

src/reducer/initialState.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const initialGraphState = {
3636
component: null,
3737
instance: null,
3838
id: null,
39+
fileHandle: null,
40+
fileName: null,
3941
};
4042

4143
export { initialState, initialGraphState };

src/reducer/reducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ const reducer = (state, action) => {
9595
graphID,
9696
serverID: action.payload.serverID,
9797
graphML: action.payload.graphML,
98+
fileHandle: action.payload.fileHandle || null,
99+
fileName: action.payload.fileName,
98100
},
99101
],
100102
};

src/toolbarActions/toolbarFunctions.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,38 @@ const downloadImg = (state, setState, format) => {
7575
getGraphFun(state).downloadImg(format);
7676
};
7777

78-
// TODO
79-
// const saveLocal = (state, d) => {
80-
// };
81-
8278
const saveAction = (state, d, fileName) => {
8379
getGraphFun(state).saveToDisk(fileName);
8480
};
8581

86-
const readFile = (state, setState, file) => {
82+
async function saveGraphMLFile(state) {
83+
if (state.curGraphInstance) {
84+
const graph = state.graphs[state.curGraphIndex];
85+
if (graph.fileHandle) {
86+
const stream = await graph.fileHandle.createWritable();
87+
await stream.write(getGraphFun(state).saveToFolder());
88+
await stream.close();
89+
} else {
90+
// eslint-disable-next-line no-alert
91+
alert('Switch to Edge/Chrome!');
92+
}
93+
} else {
94+
// eslint-disable-next-line no-alert
95+
alert('Switch to Edge/Chrome!');
96+
}
97+
}
98+
99+
const readFile = (state, setState, file, fileHandle) => {
87100
if (file) {
88101
const fr = new FileReader();
89102
const projectName = file.name;
90103
if (file.name.split('.').pop() === 'graphml') {
91104
fr.onload = (x) => {
92105
setState({
93106
type: T.ADD_GRAPH,
94-
payload: { projectName, graphML: x.target.result },
107+
payload: {
108+
projectName, graphML: x.target.result, fileHandle, fileName: file.name,
109+
},
95110
});
96111
};
97112
fr.readAsText(file);
@@ -151,7 +166,7 @@ const toggleServer = (state, dispatcher) => {
151166
};
152167

153168
export {
154-
createNode, editElement, deleteElem, downloadImg, saveAction,
169+
createNode, editElement, deleteElem, downloadImg, saveAction, saveGraphMLFile,
155170
readFile, readTextFile, newProject, clearAll, editDetails, undo, redo,
156171
openShareModal, openSettingModal, viewHistory,
157172
toggleServer,

0 commit comments

Comments
 (0)