mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Some checks failed
Auto release excalidraw next / Auto-release-excalidraw-next (push) Failing after 2m36s
Build Docker image / build-docker (push) Failing after 6s
Cancel previous runs / cancel (push) Failing after 1s
Publish Docker / publish-docker (push) Failing after 31s
New Sentry production release / sentry (push) Failing after 2m3s
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import type { EDITOR_LS_KEYS } from "@excalidraw/common";
|
|
|
|
import type { JSONValue } from "../types";
|
|
|
|
export class EditorLocalStorage {
|
|
static has(key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS]) {
|
|
try {
|
|
return !!window.localStorage.getItem(key);
|
|
} catch (error: any) {
|
|
console.warn(`localStorage.getItem error: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static get<T extends JSONValue>(
|
|
key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
|
|
) {
|
|
try {
|
|
const value = window.localStorage.getItem(key);
|
|
if (value) {
|
|
return JSON.parse(value) as T;
|
|
}
|
|
return null;
|
|
} catch (error: any) {
|
|
console.warn(`localStorage.getItem error: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static set = (
|
|
key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
|
|
value: JSONValue,
|
|
) => {
|
|
try {
|
|
window.localStorage.setItem(key, JSON.stringify(value));
|
|
return true;
|
|
} catch (error: any) {
|
|
console.warn(`localStorage.setItem error: ${error.message}`);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
static delete = (
|
|
name: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
|
|
) => {
|
|
try {
|
|
window.localStorage.removeItem(name);
|
|
} catch (error: any) {
|
|
console.warn(`localStorage.removeItem error: ${error.message}`);
|
|
}
|
|
};
|
|
}
|