From 0f18b9832f4786cacc81d9e16573b857402526ea Mon Sep 17 00:00:00 2001 From: dwelle <5153846+dwelle@users.noreply.github.com> Date: Thu, 8 May 2025 23:00:30 +0200 Subject: [PATCH] convert to polygon when creating line --- .../excalidraw/actions/actionFinalize.tsx | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/excalidraw/actions/actionFinalize.tsx b/packages/excalidraw/actions/actionFinalize.tsx index 6ddb8ab52..dea838592 100644 --- a/packages/excalidraw/actions/actionFinalize.tsx +++ b/packages/excalidraw/actions/actionFinalize.tsx @@ -8,7 +8,9 @@ import { LinearElementEditor } from "@excalidraw/element/linearElementEditor"; import { isBindingElement, + isFreeDrawElement, isLinearElement, + isLineElement, } from "@excalidraw/element/typeChecks"; import { KEYS, arrayToMap, updateActiveTool } from "@excalidraw/common"; @@ -18,6 +20,8 @@ import { isInvisiblySmallElement } from "@excalidraw/element/sizeHelpers"; import { CaptureUpdateAction } from "@excalidraw/element/store"; +import type { LocalPoint } from "@excalidraw/math"; + import { t } from "../i18n"; import { resetCursor } from "../cursor"; import { done } from "../components/icons"; @@ -85,14 +89,14 @@ export const actionFinalize = register({ const multiPointElement = appState.multiElement ? appState.multiElement - : appState.newElement?.type === "freedraw" + : isFreeDrawElement(appState.newElement) ? appState.newElement : null; if (multiPointElement) { // pen and mouse have hover if ( - multiPointElement.type !== "freedraw" && + !isFreeDrawElement(multiPointElement) && appState.lastPointerDownWith !== "touch" ) { const { points, lastCommittedPoint } = multiPointElement; @@ -118,19 +122,27 @@ export const actionFinalize = register({ // This ensures that loop remains closed at different scales. const isLoop = isPathALoop(multiPointElement.points, appState.zoom.value); if ( - multiPointElement.type === "line" || - multiPointElement.type === "freedraw" + isLineElement(multiPointElement) || + isFreeDrawElement(multiPointElement) ) { if (isLoop) { const linePoints = multiPointElement.points; const firstPoint = linePoints[0]; - scene.mutateElement(multiPointElement, { - points: linePoints.map((p, index) => - index === linePoints.length - 1 - ? pointFrom(firstPoint[0], firstPoint[1]) - : p, - ), - }); + const points: LocalPoint[] = linePoints.map((p, index) => + index === linePoints.length - 1 + ? pointFrom(firstPoint[0], firstPoint[1]) + : p, + ); + if (isLineElement(multiPointElement)) { + scene.mutateElement(multiPointElement, { + points, + loopLock: true, + }); + } else { + scene.mutateElement(multiPointElement, { + points, + }); + } } }