appState.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import oc from "open-color";
  2. import { AppState, FlooredNumber } from "./types";
  3. import { getDateTime } from "./utils";
  4. import { t } from "./i18n";
  5. export const DEFAULT_FONT = "20px Virgil";
  6. export const DEFAULT_TEXT_ALIGN = "left";
  7. export function getDefaultAppState(): AppState {
  8. return {
  9. isLoading: false,
  10. errorMessage: null,
  11. draggingElement: null,
  12. resizingElement: null,
  13. multiElement: null,
  14. editingElement: null,
  15. elementType: "selection",
  16. elementLocked: false,
  17. exportBackground: true,
  18. shouldAddWatermark: false,
  19. currentItemStrokeColor: oc.black,
  20. currentItemBackgroundColor: "transparent",
  21. currentItemFillStyle: "hachure",
  22. currentItemStrokeWidth: 1,
  23. currentItemStrokeStyle: "solid",
  24. currentItemRoughness: 1,
  25. currentItemOpacity: 100,
  26. currentItemFont: DEFAULT_FONT,
  27. currentItemTextAlign: DEFAULT_TEXT_ALIGN,
  28. viewBackgroundColor: oc.white,
  29. scrollX: 0 as FlooredNumber,
  30. scrollY: 0 as FlooredNumber,
  31. cursorX: 0,
  32. cursorY: 0,
  33. cursorButton: "up",
  34. scrolledOutside: false,
  35. name: `${t("labels.untitled")}-${getDateTime()}`,
  36. username: "",
  37. isCollaborating: false,
  38. isResizing: false,
  39. isRotating: false,
  40. selectionElement: null,
  41. zoom: 1,
  42. openMenu: null,
  43. lastPointerDownWith: "mouse",
  44. selectedElementIds: {},
  45. collaborators: new Map(),
  46. shouldCacheIgnoreZoom: false,
  47. showShortcutsDialog: false,
  48. zenModeEnabled: false,
  49. };
  50. }
  51. export function clearAppStateForLocalStorage(appState: AppState) {
  52. const {
  53. draggingElement,
  54. resizingElement,
  55. multiElement,
  56. editingElement,
  57. selectionElement,
  58. isResizing,
  59. isRotating,
  60. collaborators,
  61. isCollaborating,
  62. isLoading,
  63. errorMessage,
  64. showShortcutsDialog,
  65. ...exportedState
  66. } = appState;
  67. return exportedState;
  68. }
  69. export function clearAppStatePropertiesForHistory(
  70. appState: AppState,
  71. ): Partial<AppState> {
  72. return {
  73. selectedElementIds: appState.selectedElementIds,
  74. exportBackground: appState.exportBackground,
  75. shouldAddWatermark: appState.shouldAddWatermark,
  76. currentItemStrokeColor: appState.currentItemStrokeColor,
  77. currentItemBackgroundColor: appState.currentItemBackgroundColor,
  78. currentItemFillStyle: appState.currentItemFillStyle,
  79. currentItemStrokeWidth: appState.currentItemStrokeWidth,
  80. currentItemRoughness: appState.currentItemRoughness,
  81. currentItemOpacity: appState.currentItemOpacity,
  82. currentItemFont: appState.currentItemFont,
  83. currentItemTextAlign: appState.currentItemTextAlign,
  84. viewBackgroundColor: appState.viewBackgroundColor,
  85. name: appState.name,
  86. };
  87. }
  88. export function cleanAppStateForExport(appState: AppState) {
  89. return {
  90. viewBackgroundColor: appState.viewBackgroundColor,
  91. };
  92. }