You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
4 years ago
|
import { register } from "./register";
|
||
|
import { getSelectedElements } from "../scene";
|
||
3 years ago
|
import { getNonDeletedElements } from "../element";
|
||
4 years ago
|
import { ExcalidrawElement, NonDeleted } from "../element/types";
|
||
2 years ago
|
import { resizeMultipleElements } from "../element/resizeElements";
|
||
|
import { AppState, PointerDownState } from "../types";
|
||
3 years ago
|
import { arrayToMap } from "../utils";
|
||
2 years ago
|
import { CODES, KEYS } from "../keys";
|
||
2 years ago
|
import { getCommonBoundingBox } from "../element/bounds";
|
||
|
import {
|
||
|
bindOrUnbindSelectedElements,
|
||
|
isBindingEnabled,
|
||
|
unbindLinearElements,
|
||
|
} from "../element/binding";
|
||
2 years ago
|
import { updateFrameMembershipOfSelectedElements } from "../frame";
|
||
4 years ago
|
|
||
|
export const actionFlipHorizontal = register({
|
||
|
name: "flipHorizontal",
|
||
3 years ago
|
trackEvent: { category: "element" },
|
||
2 years ago
|
perform: (elements, appState, _, app) => {
|
||
4 years ago
|
return {
|
||
2 years ago
|
elements: updateFrameMembershipOfSelectedElements(
|
||
|
flipSelectedElements(elements, appState, "horizontal"),
|
||
|
appState,
|
||
2 years ago
|
app,
|
||
2 years ago
|
),
|
||
4 years ago
|
appState,
|
||
|
commitToHistory: true,
|
||
|
};
|
||
|
},
|
||
2 years ago
|
keyTest: (event) => event.shiftKey && event.code === CODES.H,
|
||
4 years ago
|
contextItemLabel: "labels.flipHorizontal",
|
||
|
});
|
||
|
|
||
|
export const actionFlipVertical = register({
|
||
|
name: "flipVertical",
|
||
3 years ago
|
trackEvent: { category: "element" },
|
||
2 years ago
|
perform: (elements, appState, _, app) => {
|
||
4 years ago
|
return {
|
||
2 years ago
|
elements: updateFrameMembershipOfSelectedElements(
|
||
|
flipSelectedElements(elements, appState, "vertical"),
|
||
|
appState,
|
||
2 years ago
|
app,
|
||
2 years ago
|
),
|
||
4 years ago
|
appState,
|
||
|
commitToHistory: true,
|
||
|
};
|
||
|
},
|
||
2 years ago
|
keyTest: (event) =>
|
||
2 years ago
|
event.shiftKey && event.code === CODES.V && !event[KEYS.CTRL_OR_CMD],
|
||
4 years ago
|
contextItemLabel: "labels.flipVertical",
|
||
|
});
|
||
|
|
||
|
const flipSelectedElements = (
|
||
|
elements: readonly ExcalidrawElement[],
|
||
|
appState: Readonly<AppState>,
|
||
|
flipDirection: "horizontal" | "vertical",
|
||
|
) => {
|
||
|
const selectedElements = getSelectedElements(
|
||
|
getNonDeletedElements(elements),
|
||
|
appState,
|
||
2 years ago
|
{
|
||
1 year ago
|
includeBoundTextElement: true,
|
||
2 years ago
|
includeElementsInFrames: true,
|
||
|
},
|
||
4 years ago
|
);
|
||
|
|
||
|
const updatedElements = flipElements(
|
||
|
selectedElements,
|
||
|
appState,
|
||
|
flipDirection,
|
||
|
);
|
||
|
|
||
3 years ago
|
const updatedElementsMap = arrayToMap(updatedElements);
|
||
4 years ago
|
|
||
3 years ago
|
return elements.map(
|
||
|
(element) => updatedElementsMap.get(element.id) || element,
|
||
|
);
|
||
4 years ago
|
};
|
||
|
|
||
|
const flipElements = (
|
||
|
elements: NonDeleted<ExcalidrawElement>[],
|
||
|
appState: AppState,
|
||
|
flipDirection: "horizontal" | "vertical",
|
||
|
): ExcalidrawElement[] => {
|
||
2 years ago
|
const { minX, minY, maxX, maxY } = getCommonBoundingBox(elements);
|
||
|
|
||
|
resizeMultipleElements(
|
||
|
{ originalElements: arrayToMap(elements) } as PointerDownState,
|
||
|
elements,
|
||
|
"nw",
|
||
|
true,
|
||
|
flipDirection === "horizontal" ? maxX : minX,
|
||
|
flipDirection === "horizontal" ? minY : maxY,
|
||
|
);
|
||
4 years ago
|
|
||
2 years ago
|
(isBindingEnabled(appState)
|
||
|
? bindOrUnbindSelectedElements
|
||
|
: unbindLinearElements)(elements);
|
||
4 years ago
|
|
||
2 years ago
|
return elements;
|
||
4 years ago
|
};
|