Skip to content

Commit 4ada4aa

Browse files
committed
todo: s/l nodes <-> json
1 parent c2cb52c commit 4ada4aa

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

src/lib/io/json.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// lib/io/json.ts
2+
3+
export const saveJsonToFile = (filename: string, JsonData: any): void => {
4+
try {
5+
const blob = new Blob([JSON.stringify(JsonData, null, 2)], {
6+
type: 'application/json'
7+
});
8+
const url = URL.createObjectURL(blob);
9+
const a = document.createElement('a');
10+
a.href = url;
11+
a.download = filename;
12+
document.body.appendChild(a);
13+
a.click();
14+
document.body.removeChild(a);
15+
URL.revokeObjectURL(url);
16+
alert('File saving!');
17+
} catch (error) {
18+
console.error('Error saving JSON:', error);
19+
alert('Failed to save file.');
20+
}
21+
};
22+
23+
export const loadJsonFromFile = (): Promise<any> => {
24+
return new Promise((resolve, reject) => {
25+
const fileInput = document.createElement('input');
26+
fileInput.type = 'file';
27+
fileInput.accept = '.json';
28+
fileInput.style.display = 'none';
29+
document.body.appendChild(fileInput);
30+
31+
fileInput.addEventListener('change', async (event) => {
32+
try {
33+
const file = (event.target as HTMLInputElement).files?.[0];
34+
if (!file) {
35+
reject(new Error('No file selected.'));
36+
return;
37+
}
38+
const reader = new FileReader();
39+
reader.onload = async (e) => {
40+
try {
41+
const contents = e.target?.result;
42+
if (typeof contents === 'string') {
43+
const parsedData = JSON.parse(contents);
44+
resolve(parsedData);
45+
} else {
46+
reject(new Error('File contents are not a string.'));
47+
}
48+
} catch (error) {
49+
reject(new Error('Error parsing JSON.' + error));
50+
}
51+
};
52+
reader.onerror = () => reject(new Error('Error reading file.'));
53+
reader.readAsText(file);
54+
} catch (error) {
55+
console.error('Error during file handling:', error);
56+
reject(new Error('Error loading JSON:' + error));
57+
} finally {
58+
document.body.removeChild(fileInput);
59+
}
60+
});
61+
fileInput.click();
62+
});
63+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- routes/graph/menu/json-io.svelte -->
2+
<script context="module" lang="ts">
3+
// now these really are module‐level exports
4+
export async function saveGraph(): Promise<void> {
5+
// TODO: implement saving
6+
}
7+
8+
export async function loadGraph(): Promise<void> {
9+
// TODO: implement loading
10+
}
11+
</script>
12+
13+
<!-- if you have instance‐level script, you can still have it -->
14+
<script lang="ts">
15+
// component props / local state, etc.
16+
</script>

src/routes/graph/menu/sidebar.svelte

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export let open: boolean;
55
export let onToggle: () => void;
66
import { usingSubgraph } from '../flow/graph-store.svelte'; // Import the store
7+
import { saveGraph, loadGraph } from './json-io.svelte';
78
</script>
89

910
<div class="sidebar {open ? 'open' : ''}">
@@ -14,10 +15,16 @@
1415
<li><a href="#item2">Menu item 2</a></li>
1516
<li><a href="#item3">Menu item 3</a></li>
1617
<li>
17-
<button on:click={() => usingSubgraph.set('default')}> Default Graph </button>
18+
<button on:click={() => usingSubgraph.set('default')}>Default Graph</button>
1819
</li>
1920
<li>
20-
<button on:click={() => usingSubgraph.set('anotherKey')}> Another Graph </button>
21+
<button on:click={() => usingSubgraph.set('anotherKey')}>Another Graph</button>
22+
</li>
23+
<li>
24+
<button on:click={saveGraph}>Save Graph</button>
25+
</li>
26+
<li>
27+
<button on:click={loadGraph}>Load Graph</button>
2128
</li>
2229
</ul>
2330
</nav>
@@ -59,4 +66,11 @@
5966
nav li + li {
6067
margin-top: 0.5rem;
6168
}
69+
button {
70+
cursor: pointer; /* Makes the button appear clickable */
71+
padding: 0.5rem 1rem; /* Adds some padding for better appearance */
72+
border: 1px solid #ccc; /* Optional: Adds a border */
73+
border-radius: 4px; /* Optional: Rounds the corners */
74+
background-color: #f0f0f0; /* Optional: A subtle background color */
75+
}
6276
</style>

0 commit comments

Comments
 (0)