appState.ts 2.2 KB

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