2020-12-27 18:26:30 +02:00
|
|
|
import { ColorPicker } from "../components/ColorPicker";
|
2021-10-21 17:35:28 +05:30
|
|
|
import { zoomIn, zoomOut } from "../components/icons";
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "../components/ToolButton";
|
2021-05-08 15:17:30 +05:30
|
|
|
import { DarkModeToggle } from "../components/DarkModeToggle";
|
2021-10-14 14:15:57 +05:30
|
|
|
import { THEME, ZOOM_STEP } from "../constants";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { getCommonBounds, getNonDeletedElements } from "../element";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
2020-12-01 23:36:06 +02:00
|
|
|
import { CODES, KEYS } from "../keys";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { getNormalizedZoom, getSelectedElements } from "../scene";
|
|
|
|
import { centerScrollOn } from "../scene/scroll";
|
2022-01-29 21:12:44 +01:00
|
|
|
import { getStateForZoom } from "../scene/zoom";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { AppState, NormalizedZoomValue } from "../types";
|
2020-03-09 15:06:35 +02:00
|
|
|
import { getShortcutKey } from "../utils";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { register } from "./register";
|
2021-07-15 18:48:03 +02:00
|
|
|
import { Tooltip } from "../components/Tooltip";
|
2021-10-21 17:35:28 +05:30
|
|
|
import { newElementWith } from "../element/mutateElement";
|
|
|
|
import { getDefaultAppState } from "../appState";
|
|
|
|
import ClearCanvas from "../components/ClearCanvas";
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionChangeViewBackgroundColor = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "changeViewBackgroundColor",
|
2020-02-05 22:47:10 +04:00
|
|
|
perform: (_, appState, value) => {
|
2020-03-19 14:51:05 +01:00
|
|
|
return {
|
2021-05-28 17:22:42 +05:30
|
|
|
appState: { ...appState, ...value },
|
|
|
|
commitToHistory: !!value.viewBackgroundColor,
|
2020-03-19 14:51:05 +01:00
|
|
|
};
|
2020-01-12 02:22:03 +04:00
|
|
|
},
|
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}
|
2021-05-28 17:22:42 +05:30
|
|
|
onChange={(color) => updateData({ viewBackgroundColor: color })}
|
2021-05-30 12:05:07 +02:00
|
|
|
isActive={appState.openPopup === "canvasColorPicker"}
|
2021-05-28 17:22:42 +05:30
|
|
|
setActive={(active) =>
|
2021-05-30 12:05:07 +02:00
|
|
|
updateData({ openPopup: active ? "canvasColorPicker" : null })
|
2021-05-28 17:22:42 +05:30
|
|
|
}
|
2021-04-04 15:57:14 +05:30
|
|
|
data-testid="canvas-background-picker"
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
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 actionClearCanvas = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "clearCanvas",
|
2021-10-21 22:05:48 +02:00
|
|
|
perform: (elements, appState, _, app) => {
|
|
|
|
app.imageCache.clear();
|
2020-01-12 02:22:03 +04:00
|
|
|
return {
|
2020-03-23 13:05:07 +02:00
|
|
|
elements: elements.map((element) =>
|
2020-03-14 20:46:57 -07:00
|
|
|
newElementWith(element, { isDeleted: true }),
|
|
|
|
),
|
2020-04-07 14:02:42 +01:00
|
|
|
appState: {
|
|
|
|
...getDefaultAppState(),
|
2021-10-21 22:05:48 +02:00
|
|
|
files: {},
|
2021-03-13 18:58:06 +05:30
|
|
|
theme: appState.theme,
|
2020-11-08 17:10:20 +02:00
|
|
|
elementLocked: appState.elementLocked,
|
2022-02-02 14:31:38 +01:00
|
|
|
penMode: appState.penMode,
|
|
|
|
penDetected: appState.penDetected,
|
2020-11-08 17:10:20 +02:00
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
exportEmbedScene: appState.exportEmbedScene,
|
|
|
|
gridSize: appState.gridSize,
|
2020-12-07 18:35:16 +02:00
|
|
|
showStats: appState.showStats,
|
2020-12-27 18:26:30 +02:00
|
|
|
pasteDialog: appState.pasteDialog,
|
2020-04-07 14:02:42 +01:00
|
|
|
},
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: true,
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2021-10-21 17:35:28 +05:30
|
|
|
|
|
|
|
PanelComponent: ({ updateData }) => <ClearCanvas onConfirm={updateData} />,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionZoomIn = register({
|
2020-02-15 21:03:32 +01:00
|
|
|
name: "zoomIn",
|
2022-01-29 21:12:44 +01:00
|
|
|
perform: (_elements, appState, _, app) => {
|
2020-02-15 21:03:32 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2022-01-29 21:12:44 +01:00
|
|
|
...getStateForZoom(
|
|
|
|
{
|
|
|
|
viewportX: appState.width / 2 + appState.offsetLeft,
|
|
|
|
viewportY: appState.height / 2 + appState.offsetTop,
|
|
|
|
nextZoom: getNormalizedZoom(appState.zoom.value + ZOOM_STEP),
|
|
|
|
},
|
|
|
|
appState,
|
|
|
|
),
|
2020-02-15 21:03:32 +01:00
|
|
|
},
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: false,
|
2020-02-15 21:03:32 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={zoomIn}
|
2020-04-07 14:39:06 +03:00
|
|
|
title={`${t("buttons.zoomIn")} — ${getShortcutKey("CtrlOrCmd++")}`}
|
2020-02-15 21:03:32 +01:00
|
|
|
aria-label={t("buttons.zoomIn")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
2021-07-15 18:48:03 +02:00
|
|
|
size="small"
|
2020-02-15 21:03:32 +01:00
|
|
|
/>
|
|
|
|
),
|
2020-03-23 13:05:07 +02:00
|
|
|
keyTest: (event) =>
|
2020-12-01 23:36:06 +02:00
|
|
|
(event.code === CODES.EQUAL || event.code === CODES.NUM_ADD) &&
|
2020-03-09 15:06:35 +02:00
|
|
|
(event[KEYS.CTRL_OR_CMD] || event.shiftKey),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionZoomOut = register({
|
2020-02-15 21:03:32 +01:00
|
|
|
name: "zoomOut",
|
2022-01-29 21:12:44 +01:00
|
|
|
perform: (_elements, appState, _, app) => {
|
2020-02-15 21:03:32 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2022-01-29 21:12:44 +01:00
|
|
|
...getStateForZoom(
|
|
|
|
{
|
|
|
|
viewportX: appState.width / 2 + appState.offsetLeft,
|
|
|
|
viewportY: appState.height / 2 + appState.offsetTop,
|
|
|
|
nextZoom: getNormalizedZoom(appState.zoom.value - ZOOM_STEP),
|
|
|
|
},
|
|
|
|
appState,
|
|
|
|
),
|
2020-02-15 21:03:32 +01:00
|
|
|
},
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: false,
|
2020-02-15 21:03:32 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={zoomOut}
|
2020-04-07 14:39:06 +03:00
|
|
|
title={`${t("buttons.zoomOut")} — ${getShortcutKey("CtrlOrCmd+-")}`}
|
2020-02-15 21:03:32 +01:00
|
|
|
aria-label={t("buttons.zoomOut")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
2021-07-15 18:48:03 +02:00
|
|
|
size="small"
|
2020-02-15 21:03:32 +01:00
|
|
|
/>
|
|
|
|
),
|
2020-03-23 13:05:07 +02:00
|
|
|
keyTest: (event) =>
|
2020-12-01 23:36:06 +02:00
|
|
|
(event.code === CODES.MINUS || event.code === CODES.NUM_SUBTRACT) &&
|
2020-03-09 15:06:35 +02:00
|
|
|
(event[KEYS.CTRL_OR_CMD] || event.shiftKey),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-02-16 17:00:45 +01:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionResetZoom = register({
|
2020-02-16 17:00:45 +01:00
|
|
|
name: "resetZoom",
|
2022-01-29 21:12:44 +01:00
|
|
|
perform: (_elements, appState, _, app) => {
|
2020-02-16 17:00:45 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2022-01-29 21:12:44 +01:00
|
|
|
...getStateForZoom(
|
2020-12-14 14:14:56 +00:00
|
|
|
{
|
2022-01-29 21:12:44 +01:00
|
|
|
viewportX: appState.width / 2 + appState.offsetLeft,
|
|
|
|
viewportY: appState.height / 2 + appState.offsetTop,
|
|
|
|
nextZoom: getNormalizedZoom(1),
|
2020-12-14 14:14:56 +00:00
|
|
|
},
|
2022-01-29 21:12:44 +01:00
|
|
|
appState,
|
2020-12-14 14:14:56 +00:00
|
|
|
),
|
2020-02-16 17:00:45 +01:00
|
|
|
},
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: false,
|
2020-02-16 17:00:45 +01:00
|
|
|
};
|
|
|
|
},
|
2021-07-15 18:48:03 +02:00
|
|
|
PanelComponent: ({ updateData, appState }) => (
|
2021-11-24 17:16:18 +01:00
|
|
|
<Tooltip label={t("buttons.resetZoom")} style={{ height: "100%" }}>
|
2021-07-15 18:48:03 +02:00
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
className="reset-zoom-button"
|
|
|
|
title={t("buttons.resetZoom")}
|
|
|
|
aria-label={t("buttons.resetZoom")}
|
|
|
|
onClick={() => {
|
|
|
|
updateData(null);
|
|
|
|
}}
|
|
|
|
size="small"
|
|
|
|
>
|
|
|
|
{(appState.zoom.value * 100).toFixed(0)}%
|
|
|
|
</ToolButton>
|
|
|
|
</Tooltip>
|
2020-02-22 21:24:34 +02:00
|
|
|
),
|
2020-03-23 13:05:07 +02:00
|
|
|
keyTest: (event) =>
|
2020-12-01 23:36:06 +02:00
|
|
|
(event.code === CODES.ZERO || event.code === CODES.NUM_ZERO) &&
|
2020-03-09 15:06:35 +02:00
|
|
|
(event[KEYS.CTRL_OR_CMD] || event.shiftKey),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-04-18 15:50:30 +02:00
|
|
|
|
2020-11-04 17:49:15 +00:00
|
|
|
const zoomValueToFitBoundsOnViewport = (
|
|
|
|
bounds: [number, number, number, number],
|
|
|
|
viewportDimensions: { width: number; height: number },
|
|
|
|
) => {
|
|
|
|
const [x1, y1, x2, y2] = bounds;
|
|
|
|
const commonBoundsWidth = x2 - x1;
|
|
|
|
const zoomValueForWidth = viewportDimensions.width / commonBoundsWidth;
|
|
|
|
const commonBoundsHeight = y2 - y1;
|
|
|
|
const zoomValueForHeight = viewportDimensions.height / commonBoundsHeight;
|
|
|
|
const smallestZoomValue = Math.min(zoomValueForWidth, zoomValueForHeight);
|
|
|
|
const zoomAdjustedToSteps =
|
|
|
|
Math.floor(smallestZoomValue / ZOOM_STEP) * ZOOM_STEP;
|
|
|
|
const clampedZoomValueToFitElements = Math.min(
|
|
|
|
Math.max(zoomAdjustedToSteps, ZOOM_STEP),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
return clampedZoomValueToFitElements as NormalizedZoomValue;
|
2020-04-18 15:50:30 +02:00
|
|
|
};
|
|
|
|
|
2020-12-13 14:54:35 -06:00
|
|
|
const zoomToFitElements = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: Readonly<AppState>,
|
|
|
|
zoomToSelection: boolean,
|
|
|
|
) => {
|
|
|
|
const nonDeletedElements = getNonDeletedElements(elements);
|
|
|
|
const selectedElements = getSelectedElements(nonDeletedElements, appState);
|
2020-11-04 17:49:15 +00:00
|
|
|
|
2020-12-13 14:54:35 -06:00
|
|
|
const commonBounds =
|
|
|
|
zoomToSelection && selectedElements.length > 0
|
|
|
|
? getCommonBounds(selectedElements)
|
|
|
|
: getCommonBounds(nonDeletedElements);
|
2020-11-04 17:49:15 +00:00
|
|
|
|
2022-01-29 21:12:44 +01:00
|
|
|
const newZoom = {
|
|
|
|
value: zoomValueToFitBoundsOnViewport(commonBounds, {
|
|
|
|
width: appState.width,
|
|
|
|
height: appState.height,
|
|
|
|
}),
|
|
|
|
};
|
2020-12-13 14:54:35 -06:00
|
|
|
|
|
|
|
const [x1, y1, x2, y2] = commonBounds;
|
|
|
|
const centerX = (x1 + x2) / 2;
|
|
|
|
const centerY = (y1 + y2) / 2;
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
...centerScrollOn({
|
|
|
|
scenePoint: { x: centerX, y: centerY },
|
|
|
|
viewportDimensions: {
|
|
|
|
width: appState.width,
|
|
|
|
height: appState.height,
|
|
|
|
},
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom: newZoom,
|
2020-12-13 14:54:35 -06:00
|
|
|
}),
|
|
|
|
zoom: newZoom,
|
|
|
|
},
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actionZoomToSelected = register({
|
|
|
|
name: "zoomToSelection",
|
|
|
|
perform: (elements, appState) => zoomToFitElements(elements, appState, true),
|
|
|
|
keyTest: (event) =>
|
|
|
|
event.code === CODES.TWO &&
|
|
|
|
event.shiftKey &&
|
|
|
|
!event.altKey &&
|
|
|
|
!event[KEYS.CTRL_OR_CMD],
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionZoomToFit = register({
|
|
|
|
name: "zoomToFit",
|
|
|
|
perform: (elements, appState) => zoomToFitElements(elements, appState, false),
|
2020-04-18 15:50:30 +02:00
|
|
|
keyTest: (event) =>
|
2020-12-01 23:36:06 +02:00
|
|
|
event.code === CODES.ONE &&
|
2020-04-18 15:50:30 +02:00
|
|
|
event.shiftKey &&
|
|
|
|
!event.altKey &&
|
|
|
|
!event[KEYS.CTRL_OR_CMD],
|
|
|
|
});
|
2021-05-08 15:17:30 +05:30
|
|
|
|
|
|
|
export const actionToggleTheme = register({
|
|
|
|
name: "toggleTheme",
|
|
|
|
perform: (_, appState, value) => {
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2021-10-14 14:15:57 +05:30
|
|
|
theme:
|
|
|
|
value || (appState.theme === THEME.LIGHT ? THEME.DARK : THEME.LIGHT),
|
2021-05-08 15:17:30 +05:30
|
|
|
},
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
|
|
|
<div style={{ marginInlineStart: "0.25rem" }}>
|
|
|
|
<DarkModeToggle
|
|
|
|
value={appState.theme}
|
|
|
|
onChange={(theme) => {
|
|
|
|
updateData(theme);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
keyTest: (event) => event.altKey && event.shiftKey && event.code === CODES.D,
|
|
|
|
});
|