mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix: set the dimensions of bound text correctly (#5710)
* fix: set the dimensions of bound text correctly * use original Text when wrapping * fix text align * fix specs * fix * newline
This commit is contained in:
parent
8636ef1017
commit
4cb6f09559
8 changed files with 79 additions and 57 deletions
|
@ -28,6 +28,7 @@ import {
|
|||
} from "../actions/actionProperties";
|
||||
import { actionZoomIn, actionZoomOut } from "../actions/actionCanvas";
|
||||
import App from "../components/App";
|
||||
import { getMaxContainerWidth } from "./newElement";
|
||||
|
||||
const normalizeText = (text: string) => {
|
||||
return (
|
||||
|
@ -114,13 +115,13 @@ export const textWysiwyg = ({
|
|||
getFontString(updatedTextElement),
|
||||
);
|
||||
if (updatedTextElement && isTextElement(updatedTextElement)) {
|
||||
let coordX = updatedTextElement.x;
|
||||
const coordX = updatedTextElement.x;
|
||||
let coordY = updatedTextElement.y;
|
||||
const container = getContainerElement(updatedTextElement);
|
||||
let maxWidth = updatedTextElement.width;
|
||||
|
||||
let maxHeight = updatedTextElement.height;
|
||||
let width = updatedTextElement.width;
|
||||
const width = updatedTextElement.width;
|
||||
// Set to element height by default since that's
|
||||
// what is going to be used for unbounded text
|
||||
let height = updatedTextElement.height;
|
||||
|
@ -146,10 +147,6 @@ export const textWysiwyg = ({
|
|||
}
|
||||
maxWidth = containerDims.width - BOUND_TEXT_PADDING * 2;
|
||||
maxHeight = containerDims.height - BOUND_TEXT_PADDING * 2;
|
||||
width = maxWidth;
|
||||
// The coordinates of text box set a distance of
|
||||
// 5px to preserve padding
|
||||
coordX = container.x + BOUND_TEXT_PADDING;
|
||||
// autogrow container height if text exceeds
|
||||
if (height > maxHeight) {
|
||||
const diff = Math.min(height - maxHeight, approxLineHeight);
|
||||
|
@ -212,7 +209,7 @@ export const textWysiwyg = ({
|
|||
font: getFontString(updatedTextElement),
|
||||
// must be defined *after* font ¯\_(ツ)_/¯
|
||||
lineHeight: `${lineHeight}px`,
|
||||
width: `${width}px`,
|
||||
width: `${Math.min(width, maxWidth)}px`,
|
||||
height: `${height}px`,
|
||||
left: `${viewportX}px`,
|
||||
top: `${viewportY}px`,
|
||||
|
@ -229,7 +226,6 @@ export const textWysiwyg = ({
|
|||
color: updatedTextElement.strokeColor,
|
||||
opacity: updatedTextElement.opacity / 100,
|
||||
filter: "var(--theme-filter)",
|
||||
maxWidth: `${maxWidth}px`,
|
||||
maxHeight: `${editorMaxHeight}px`,
|
||||
});
|
||||
// For some reason updating font attribute doesn't set font family
|
||||
|
@ -301,13 +297,14 @@ export const textWysiwyg = ({
|
|||
// doubles the height as soon as user starts typing
|
||||
if (isBoundToContainer(element) && lines > 1) {
|
||||
let height = "auto";
|
||||
|
||||
editable.style.height = "0px";
|
||||
let heightSet = false;
|
||||
if (lines === 2) {
|
||||
const container = getContainerElement(element);
|
||||
const actualLineCount = wrapText(
|
||||
editable.value,
|
||||
font,
|
||||
container!.width,
|
||||
getMaxContainerWidth(container!),
|
||||
).split("\n").length;
|
||||
// This is browser behaviour when setting height to "auto"
|
||||
// It sets the height needed for 2 lines even if actual
|
||||
|
@ -316,10 +313,13 @@ export const textWysiwyg = ({
|
|||
// so single line aligns vertically when deleting
|
||||
if (actualLineCount === 1) {
|
||||
height = `${editable.scrollHeight / 2}px`;
|
||||
editable.style.height = height;
|
||||
heightSet = true;
|
||||
}
|
||||
}
|
||||
editable.style.height = height;
|
||||
editable.style.height = `${editable.scrollHeight}px`;
|
||||
if (!heightSet) {
|
||||
editable.style.height = `${editable.scrollHeight}px`;
|
||||
}
|
||||
}
|
||||
onChange(normalizeText(editable.value));
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue