localStorage.ts 2.6 KB

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