Revert "use DOM height only for safari to fix LS"

This reverts commit d75889238da59b7954ec3a6ac2c29dc0ba420635.
This commit is contained in:
Aakansha Doshi 2023-04-07 12:06:48 +05:30
parent d75889238d
commit ca5e246074
3 changed files with 30 additions and 18 deletions

View File

@ -289,13 +289,10 @@ export const measureText = (
.map((x) => x || " ") .map((x) => x || " ")
.join("\n"); .join("\n");
const fontSize = parseFloat(font); const fontSize = parseFloat(font);
let height = getTextHeight(text, fontSize, lineHeight); const height = getTextHeight(text, fontSize, lineHeight);
const width = getTextWidth(text, font); const width = getTextWidth(text, font);
const domMetrics = getDOMMetrics(text, font, lineHeight); const { baseline } = getDOMMetrics(text, font, lineHeight);
if (isSafari) { return { width, height, baseline };
height = domMetrics.height;
}
return { width, height, baseline: domMetrics.baseline };
}; };
export const getDOMMetrics = ( export const getDOMMetrics = (
@ -316,6 +313,7 @@ export const getDOMMetrics = (
} }
container.style.lineHeight = String(lineHeight); container.style.lineHeight = String(lineHeight);
const canvasHeight = getTextHeight(text, parseFloat(font), lineHeight);
container.innerText = text; container.innerText = text;
@ -328,9 +326,23 @@ export const getDOMMetrics = (
span.style.width = "1px"; span.style.width = "1px";
span.style.height = "1px"; span.style.height = "1px";
container.appendChild(span); container.appendChild(span);
const baseline = span.offsetTop + span.offsetHeight; let baseline = span.offsetTop + span.offsetHeight;
const height = container.offsetHeight; const height = container.offsetHeight;
if (isSafari) {
// In Safari sometimes DOM height could be less than canvas height due to
// which text could go out of the bounding box hence shifting the baseline
// to make sure text is rendered correctly
if (canvasHeight > height) {
baseline += canvasHeight - height;
}
// In Safari sometimes DOM height could be more than canvas height due to
// which text could go out of the bounding box hence shifting the baseline
// to make sure text is rendered correctly
if (height > canvasHeight) {
baseline -= height - canvasHeight;
}
}
document.body.removeChild(container); document.body.removeChild(container);
return { baseline, height }; return { baseline, height };
}; };

View File

@ -35,6 +35,8 @@ import {
getMaxContainerHeight, getMaxContainerHeight,
getMaxContainerWidth, getMaxContainerWidth,
computeContainerDimensionForBoundText, computeContainerDimensionForBoundText,
getDOMMetrics,
splitIntoLines,
} from "./textElement"; } from "./textElement";
import { import {
actionDecreaseFontSize, actionDecreaseFontSize,
@ -44,7 +46,6 @@ import { actionZoomIn, actionZoomOut } from "../actions/actionCanvas";
import App from "../components/App"; import App from "../components/App";
import { LinearElementEditor } from "./linearElementEditor"; import { LinearElementEditor } from "./linearElementEditor";
import { parseClipboard } from "../clipboard"; import { parseClipboard } from "../clipboard";
import { splitIntoLines } from "./textElement";
const getTransform = ( const getTransform = (
width: number, width: number,
@ -272,13 +273,16 @@ export const textWysiwyg = ({
} else { } else {
textElementWidth += 0.5; textElementWidth += 0.5;
} }
const { height: domHeight } = getDOMMetrics(
updatedTextElement.text,
getFontString(updatedTextElement),
updatedTextElement.lineHeight,
);
let lineHeight = element.lineHeight; let lineHeight = element.lineHeight;
if (isSafari) { if (isSafari && domHeight > textElementHeight) {
//@ts-ignore lineHeight = (Math.floor(element.lineHeight * element.fontSize) /
lineHeight = `${ element.fontSize) as ExcalidrawTextElement["lineHeight"];
element.height / splitIntoLines(element.text).length
}px`;
} }
// Make sure text editor height doesn't go beyond viewport // Make sure text editor height doesn't go beyond viewport

View File

@ -34,7 +34,6 @@ import { AppState, BinaryFiles, Zoom } from "../types";
import { getDefaultAppState } from "../appState"; import { getDefaultAppState } from "../appState";
import { import {
BOUND_TEXT_PADDING, BOUND_TEXT_PADDING,
isSafari,
MAX_DECIMALS_FOR_SVG_EXPORT, MAX_DECIMALS_FOR_SVG_EXPORT,
MIME_TYPES, MIME_TYPES,
SVG_NS, SVG_NS,
@ -286,13 +285,10 @@ const drawElementOnCanvas = (
: element.textAlign === "right" : element.textAlign === "right"
? element.width ? element.width
: 0; : 0;
let lineHeightPx = getLineHeightInPx( const lineHeightPx = getLineHeightInPx(
element.fontSize, element.fontSize,
element.lineHeight, element.lineHeight,
); );
if (isSafari) {
lineHeightPx = element.height / lines.length;
}
const verticalOffset = element.height - element.baseline; const verticalOffset = element.height - element.baseline;
for (let index = 0; index < lines.length; index++) { for (let index = 0; index < lines.length; index++) {
context.fillText( context.fillText(