localStorage.ts 3.2 KB

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