reduce safari LS

This commit is contained in:
Aakansha Doshi 2023-04-04 20:03:34 +05:30
parent 1c66f85ec9
commit 6001f59d38
2 changed files with 54 additions and 3 deletions

View file

@ -347,6 +347,41 @@ export const measureBaseline = (
return baseline;
};
export const measureDOMHeight = (
text: string,
font: FontString,
lineHeight: ExcalidrawTextElement["lineHeight"],
wrapInContainer?: boolean,
) => {
const container = document.createElement("div");
container.style.position = "absolute";
container.style.whiteSpace = "pre";
container.style.font = font;
container.style.minHeight = "1em";
if (wrapInContainer) {
container.style.overflow = "hidden";
container.style.wordBreak = "break-word";
container.style.whiteSpace = "pre-wrap";
}
container.style.lineHeight = String(lineHeight);
container.innerText = text;
// Baseline is important for positioning text on canvas
document.body.appendChild(container);
const span = document.createElement("span");
span.style.display = "inline-block";
span.style.overflow = "hidden";
span.style.width = "1px";
span.style.height = "1px";
container.appendChild(span);
const domHeight = container.offsetHeight;
document.body.removeChild(container);
return domHeight;
};
/**
* To get unitless line-height (if unknown) we can calculate it by dividing
* height-per-line by fontSize.
@ -366,7 +401,9 @@ export const getLineHeightInPx = (
fontSize: ExcalidrawTextElement["fontSize"],
lineHeight: ExcalidrawTextElement["lineHeight"],
) => {
return fontSize * lineHeight;
const res = fontSize * lineHeight;
return res;
};
// FIXME rename to getApproxMinContainerHeight