2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2020-01-11 20:34:21 -08:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { ColorPicker } from "../components/ColorPicker";
|
|
|
|
import { resetZoom, trash, zoomIn, zoomOut } from "../components/icons";
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "../components/ToolButton";
|
2021-01-30 18:03:23 +01:00
|
|
|
import { ZOOM_STEP } from "../constants";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { getCommonBounds, getNonDeletedElements } from "../element";
|
|
|
|
import { newElementWith } from "../element/mutateElement";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
2020-12-27 18:26:30 +02:00
|
|
|
import useIsMobile from "../is-mobile";
|
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";
|
|
|
|
import { getNewZoom } from "../scene/zoom";
|
|
|
|
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";
|
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 {
|
|
|
|
appState: { ...appState, viewBackgroundColor: value },
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
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}
|
2020-03-23 13:05:07 +02:00
|
|
|
onChange={(color) => updateData(color)}
|
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",
|
2020-04-07 14:02:42 +01:00
|
|
|
perform: (elements, appState: AppState) => {
|
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-03-13 18:58:06 +05:30
|
|
|
theme: appState.theme,
|
2020-11-08 17:10:20 +02:00
|
|
|
elementLocked: appState.elementLocked,
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
exportEmbedScene: appState.exportEmbedScene,
|
|
|
|
gridSize: appState.gridSize,
|
|
|
|
shouldAddWatermark: appState.shouldAddWatermark,
|
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
|
|
|
};
|
|
|
|
},
|
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-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-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",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_elements, appState) => {
|
2020-12-02 23:57:51 +02:00
|
|
|
const zoom = getNewZoom(
|
|
|
|
getNormalizedZoom(appState.zoom.value + ZOOM_STEP),
|
|
|
|
appState.zoom,
|
2020-12-14 14:14:56 +00:00
|
|
|
{ left: appState.offsetLeft, top: appState.offsetTop },
|
2020-12-02 23:57:51 +02:00
|
|
|
{ x: appState.width / 2, y: appState.height / 2 },
|
|
|
|
);
|
2020-02-15 21:03:32 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2020-12-02 23:57:51 +02:00
|
|
|
zoom,
|
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);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
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",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_elements, appState) => {
|
2020-12-02 23:57:51 +02:00
|
|
|
const zoom = getNewZoom(
|
|
|
|
getNormalizedZoom(appState.zoom.value - ZOOM_STEP),
|
|
|
|
appState.zoom,
|
2020-12-14 14:14:56 +00:00
|
|
|
{ left: appState.offsetLeft, top: appState.offsetTop },
|
2020-12-02 23:57:51 +02:00
|
|
|
{ x: appState.width / 2, y: appState.height / 2 },
|
|
|
|
);
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2020-12-02 23:57:51 +02:00
|
|
|
zoom,
|
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);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
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",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_elements, appState) => {
|
2020-02-16 17:00:45 +01:00
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2020-12-14 14:14:56 +00:00
|
|
|
zoom: getNewZoom(
|
|
|
|
1 as NormalizedZoomValue,
|
|
|
|
appState.zoom,
|
|
|
|
{ left: appState.offsetLeft, top: appState.offsetTop },
|
|
|
|
{
|
|
|
|
x: appState.width / 2,
|
|
|
|
y: appState.height / 2,
|
|
|
|
},
|
|
|
|
),
|
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
|
|
|
};
|
|
|
|
},
|
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-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
|
|
|
|
2020-12-13 14:54:35 -06:00
|
|
|
const zoomValue = zoomValueToFitBoundsOnViewport(commonBounds, {
|
|
|
|
width: appState.width,
|
|
|
|
height: appState.height,
|
|
|
|
});
|
2020-12-14 14:14:56 +00:00
|
|
|
const newZoom = getNewZoom(zoomValue, appState.zoom, {
|
|
|
|
left: appState.offsetLeft,
|
|
|
|
top: appState.offsetTop,
|
|
|
|
});
|
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],
|
|
|
|
});
|