mirror of
https://github.com/excalidraw/excalidraw.git
synced 2024-11-10 11:35:52 +01:00
fix: scale font correctly when using shift (#5935)
* fix: scale font correctly when using shift * fix * Empty-Commit * Add spec * fix
This commit is contained in:
parent
88b2f4707d
commit
66bbfda460
@ -20,6 +20,7 @@ import {
|
|||||||
getCommonBoundingBox,
|
getCommonBoundingBox,
|
||||||
} from "./bounds";
|
} from "./bounds";
|
||||||
import {
|
import {
|
||||||
|
isBoundToContainer,
|
||||||
isFreeDrawElement,
|
isFreeDrawElement,
|
||||||
isLinearElement,
|
isLinearElement,
|
||||||
isTextElement,
|
isTextElement,
|
||||||
@ -39,9 +40,11 @@ import {
|
|||||||
getApproxMinLineWidth,
|
getApproxMinLineWidth,
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getBoundTextElementId,
|
getBoundTextElementId,
|
||||||
|
getContainerElement,
|
||||||
handleBindTextResize,
|
handleBindTextResize,
|
||||||
measureText,
|
measureText,
|
||||||
} from "./textElement";
|
} from "./textElement";
|
||||||
|
import { getMaxContainerWidth } from "./newElement";
|
||||||
|
|
||||||
export const normalizeAngle = (angle: number): number => {
|
export const normalizeAngle = (angle: number): number => {
|
||||||
if (angle >= 2 * Math.PI) {
|
if (angle >= 2 * Math.PI) {
|
||||||
@ -182,14 +185,21 @@ const measureFontSizeFromWH = (
|
|||||||
nextHeight: number,
|
nextHeight: number,
|
||||||
): { size: number; baseline: number } | null => {
|
): { size: number; baseline: number } | null => {
|
||||||
// We only use width to scale font on resize
|
// We only use width to scale font on resize
|
||||||
const nextFontSize = element.fontSize * (nextWidth / element.width);
|
let width = element.width;
|
||||||
|
|
||||||
|
const hasContainer = isBoundToContainer(element);
|
||||||
|
if (hasContainer) {
|
||||||
|
const container = getContainerElement(element)!;
|
||||||
|
width = getMaxContainerWidth(container);
|
||||||
|
}
|
||||||
|
const nextFontSize = element.fontSize * (nextWidth / width);
|
||||||
if (nextFontSize < MIN_FONT_SIZE) {
|
if (nextFontSize < MIN_FONT_SIZE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const metrics = measureText(
|
const metrics = measureText(
|
||||||
element.text,
|
element.text,
|
||||||
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
|
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
|
||||||
element.containerId ? element.width : null,
|
hasContainer ? width : null,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
size: nextFontSize,
|
size: nextFontSize,
|
||||||
|
@ -1099,5 +1099,27 @@ describe("textWysiwyg", () => {
|
|||||||
{ id: text.id, type: "text" },
|
{ id: text.id, type: "text" },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should scale font size correctly when resizing using shift", async () => {
|
||||||
|
Keyboard.keyPress(KEYS.ENTER);
|
||||||
|
|
||||||
|
const editor = document.querySelector(
|
||||||
|
".excalidraw-textEditorContainer > textarea",
|
||||||
|
) as HTMLTextAreaElement;
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
fireEvent.change(editor, { target: { value: "Hello" } });
|
||||||
|
editor.blur();
|
||||||
|
const textElement = h.elements[1] as ExcalidrawTextElement;
|
||||||
|
expect(rectangle.width).toBe(90);
|
||||||
|
expect(rectangle.height).toBe(75);
|
||||||
|
expect(textElement.fontSize).toBe(20);
|
||||||
|
|
||||||
|
resize(rectangle, "ne", [rectangle.x + 100, rectangle.y - 50], {
|
||||||
|
shift: true,
|
||||||
|
});
|
||||||
|
expect(rectangle.width).toBe(200);
|
||||||
|
expect(rectangle.height).toBe(166.66666666666669);
|
||||||
|
expect(textElement.fontSize).toBe(47.5);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -139,6 +139,9 @@ export const isBoundToContainer = (
|
|||||||
element: ExcalidrawElement | null,
|
element: ExcalidrawElement | null,
|
||||||
): element is ExcalidrawTextElementWithContainer => {
|
): element is ExcalidrawTextElementWithContainer => {
|
||||||
return (
|
return (
|
||||||
element !== null && isTextElement(element) && element.containerId !== null
|
element !== null &&
|
||||||
|
"containerId" in element &&
|
||||||
|
element.containerId !== null &&
|
||||||
|
isTextElement(element)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user