From a443d65512988f4ffa8ee38e3958682d3db464fd Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Tue, 16 May 2023 17:49:24 +0530 Subject: [PATCH] support labelled arrows --- src/element/newElement.ts | 9 ++------- src/element/textElement.ts | 40 ++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/element/newElement.ts b/src/element/newElement.ts index 2c9f5abf99..fc3b54b232 100644 --- a/src/element/newElement.ts +++ b/src/element/newElement.ts @@ -667,13 +667,8 @@ export const convertToExcalidrawElements = ( if (!element) { return; } - if ( - isValidTextContainer(element) && - element?.label?.text && - (element.type === "rectangle" || - element.type === "ellipse" || - element.type === "diamond") - ) { + if (isValidTextContainer(element) && element?.label?.text) { + //@ts-ignore const elements = bindTextToContainer(element, element.label); res.push(...elements); } else { diff --git a/src/element/textElement.ts b/src/element/textElement.ts index a23bb2ce56..787d7a9add 100644 --- a/src/element/textElement.ts +++ b/src/element/textElement.ts @@ -2,6 +2,7 @@ import { getFontString, arrayToMap, isTestEnv } from "../utils"; import { ExcalidrawElement, ExcalidrawGenericElement, + ExcalidrawLinearElement, ExcalidrawTextContainer, ExcalidrawTextElement, ExcalidrawTextElementWithContainer, @@ -34,7 +35,7 @@ import { updateOriginalContainerCache, } from "./textWysiwyg"; import { ExtractSetType, MarkOptional } from "../utility-types"; -import { ElementConstructorOpts } from "./newElement"; +import { ElementConstructorOpts, newLinearElement } from "./newElement"; export const normalizeText = (text: string) => { return ( @@ -90,7 +91,7 @@ export const redrawTextBoundingBox = ( textElement as ExcalidrawTextElementWithContainer, ); const maxContainerWidth = getBoundTextMaxWidth(container); - + console.log(metrics, maxContainerWidth, container); if (metrics.height > maxContainerHeight) { const nextHeight = computeContainerDimensionForBoundText( metrics.height, @@ -984,15 +985,38 @@ export const getDefaultLineHeight = (fontFamily: FontFamilyValues) => { export const bindTextToContainer = ( containerProps: | { - type: Exclude; + type: + | Exclude + | ExcalidrawLinearElement["type"]; } & MarkOptional, textProps: { text: string } & MarkOptional, ) => { - const container = newElement({ - x: 0, - y: 0, - ...containerProps, - }); + let container; + if (containerProps.type === "arrow") { + container = newLinearElement({ + //@ts-ignore + x: 0, + //@ts-ignore + y: 0, + //@ts-ignore + type: containerProps.type, + //@ts-ignore, + endArrowhead: containerProps.type === "arrow" ? "arrow" : null, + //@ts-ignore + points: [ + [0, 0], + [300, 0], + ], + ...containerProps, + }); + } else { + //@ts-ignore + container = newElement({ + x: 0, + y: 0, + ...containerProps, + }); + } const textElement: ExcalidrawTextElement = newTextElement({ x: 0, y: 0,