cleanup getMaxContainerHeight and getMaxContainerWidth and add specs

This commit is contained in:
Aakansha Doshi 2023-02-28 13:31:55 +05:30
parent 7722de4ef2
commit eacee9a158
4 changed files with 87 additions and 52 deletions

View file

@ -71,7 +71,10 @@ export const redrawTextBoundingBox = (
if (container) {
const containerDims = getContainerDims(container);
const maxContainerHeight = getMaxContainerHeight(container);
const maxContainerHeight = getMaxContainerHeight(
container,
textElement as ExcalidrawTextElementWithContainer,
);
let nextHeight = containerDims.height;
if (metrics.height > maxContainerHeight) {
@ -162,7 +165,10 @@ export const handleBindTextResize = (
let nextWidth = textElement.width;
const containerDims = getContainerDims(container);
const maxWidth = getMaxContainerWidth(container);
const maxHeight = getMaxContainerHeight(container);
const maxHeight = getMaxContainerHeight(
container,
textElement as ExcalidrawTextElementWithContainer,
);
let containerHeight = containerDims.height;
if (transformHandleType !== "n" && transformHandleType !== "s") {
if (text) {
@ -221,7 +227,7 @@ export const computeBoundTextPosition = (
boundTextElement: ExcalidrawTextElementWithContainer,
) => {
const containerCoords = getContainerCoords(container);
const maxContainerHeight = getMaxContainerHeight(container);
const maxContainerHeight = getMaxContainerHeight(container, boundTextElement);
const maxContainerWidth = getMaxContainerWidth(container);
let x;
@ -638,18 +644,6 @@ export const getBoundTextElementOffset = (
return BOUND_TEXT_PADDING;
};
export const getBoundTextElementPosition = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
) => {
if (isArrowElement(container)) {
return LinearElementEditor.getBoundTextElementPosition(
container,
boundTextElement,
);
}
};
export const shouldAllowVerticalAlign = (
selectedElements: NonDeletedExcalidrawElement[],
) => {
@ -741,15 +735,7 @@ export const computeContainerHeightForBoundText = (
export const getMaxContainerWidth = (container: ExcalidrawElement) => {
const width = getContainerDims(container).width;
if (isArrowElement(container)) {
const containerWidth = width - BOUND_TEXT_PADDING * 8 * 2;
if (containerWidth <= 0) {
const boundText = getBoundTextElement(container);
if (boundText) {
return boundText.width;
}
return BOUND_TEXT_PADDING * 8 * 2;
}
return containerWidth;
return width - BOUND_TEXT_PADDING * 8 * 2;
}
if (container.type === "ellipse") {
@ -766,16 +752,15 @@ export const getMaxContainerWidth = (container: ExcalidrawElement) => {
return width - BOUND_TEXT_PADDING * 2;
};
export const getMaxContainerHeight = (container: ExcalidrawElement) => {
export const getMaxContainerHeight = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
) => {
const height = getContainerDims(container).height;
if (isArrowElement(container)) {
const containerHeight = height - BOUND_TEXT_PADDING * 8 * 2;
if (containerHeight <= 0) {
const boundText = getBoundTextElement(container);
if (boundText) {
return boundText.height;
}
return BOUND_TEXT_PADDING * 8 * 2;
return boundTextElement.height;
}
return height;
}