support labelled arrows

This commit is contained in:
Aakansha Doshi 2023-05-16 17:49:24 +05:30
parent fbf2d533c0
commit a443d65512
2 changed files with 34 additions and 15 deletions

View file

@ -667,13 +667,8 @@ export const convertToExcalidrawElements = (
if (!element) { if (!element) {
return; return;
} }
if ( if (isValidTextContainer(element) && element?.label?.text) {
isValidTextContainer(element) && //@ts-ignore
element?.label?.text &&
(element.type === "rectangle" ||
element.type === "ellipse" ||
element.type === "diamond")
) {
const elements = bindTextToContainer(element, element.label); const elements = bindTextToContainer(element, element.label);
res.push(...elements); res.push(...elements);
} else { } else {

View file

@ -2,6 +2,7 @@ import { getFontString, arrayToMap, isTestEnv } from "../utils";
import { import {
ExcalidrawElement, ExcalidrawElement,
ExcalidrawGenericElement, ExcalidrawGenericElement,
ExcalidrawLinearElement,
ExcalidrawTextContainer, ExcalidrawTextContainer,
ExcalidrawTextElement, ExcalidrawTextElement,
ExcalidrawTextElementWithContainer, ExcalidrawTextElementWithContainer,
@ -34,7 +35,7 @@ import {
updateOriginalContainerCache, updateOriginalContainerCache,
} from "./textWysiwyg"; } from "./textWysiwyg";
import { ExtractSetType, MarkOptional } from "../utility-types"; import { ExtractSetType, MarkOptional } from "../utility-types";
import { ElementConstructorOpts } from "./newElement"; import { ElementConstructorOpts, newLinearElement } from "./newElement";
export const normalizeText = (text: string) => { export const normalizeText = (text: string) => {
return ( return (
@ -90,7 +91,7 @@ export const redrawTextBoundingBox = (
textElement as ExcalidrawTextElementWithContainer, textElement as ExcalidrawTextElementWithContainer,
); );
const maxContainerWidth = getBoundTextMaxWidth(container); const maxContainerWidth = getBoundTextMaxWidth(container);
console.log(metrics, maxContainerWidth, container);
if (metrics.height > maxContainerHeight) { if (metrics.height > maxContainerHeight) {
const nextHeight = computeContainerDimensionForBoundText( const nextHeight = computeContainerDimensionForBoundText(
metrics.height, metrics.height,
@ -984,15 +985,38 @@ export const getDefaultLineHeight = (fontFamily: FontFamilyValues) => {
export const bindTextToContainer = ( export const bindTextToContainer = (
containerProps: containerProps:
| { | {
type: Exclude<ExcalidrawGenericElement["type"], "selection">; type:
| Exclude<ExcalidrawGenericElement["type"], "selection">
| ExcalidrawLinearElement["type"];
} & MarkOptional<ElementConstructorOpts, "x" | "y">, } & MarkOptional<ElementConstructorOpts, "x" | "y">,
textProps: { text: string } & MarkOptional<ElementConstructorOpts, "x" | "y">, textProps: { text: string } & MarkOptional<ElementConstructorOpts, "x" | "y">,
) => { ) => {
const container = newElement({ 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, x: 0,
y: 0, y: 0,
...containerProps, ...containerProps,
}); });
}
const textElement: ExcalidrawTextElement = newTextElement({ const textElement: ExcalidrawTextElement = newTextElement({
x: 0, x: 0,
y: 0, y: 0,