2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2020-12-01 14:00:13 +01:00
|
|
|
import { ProjectName } from "../components/ProjectName";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { saveAsJSON, loadFromJSON } from "../data";
|
2020-06-12 18:35:04 +02:00
|
|
|
import { load, save, saveAs } from "../components/icons";
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "../components/ToolButton";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
2020-02-20 18:44:38 -05:00
|
|
|
import useIsMobile from "../is-mobile";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { register } from "./register";
|
2020-04-06 23:24:50 +01:00
|
|
|
import { KEYS } from "../keys";
|
2020-07-27 15:29:19 +03:00
|
|
|
import { muteFSAbortError } from "../utils";
|
2020-07-17 11:34:21 +02:00
|
|
|
|
2020-12-01 14:00:13 +01:00
|
|
|
export const actionChangeProjectName = register({
|
|
|
|
name: "changeProjectName",
|
|
|
|
perform: (_elements, appState, value) => {
|
|
|
|
return { appState: { ...appState, name: value }, commitToHistory: false };
|
|
|
|
},
|
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
|
|
|
<ProjectName
|
|
|
|
label={t("labels.fileTitle")}
|
|
|
|
value={appState.name || "Unnamed"}
|
|
|
|
onChange={(name: string) => updateData(name)}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionChangeExportBackground = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "changeExportBackground",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_elements, appState, value) => {
|
2020-03-19 14:51:05 +01:00
|
|
|
return {
|
|
|
|
appState: { ...appState, exportBackground: value },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
2020-01-12 02:22:03 +04:00
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={appState.exportBackground}
|
2020-03-23 13:05:07 +02:00
|
|
|
onChange={(event) => updateData(event.target.checked)}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>{" "}
|
2020-01-21 01:14:10 +02:00
|
|
|
{t("labels.withBackground")}
|
2020-01-12 02:22:03 +04:00
|
|
|
</label>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-10-13 14:47:07 +02:00
|
|
|
export const actionChangeExportEmbedScene = register({
|
|
|
|
name: "changeExportEmbedScene",
|
|
|
|
perform: (_elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
appState: { ...appState, exportEmbedScene: value },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
|
|
|
<label title={t("labels.exportEmbedScene_details")}>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={appState.exportEmbedScene}
|
|
|
|
onChange={(event) => updateData(event.target.checked)}
|
|
|
|
/>{" "}
|
|
|
|
{t("labels.exportEmbedScene")}
|
|
|
|
</label>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2020-04-19 20:50:23 +01:00
|
|
|
export const actionChangeShouldAddWatermark = register({
|
|
|
|
name: "changeShouldAddWatermark",
|
|
|
|
perform: (_elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
appState: { ...appState, shouldAddWatermark: value },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={appState.shouldAddWatermark}
|
|
|
|
onChange={(event) => updateData(event.target.checked)}
|
|
|
|
/>{" "}
|
|
|
|
{t("labels.addWatermark")}
|
|
|
|
</label>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionSaveScene = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "saveScene",
|
2020-10-19 10:53:37 +02:00
|
|
|
perform: async (elements, appState, value) => {
|
|
|
|
try {
|
|
|
|
const { fileHandle } = await saveAsJSON(elements, appState);
|
|
|
|
return { commitToHistory: false, appState: { ...appState, fileHandle } };
|
|
|
|
} catch (error) {
|
|
|
|
if (error?.name !== "AbortError") {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return { commitToHistory: false };
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
2020-04-06 23:24:50 +01:00
|
|
|
keyTest: (event) => {
|
2020-06-12 18:35:04 +02:00
|
|
|
return event.key === "s" && event[KEYS.CTRL_OR_CMD] && !event.shiftKey;
|
2020-04-06 23:24:50 +01:00
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<ToolButton
|
2020-01-15 20:42:02 +05:00
|
|
|
type="button"
|
|
|
|
icon={save}
|
2020-01-21 01:14:10 +02:00
|
|
|
title={t("buttons.save")}
|
|
|
|
aria-label={t("buttons.save")}
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel={useIsMobile()}
|
2020-01-15 20:42:02 +05:00
|
|
|
onClick={() => updateData(null)}
|
2020-06-12 18:35:04 +02:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionSaveAsScene = register({
|
|
|
|
name: "saveAsScene",
|
2020-10-19 10:53:37 +02:00
|
|
|
perform: async (elements, appState, value) => {
|
|
|
|
try {
|
|
|
|
const { fileHandle } = await saveAsJSON(elements, {
|
|
|
|
...appState,
|
|
|
|
fileHandle: null,
|
|
|
|
});
|
|
|
|
return { commitToHistory: false, appState: { ...appState, fileHandle } };
|
|
|
|
} catch (error) {
|
|
|
|
if (error?.name !== "AbortError") {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return { commitToHistory: false };
|
|
|
|
}
|
2020-06-12 18:35:04 +02:00
|
|
|
},
|
|
|
|
keyTest: (event) => {
|
|
|
|
return event.key === "s" && event.shiftKey && event[KEYS.CTRL_OR_CMD];
|
|
|
|
},
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={saveAs}
|
|
|
|
title={t("buttons.saveAs")}
|
|
|
|
aria-label={t("buttons.saveAs")}
|
|
|
|
showAriaLabel={useIsMobile()}
|
2020-07-25 15:34:51 +02:00
|
|
|
hidden={
|
|
|
|
!("chooseFileSystemEntries" in window || "showOpenFilePicker" in window)
|
|
|
|
}
|
2020-06-12 18:35:04 +02:00
|
|
|
onClick={() => updateData(null)}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionLoadScene = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "loadScene",
|
2020-01-15 21:08:52 -05:00
|
|
|
perform: (
|
|
|
|
elements,
|
|
|
|
appState,
|
2020-04-03 12:50:51 +01:00
|
|
|
{ elements: loadedElements, appState: loadedAppState, error },
|
2020-01-15 21:08:52 -05:00
|
|
|
) => {
|
2020-03-19 14:51:05 +01:00
|
|
|
return {
|
|
|
|
elements: loadedElements,
|
2020-04-03 12:50:51 +01:00
|
|
|
appState: {
|
|
|
|
...loadedAppState,
|
|
|
|
errorMessage: error,
|
|
|
|
},
|
2020-09-09 21:08:06 +02:00
|
|
|
commitToHistory: true,
|
2020-03-19 14:51:05 +01:00
|
|
|
};
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
2020-07-27 17:18:49 +05:30
|
|
|
PanelComponent: ({ updateData, appState }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<ToolButton
|
2020-01-15 20:42:02 +05:00
|
|
|
type="button"
|
|
|
|
icon={load}
|
2020-01-21 01:14:10 +02:00
|
|
|
title={t("buttons.load")}
|
|
|
|
aria-label={t("buttons.load")}
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel={useIsMobile()}
|
2020-01-12 02:22:03 +04:00
|
|
|
onClick={() => {
|
2020-07-27 17:18:49 +05:30
|
|
|
loadFromJSON(appState)
|
2020-01-17 11:25:05 +01:00
|
|
|
.then(({ elements, appState }) => {
|
2020-11-29 18:32:51 +02:00
|
|
|
updateData({ elements, appState });
|
2020-01-17 11:25:05 +01:00
|
|
|
})
|
2020-07-17 11:34:21 +02:00
|
|
|
.catch(muteFSAbortError)
|
2020-04-03 12:50:51 +01:00
|
|
|
.catch((error) => {
|
2020-04-10 10:58:09 +01:00
|
|
|
updateData({ error: error.message });
|
2020-04-03 12:50:51 +01:00
|
|
|
});
|
2020-01-12 02:22:03 +04:00
|
|
|
}}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|