appState.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. currentItemRoughness: 1,
  24. currentItemOpacity: 100,
  25. currentItemFont: DEFAULT_FONT,
  26. currentItemTextAlign: DEFAULT_TEXT_ALIGN,
  27. viewBackgroundColor: oc.white,
  28. scrollX: 0 as FlooredNumber,
  29. scrollY: 0 as FlooredNumber,
  30. cursorX: 0,
  31. cursorY: 0,
  32. cursorButton: "up",
  33. scrolledOutside: false,
  34. name: `${t("labels.untitled")}-${getDateTime()}`,
  35. username: "",
  36. isCollaborating: false,
  37. isResizing: false,
  38. isRotating: false,
  39. selectionElement: null,
  40. zoom: 1,
  41. openMenu: null,
  42. lastPointerDownWith: "mouse",
  43. selectedElementIds: {},
  44. collaborators: new Map(),
  45. shouldCacheIgnoreZoom: false,
  46. showShortcutsDialog: false,
  47. zenModeEnabled: false,
  48. };
  49. }
  50. export function clearAppStateForLocalStorage(appState: AppState) {
  51. const {
  52. draggingElement,
  53. resizingElement,
  54. multiElement,
  55. editingElement,
  56. selectionElement,
  57. isResizing,
  58. isRotating,
  59. collaborators,
  60. isCollaborating,
  61. isLoading,
  62. errorMessage,
  63. showShortcutsDialog,
  64. ...exportedState
  65. } = appState;
  66. return exportedState;
  67. }
  68. export function clearAppStatePropertiesForHistory(
  69. appState: AppState,
  70. ): Partial<AppState> {
  71. return {
  72. selectedElementIds: appState.selectedElementIds,
  73. exportBackground: appState.exportBackground,
  74. shouldAddWatermark: appState.shouldAddWatermark,
  75. currentItemStrokeColor: appState.currentItemStrokeColor,
  76. currentItemBackgroundColor: appState.currentItemBackgroundColor,
  77. currentItemFillStyle: appState.currentItemFillStyle,
  78. currentItemStrokeWidth: appState.currentItemStrokeWidth,
  79. currentItemRoughness: appState.currentItemRoughness,
  80. currentItemOpacity: appState.currentItemOpacity,
  81. currentItemFont: appState.currentItemFont,
  82. currentItemTextAlign: appState.currentItemTextAlign,
  83. viewBackgroundColor: appState.viewBackgroundColor,
  84. name: appState.name,
  85. };
  86. }
  87. export function cleanAppStateForExport(appState: AppState) {
  88. return {
  89. viewBackgroundColor: appState.viewBackgroundColor,
  90. };
  91. }