2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { trackEvent } from "../analytics";
|
2021-06-13 21:11:07 +05:30
|
|
|
import { load, questionCircle, 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";
|
2021-04-08 19:54:50 +02:00
|
|
|
import { useIsMobile } from "../components/App";
|
2020-04-06 23:24:50 +01:00
|
|
|
import { KEYS } from "../keys";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { register } from "./register";
|
2021-05-25 21:37:14 +02:00
|
|
|
import { supported as fsSupported } from "browser-fs-access";
|
|
|
|
import { CheckboxItem } from "../components/CheckboxItem";
|
2021-05-30 15:31:12 +01:00
|
|
|
import { getExportSize } from "../scene/export";
|
|
|
|
import { DEFAULT_EXPORT_PADDING, EXPORT_SCALES } from "../constants";
|
|
|
|
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
|
|
|
import { getNonDeletedElements } from "../element";
|
2021-06-13 21:11:07 +05:30
|
|
|
import { ActiveFile } from "../components/ActiveFile";
|
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
|
|
|
/>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
export const actionChangeExportScale = register({
|
|
|
|
name: "changeExportScale",
|
|
|
|
perform: (_elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
appState: { ...appState, exportScale: value },
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ elements: allElements, appState, updateData }) => {
|
|
|
|
const elements = getNonDeletedElements(allElements);
|
|
|
|
const exportSelected = isSomeElementSelected(elements, appState);
|
|
|
|
const exportedElements = exportSelected
|
|
|
|
? getSelectedElements(elements, appState)
|
|
|
|
: elements;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{EXPORT_SCALES.map((s) => {
|
|
|
|
const [width, height] = getExportSize(
|
|
|
|
exportedElements,
|
|
|
|
DEFAULT_EXPORT_PADDING,
|
|
|
|
s,
|
|
|
|
);
|
|
|
|
|
|
|
|
const scaleButtonTitle = `${t(
|
|
|
|
"buttons.scale",
|
|
|
|
)} ${s}x (${width}x${height})`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ToolButton
|
|
|
|
key={s}
|
|
|
|
size="s"
|
|
|
|
type="radio"
|
|
|
|
icon={`${s}x`}
|
|
|
|
name="export-canvas-scale"
|
|
|
|
title={scaleButtonTitle}
|
|
|
|
aria-label={scaleButtonTitle}
|
|
|
|
id="export-canvas-scale"
|
|
|
|
checked={s === appState.exportScale}
|
|
|
|
onChange={() => updateData(s)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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 }) => (
|
2021-05-25 21:37:14 +02:00
|
|
|
<CheckboxItem
|
|
|
|
checked={appState.exportBackground}
|
|
|
|
onChange={(checked) => updateData(checked)}
|
|
|
|
>
|
2020-01-21 01:14:10 +02:00
|
|
|
{t("labels.withBackground")}
|
2021-05-25 21:37:14 +02:00
|
|
|
</CheckboxItem>
|
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 }) => (
|
2021-05-25 21:37:14 +02:00
|
|
|
<CheckboxItem
|
|
|
|
checked={appState.exportEmbedScene}
|
|
|
|
onChange={(checked) => updateData(checked)}
|
|
|
|
>
|
2020-10-13 14:47:07 +02:00
|
|
|
{t("labels.exportEmbedScene")}
|
2021-05-25 13:52:04 +02:00
|
|
|
<Tooltip label={t("labels.exportEmbedScene_details")} long={true}>
|
2021-05-25 21:37:14 +02:00
|
|
|
<div className="Tooltip-icon">{questionCircle}</div>
|
2020-12-14 18:54:54 +05:30
|
|
|
</Tooltip>
|
2021-05-25 21:37:14 +02:00
|
|
|
</CheckboxItem>
|
2020-10-13 14:47:07 +02:00
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2021-05-28 02:10:33 +05:30
|
|
|
export const actionSaveToActiveFile = register({
|
|
|
|
name: "saveToActiveFile",
|
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,
|
2021-06-13 21:11:07 +05:30
|
|
|
PanelComponent: ({ updateData, appState }) => (
|
|
|
|
<ActiveFile
|
|
|
|
onSave={() => updateData(null)}
|
|
|
|
fileName={appState.fileHandle?.name}
|
2020-06-12 18:35:04 +02:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2021-05-29 02:56:25 +05:30
|
|
|
export const actionSaveFileToDisk = register({
|
|
|
|
name: "saveFileToDisk",
|
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()}
|
2021-05-25 21:37:14 +02:00
|
|
|
hidden={!fsSupported}
|
2020-06-12 18:35:04 +02:00
|
|
|
onClick={() => updateData(null)}
|
2021-04-04 15:57:14 +05:30
|
|
|
data-testid="save-as-button"
|
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}
|
2021-04-04 15:57:14 +05:30
|
|
|
data-testid="load-button"
|
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>
|
|
|
|
),
|
|
|
|
});
|