appState.ts 1.9 KB

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