rename to getLineHeight and use the same line height for regular text elements

This commit is contained in:
Aakansha Doshi 2023-03-01 13:38:03 +05:30
parent 91f6e87317
commit 0e54994187
5 changed files with 21 additions and 30 deletions

View File

@ -259,7 +259,7 @@ import throttle from "lodash.throttle";
import { fileOpen, FileSystemHandle } from "../data/filesystem"; import { fileOpen, FileSystemHandle } from "../data/filesystem";
import { import {
bindTextToShapeAfterDuplication, bindTextToShapeAfterDuplication,
getApproxLineHeight, getLineHeight,
getApproxMinLineHeight, getApproxMinLineHeight,
getApproxMinLineWidth, getApproxMinLineWidth,
getBoundTextElement, getBoundTextElement,
@ -1734,7 +1734,7 @@ class App extends React.Component<AppProps, AppState> {
// add paragraph only if previous line was not empty, IOW don't add // add paragraph only if previous line was not empty, IOW don't add
// more than one empty line // more than one empty line
if (prevLine) { if (prevLine) {
const defaultLineHeight = getApproxLineHeight( const defaultLineHeight = getLineHeight(
getFontString({ getFontString({
fontSize: textElementProps.fontSize, fontSize: textElementProps.fontSize,
fontFamily: textElementProps.fontFamily, fontFamily: textElementProps.fontFamily,

View File

@ -275,17 +275,17 @@ export const measureText = (text: string, font: FontString) => {
}; };
const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase(); const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase();
const cacheApproxLineHeight: { [key: FontString]: number } = {}; const cacheLineHeight: { [key: FontString]: number } = {};
export const getApproxLineHeight = (font: FontString) => { export const getLineHeight = (font: FontString) => {
if (cacheApproxLineHeight[font]) { if (cacheLineHeight[font]) {
return cacheApproxLineHeight[font]; return cacheLineHeight[font];
} }
const fontSize = parseInt(font); const fontSize = parseInt(font);
// Calculate line height relative to font size // Calculate line height relative to font size
cacheApproxLineHeight[font] = fontSize * 1.2; cacheLineHeight[font] = fontSize * 1.2;
return cacheApproxLineHeight[font]; return cacheLineHeight[font];
}; };
let canvas: HTMLCanvasElement | undefined; let canvas: HTMLCanvasElement | undefined;
@ -318,7 +318,7 @@ export const getTextWidth = (text: string, font: FontString) => {
export const getTextHeight = (text: string, font: FontString) => { export const getTextHeight = (text: string, font: FontString) => {
const lines = text.replace(/\r\n?/g, "\n").split("\n"); const lines = text.replace(/\r\n?/g, "\n").split("\n");
const lineHeight = getApproxLineHeight(font); const lineHeight = getLineHeight(font);
return lineHeight * lines.length; return lineHeight * lines.length;
}; };
@ -473,7 +473,7 @@ export const getApproxMinLineWidth = (font: FontString) => {
}; };
export const getApproxMinLineHeight = (font: FontString) => { export const getApproxMinLineHeight = (font: FontString) => {
return getApproxLineHeight(font) + BOUND_TEXT_PADDING * 2; return getLineHeight(font) + BOUND_TEXT_PADDING * 2;
}; };
export const getMinCharWidth = (font: FontString) => { export const getMinCharWidth = (font: FontString) => {

View File

@ -22,7 +22,7 @@ import {
import { AppState } from "../types"; import { AppState } from "../types";
import { mutateElement } from "./mutateElement"; import { mutateElement } from "./mutateElement";
import { import {
getApproxLineHeight, getLineHeight,
getBoundTextElementId, getBoundTextElementId,
getContainerDims, getContainerDims,
getContainerElement, getContainerElement,
@ -151,9 +151,8 @@ export const textWysiwyg = ({
return; return;
} }
const { textAlign, verticalAlign } = updatedTextElement; const { textAlign, verticalAlign } = updatedTextElement;
const approxLineHeight = getApproxLineHeight( const lineHeight = getLineHeight(getFontString(updatedTextElement));
getFontString(updatedTextElement),
);
if (updatedTextElement && isTextElement(updatedTextElement)) { if (updatedTextElement && isTextElement(updatedTextElement)) {
let coordX = updatedTextElement.x; let coordX = updatedTextElement.x;
let coordY = updatedTextElement.y; let coordY = updatedTextElement.y;
@ -211,10 +210,7 @@ export const textWysiwyg = ({
// autogrow container height if text exceeds // autogrow container height if text exceeds
if (!isArrowElement(container) && textElementHeight > maxHeight) { if (!isArrowElement(container) && textElementHeight > maxHeight) {
const diff = Math.min( const diff = Math.min(textElementHeight - maxHeight, lineHeight);
textElementHeight - maxHeight,
approxLineHeight,
);
mutateElement(container, { height: containerDims.height + diff }); mutateElement(container, { height: containerDims.height + diff });
return; return;
} else if ( } else if (
@ -224,10 +220,7 @@ export const textWysiwyg = ({
containerDims.height > originalContainerData.height && containerDims.height > originalContainerData.height &&
textElementHeight < maxHeight textElementHeight < maxHeight
) { ) {
const diff = Math.min( const diff = Math.min(maxHeight - textElementHeight, lineHeight);
maxHeight - textElementHeight,
approxLineHeight,
);
mutateElement(container, { height: containerDims.height - diff }); mutateElement(container, { height: containerDims.height - diff });
} else { } else {
const { y } = computeBoundTextPosition( const { y } = computeBoundTextPosition(
@ -257,9 +250,7 @@ export const textWysiwyg = ({
} }
const lines = updatedTextElement.originalText.split("\n"); const lines = updatedTextElement.originalText.split("\n");
const lineHeight = updatedTextElement.containerId
? approxLineHeight
: updatedTextElement.height / lines.length;
if (!container) { if (!container) {
maxWidth = (appState.width - 8 - viewportX) / appState.zoom.value; maxWidth = (appState.width - 8 - viewportX) / appState.zoom.value;
textElementWidth = Math.min(textElementWidth, maxWidth); textElementWidth = Math.min(textElementWidth, maxWidth);

View File

@ -39,7 +39,7 @@ import {
} from "../constants"; } from "../constants";
import { getStroke, StrokeOptions } from "perfect-freehand"; import { getStroke, StrokeOptions } from "perfect-freehand";
import { import {
getApproxLineHeight, getLineHeight,
getBoundTextElement, getBoundTextElement,
getContainerElement, getContainerElement,
} from "../element/textElement"; } from "../element/textElement";
@ -276,7 +276,7 @@ const drawElementOnCanvas = (
// Canvas does not support multiline text by default // Canvas does not support multiline text by default
const lines = element.text.replace(/\r\n?/g, "\n").split("\n"); const lines = element.text.replace(/\r\n?/g, "\n").split("\n");
const lineHeight = element.containerId const lineHeight = element.containerId
? getApproxLineHeight(getFontString(element)) ? getLineHeight(getFontString(element))
: element.height / lines.length; : element.height / lines.length;
const horizontalOffset = const horizontalOffset =
element.textAlign === "center" element.textAlign === "center"

View File

@ -3,7 +3,7 @@ import { render, waitFor, GlobalTestState } from "./test-utils";
import { Pointer, Keyboard } from "./helpers/ui"; import { Pointer, Keyboard } from "./helpers/ui";
import ExcalidrawApp from "../excalidraw-app"; import ExcalidrawApp from "../excalidraw-app";
import { KEYS } from "../keys"; import { KEYS } from "../keys";
import { getApproxLineHeight } from "../element/textElement"; import { getLineHeight } from "../element/textElement";
import { getFontString } from "../utils"; import { getFontString } from "../utils";
import { getElementBounds } from "../element"; import { getElementBounds } from "../element";
import { NormalizedZoomValue } from "../types"; import { NormalizedZoomValue } from "../types";
@ -119,7 +119,7 @@ describe("paste text as single lines", () => {
it("should space items correctly", async () => { it("should space items correctly", async () => {
const text = "hkhkjhki\njgkjhffjh\njgkjhffjh"; const text = "hkhkjhki\njgkjhffjh\njgkjhffjh";
const lineHeight = const lineHeight =
getApproxLineHeight( getLineHeight(
getFontString({ getFontString({
fontSize: h.app.state.currentItemFontSize, fontSize: h.app.state.currentItemFontSize,
fontFamily: h.app.state.currentItemFontFamily, fontFamily: h.app.state.currentItemFontFamily,
@ -143,7 +143,7 @@ describe("paste text as single lines", () => {
it("should leave a space for blank new lines", async () => { it("should leave a space for blank new lines", async () => {
const text = "hkhkjhki\n\njgkjhffjh"; const text = "hkhkjhki\n\njgkjhffjh";
const lineHeight = const lineHeight =
getApproxLineHeight( getLineHeight(
getFontString({ getFontString({
fontSize: h.app.state.currentItemFontSize, fontSize: h.app.state.currentItemFontSize,
fontFamily: h.app.state.currentItemFontFamily, fontFamily: h.app.state.currentItemFontFamily,