From 6ec259a0e71174651bae95d4628138bf6fd68742 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:35 -0400 Subject: refactor: packages/ --- packages/excalidraw/data/EditorLocalStorage.ts | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/excalidraw/data/EditorLocalStorage.ts (limited to 'packages/excalidraw/data/EditorLocalStorage.ts') diff --git a/packages/excalidraw/data/EditorLocalStorage.ts b/packages/excalidraw/data/EditorLocalStorage.ts new file mode 100644 index 0000000..bb6eeb4 --- /dev/null +++ b/packages/excalidraw/data/EditorLocalStorage.ts @@ -0,0 +1,51 @@ +import type { EDITOR_LS_KEYS } from "../constants"; +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( + 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}`); + } + }; +} -- cgit v1.2.3