53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { createDefaultWorkbenchState } from './defaultWorkspace';
|
|
import type { WorkbenchState } from './workspaceTypes';
|
|
|
|
const STORAGE_KEY = 'xslt-tools:workspace:v1';
|
|
|
|
function isWorkbenchState(value: unknown): value is WorkbenchState {
|
|
if (!value || typeof value !== 'object') return false;
|
|
|
|
const candidate = value as Partial<WorkbenchState>;
|
|
return (
|
|
candidate.schemaVersion === 1 &&
|
|
Boolean(candidate.xmlInput) &&
|
|
Boolean(candidate.xsltCode) &&
|
|
Boolean(candidate.xmlOutput)
|
|
);
|
|
}
|
|
|
|
export function loadWorkbenchState(): WorkbenchState {
|
|
const fallback = createDefaultWorkbenchState();
|
|
|
|
try {
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return fallback;
|
|
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
if (!isWorkbenchState(parsed)) return fallback;
|
|
|
|
return {
|
|
...fallback,
|
|
...parsed,
|
|
options: { ...fallback.options, ...parsed.options },
|
|
xmlInput: { ...fallback.xmlInput, ...parsed.xmlInput },
|
|
xsltCode: { ...fallback.xsltCode, ...parsed.xsltCode },
|
|
xmlOutput: { ...fallback.xmlOutput, ...parsed.xmlOutput },
|
|
};
|
|
} catch (error) {
|
|
console.warn('Failed to load workspace from LocalStorage.', error);
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function saveWorkbenchState(state: WorkbenchState): void {
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
} catch (error) {
|
|
console.warn('Failed to save workspace to LocalStorage.', error);
|
|
}
|
|
}
|
|
|
|
export function clearWorkbenchState(): void {
|
|
window.localStorage.removeItem(STORAGE_KEY);
|
|
}
|