From 752c7cf66eec7cc90d1fc095f42e86a716d499c1 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Mon, 6 Mar 2023 15:40:14 +0530 Subject: [PATCH] add utility to compute container coords from bound text --- src/element/textElement.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/element/textElement.ts b/src/element/textElement.ts index 5134c76f2..13745111e 100644 --- a/src/element/textElement.ts +++ b/src/element/textElement.ts @@ -231,8 +231,9 @@ export const handleBindTextResize = ( container, nextHeight, ); + const { y } = computeContainerCoords(textElement, container.type); mutateElement(container, { - y: textElement.y - BOUND_TEXT_PADDING, + y, height: containerHeight, }); } @@ -636,6 +637,27 @@ export const computeBoundTextElementCoords = ( }; }; +export const computeContainerCoords = ( + boundTextElement: ExcalidrawTextElement, + containerType: string, +) => { + let offsetX = BOUND_TEXT_PADDING; + let offsetY = BOUND_TEXT_PADDING; + + if (containerType === "ellipse") { + offsetX += (boundTextElement.width / 2) * (1 - Math.sqrt(2) / 2); + offsetY += (boundTextElement.height / 2) * (1 - Math.sqrt(2) / 2); + } + if (containerType === "diamond") { + offsetX += boundTextElement.width / 4; + offsetY += boundTextElement.height / 4; + } + return { + x: boundTextElement.x - offsetX, + y: boundTextElement.y - offsetY, + }; +}; + export const getTextElementAngle = (textElement: ExcalidrawTextElement) => { const container = getContainerElement(textElement); if (!container || isArrowElement(container)) {