-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uip-editor): store editor state
- Loading branch information
Showing
3 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
interface EditorStorageEntry { | ||
ts: string; | ||
data: string; | ||
} | ||
|
||
export class EditorStorage { | ||
public static readonly STORAGE_KEY = 'uip-editor-storage'; | ||
|
||
protected static get(): Record<string, any> { | ||
return JSON.parse(localStorage.getItem(EditorStorage.STORAGE_KEY) || '{}'); | ||
} | ||
|
||
protected static set(value: Record<string, any>): void { | ||
localStorage.setItem(EditorStorage.STORAGE_KEY, JSON.stringify(value)); | ||
} | ||
|
||
protected static serializeWithPathname(key: string): string { | ||
return JSON.stringify({path: location.pathname, key}); | ||
} | ||
|
||
public static save(key: string, value: string): void { | ||
const state = {[EditorStorage.serializeWithPathname(key)]: {ts: Date.now(), value}}; | ||
EditorStorage.set(Object.assign(EditorStorage.get(), state)); | ||
} | ||
|
||
public static load(key: string): string | null { | ||
const entry = EditorStorage.get()[EditorStorage.serializeWithPathname(key)] || {} as EditorStorageEntry; | ||
const expirationTime = 3600000 * 12; | ||
if (entry?.ts + expirationTime > Date.now()) return entry.value || null; | ||
EditorStorage.remove(key); | ||
return null; | ||
} | ||
|
||
public static remove(key: string): void { | ||
const data = EditorStorage.get(); | ||
delete data[key]; | ||
EditorStorage.set(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters