2021-03-26 11:45:08 -04:00
|
|
|
import { register } from "./register";
|
|
|
|
import { getSelectedElements } from "../scene";
|
2021-11-26 12:46:23 +01:00
|
|
|
import { getNonDeletedElements } from "../element";
|
2021-03-26 11:45:08 -04:00
|
|
|
import { mutateElement } from "../element/mutateElement";
|
|
|
|
import { ExcalidrawElement, NonDeleted } from "../element/types";
|
|
|
|
import { normalizeAngle, resizeSingleElement } from "../element/resizeElements";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { getTransformHandles } from "../element/transformHandles";
|
|
|
|
import { updateBoundElements } from "../element/binding";
|
2021-11-26 12:46:23 +01:00
|
|
|
import { arrayToMap } from "../utils";
|
2022-10-19 00:03:58 +02:00
|
|
|
import {
|
|
|
|
getElementAbsoluteCoords,
|
|
|
|
getElementPointsCoords,
|
|
|
|
} from "../element/bounds";
|
|
|
|
import { isLinearElement } from "../element/typeChecks";
|
|
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
2022-11-26 23:44:26 +01:00
|
|
|
import { KEYS } from "../keys";
|
2021-03-26 11:45:08 -04:00
|
|
|
|
|
|
|
const enableActionFlipHorizontal = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) => {
|
|
|
|
const eligibleElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
return eligibleElements.length === 1 && eligibleElements[0].type !== "text";
|
|
|
|
};
|
|
|
|
|
|
|
|
const enableActionFlipVertical = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) => {
|
|
|
|
const eligibleElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
return eligibleElements.length === 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actionFlipHorizontal = register({
|
|
|
|
name: "flipHorizontal",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-03-26 11:45:08 -04:00
|
|
|
perform: (elements, appState) => {
|
|
|
|
return {
|
|
|
|
elements: flipSelectedElements(elements, appState, "horizontal"),
|
|
|
|
appState,
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
keyTest: (event) => event.shiftKey && event.code === "KeyH",
|
|
|
|
contextItemLabel: "labels.flipHorizontal",
|
2023-01-06 14:32:55 +01:00
|
|
|
predicate: (elements, appState) =>
|
2021-03-26 11:45:08 -04:00
|
|
|
enableActionFlipHorizontal(elements, appState),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionFlipVertical = register({
|
|
|
|
name: "flipVertical",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-03-26 11:45:08 -04:00
|
|
|
perform: (elements, appState) => {
|
|
|
|
return {
|
|
|
|
elements: flipSelectedElements(elements, appState, "vertical"),
|
|
|
|
appState,
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
|
|
|
},
|
2022-11-26 23:44:26 +01:00
|
|
|
keyTest: (event) =>
|
|
|
|
event.shiftKey && event.code === "KeyV" && !event[KEYS.CTRL_OR_CMD],
|
2021-03-26 11:45:08 -04:00
|
|
|
contextItemLabel: "labels.flipVertical",
|
2023-01-06 14:32:55 +01:00
|
|
|
predicate: (elements, appState) =>
|
2021-03-26 11:45:08 -04:00
|
|
|
enableActionFlipVertical(elements, appState),
|
|
|
|
});
|
|
|
|
|
|
|
|
const flipSelectedElements = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: Readonly<AppState>,
|
|
|
|
flipDirection: "horizontal" | "vertical",
|
|
|
|
) => {
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
|
|
|
|
// remove once we allow for groups of elements to be flipped
|
|
|
|
if (selectedElements.length > 1) {
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedElements = flipElements(
|
|
|
|
selectedElements,
|
|
|
|
appState,
|
|
|
|
flipDirection,
|
|
|
|
);
|
|
|
|
|
2021-11-26 12:46:23 +01:00
|
|
|
const updatedElementsMap = arrayToMap(updatedElements);
|
2021-03-26 11:45:08 -04:00
|
|
|
|
2021-11-26 12:46:23 +01:00
|
|
|
return elements.map(
|
|
|
|
(element) => updatedElementsMap.get(element.id) || element,
|
|
|
|
);
|
2021-03-26 11:45:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const flipElements = (
|
|
|
|
elements: NonDeleted<ExcalidrawElement>[],
|
|
|
|
appState: AppState,
|
|
|
|
flipDirection: "horizontal" | "vertical",
|
|
|
|
): ExcalidrawElement[] => {
|
2021-10-21 22:05:48 +02:00
|
|
|
elements.forEach((element) => {
|
|
|
|
flipElement(element, appState);
|
2021-03-26 11:45:08 -04:00
|
|
|
// If vertical flip, rotate an extra 180
|
|
|
|
if (flipDirection === "vertical") {
|
2021-10-21 22:05:48 +02:00
|
|
|
rotateElement(element, Math.PI);
|
2021-03-26 11:45:08 -04:00
|
|
|
}
|
2021-10-21 22:05:48 +02:00
|
|
|
});
|
2021-03-26 11:45:08 -04:00
|
|
|
return elements;
|
|
|
|
};
|
|
|
|
|
|
|
|
const flipElement = (
|
|
|
|
element: NonDeleted<ExcalidrawElement>,
|
|
|
|
appState: AppState,
|
|
|
|
) => {
|
|
|
|
const originalX = element.x;
|
|
|
|
const originalY = element.y;
|
|
|
|
const width = element.width;
|
|
|
|
const height = element.height;
|
|
|
|
const originalAngle = normalizeAngle(element.angle);
|
|
|
|
|
|
|
|
// Rotate back to zero, if necessary
|
|
|
|
mutateElement(element, {
|
|
|
|
angle: normalizeAngle(0),
|
|
|
|
});
|
|
|
|
// Flip unrotated by pulling TransformHandle to opposite side
|
|
|
|
const transformHandles = getTransformHandles(element, appState.zoom);
|
|
|
|
let usingNWHandle = true;
|
|
|
|
let nHandle = transformHandles.nw;
|
|
|
|
if (!nHandle) {
|
|
|
|
// Use ne handle instead
|
|
|
|
usingNWHandle = false;
|
|
|
|
nHandle = transformHandles.ne;
|
|
|
|
if (!nHandle) {
|
|
|
|
mutateElement(element, {
|
|
|
|
angle: originalAngle,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:03:58 +02:00
|
|
|
let finalOffsetX = 0;
|
|
|
|
if (isLinearElement(element) && element.points.length < 3) {
|
|
|
|
finalOffsetX =
|
|
|
|
element.points.reduce((max, point) => Math.max(max, point[0]), 0) * 2 -
|
|
|
|
element.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
let initialPointsCoords;
|
2021-03-26 11:45:08 -04:00
|
|
|
if (isLinearElement(element)) {
|
2022-12-08 23:48:49 +08:00
|
|
|
initialPointsCoords = getElementPointsCoords(element, element.points);
|
2022-10-19 00:03:58 +02:00
|
|
|
}
|
|
|
|
const initialElementAbsoluteCoords = getElementAbsoluteCoords(element);
|
|
|
|
|
|
|
|
if (isLinearElement(element) && element.points.length < 3) {
|
2021-12-13 13:35:07 +01:00
|
|
|
for (let index = 1; index < element.points.length; index++) {
|
|
|
|
LinearElementEditor.movePoints(element, [
|
2022-10-19 00:03:58 +02:00
|
|
|
{
|
|
|
|
index,
|
|
|
|
point: [-element.points[index][0], element.points[index][1]],
|
|
|
|
},
|
2021-03-26 11:45:08 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
LinearElementEditor.normalizePoints(element);
|
|
|
|
} else {
|
2022-10-19 00:03:58 +02:00
|
|
|
const elWidth = initialPointsCoords
|
|
|
|
? initialPointsCoords[2] - initialPointsCoords[0]
|
|
|
|
: initialElementAbsoluteCoords[2] - initialElementAbsoluteCoords[0];
|
|
|
|
|
|
|
|
const startPoint = initialPointsCoords
|
|
|
|
? [initialPointsCoords[0], initialPointsCoords[1]]
|
|
|
|
: [initialElementAbsoluteCoords[0], initialElementAbsoluteCoords[1]];
|
|
|
|
|
2021-03-26 11:45:08 -04:00
|
|
|
resizeSingleElement(
|
2022-02-22 18:45:59 +05:30
|
|
|
new Map().set(element.id, element),
|
2022-10-19 00:03:58 +02:00
|
|
|
false,
|
2021-03-26 11:45:08 -04:00
|
|
|
element,
|
|
|
|
usingNWHandle ? "nw" : "ne",
|
2022-10-19 00:03:58 +02:00
|
|
|
true,
|
|
|
|
usingNWHandle ? startPoint[0] + elWidth : startPoint[0] - elWidth,
|
|
|
|
startPoint[1],
|
2021-03-26 11:45:08 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate by (360 degrees - original angle)
|
|
|
|
let angle = normalizeAngle(2 * Math.PI - originalAngle);
|
|
|
|
if (angle < 0) {
|
|
|
|
// check, probably unnecessary
|
|
|
|
angle = normalizeAngle(angle + 2 * Math.PI);
|
|
|
|
}
|
|
|
|
mutateElement(element, {
|
|
|
|
angle,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Move back to original spot to appear "flipped in place"
|
|
|
|
mutateElement(element, {
|
|
|
|
x: originalX + finalOffsetX,
|
|
|
|
y: originalY,
|
2022-10-19 00:03:58 +02:00
|
|
|
width,
|
|
|
|
height,
|
2021-03-26 11:45:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
updateBoundElements(element);
|
2022-10-19 00:03:58 +02:00
|
|
|
|
|
|
|
if (initialPointsCoords && isLinearElement(element)) {
|
|
|
|
// Adjusting origin because when a beizer curve path exceeds min/max points it offsets the origin.
|
|
|
|
// There's still room for improvement since when the line roughness is > 1
|
|
|
|
// we still have a small offset of the origin when fliipping the element.
|
2022-12-08 23:48:49 +08:00
|
|
|
const finalPointsCoords = getElementPointsCoords(element, element.points);
|
2022-10-19 00:03:58 +02:00
|
|
|
|
|
|
|
const topLeftCoordsDiff = initialPointsCoords[0] - finalPointsCoords[0];
|
|
|
|
const topRightCoordDiff = initialPointsCoords[2] - finalPointsCoords[2];
|
|
|
|
|
|
|
|
const coordsDiff = topLeftCoordsDiff + topRightCoordDiff;
|
|
|
|
|
|
|
|
mutateElement(element, {
|
|
|
|
x: element.x + coordsDiff * 0.5,
|
|
|
|
y: element.y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
});
|
|
|
|
}
|
2021-03-26 11:45:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const rotateElement = (element: ExcalidrawElement, rotationAngle: number) => {
|
|
|
|
const originalX = element.x;
|
|
|
|
const originalY = element.y;
|
|
|
|
let angle = normalizeAngle(element.angle + rotationAngle);
|
|
|
|
if (angle < 0) {
|
|
|
|
// check, probably unnecessary
|
|
|
|
angle = normalizeAngle(2 * Math.PI + angle);
|
|
|
|
}
|
|
|
|
mutateElement(element, {
|
|
|
|
angle,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Move back to original spot
|
|
|
|
mutateElement(element, {
|
|
|
|
x: originalX,
|
|
|
|
y: originalY,
|
|
|
|
});
|
|
|
|
};
|