You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.1 KiB
TypeScript
116 lines
3.1 KiB
TypeScript
1 year ago
|
import { ExcalidrawElement } from "../../src/element/types";
|
||
|
import { AppState } from "../../src/types";
|
||
4 years ago
|
import {
|
||
|
clearAppStateForLocalStorage,
|
||
|
getDefaultAppState,
|
||
1 year ago
|
} from "../../src/appState";
|
||
|
import { clearElementsForLocalStorage } from "../../src/element";
|
||
3 years ago
|
import { STORAGE_KEYS } from "../app_constants";
|
||
1 year ago
|
import { ImportedDataState } from "../../src/data/types";
|
||
5 years ago
|
|
||
5 years ago
|
export const saveUsernameToLocalStorage = (username: string) => {
|
||
5 years ago
|
try {
|
||
|
localStorage.setItem(
|
||
4 years ago
|
STORAGE_KEYS.LOCAL_STORAGE_COLLAB,
|
||
5 years ago
|
JSON.stringify({ username }),
|
||
|
);
|
||
3 years ago
|
} catch (error: any) {
|
||
5 years ago
|
// Unable to access window.localStorage
|
||
|
console.error(error);
|
||
|
}
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
export const importUsernameFromLocalStorage = (): string | null => {
|
||
5 years ago
|
try {
|
||
4 years ago
|
const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
|
||
5 years ago
|
if (data) {
|
||
|
return JSON.parse(data).username;
|
||
|
}
|
||
3 years ago
|
} catch (error: any) {
|
||
5 years ago
|
// Unable to access localStorage
|
||
|
console.error(error);
|
||
|
}
|
||
|
|
||
|
return null;
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
export const importFromLocalStorage = () => {
|
||
5 years ago
|
let savedElements = null;
|
||
|
let savedState = null;
|
||
|
|
||
|
try {
|
||
4 years ago
|
savedElements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
|
||
|
savedState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
|
||
3 years ago
|
} catch (error: any) {
|
||
5 years ago
|
// Unable to access localStorage
|
||
|
console.error(error);
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
let elements: ExcalidrawElement[] = [];
|
||
5 years ago
|
if (savedElements) {
|
||
|
try {
|
||
4 years ago
|
elements = clearElementsForLocalStorage(JSON.parse(savedElements));
|
||
3 years ago
|
} catch (error: any) {
|
||
5 years ago
|
console.error(error);
|
||
5 years ago
|
// Do nothing because elements array is already empty
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let appState = null;
|
||
|
if (savedState) {
|
||
|
try {
|
||
5 years ago
|
appState = {
|
||
|
...getDefaultAppState(),
|
||
|
...clearAppStateForLocalStorage(
|
||
|
JSON.parse(savedState) as Partial<AppState>,
|
||
|
),
|
||
|
};
|
||
3 years ago
|
} catch (error: any) {
|
||
5 years ago
|
console.error(error);
|
||
5 years ago
|
// Do nothing because appState is already null
|
||
|
}
|
||
|
}
|
||
5 years ago
|
return { elements, appState };
|
||
5 years ago
|
};
|
||
4 years ago
|
|
||
4 years ago
|
export const getElementsStorageSize = () => {
|
||
4 years ago
|
try {
|
||
|
const elements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
|
||
4 years ago
|
const elementsSize = elements?.length || 0;
|
||
4 years ago
|
return elementsSize;
|
||
3 years ago
|
} catch (error: any) {
|
||
4 years ago
|
console.error(error);
|
||
|
return 0;
|
||
|
}
|
||
4 years ago
|
};
|
||
|
|
||
4 years ago
|
export const getTotalStorageSize = () => {
|
||
4 years ago
|
try {
|
||
|
const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
|
||
|
const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
|
||
3 years ago
|
const library = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
|
||
4 years ago
|
|
||
4 years ago
|
const appStateSize = appState?.length || 0;
|
||
|
const collabSize = collab?.length || 0;
|
||
|
const librarySize = library?.length || 0;
|
||
4 years ago
|
|
||
4 years ago
|
return appStateSize + collabSize + librarySize + getElementsStorageSize();
|
||
3 years ago
|
} catch (error: any) {
|
||
4 years ago
|
console.error(error);
|
||
|
return 0;
|
||
|
}
|
||
4 years ago
|
};
|
||
3 years ago
|
|
||
|
export const getLibraryItemsFromStorage = () => {
|
||
|
try {
|
||
3 years ago
|
const libraryItems: ImportedDataState["libraryItems"] = JSON.parse(
|
||
|
localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY) as string,
|
||
|
);
|
||
3 years ago
|
|
||
3 years ago
|
return libraryItems || [];
|
||
|
} catch (error) {
|
||
|
console.error(error);
|
||
3 years ago
|
return [];
|
||
|
}
|
||
|
};
|