56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
//#region src/lib/persistence.ts
|
|
function loadInspectorState(storageKey) {
|
|
if (typeof window === "undefined") return null;
|
|
const raw = window.localStorage.getItem(storageKey);
|
|
if (raw) try {
|
|
const parsed = JSON.parse(raw);
|
|
if (parsed && typeof parsed === "object") return parsed;
|
|
} catch {}
|
|
if (typeof document !== "undefined") {
|
|
const prefix = `${storageKey}=`;
|
|
const entry = document.cookie.split("; ").find((cookie) => cookie.startsWith(prefix));
|
|
if (entry) {
|
|
const legacyRaw = entry.substring(prefix.length);
|
|
try {
|
|
const parsed = JSON.parse(decodeURIComponent(legacyRaw));
|
|
if (parsed && typeof parsed === "object") return parsed;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
function saveInspectorState(storageKey, state) {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(storageKey, JSON.stringify(state));
|
|
} catch (error) {
|
|
console.warn("Failed to persist inspector state", error);
|
|
}
|
|
}
|
|
function isValidAnchor(value) {
|
|
if (!value || typeof value !== "object") return false;
|
|
const candidate = value;
|
|
return (candidate.horizontal === "left" || candidate.horizontal === "right") && (candidate.vertical === "top" || candidate.vertical === "bottom");
|
|
}
|
|
function isValidPosition(value) {
|
|
if (!value || typeof value !== "object") return false;
|
|
const candidate = value;
|
|
return isFiniteNumber(candidate.x) && isFiniteNumber(candidate.y);
|
|
}
|
|
function isValidSize(value) {
|
|
if (!value || typeof value !== "object") return false;
|
|
const candidate = value;
|
|
return isFiniteNumber(candidate.width) && isFiniteNumber(candidate.height);
|
|
}
|
|
function isFiniteNumber(value) {
|
|
return typeof value === "number" && Number.isFinite(value);
|
|
}
|
|
function isValidDockMode(value) {
|
|
return value === "floating" || value === "docked-left";
|
|
}
|
|
|
|
//#endregion
|
|
export { isValidAnchor, isValidDockMode, isValidPosition, isValidSize, loadInspectorState, saveInspectorState };
|
|
//# sourceMappingURL=persistence.mjs.map
|