localStorage.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. clearAppStateForLocalStorage,
  3. getDefaultAppState,
  4. } from "@excalidraw/excalidraw/appState";
  5. import type { ExcalidrawElement } from "@excalidraw/element/types";
  6. import type { AppState } from "@excalidraw/excalidraw/types";
  7. import { STORAGE_KEYS } from "../app_constants";
  8. export const saveUsernameToLocalStorage = (username: string) => {
  9. try {
  10. localStorage.setItem(
  11. STORAGE_KEYS.LOCAL_STORAGE_COLLAB,
  12. JSON.stringify({ username }),
  13. );
  14. } catch (error: any) {
  15. // Unable to access window.localStorage
  16. console.error(error);
  17. }
  18. };
  19. export const importUsernameFromLocalStorage = (): string | null => {
  20. try {
  21. const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
  22. if (data) {
  23. return JSON.parse(data).username;
  24. }
  25. } catch (error: any) {
  26. // Unable to access localStorage
  27. console.error(error);
  28. }
  29. return null;
  30. };
  31. export const importFromLocalStorage = () => {
  32. let savedElements = null;
  33. let savedState = null;
  34. try {
  35. savedElements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
  36. savedState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
  37. } catch (error: any) {
  38. // Unable to access localStorage
  39. console.error(error);
  40. }
  41. let elements: ExcalidrawElement[] = [];
  42. if (savedElements) {
  43. try {
  44. elements = JSON.parse(savedElements);
  45. } catch (error: any) {
  46. console.error(error);
  47. // Do nothing because elements array is already empty
  48. }
  49. }
  50. let appState = null;
  51. if (savedState) {
  52. try {
  53. appState = {
  54. ...getDefaultAppState(),
  55. ...clearAppStateForLocalStorage(
  56. JSON.parse(savedState) as Partial<AppState>,
  57. ),
  58. };
  59. } catch (error: any) {
  60. console.error(error);
  61. // Do nothing because appState is already null
  62. }
  63. }
  64. return { elements, appState };
  65. };
  66. export const getElementsStorageSize = () => {
  67. try {
  68. const elements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
  69. const elementsSize = elements?.length || 0;
  70. return elementsSize;
  71. } catch (error: any) {
  72. console.error(error);
  73. return 0;
  74. }
  75. };
  76. export const getTotalStorageSize = () => {
  77. try {
  78. const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
  79. const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
  80. const appStateSize = appState?.length || 0;
  81. const collabSize = collab?.length || 0;
  82. return appStateSize + collabSize + getElementsStorageSize();
  83. } catch (error: any) {
  84. console.error(error);
  85. return 0;
  86. }
  87. };