瀏覽代碼

add utility to compute container coords from bound text

Aakansha Doshi 2 年之前
父節點
當前提交
752c7cf66e
共有 1 個文件被更改,包括 23 次插入1 次删除
  1. 23 1
      src/element/textElement.ts

+ 23 - 1
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)) {