2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
|
|
|
import { Action } from "./types";
|
|
|
|
import { ColorPicker } from "../components/ColorPicker";
|
2020-01-11 20:34:21 -08:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-02-22 21:24:34 +02:00
|
|
|
import { trash, zoomIn, zoomOut, resetZoom } 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-16 14:38:53 +01:00
|
|
|
import { getNormalizedZoom } from "../scene";
|
2020-02-16 17:00:45 +01:00
|
|
|
import { KEYS } from "../keys";
|
2020-02-20 18:44:38 -05:00
|
|
|
import useIsMobile from "../is-mobile";
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
export const actionChangeViewBackgroundColor: Action = {
|
|
|
|
name: "changeViewBackgroundColor",
|
2020-02-05 22:47:10 +04:00
|
|
|
perform: (_, appState, value) => {
|
2020-01-12 02:22:03 +04:00
|
|
|
return { appState: { ...appState, viewBackgroundColor: value } };
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ appState, updateData }) => {
|
2020-01-15 20:42:02 +05:00
|
|
|
return (
|
|
|
|
<div style={{ position: "relative" }}>
|
|
|
|
<ColorPicker
|
2020-01-25 19:37:58 -03:00
|
|
|
label={t("labels.canvasBackground")}
|
2020-01-15 20:42:02 +05:00
|
|
|
type="canvasBackground"
|
|
|
|
color={appState.viewBackgroundColor}
|
|
|
|
onChange={color => updateData(color)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2020-01-24 12:04:54 +02:00
|
|
|
},
|
2020-02-05 22:47:10 +04:00
|
|
|
commitToHistory: () => true,
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionClearCanvas: Action = {
|
|
|
|
name: "clearCanvas",
|
2020-02-05 22:47:10 +04:00
|
|
|
commitToHistory: () => true,
|
2020-01-15 20:42:02 +05:00
|
|
|
perform: () => {
|
2020-01-12 02:22:03 +04:00
|
|
|
return {
|
|
|
|
elements: [],
|
2020-01-24 12:04:54 +02:00
|
|
|
appState: getDefaultAppState(),
|
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-12 02:22:03 +04:00
|
|
|
type="button"
|
2020-01-15 20:42:02 +05:00
|
|
|
icon={trash}
|
2020-01-21 01:14:10 +02:00
|
|
|
title={t("buttons.clearReset")}
|
|
|
|
aria-label={t("buttons.clearReset")}
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel={useIsMobile()}
|
2020-01-12 02:22:03 +04:00
|
|
|
onClick={() => {
|
2020-01-21 01:14:10 +02:00
|
|
|
if (window.confirm(t("alerts.clearReset"))) {
|
2020-01-17 11:25:05 +01:00
|
|
|
// TODO: Defined globally, since file handles aren't yet serializable.
|
|
|
|
// Once `FileSystemFileHandle` can be serialized, make this
|
|
|
|
// part of `AppState`.
|
|
|
|
(window as any).handle = null;
|
2020-01-12 02:22:03 +04:00
|
|
|
updateData(null);
|
|
|
|
}
|
|
|
|
}}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
const ZOOM_STEP = 0.1;
|
|
|
|
|
2020-02-16 17:00:45 +01:00
|
|
|
const KEY_CODES = {
|
|
|
|
MINUS: "Minus",
|
|
|
|
EQUAL: "Equal",
|
|
|
|
ZERO: "Digit0",
|
|
|
|
NUM_SUBTRACT: "NumpadSubtract",
|
|
|
|
NUM_ADD: "NumpadAdd",
|
|
|
|
NUM_ZERO: "Numpad0",
|
|
|
|
};
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
export const actionZoomIn: Action = {
|
|
|
|
name: "zoomIn",
|
|
|
|
perform: (elements, appState) => {
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
zoom: getNormalizedZoom(appState.zoom + ZOOM_STEP),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={zoomIn}
|
|
|
|
title={t("buttons.zoomIn")}
|
|
|
|
aria-label={t("buttons.zoomIn")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
2020-02-16 17:00:45 +01:00
|
|
|
keyTest: event =>
|
|
|
|
(event.code === KEY_CODES.EQUAL || event.code === KEY_CODES.NUM_ADD) &&
|
|
|
|
(event[KEYS.META] || event.shiftKey),
|
2020-02-15 21:03:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionZoomOut: Action = {
|
|
|
|
name: "zoomOut",
|
|
|
|
perform: (elements, appState) => {
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
zoom: getNormalizedZoom(appState.zoom - ZOOM_STEP),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={zoomOut}
|
|
|
|
title={t("buttons.zoomOut")}
|
|
|
|
aria-label={t("buttons.zoomOut")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
2020-02-16 17:00:45 +01:00
|
|
|
keyTest: event =>
|
|
|
|
(event.code === KEY_CODES.MINUS || event.code === KEY_CODES.NUM_SUBTRACT) &&
|
|
|
|
(event[KEYS.META] || event.shiftKey),
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actionResetZoom: Action = {
|
|
|
|
name: "resetZoom",
|
|
|
|
perform: (elements, appState) => {
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
zoom: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2020-02-22 21:24:34 +02:00
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={resetZoom}
|
|
|
|
title={t("buttons.resetZoom")}
|
|
|
|
aria-label={t("buttons.resetZoom")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
2020-02-16 17:00:45 +01:00
|
|
|
keyTest: event =>
|
|
|
|
(event.code === KEY_CODES.ZERO || event.code === KEY_CODES.NUM_ZERO) &&
|
|
|
|
(event[KEYS.META] || event.shiftKey),
|
2020-02-15 21:03:32 +01:00
|
|
|
};
|