2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2020-01-29 02:25:47 +02: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-01-12 02:22:03 +04:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionChangeProjectName = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "changeProjectName",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_elements, appState, value) => {
|
2020-03-19 14:51:05 +01:00
|
|
|
return { appState: { ...appState, name: value }, commitToHistory: false };
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
2020-01-29 02:25:47 +02:00
|
|
|
<ProjectName
|
2020-01-25 19:37:58 -03:00
|
|
|
label={t("labels.fileTitle")}
|
2020-01-15 20:42:02 +05:00
|
|
|
value={appState.name || "Unnamed"}
|
|
|
|
onChange={(name: string) => updateData(name)}
|
|
|
|
/>
|
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 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-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",
|
|
|
|
perform: (elements, appState, value) => {
|
2020-06-12 18:35:04 +02:00
|
|
|
saveAsJSON(elements, appState, (window as any).handle).catch((error) =>
|
|
|
|
console.error(error),
|
|
|
|
);
|
2020-03-19 14:51:05 +01:00
|
|
|
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",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
saveAsJSON(elements, appState, null).catch((error) => console.error(error));
|
|
|
|
return { commitToHistory: false };
|
|
|
|
},
|
|
|
|
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()}
|
|
|
|
hidden={!("chooseFileSystemEntries" in window)}
|
|
|
|
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-03-19 14:51:05 +01:00
|
|
|
commitToHistory: false,
|
|
|
|
};
|
2020-01-12 02:22:03 +04: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={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-01-17 11:25:05 +01:00
|
|
|
loadFromJSON()
|
|
|
|
.then(({ elements, appState }) => {
|
|
|
|
updateData({ elements: elements, appState: appState });
|
|
|
|
})
|
2020-04-03 12:50:51 +01:00
|
|
|
.catch((error) => {
|
2020-04-25 18:02:31 +02:00
|
|
|
// if user cancels, ignore the error
|
|
|
|
if (error.name === "AbortError") {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
});
|