diff options
Diffstat (limited to 'packages/excalidraw/data/EditorLocalStorage.ts')
| -rw-r--r-- | packages/excalidraw/data/EditorLocalStorage.ts | 51 |
1 files changed, 51 insertions, 0 deletions
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<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}`); + } + }; +} |
