appState.ts 2.6 KB

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