localStorage.ts 3.4 KB

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