Replace the writable+derived store pipeline in state.ts with a state.svelte.ts module: a deep $state inputState mutated only through the exported update functions, each of which persists the snapshot and re-validates it asynchronously into validatedState.current (with urls.current derived from it). Store subscriptions in components become $effect blocks, and the rough/grid toggles use function bindings. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import type { Loader, State } from '$lib/types';
|
|
import { defaultState, sanitizeConfig, updateCodeStore } from '$lib/util/state.svelte';
|
|
import { fetchText } from '$lib/util/util';
|
|
import { loadGistData } from './gist';
|
|
|
|
const loaders: Record<string, Loader> = {
|
|
gist: loadGistData
|
|
};
|
|
|
|
export const loadDataFromUrl = async (): Promise<void> => {
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
let state: Partial<State> = defaultState;
|
|
let loaded = false;
|
|
const codeURL: string | undefined = searchParams.get('code') ?? undefined;
|
|
const configURL: string | undefined = searchParams.get('config') ?? undefined;
|
|
|
|
let code: string | undefined;
|
|
const config = configURL ? await fetchText(configURL) : defaultState.mermaid;
|
|
|
|
if (codeURL) {
|
|
code = await fetchText(codeURL);
|
|
loaded = true;
|
|
}
|
|
if (code) {
|
|
if (!codeURL) {
|
|
throw new Error('Code URL is not defined');
|
|
}
|
|
state = {
|
|
code,
|
|
loader: {
|
|
config: {
|
|
codeURL,
|
|
configURL
|
|
},
|
|
type: 'files'
|
|
},
|
|
mermaid: config
|
|
};
|
|
} else {
|
|
for (const [key, value] of searchParams.entries()) {
|
|
if (key in loaders) {
|
|
try {
|
|
state = await loaders[key](value);
|
|
loaded = true;
|
|
break;
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (loaded) {
|
|
state.mermaid = sanitizeConfig(state.mermaid || defaultState.mermaid);
|
|
updateCodeStore({
|
|
...state,
|
|
updateDiagram: true
|
|
});
|
|
}
|
|
};
|