appState.ts 2.1 KB

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