actionExport.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from "react";
  2. import { Action } from "./types";
  3. import { ProjectName } from "../components/ProjectName";
  4. import { saveAsJSON, loadFromJSON } from "../scene";
  5. import { load, save } from "../components/icons";
  6. import { ToolButton } from "../components/ToolButton";
  7. import { t } from "../i18n";
  8. import useIsMobile from "../is-mobile";
  9. export const actionChangeProjectName: Action = {
  10. name: "changeProjectName",
  11. perform: (elements, appState, value) => {
  12. return { appState: { ...appState, name: value } };
  13. },
  14. PanelComponent: ({ appState, updateData }) => (
  15. <ProjectName
  16. label={t("labels.fileTitle")}
  17. value={appState.name || "Unnamed"}
  18. onChange={(name: string) => updateData(name)}
  19. />
  20. ),
  21. };
  22. export const actionChangeExportBackground: Action = {
  23. name: "changeExportBackground",
  24. perform: (elements, appState, value) => {
  25. return { appState: { ...appState, exportBackground: value } };
  26. },
  27. PanelComponent: ({ appState, updateData }) => (
  28. <label>
  29. <input
  30. type="checkbox"
  31. checked={appState.exportBackground}
  32. onChange={e => {
  33. updateData(e.target.checked);
  34. }}
  35. />{" "}
  36. {t("labels.withBackground")}
  37. </label>
  38. ),
  39. };
  40. export const actionSaveScene: Action = {
  41. name: "saveScene",
  42. perform: (elements, appState, value) => {
  43. saveAsJSON(elements, appState).catch(err => console.error(err));
  44. return {};
  45. },
  46. PanelComponent: ({ updateData }) => (
  47. <ToolButton
  48. type="button"
  49. icon={save}
  50. title={t("buttons.save")}
  51. aria-label={t("buttons.save")}
  52. showAriaLabel={useIsMobile()}
  53. onClick={() => updateData(null)}
  54. />
  55. ),
  56. };
  57. export const actionLoadScene: Action = {
  58. name: "loadScene",
  59. perform: (
  60. elements,
  61. appState,
  62. { elements: loadedElements, appState: loadedAppState },
  63. ) => {
  64. return { elements: loadedElements, appState: loadedAppState };
  65. },
  66. PanelComponent: ({ updateData }) => (
  67. <ToolButton
  68. type="button"
  69. icon={load}
  70. title={t("buttons.load")}
  71. aria-label={t("buttons.load")}
  72. showAriaLabel={useIsMobile()}
  73. onClick={() => {
  74. loadFromJSON()
  75. .then(({ elements, appState }) => {
  76. updateData({ elements: elements, appState: appState });
  77. })
  78. .catch(err => console.error(err));
  79. }}
  80. />
  81. ),
  82. };