2020-02-01 15:49:18 +04:00
|
|
|
import { KEYS } from "../keys";
|
2020-02-05 22:47:10 +04:00
|
|
|
import { isInvisiblySmallElement } from "../element";
|
2020-02-10 15:09:50 +01:00
|
|
|
import { resetCursor } from "../utils";
|
2020-02-21 08:17:20 -05:00
|
|
|
import React from "react";
|
|
|
|
import { ToolButton } from "../components/ToolButton";
|
2020-02-21 14:34:18 -05:00
|
|
|
import { done } from "../components/icons";
|
2020-02-21 08:17:20 -05:00
|
|
|
import { t } from "../i18n";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { register } from "./register";
|
2020-03-09 22:34:50 -07:00
|
|
|
import { mutateElement } from "../element/mutateElement";
|
2020-04-09 01:46:47 -07:00
|
|
|
import { isPathALoop } from "../math";
|
2020-06-01 11:35:44 +02:00
|
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
2020-08-08 21:04:15 -07:00
|
|
|
import Scene from "../scene/Scene";
|
|
|
|
import {
|
|
|
|
maybeBindLinearElement,
|
|
|
|
bindOrUnbindLinearElement,
|
|
|
|
} from "../element/binding";
|
|
|
|
import { isBindingElement } from "../element/typeChecks";
|
2020-02-01 15:49:18 +04:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionFinalize = register({
|
2020-02-01 15:49:18 +04:00
|
|
|
name: "finalize",
|
2021-04-13 01:29:25 +05:30
|
|
|
perform: (elements, appState, _, { canvas, focusContainer }) => {
|
2020-06-01 11:35:44 +02:00
|
|
|
if (appState.editingLinearElement) {
|
2020-08-08 21:04:15 -07:00
|
|
|
const {
|
|
|
|
elementId,
|
|
|
|
startBindingElement,
|
|
|
|
endBindingElement,
|
|
|
|
} = appState.editingLinearElement;
|
2020-06-01 11:35:44 +02:00
|
|
|
const element = LinearElementEditor.getElement(elementId);
|
|
|
|
|
|
|
|
if (element) {
|
2020-08-08 21:04:15 -07:00
|
|
|
if (isBindingElement(element)) {
|
|
|
|
bindOrUnbindLinearElement(
|
|
|
|
element,
|
|
|
|
startBindingElement,
|
|
|
|
endBindingElement,
|
|
|
|
);
|
|
|
|
}
|
2020-06-01 11:35:44 +02:00
|
|
|
return {
|
|
|
|
elements:
|
|
|
|
element.points.length < 2 || isInvisiblySmallElement(element)
|
|
|
|
? elements.filter((el) => el.id !== element.id)
|
|
|
|
: undefined,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
editingLinearElement: null,
|
|
|
|
},
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
let newElements = elements;
|
2020-02-01 15:49:18 +04:00
|
|
|
if (window.document.activeElement instanceof HTMLElement) {
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer();
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
2020-05-12 20:10:11 +01:00
|
|
|
|
|
|
|
const multiPointElement = appState.multiElement
|
|
|
|
? appState.multiElement
|
|
|
|
: appState.editingElement?.type === "draw"
|
|
|
|
? appState.editingElement
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (multiPointElement) {
|
2020-02-21 14:34:18 -05:00
|
|
|
// pen and mouse have hover
|
2020-05-12 20:10:11 +01:00
|
|
|
if (
|
|
|
|
multiPointElement.type !== "draw" &&
|
|
|
|
appState.lastPointerDownWith !== "touch"
|
|
|
|
) {
|
|
|
|
const { points, lastCommittedPoint } = multiPointElement;
|
2020-03-18 16:43:06 +01:00
|
|
|
if (
|
|
|
|
!lastCommittedPoint ||
|
|
|
|
points[points.length - 1] !== lastCommittedPoint
|
|
|
|
) {
|
2020-05-12 20:10:11 +01:00
|
|
|
mutateElement(multiPointElement, {
|
|
|
|
points: multiPointElement.points.slice(0, -1),
|
2020-03-18 16:43:06 +01:00
|
|
|
});
|
|
|
|
}
|
2020-02-21 14:34:18 -05:00
|
|
|
}
|
2020-05-12 20:10:11 +01:00
|
|
|
if (isInvisiblySmallElement(multiPointElement)) {
|
2020-02-05 22:47:10 +04:00
|
|
|
newElements = newElements.slice(0, -1);
|
|
|
|
}
|
2020-03-14 21:48:51 -07:00
|
|
|
|
2020-04-09 01:46:47 -07:00
|
|
|
// If the multi point line closes the loop,
|
|
|
|
// set the last point to first point.
|
|
|
|
// This ensures that loop remains closed at different scales.
|
2021-02-14 14:43:23 +01:00
|
|
|
const isLoop = isPathALoop(multiPointElement.points, appState.zoom.value);
|
2020-05-12 20:10:11 +01:00
|
|
|
if (
|
|
|
|
multiPointElement.type === "line" ||
|
|
|
|
multiPointElement.type === "draw"
|
|
|
|
) {
|
2020-08-08 21:04:15 -07:00
|
|
|
if (isLoop) {
|
2020-05-12 20:10:11 +01:00
|
|
|
const linePoints = multiPointElement.points;
|
2020-04-09 01:46:47 -07:00
|
|
|
const firstPoint = linePoints[0];
|
2020-05-12 20:10:11 +01:00
|
|
|
mutateElement(multiPointElement, {
|
2020-11-06 22:06:30 +02:00
|
|
|
points: linePoints.map((point, index) =>
|
|
|
|
index === linePoints.length - 1
|
2020-04-09 01:46:47 -07:00
|
|
|
? ([firstPoint[0], firstPoint[1]] as const)
|
|
|
|
: point,
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
if (
|
|
|
|
isBindingElement(multiPointElement) &&
|
|
|
|
!isLoop &&
|
|
|
|
multiPointElement.points.length > 1
|
|
|
|
) {
|
|
|
|
const [x, y] = LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
|
|
|
multiPointElement,
|
|
|
|
-1,
|
|
|
|
);
|
|
|
|
maybeBindLinearElement(
|
|
|
|
multiPointElement,
|
|
|
|
appState,
|
|
|
|
Scene.getScene(multiPointElement)!,
|
|
|
|
{ x, y },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:34:06 +05:30
|
|
|
if (!appState.elementLocked && appState.elementType !== "draw") {
|
2020-05-12 20:10:11 +01:00
|
|
|
appState.selectedElementIds[multiPointElement.id] = true;
|
2020-02-22 10:21:54 +01:00
|
|
|
}
|
2020-02-04 17:39:08 +04:00
|
|
|
}
|
2021-01-05 18:34:06 +05:30
|
|
|
if (
|
|
|
|
(!appState.elementLocked && appState.elementType !== "draw") ||
|
|
|
|
!multiPointElement
|
|
|
|
) {
|
2021-03-03 14:04:02 +01:00
|
|
|
resetCursor(canvas);
|
2020-02-10 15:09:50 +01:00
|
|
|
}
|
2020-02-01 15:49:18 +04:00
|
|
|
return {
|
2020-02-05 22:47:10 +04:00
|
|
|
elements: newElements,
|
2020-02-01 15:49:18 +04:00
|
|
|
appState: {
|
|
|
|
...appState,
|
2020-02-10 15:09:50 +01:00
|
|
|
elementType:
|
2021-01-05 18:34:06 +05:30
|
|
|
(appState.elementLocked || appState.elementType === "draw") &&
|
|
|
|
multiPointElement
|
2020-02-10 15:09:50 +01:00
|
|
|
? appState.elementType
|
|
|
|
: "selection",
|
2020-02-01 15:49:18 +04:00
|
|
|
draggingElement: null,
|
|
|
|
multiElement: null,
|
2020-03-12 18:04:56 +01:00
|
|
|
editingElement: null,
|
2020-08-08 21:04:15 -07:00
|
|
|
startBoundElement: null,
|
|
|
|
suggestedBindings: [],
|
2020-05-12 20:10:11 +01:00
|
|
|
selectedElementIds:
|
2021-01-05 18:34:06 +05:30
|
|
|
multiPointElement &&
|
|
|
|
!appState.elementLocked &&
|
|
|
|
appState.elementType !== "draw"
|
2020-05-12 20:10:11 +01:00
|
|
|
? {
|
|
|
|
...appState.selectedElementIds,
|
|
|
|
[multiPointElement.id]: true,
|
|
|
|
}
|
|
|
|
: appState.selectedElementIds,
|
2020-02-01 15:49:18 +04:00
|
|
|
},
|
2020-05-14 14:51:33 +02:00
|
|
|
commitToHistory: appState.elementType === "draw",
|
2020-02-01 15:49:18 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
keyTest: (event, appState) =>
|
|
|
|
(event.key === KEYS.ESCAPE &&
|
2020-06-01 11:35:44 +02:00
|
|
|
(appState.editingLinearElement !== null ||
|
|
|
|
(!appState.draggingElement && appState.multiElement === null))) ||
|
2020-02-01 15:49:18 +04:00
|
|
|
((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) &&
|
|
|
|
appState.multiElement !== null),
|
2020-02-21 08:17:20 -05:00
|
|
|
PanelComponent: ({ appState, updateData }) => (
|
2020-03-01 14:39:03 -05:00
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={done}
|
|
|
|
title={t("buttons.done")}
|
|
|
|
aria-label={t("buttons.done")}
|
|
|
|
onClick={updateData}
|
|
|
|
visible={appState.multiElement != null}
|
|
|
|
/>
|
2020-02-21 08:17:20 -05:00
|
|
|
),
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|