convert to polygon when creating line

This commit is contained in:
dwelle 2025-05-08 23:00:30 +02:00
parent d1fa9005b9
commit 0f18b9832f

View File

@ -8,7 +8,9 @@ import { LinearElementEditor } from "@excalidraw/element/linearElementEditor";
import { import {
isBindingElement, isBindingElement,
isFreeDrawElement,
isLinearElement, isLinearElement,
isLineElement,
} from "@excalidraw/element/typeChecks"; } from "@excalidraw/element/typeChecks";
import { KEYS, arrayToMap, updateActiveTool } from "@excalidraw/common"; import { KEYS, arrayToMap, updateActiveTool } from "@excalidraw/common";
@ -18,6 +20,8 @@ import { isInvisiblySmallElement } from "@excalidraw/element/sizeHelpers";
import { CaptureUpdateAction } from "@excalidraw/element/store"; import { CaptureUpdateAction } from "@excalidraw/element/store";
import type { LocalPoint } from "@excalidraw/math";
import { t } from "../i18n"; import { t } from "../i18n";
import { resetCursor } from "../cursor"; import { resetCursor } from "../cursor";
import { done } from "../components/icons"; import { done } from "../components/icons";
@ -85,14 +89,14 @@ export const actionFinalize = register({
const multiPointElement = appState.multiElement const multiPointElement = appState.multiElement
? appState.multiElement ? appState.multiElement
: appState.newElement?.type === "freedraw" : isFreeDrawElement(appState.newElement)
? appState.newElement ? appState.newElement
: null; : null;
if (multiPointElement) { if (multiPointElement) {
// pen and mouse have hover // pen and mouse have hover
if ( if (
multiPointElement.type !== "freedraw" && !isFreeDrawElement(multiPointElement) &&
appState.lastPointerDownWith !== "touch" appState.lastPointerDownWith !== "touch"
) { ) {
const { points, lastCommittedPoint } = multiPointElement; const { points, lastCommittedPoint } = multiPointElement;
@ -118,19 +122,27 @@ export const actionFinalize = register({
// This ensures that loop remains closed at different scales. // This ensures that loop remains closed at different scales.
const isLoop = isPathALoop(multiPointElement.points, appState.zoom.value); const isLoop = isPathALoop(multiPointElement.points, appState.zoom.value);
if ( if (
multiPointElement.type === "line" || isLineElement(multiPointElement) ||
multiPointElement.type === "freedraw" isFreeDrawElement(multiPointElement)
) { ) {
if (isLoop) { if (isLoop) {
const linePoints = multiPointElement.points; const linePoints = multiPointElement.points;
const firstPoint = linePoints[0]; const firstPoint = linePoints[0];
scene.mutateElement(multiPointElement, { const points: LocalPoint[] = linePoints.map((p, index) =>
points: linePoints.map((p, index) => index === linePoints.length - 1
index === linePoints.length - 1 ? pointFrom(firstPoint[0], firstPoint[1])
? pointFrom(firstPoint[0], firstPoint[1]) : p,
: p, );
), if (isLineElement(multiPointElement)) {
}); scene.mutateElement(multiPointElement, {
points,
loopLock: true,
});
} else {
scene.mutateElement(multiPointElement, {
points,
});
}
} }
} }