add secret save functionality (cmd-s) to output json of graph

This commit is contained in:
Sam Lavigne 2023-08-31 00:36:44 -04:00
parent e7e7074a05
commit e113e81b91
1 changed files with 19 additions and 0 deletions

View File

@ -20,6 +20,9 @@
default: ButtonEdge, default: ButtonEdge,
}; };
let downloadLink;
let savedData;
function onClick(e) { function onClick(e) {
if (e.detail.nodeType === "filter") { if (e.detail.nodeType === "filter") {
const newSelected = $nodes.findIndex((n) => n.id === e.detail.id); const newSelected = $nodes.findIndex((n) => n.id === e.detail.id);
@ -28,8 +31,24 @@
} }
} }
} }
function onKey(e) {
if ((e.ctrlKey || e.metaKey) && e.key === "s") {
e.preventDefault();
const out = JSON.stringify({ nodes: $nodes, edges: $edges }, null, 2);
savedData = "data:text/json;charset=utf-8," + encodeURIComponent(out);
setTimeout(() => {
downloadLink.click();
}, 600);
}
}
</script> </script>
<svelte:window on:keydown={onKey} />
<a style="display:none;" download="graph.json" href={savedData} bind:this={downloadLink}>Download</a
>
<SvelteFlowProvider> <SvelteFlowProvider>
<FitComp /> <FitComp />
<div class="flow"> <div class="flow">