fix: use cached width when calculating min width during resize (#4585)

This commit is contained in:
Aakansha Doshi 2022-01-13 21:35:38 +05:30 committed by GitHub
parent 1ef287027b
commit dba71e358d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View File

@ -36,6 +36,7 @@ import { Point, PointerDownState } from "../types";
import Scene from "../scene/Scene";
import {
getApproxMinLineWidth,
getBoundTextElement,
getBoundTextElementId,
handleBindTextResize,
measureText,
@ -588,15 +589,12 @@ export const resizeSingleElement = (
});
}
let minWidth = 0;
if (boundTextElementId) {
const boundTextElement = Scene.getScene(element)!.getElement(
boundTextElementId,
) as ExcalidrawTextElement;
const boundTextElement = getBoundTextElement(element);
if (boundTextElement) {
minWidth = getApproxMinLineWidth(getFontString(boundTextElement));
}
if (
resizedElement.width > minWidth &&
resizedElement.width >= minWidth &&
resizedElement.height !== 0 &&
Number.isFinite(resizedElement.x) &&
Number.isFinite(resizedElement.y)

View File

@ -362,10 +362,14 @@ export const charWidth = (() => {
};
})();
export const getApproxMinLineWidth = (font: FontString) => {
return (
measureText(DUMMY_TEXT.split("").join("\n"), font).width +
BOUND_TEXT_PADDING * 2
);
const minCharWidth = getMinCharWidth(font);
if (minCharWidth === 0) {
return (
measureText(DUMMY_TEXT.split("").join("\n"), font).width +
BOUND_TEXT_PADDING * 2
);
}
return minCharWidth + BOUND_TEXT_PADDING * 2;
};
export const getApproxMinLineHeight = (font: FontString) => {