2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { trackEvent } from "../analytics";
|
|
|
|
import { load, questionCircle, save, saveAs } from "../components/icons";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { ProjectName } from "../components/ProjectName";
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "../components/ToolButton";
|
2021-01-05 20:06:14 +02:00
|
|
|
import "../components/ToolIcon.scss";
|
2020-12-14 18:54:54 +05:30
|
|
|
import { Tooltip } from "../components/Tooltip";
|
2021-02-24 19:52:17 +05:30
|
|
|
import { DarkModeToggle, Appearence } from "../components/DarkModeToggle";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { loadFromJSON, saveAsJSON } from "../data";
|
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-04-06 23:24:50 +01:00
|
|
|
import { KEYS } from "../keys";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { register } from "./register";
|
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) => {
|
2021-01-05 20:06:14 +02:00
|
|
|
trackEvent("change", "title");
|
2020-12-01 14:00:13 +01:00
|
|
|
return { appState: { ...appState, name: value }, commitToHistory: false };
|
|
|
|
},
|
2021-03-20 16:08:03 +05:30
|
|
|
PanelComponent: ({ appState, updateData, appProps }) => (
|
2020-12-01 14:00:13 +01:00
|
|
|
<ProjectName
|
|
|
|
label={t("labels.fileTitle")}
|
|
|
|
value={appState.name || "Unnamed"}
|
|
|
|
onChange={(name: string) => updateData(name)}
|
2021-03-20 21:57:58 +05:30
|
|
|
isNameEditable={
|
|
|
|
typeof appProps.name === "undefined" && !appState.viewModeEnabled
|
|
|
|
}
|
2020-12-01 14:00:13 +01: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-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 }) => (
|
2020-12-14 18:54:54 +05:30
|
|
|
<label style={{ display: "flex" }}>
|
2020-10-13 14:47:07 +02:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={appState.exportEmbedScene}
|
|
|
|
onChange={(event) => updateData(event.target.checked)}
|
|
|
|
/>{" "}
|
|
|
|
{t("labels.exportEmbedScene")}
|
2020-12-14 18:54:54 +05:30
|
|
|
<Tooltip
|
|
|
|
label={t("labels.exportEmbedScene_details")}
|
|
|
|
position="above"
|
|
|
|
long={true}
|
|
|
|
>
|
|
|
|
<div className="TooltipIcon">{questionCircle}</div>
|
|
|
|
</Tooltip>
|
2020-10-13 14:47:07 +02:00
|
|
|
</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) => {
|
2021-02-07 22:01:22 +01:00
|
|
|
const fileHandleExists = !!appState.fileHandle;
|
2020-10-19 10:53:37 +02:00
|
|
|
try {
|
|
|
|
const { fileHandle } = await saveAsJSON(elements, appState);
|
2021-02-07 22:01:22 +01:00
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
fileHandle,
|
|
|
|
toastMessage: fileHandleExists
|
|
|
|
? fileHandle.name
|
|
|
|
? t("toast.fileSavedToFilename").replace(
|
|
|
|
"{filename}",
|
|
|
|
`"${fileHandle.name}"`,
|
|
|
|
)
|
|
|
|
: t("toast.fileSaved")
|
|
|
|
: null,
|
|
|
|
},
|
|
|
|
};
|
2020-10-19 10:53:37 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (error?.name !== "AbortError") {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return { commitToHistory: false };
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
2020-12-01 23:36:06 +02:00
|
|
|
keyTest: (event) =>
|
|
|
|
event.key === KEYS.S && event[KEYS.CTRL_OR_CMD] && !event.shiftKey,
|
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
|
|
|
},
|
2020-12-01 23:36:06 +02:00
|
|
|
keyTest: (event) =>
|
|
|
|
event.key === KEYS.S && event.shiftKey && event[KEYS.CTRL_OR_CMD],
|
2020-06-12 18:35:04 +02:00
|
|
|
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",
|
2021-01-06 13:36:55 +01:00
|
|
|
perform: async (elements, appState) => {
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
elements: loadedElements,
|
|
|
|
appState: loadedAppState,
|
|
|
|
} = await loadFromJSON(appState);
|
|
|
|
return {
|
|
|
|
elements: loadedElements,
|
|
|
|
appState: loadedAppState,
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
if (error?.name === "AbortError") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
elements,
|
|
|
|
appState: { ...appState, errorMessage: error.message },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === KEYS.O,
|
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()}
|
2021-01-06 13:36:55 +01:00
|
|
|
onClick={updateData}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2021-02-24 19:52:17 +05:30
|
|
|
|
|
|
|
export const actionExportWithDarkMode = register({
|
|
|
|
name: "exportWithDarkMode",
|
|
|
|
perform: (_elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
appState: { ...appState, exportWithDarkMode: value },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
justifyContent: "flex-end",
|
|
|
|
marginTop: "-45px",
|
|
|
|
marginBottom: "10px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<DarkModeToggle
|
|
|
|
value={appState.exportWithDarkMode ? "dark" : "light"}
|
2021-03-13 18:58:06 +05:30
|
|
|
onChange={(theme: Appearence) => {
|
|
|
|
updateData(theme === "dark");
|
2021-02-24 19:52:17 +05:30
|
|
|
}}
|
|
|
|
title={t("labels.toggleExportColorScheme")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
});
|