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

View File

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