appState.ts 2.6 KB

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