Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 993294ac08 | |||
| f584416c9a | |||
| 87b0c7a679 | |||
| ee8fff8e8b | |||
| b799490ece | |||
| 83383977f5 | |||
| ac4c8b3ca7 | |||
| 5c8941467d | |||
| 0726911fa6 | |||
| fd18896293 | |||
| e900cb0b64 | |||
| 54bf3d9092 | |||
| 15f19835fe | |||
| 96c4cff805 | |||
| 1ac580136d | |||
| 8c89fdfa51 | |||
| 0e54994187 | |||
| 91f6e87317 | |||
| a05db6864e | |||
| eacee9a158 | |||
| 7722de4ef2 | |||
| 0a295e523b | |||
| 60deddb0e2 |
@@ -4,9 +4,9 @@ import { mutateElement } from "../element/mutateElement";
|
|||||||
import {
|
import {
|
||||||
computeContainerDimensionForBoundText,
|
computeContainerDimensionForBoundText,
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
measureText,
|
|
||||||
redrawTextBoundingBox,
|
redrawTextBoundingBox,
|
||||||
} from "../element/textElement";
|
} from "../element/textElement";
|
||||||
|
import { measureText } from "../element/textMeasurements";
|
||||||
import {
|
import {
|
||||||
getOriginalContainerHeightFromCache,
|
getOriginalContainerHeightFromCache,
|
||||||
resetOriginalContainerCache,
|
resetOriginalContainerCache,
|
||||||
@@ -45,6 +45,7 @@ export const actionUnbindText = register({
|
|||||||
const { width, height } = measureText(
|
const { width, height } = measureText(
|
||||||
boundTextElement.originalText,
|
boundTextElement.originalText,
|
||||||
getFontString(boundTextElement),
|
getFontString(boundTextElement),
|
||||||
|
boundTextElement.lineHeight,
|
||||||
);
|
);
|
||||||
const originalContainerHeight = getOriginalContainerHeightFromCache(
|
const originalContainerHeight = getOriginalContainerHeightFromCache(
|
||||||
element.id,
|
element.id,
|
||||||
@@ -239,15 +240,23 @@ export const actionCreateContainerFromText = register({
|
|||||||
linearElementIds.includes(ele.id),
|
linearElementIds.includes(ele.id),
|
||||||
) as ExcalidrawLinearElement[];
|
) as ExcalidrawLinearElement[];
|
||||||
linearElements.forEach((ele) => {
|
linearElements.forEach((ele) => {
|
||||||
let startBinding = null;
|
let startBinding = ele.startBinding;
|
||||||
let endBinding = null;
|
let endBinding = ele.endBinding;
|
||||||
if (ele.startBinding) {
|
|
||||||
startBinding = { ...ele.startBinding, elementId: container.id };
|
if (startBinding?.elementId === textElement.id) {
|
||||||
|
startBinding = {
|
||||||
|
...startBinding,
|
||||||
|
elementId: container.id,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if (ele.endBinding) {
|
|
||||||
endBinding = { ...ele.endBinding, elementId: container.id };
|
if (endBinding?.elementId === textElement.id) {
|
||||||
|
endBinding = { ...endBinding, elementId: container.id };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startBinding || endBinding) {
|
||||||
|
mutateElement(ele, { startBinding, endBinding });
|
||||||
}
|
}
|
||||||
mutateElement(ele, { startBinding, endBinding });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ import {
|
|||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
} from "../element/textElement";
|
} from "../element/textElement";
|
||||||
|
import { getDefaultLineHeight } from "../element/textMeasurements";
|
||||||
import {
|
import {
|
||||||
isBoundToContainer,
|
isBoundToContainer,
|
||||||
isLinearElement,
|
isLinearElement,
|
||||||
@@ -637,6 +638,7 @@ export const actionChangeFontFamily = register({
|
|||||||
oldElement,
|
oldElement,
|
||||||
{
|
{
|
||||||
fontFamily: value,
|
fontFamily: value,
|
||||||
|
lineHeight: getDefaultLineHeight(value),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
redrawTextBoundingBox(newElement, getContainerElement(oldElement));
|
redrawTextBoundingBox(newElement, getContainerElement(oldElement));
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
getDefaultRoundnessTypeForElement,
|
getDefaultRoundnessTypeForElement,
|
||||||
} from "../element/typeChecks";
|
} from "../element/typeChecks";
|
||||||
import { getSelectedElements } from "../scene";
|
import { getSelectedElements } from "../scene";
|
||||||
|
import { getDefaultLineHeight } from "../element/textMeasurements";
|
||||||
|
|
||||||
// `copiedStyles` is exported only for tests.
|
// `copiedStyles` is exported only for tests.
|
||||||
export let copiedStyles: string = "{}";
|
export let copiedStyles: string = "{}";
|
||||||
@@ -92,12 +93,18 @@ export const actionPasteStyles = register({
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (isTextElement(newElement)) {
|
if (isTextElement(newElement)) {
|
||||||
|
const fontSize =
|
||||||
|
elementStylesToCopyFrom?.fontSize || DEFAULT_FONT_SIZE;
|
||||||
|
const fontFamily =
|
||||||
|
elementStylesToCopyFrom?.fontFamily || DEFAULT_FONT_FAMILY;
|
||||||
newElement = newElementWith(newElement, {
|
newElement = newElementWith(newElement, {
|
||||||
fontSize: elementStylesToCopyFrom?.fontSize || DEFAULT_FONT_SIZE,
|
fontSize,
|
||||||
fontFamily:
|
fontFamily,
|
||||||
elementStylesToCopyFrom?.fontFamily || DEFAULT_FONT_FAMILY,
|
|
||||||
textAlign:
|
textAlign:
|
||||||
elementStylesToCopyFrom?.textAlign || DEFAULT_TEXT_ALIGN,
|
elementStylesToCopyFrom?.textAlign || DEFAULT_TEXT_ALIGN,
|
||||||
|
lineHeight:
|
||||||
|
elementStylesToCopyFrom.lineHeight ||
|
||||||
|
getDefaultLineHeight(fontFamily),
|
||||||
});
|
});
|
||||||
let container = null;
|
let container = null;
|
||||||
if (newElement.containerId) {
|
if (newElement.containerId) {
|
||||||
|
|||||||
+29
-18
@@ -260,17 +260,20 @@ import throttle from "lodash.throttle";
|
|||||||
import { fileOpen, FileSystemHandle } from "../data/filesystem";
|
import { fileOpen, FileSystemHandle } from "../data/filesystem";
|
||||||
import {
|
import {
|
||||||
bindTextToShapeAfterDuplication,
|
bindTextToShapeAfterDuplication,
|
||||||
getApproxLineHeight,
|
|
||||||
getApproxMinLineHeight,
|
|
||||||
getApproxMinLineWidth,
|
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getContainerCenter,
|
getContainerCenter,
|
||||||
getContainerDims,
|
getContainerDims,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
getTextBindableContainerAtPosition,
|
getTextBindableContainerAtPosition,
|
||||||
isMeasureTextSupported,
|
|
||||||
isValidTextContainer,
|
isValidTextContainer,
|
||||||
} from "../element/textElement";
|
} from "../element/textElement";
|
||||||
|
import {
|
||||||
|
getApproxMinContainerHeight,
|
||||||
|
getApproxMinContainerWidth,
|
||||||
|
isMeasureTextSupported,
|
||||||
|
getLineHeightInPx,
|
||||||
|
getDefaultLineHeight,
|
||||||
|
} from "../element/textMeasurements";
|
||||||
import { isHittingElementNotConsideringBoundingBox } from "../element/collision";
|
import { isHittingElementNotConsideringBoundingBox } from "../element/collision";
|
||||||
import {
|
import {
|
||||||
normalizeLink,
|
normalizeLink,
|
||||||
@@ -1731,12 +1734,14 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
(acc: ExcalidrawTextElement[], line, idx) => {
|
(acc: ExcalidrawTextElement[], line, idx) => {
|
||||||
const text = line.trim();
|
const text = line.trim();
|
||||||
|
|
||||||
|
const lineHeight = getDefaultLineHeight(textElementProps.fontFamily);
|
||||||
if (text.length) {
|
if (text.length) {
|
||||||
const element = newTextElement({
|
const element = newTextElement({
|
||||||
...textElementProps,
|
...textElementProps,
|
||||||
x,
|
x,
|
||||||
y: currentY,
|
y: currentY,
|
||||||
text,
|
text,
|
||||||
|
lineHeight,
|
||||||
});
|
});
|
||||||
acc.push(element);
|
acc.push(element);
|
||||||
currentY += element.height + LINE_GAP;
|
currentY += element.height + LINE_GAP;
|
||||||
@@ -1745,14 +1750,9 @@ 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(
|
currentY +=
|
||||||
getFontString({
|
getLineHeightInPx(textElementProps.fontSize, lineHeight) +
|
||||||
fontSize: textElementProps.fontSize,
|
LINE_GAP;
|
||||||
fontFamily: textElementProps.fontFamily,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
currentY += defaultLineHeight + LINE_GAP;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2607,6 +2607,13 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
existingTextElement = this.getTextElementAtPosition(sceneX, sceneY);
|
existingTextElement = this.getTextElementAtPosition(sceneX, sceneY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fontFamily =
|
||||||
|
existingTextElement?.fontFamily || this.state.currentItemFontFamily;
|
||||||
|
|
||||||
|
const lineHeight =
|
||||||
|
existingTextElement?.lineHeight || getDefaultLineHeight(fontFamily);
|
||||||
|
const fontSize = this.state.currentItemFontSize;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!existingTextElement &&
|
!existingTextElement &&
|
||||||
shouldBindToContainer &&
|
shouldBindToContainer &&
|
||||||
@@ -2614,11 +2621,14 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
!isArrowElement(container)
|
!isArrowElement(container)
|
||||||
) {
|
) {
|
||||||
const fontString = {
|
const fontString = {
|
||||||
fontSize: this.state.currentItemFontSize,
|
fontSize,
|
||||||
fontFamily: this.state.currentItemFontFamily,
|
fontFamily,
|
||||||
};
|
};
|
||||||
const minWidth = getApproxMinLineWidth(getFontString(fontString));
|
const minWidth = getApproxMinContainerWidth(
|
||||||
const minHeight = getApproxMinLineHeight(getFontString(fontString));
|
getFontString(fontString),
|
||||||
|
lineHeight,
|
||||||
|
);
|
||||||
|
const minHeight = getApproxMinContainerHeight(fontSize, lineHeight);
|
||||||
const containerDims = getContainerDims(container);
|
const containerDims = getContainerDims(container);
|
||||||
const newHeight = Math.max(containerDims.height, minHeight);
|
const newHeight = Math.max(containerDims.height, minHeight);
|
||||||
const newWidth = Math.max(containerDims.width, minWidth);
|
const newWidth = Math.max(containerDims.width, minWidth);
|
||||||
@@ -2652,8 +2662,8 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
opacity: this.state.currentItemOpacity,
|
opacity: this.state.currentItemOpacity,
|
||||||
roundness: null,
|
roundness: null,
|
||||||
text: "",
|
text: "",
|
||||||
fontSize: this.state.currentItemFontSize,
|
fontSize,
|
||||||
fontFamily: this.state.currentItemFontFamily,
|
fontFamily,
|
||||||
textAlign: parentCenterPosition
|
textAlign: parentCenterPosition
|
||||||
? "center"
|
? "center"
|
||||||
: this.state.currentItemTextAlign,
|
: this.state.currentItemTextAlign,
|
||||||
@@ -2663,6 +2673,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
containerId: shouldBindToContainer ? container?.id : undefined,
|
containerId: shouldBindToContainer ? container?.id : undefined,
|
||||||
groupIds: container?.groupIds ?? [],
|
groupIds: container?.groupIds ?? [],
|
||||||
locked: false,
|
locked: false,
|
||||||
|
lineHeight,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existingTextElement && shouldBindToContainer && container) {
|
if (!existingTextElement && shouldBindToContainer && container) {
|
||||||
|
|||||||
+3
-1
@@ -89,7 +89,9 @@ export const exportCanvas = async (
|
|||||||
return await fileSave(blob, {
|
return await fileSave(blob, {
|
||||||
description: "Export to PNG",
|
description: "Export to PNG",
|
||||||
name,
|
name,
|
||||||
extension: appState.exportEmbedScene ? "excalidraw.png" : "png",
|
// FIXME reintroduce `excalidraw.png` when most people upgrade away
|
||||||
|
// from 111.0.5563.64 (arm64), see #6349
|
||||||
|
extension: /* appState.exportEmbedScene ? "excalidraw.png" : */ "png",
|
||||||
fileHandle,
|
fileHandle,
|
||||||
});
|
});
|
||||||
} else if (type === "clipboard") {
|
} else if (type === "clipboard") {
|
||||||
|
|||||||
+22
-3
@@ -35,6 +35,10 @@ import { getUpdatedTimestamp, updateActiveTool } from "../utils";
|
|||||||
import { arrayToMap } from "../utils";
|
import { arrayToMap } from "../utils";
|
||||||
import oc from "open-color";
|
import oc from "open-color";
|
||||||
import { MarkOptional, Mutable } from "../utility-types";
|
import { MarkOptional, Mutable } from "../utility-types";
|
||||||
|
import {
|
||||||
|
detectLineHeight,
|
||||||
|
getDefaultLineHeight,
|
||||||
|
} from "../element/textMeasurements";
|
||||||
|
|
||||||
type RestoredAppState = Omit<
|
type RestoredAppState = Omit<
|
||||||
AppState,
|
AppState,
|
||||||
@@ -165,17 +169,32 @@ const restoreElement = (
|
|||||||
const [fontPx, _fontFamily]: [string, string] = (
|
const [fontPx, _fontFamily]: [string, string] = (
|
||||||
element as any
|
element as any
|
||||||
).font.split(" ");
|
).font.split(" ");
|
||||||
fontSize = parseInt(fontPx, 10);
|
fontSize = parseFloat(fontPx);
|
||||||
fontFamily = getFontFamilyByName(_fontFamily);
|
fontFamily = getFontFamilyByName(_fontFamily);
|
||||||
}
|
}
|
||||||
|
const text = element.text ?? "";
|
||||||
|
|
||||||
element = restoreElementWithProperties(element, {
|
element = restoreElementWithProperties(element, {
|
||||||
fontSize,
|
fontSize,
|
||||||
fontFamily,
|
fontFamily,
|
||||||
text: element.text ?? "",
|
text,
|
||||||
textAlign: element.textAlign || DEFAULT_TEXT_ALIGN,
|
textAlign: element.textAlign || DEFAULT_TEXT_ALIGN,
|
||||||
verticalAlign: element.verticalAlign || DEFAULT_VERTICAL_ALIGN,
|
verticalAlign: element.verticalAlign || DEFAULT_VERTICAL_ALIGN,
|
||||||
containerId: element.containerId ?? null,
|
containerId: element.containerId ?? null,
|
||||||
originalText: element.originalText || element.text,
|
originalText: element.originalText || text,
|
||||||
|
// line-height might not be specified either when creating elements
|
||||||
|
// programmatically, or when importing old diagrams.
|
||||||
|
// For the latter we want to detect the original line height which
|
||||||
|
// will likely differ from our per-font fixed line height we now use,
|
||||||
|
// to maintain backward compatibility.
|
||||||
|
lineHeight:
|
||||||
|
element.lineHeight ||
|
||||||
|
(element.height
|
||||||
|
? // detect line-height from current element height and font-size
|
||||||
|
detectLineHeight(element)
|
||||||
|
: // no element height likely means programmatic use, so default
|
||||||
|
// to a fixed line height
|
||||||
|
getDefaultLineHeight(element.fontFamily)),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (refreshDimensions) {
|
if (refreshDimensions) {
|
||||||
|
|||||||
@@ -786,7 +786,12 @@ export const findFocusPointForEllipse = (
|
|||||||
orientation * py * Math.sqrt(Math.max(0, squares - a ** 2 * b ** 2))) /
|
orientation * py * Math.sqrt(Math.max(0, squares - a ** 2 * b ** 2))) /
|
||||||
squares;
|
squares;
|
||||||
|
|
||||||
const n = (-m * px - 1) / py;
|
let n = (-m * px - 1) / py;
|
||||||
|
|
||||||
|
if (n === 0) {
|
||||||
|
// if zero {-0, 0}, fall back to a same-sign value in the similar range
|
||||||
|
n = (Object.is(n, -0) ? -1 : 1) * 0.01;
|
||||||
|
}
|
||||||
|
|
||||||
const x = -(a ** 2 * m) / (n ** 2 * b ** 2 + m ** 2 * a ** 2);
|
const x = -(a ** 2 * m) / (n ** 2 * b ** 2 + m ** 2 * a ** 2);
|
||||||
return GA.point(x, (-m * x - 1) / n);
|
return GA.point(x, (-m * x - 1) / n);
|
||||||
|
|||||||
@@ -25,14 +25,17 @@ import {
|
|||||||
getBoundTextElementOffset,
|
getBoundTextElementOffset,
|
||||||
getContainerDims,
|
getContainerDims,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
measureText,
|
|
||||||
normalizeText,
|
normalizeText,
|
||||||
wrapText,
|
getBoundTextMaxWidth,
|
||||||
getMaxContainerWidth,
|
|
||||||
} from "./textElement";
|
} from "./textElement";
|
||||||
import { VERTICAL_ALIGN } from "../constants";
|
import { VERTICAL_ALIGN } from "../constants";
|
||||||
import { isArrowElement } from "./typeChecks";
|
import { isArrowElement } from "./typeChecks";
|
||||||
import { MarkOptional, Merge, Mutable } from "../utility-types";
|
import { MarkOptional, Merge, Mutable } from "../utility-types";
|
||||||
|
import {
|
||||||
|
measureText,
|
||||||
|
wrapText,
|
||||||
|
getDefaultLineHeight,
|
||||||
|
} from "./textMeasurements";
|
||||||
|
|
||||||
type ElementConstructorOpts = MarkOptional<
|
type ElementConstructorOpts = MarkOptional<
|
||||||
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
|
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
|
||||||
@@ -137,10 +140,12 @@ export const newTextElement = (
|
|||||||
textAlign: TextAlign;
|
textAlign: TextAlign;
|
||||||
verticalAlign: VerticalAlign;
|
verticalAlign: VerticalAlign;
|
||||||
containerId?: ExcalidrawTextContainer["id"];
|
containerId?: ExcalidrawTextContainer["id"];
|
||||||
|
lineHeight?: ExcalidrawTextElement["lineHeight"];
|
||||||
} & ElementConstructorOpts,
|
} & ElementConstructorOpts,
|
||||||
): NonDeleted<ExcalidrawTextElement> => {
|
): NonDeleted<ExcalidrawTextElement> => {
|
||||||
|
const lineHeight = opts.lineHeight || getDefaultLineHeight(opts.fontFamily);
|
||||||
const text = normalizeText(opts.text);
|
const text = normalizeText(opts.text);
|
||||||
const metrics = measureText(text, getFontString(opts));
|
const metrics = measureText(text, getFontString(opts), lineHeight);
|
||||||
const offsets = getTextElementPositionOffsets(opts, metrics);
|
const offsets = getTextElementPositionOffsets(opts, metrics);
|
||||||
const textElement = newElementWith(
|
const textElement = newElementWith(
|
||||||
{
|
{
|
||||||
@@ -156,6 +161,7 @@ export const newTextElement = (
|
|||||||
height: metrics.height,
|
height: metrics.height,
|
||||||
containerId: opts.containerId || null,
|
containerId: opts.containerId || null,
|
||||||
originalText: text,
|
originalText: text,
|
||||||
|
lineHeight,
|
||||||
},
|
},
|
||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
@@ -176,6 +182,7 @@ const getAdjustedDimensions = (
|
|||||||
const { width: nextWidth, height: nextHeight } = measureText(
|
const { width: nextWidth, height: nextHeight } = measureText(
|
||||||
nextText,
|
nextText,
|
||||||
getFontString(element),
|
getFontString(element),
|
||||||
|
element.lineHeight,
|
||||||
);
|
);
|
||||||
const { textAlign, verticalAlign } = element;
|
const { textAlign, verticalAlign } = element;
|
||||||
let x: number;
|
let x: number;
|
||||||
@@ -185,7 +192,11 @@ const getAdjustedDimensions = (
|
|||||||
verticalAlign === VERTICAL_ALIGN.MIDDLE &&
|
verticalAlign === VERTICAL_ALIGN.MIDDLE &&
|
||||||
!element.containerId
|
!element.containerId
|
||||||
) {
|
) {
|
||||||
const prevMetrics = measureText(element.text, getFontString(element));
|
const prevMetrics = measureText(
|
||||||
|
element.text,
|
||||||
|
getFontString(element),
|
||||||
|
element.lineHeight,
|
||||||
|
);
|
||||||
const offsets = getTextElementPositionOffsets(element, {
|
const offsets = getTextElementPositionOffsets(element, {
|
||||||
width: nextWidth - prevMetrics.width,
|
width: nextWidth - prevMetrics.width,
|
||||||
height: nextHeight - prevMetrics.height,
|
height: nextHeight - prevMetrics.height,
|
||||||
@@ -261,7 +272,7 @@ export const refreshTextDimensions = (
|
|||||||
text = wrapText(
|
text = wrapText(
|
||||||
text,
|
text,
|
||||||
getFontString(textElement),
|
getFontString(textElement),
|
||||||
getMaxContainerWidth(container),
|
getBoundTextMaxWidth(container),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const dimensions = getAdjustedDimensions(textElement, text);
|
const dimensions = getAdjustedDimensions(textElement, text);
|
||||||
|
|||||||
@@ -39,15 +39,16 @@ import {
|
|||||||
import { Point, PointerDownState } from "../types";
|
import { Point, PointerDownState } from "../types";
|
||||||
import Scene from "../scene/Scene";
|
import Scene from "../scene/Scene";
|
||||||
import {
|
import {
|
||||||
getApproxMinLineHeight,
|
|
||||||
getApproxMinLineWidth,
|
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getBoundTextElementId,
|
getBoundTextElementId,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
handleBindTextResize,
|
handleBindTextResize,
|
||||||
getMaxContainerWidth,
|
getBoundTextMaxWidth,
|
||||||
} from "./textElement";
|
} from "./textElement";
|
||||||
|
import {
|
||||||
|
getApproxMinContainerHeight,
|
||||||
|
getApproxMinContainerWidth,
|
||||||
|
} from "./textMeasurements";
|
||||||
export const normalizeAngle = (angle: number): number => {
|
export const normalizeAngle = (angle: number): number => {
|
||||||
if (angle >= 2 * Math.PI) {
|
if (angle >= 2 * Math.PI) {
|
||||||
return angle - 2 * Math.PI;
|
return angle - 2 * Math.PI;
|
||||||
@@ -201,7 +202,7 @@ const measureFontSizeFromWidth = (
|
|||||||
if (hasContainer) {
|
if (hasContainer) {
|
||||||
const container = getContainerElement(element);
|
const container = getContainerElement(element);
|
||||||
if (container) {
|
if (container) {
|
||||||
width = getMaxContainerWidth(container);
|
width = getBoundTextMaxWidth(container);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const nextFontSize = element.fontSize * (nextWidth / width);
|
const nextFontSize = element.fontSize * (nextWidth / width);
|
||||||
@@ -360,7 +361,7 @@ export const resizeSingleElement = (
|
|||||||
let scaleX = atStartBoundsWidth / boundsCurrentWidth;
|
let scaleX = atStartBoundsWidth / boundsCurrentWidth;
|
||||||
let scaleY = atStartBoundsHeight / boundsCurrentHeight;
|
let scaleY = atStartBoundsHeight / boundsCurrentHeight;
|
||||||
|
|
||||||
let boundTextFont: { fontSize?: number } = {};
|
let boundTextFontSize: number | null = null;
|
||||||
const boundTextElement = getBoundTextElement(element);
|
const boundTextElement = getBoundTextElement(element);
|
||||||
|
|
||||||
if (transformHandleDirection.includes("e")) {
|
if (transformHandleDirection.includes("e")) {
|
||||||
@@ -410,9 +411,7 @@ export const resizeSingleElement = (
|
|||||||
boundTextElement.id,
|
boundTextElement.id,
|
||||||
) as typeof boundTextElement | undefined;
|
) as typeof boundTextElement | undefined;
|
||||||
if (stateOfBoundTextElementAtResize) {
|
if (stateOfBoundTextElementAtResize) {
|
||||||
boundTextFont = {
|
boundTextFontSize = stateOfBoundTextElementAtResize.fontSize;
|
||||||
fontSize: stateOfBoundTextElementAtResize.fontSize,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if (shouldMaintainAspectRatio) {
|
if (shouldMaintainAspectRatio) {
|
||||||
const updatedElement = {
|
const updatedElement = {
|
||||||
@@ -423,17 +422,21 @@ export const resizeSingleElement = (
|
|||||||
|
|
||||||
const nextFontSize = measureFontSizeFromWidth(
|
const nextFontSize = measureFontSizeFromWidth(
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
getMaxContainerWidth(updatedElement),
|
getBoundTextMaxWidth(updatedElement),
|
||||||
);
|
);
|
||||||
if (nextFontSize === null) {
|
if (nextFontSize === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boundTextFont = {
|
boundTextFontSize = nextFontSize;
|
||||||
fontSize: nextFontSize,
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
const minWidth = getApproxMinLineWidth(getFontString(boundTextElement));
|
const minWidth = getApproxMinContainerWidth(
|
||||||
const minHeight = getApproxMinLineHeight(getFontString(boundTextElement));
|
getFontString(boundTextElement),
|
||||||
|
boundTextElement.lineHeight,
|
||||||
|
);
|
||||||
|
const minHeight = getApproxMinContainerHeight(
|
||||||
|
boundTextElement.fontSize,
|
||||||
|
boundTextElement.lineHeight,
|
||||||
|
);
|
||||||
eleNewWidth = Math.ceil(Math.max(eleNewWidth, minWidth));
|
eleNewWidth = Math.ceil(Math.max(eleNewWidth, minWidth));
|
||||||
eleNewHeight = Math.ceil(Math.max(eleNewHeight, minHeight));
|
eleNewHeight = Math.ceil(Math.max(eleNewHeight, minHeight));
|
||||||
}
|
}
|
||||||
@@ -566,8 +569,10 @@ export const resizeSingleElement = (
|
|||||||
});
|
});
|
||||||
|
|
||||||
mutateElement(element, resizedElement);
|
mutateElement(element, resizedElement);
|
||||||
if (boundTextElement && boundTextFont) {
|
if (boundTextElement && boundTextFontSize != null) {
|
||||||
mutateElement(boundTextElement, { fontSize: boundTextFont.fontSize });
|
mutateElement(boundTextElement, {
|
||||||
|
fontSize: boundTextFontSize,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
handleBindTextResize(element, transformHandleDirection);
|
handleBindTextResize(element, transformHandleDirection);
|
||||||
}
|
}
|
||||||
@@ -694,7 +699,7 @@ const resizeMultipleElements = (
|
|||||||
const fontSize = measureFontSizeFromWidth(
|
const fontSize = measureFontSizeFromWidth(
|
||||||
boundTextElement ?? (element.orig as ExcalidrawTextElement),
|
boundTextElement ?? (element.orig as ExcalidrawTextElement),
|
||||||
boundTextElement
|
boundTextElement
|
||||||
? getMaxContainerWidth(updatedElement)
|
? getBoundTextMaxWidth(updatedElement)
|
||||||
: updatedElement.width,
|
: updatedElement.width,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
+56
-190
@@ -1,190 +1,11 @@
|
|||||||
import { BOUND_TEXT_PADDING } from "../constants";
|
|
||||||
import { API } from "../tests/helpers/api";
|
import { API } from "../tests/helpers/api";
|
||||||
import {
|
import {
|
||||||
computeContainerDimensionForBoundText,
|
computeContainerDimensionForBoundText,
|
||||||
getContainerCoords,
|
getContainerCoords,
|
||||||
getMaxContainerWidth,
|
getBoundTextMaxWidth,
|
||||||
getMaxContainerHeight,
|
getBoundTextMaxHeight,
|
||||||
wrapText,
|
|
||||||
} from "./textElement";
|
} from "./textElement";
|
||||||
import { FontString } from "./types";
|
import { ExcalidrawTextElementWithContainer } from "./types";
|
||||||
|
|
||||||
describe("Test wrapText", () => {
|
|
||||||
const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
|
|
||||||
|
|
||||||
it("shouldn't add new lines for trailing spaces", () => {
|
|
||||||
const text = "Hello whats up ";
|
|
||||||
const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
|
|
||||||
const res = wrapText(text, font, maxWidth);
|
|
||||||
expect(res).toBe(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should work with emojis", () => {
|
|
||||||
const text = "😀";
|
|
||||||
const maxWidth = 1;
|
|
||||||
const res = wrapText(text, font, maxWidth);
|
|
||||||
expect(res).toBe("😀");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should show the text correctly when max width reached", () => {
|
|
||||||
const text = "Hello😀";
|
|
||||||
const maxWidth = 10;
|
|
||||||
const res = wrapText(text, font, maxWidth);
|
|
||||||
expect(res).toBe("H\ne\nl\nl\no\n😀");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("When text doesn't contain new lines", () => {
|
|
||||||
const text = "Hello whats up";
|
|
||||||
|
|
||||||
[
|
|
||||||
{
|
|
||||||
desc: "break all words when width of each word is less than container width",
|
|
||||||
width: 80,
|
|
||||||
res: `Hello
|
|
||||||
whats
|
|
||||||
up`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "break all characters when width of each character is less than container width",
|
|
||||||
width: 25,
|
|
||||||
res: `H
|
|
||||||
e
|
|
||||||
l
|
|
||||||
l
|
|
||||||
o
|
|
||||||
w
|
|
||||||
h
|
|
||||||
a
|
|
||||||
t
|
|
||||||
s
|
|
||||||
u
|
|
||||||
p`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "break words as per the width",
|
|
||||||
|
|
||||||
width: 140,
|
|
||||||
res: `Hello whats
|
|
||||||
up`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "fit the container",
|
|
||||||
|
|
||||||
width: 250,
|
|
||||||
res: "Hello whats up",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "should push the word if its equal to max width",
|
|
||||||
width: 60,
|
|
||||||
res: `Hello
|
|
||||||
whats
|
|
||||||
up`,
|
|
||||||
},
|
|
||||||
].forEach((data) => {
|
|
||||||
it(`should ${data.desc}`, () => {
|
|
||||||
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
|
||||||
expect(res).toEqual(data.res);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("When text contain new lines", () => {
|
|
||||||
const text = `Hello
|
|
||||||
whats up`;
|
|
||||||
[
|
|
||||||
{
|
|
||||||
desc: "break all words when width of each word is less than container width",
|
|
||||||
width: 80,
|
|
||||||
res: `Hello
|
|
||||||
whats
|
|
||||||
up`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "break all characters when width of each character is less than container width",
|
|
||||||
width: 25,
|
|
||||||
res: `H
|
|
||||||
e
|
|
||||||
l
|
|
||||||
l
|
|
||||||
o
|
|
||||||
w
|
|
||||||
h
|
|
||||||
a
|
|
||||||
t
|
|
||||||
s
|
|
||||||
u
|
|
||||||
p`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "break words as per the width",
|
|
||||||
|
|
||||||
width: 150,
|
|
||||||
res: `Hello
|
|
||||||
whats up`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "fit the container",
|
|
||||||
|
|
||||||
width: 250,
|
|
||||||
res: `Hello
|
|
||||||
whats up`,
|
|
||||||
},
|
|
||||||
].forEach((data) => {
|
|
||||||
it(`should respect new lines and ${data.desc}`, () => {
|
|
||||||
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
|
||||||
expect(res).toEqual(data.res);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("When text is long", () => {
|
|
||||||
const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
|
|
||||||
[
|
|
||||||
{
|
|
||||||
desc: "fit characters of long string as per container width",
|
|
||||||
width: 170,
|
|
||||||
res: `hellolongtextth
|
|
||||||
isiswhatsupwith
|
|
||||||
youIamtypingggg
|
|
||||||
gandtypinggg
|
|
||||||
break it now`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
desc: "fit characters of long string as per container width and break words as per the width",
|
|
||||||
|
|
||||||
width: 130,
|
|
||||||
res: `hellolongte
|
|
||||||
xtthisiswha
|
|
||||||
tsupwithyou
|
|
||||||
Iamtypinggg
|
|
||||||
ggandtyping
|
|
||||||
gg break it
|
|
||||||
now`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "fit the long text when container width is greater than text length and move the rest to next line",
|
|
||||||
|
|
||||||
width: 600,
|
|
||||||
res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg
|
|
||||||
break it now`,
|
|
||||||
},
|
|
||||||
].forEach((data) => {
|
|
||||||
it(`should ${data.desc}`, () => {
|
|
||||||
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
|
||||||
expect(res).toEqual(data.res);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should wrap the text correctly when word length is exactly equal to max width", () => {
|
|
||||||
const text = "Hello Excalidraw";
|
|
||||||
// Length of "Excalidraw" is 100 and exacty equal to max width
|
|
||||||
const res = wrapText(text, font, 100);
|
|
||||||
expect(res).toEqual(`Hello
|
|
||||||
Excalidraw`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Test measureText", () => {
|
describe("Test measureText", () => {
|
||||||
describe("Test getContainerCoords", () => {
|
describe("Test getContainerCoords", () => {
|
||||||
@@ -261,7 +82,7 @@ describe("Test measureText", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Test getMaxContainerWidth", () => {
|
describe("Test getBoundTextMaxWidth", () => {
|
||||||
const params = {
|
const params = {
|
||||||
width: 178,
|
width: 178,
|
||||||
height: 194,
|
height: 194,
|
||||||
@@ -269,39 +90,84 @@ describe("Test measureText", () => {
|
|||||||
|
|
||||||
it("should return max width when container is rectangle", () => {
|
it("should return max width when container is rectangle", () => {
|
||||||
const container = API.createElement({ type: "rectangle", ...params });
|
const container = API.createElement({ type: "rectangle", ...params });
|
||||||
expect(getMaxContainerWidth(container)).toBe(168);
|
expect(getBoundTextMaxWidth(container)).toBe(168);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return max width when container is ellipse", () => {
|
it("should return max width when container is ellipse", () => {
|
||||||
const container = API.createElement({ type: "ellipse", ...params });
|
const container = API.createElement({ type: "ellipse", ...params });
|
||||||
expect(getMaxContainerWidth(container)).toBe(116);
|
expect(getBoundTextMaxWidth(container)).toBe(116);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return max width when container is diamond", () => {
|
it("should return max width when container is diamond", () => {
|
||||||
const container = API.createElement({ type: "diamond", ...params });
|
const container = API.createElement({ type: "diamond", ...params });
|
||||||
expect(getMaxContainerWidth(container)).toBe(79);
|
expect(getBoundTextMaxWidth(container)).toBe(79);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return max width when container is arrow", () => {
|
||||||
|
const container = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
...params,
|
||||||
|
});
|
||||||
|
expect(getBoundTextMaxWidth(container)).toBe(220);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Test getMaxContainerHeight", () => {
|
describe("Test getBoundTextMaxHeight", () => {
|
||||||
const params = {
|
const params = {
|
||||||
width: 178,
|
width: 178,
|
||||||
height: 194,
|
height: 194,
|
||||||
|
id: "container-id",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const boundTextElement = API.createElement({
|
||||||
|
type: "text",
|
||||||
|
id: "text-id",
|
||||||
|
x: 560.51171875,
|
||||||
|
y: 202.033203125,
|
||||||
|
width: 154,
|
||||||
|
height: 175,
|
||||||
|
fontSize: 20,
|
||||||
|
fontFamily: 1,
|
||||||
|
text: "Excalidraw is a\nvirtual \nopensource \nwhiteboard for \nsketching \nhand-drawn like\ndiagrams",
|
||||||
|
textAlign: "center",
|
||||||
|
verticalAlign: "middle",
|
||||||
|
containerId: params.id,
|
||||||
|
}) as ExcalidrawTextElementWithContainer;
|
||||||
|
|
||||||
it("should return max height when container is rectangle", () => {
|
it("should return max height when container is rectangle", () => {
|
||||||
const container = API.createElement({ type: "rectangle", ...params });
|
const container = API.createElement({ type: "rectangle", ...params });
|
||||||
expect(getMaxContainerHeight(container)).toBe(184);
|
expect(getBoundTextMaxHeight(container, boundTextElement)).toBe(184);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return max height when container is ellipse", () => {
|
it("should return max height when container is ellipse", () => {
|
||||||
const container = API.createElement({ type: "ellipse", ...params });
|
const container = API.createElement({ type: "ellipse", ...params });
|
||||||
expect(getMaxContainerHeight(container)).toBe(127);
|
expect(getBoundTextMaxHeight(container, boundTextElement)).toBe(127);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return max height when container is diamond", () => {
|
it("should return max height when container is diamond", () => {
|
||||||
const container = API.createElement({ type: "diamond", ...params });
|
const container = API.createElement({ type: "diamond", ...params });
|
||||||
expect(getMaxContainerHeight(container)).toBe(87);
|
expect(getBoundTextMaxHeight(container, boundTextElement)).toBe(87);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return max height when container is arrow", () => {
|
||||||
|
const container = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
...params,
|
||||||
|
});
|
||||||
|
expect(getBoundTextMaxHeight(container, boundTextElement)).toBe(194);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return max height when container is arrow and height is less than threshold", () => {
|
||||||
|
const container = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
...params,
|
||||||
|
height: 70,
|
||||||
|
boundElements: [{ type: "text", id: "text-id" }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(getBoundTextMaxHeight(container, boundTextElement)).toBe(
|
||||||
|
boundTextElement.height,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+53
-345
@@ -1,20 +1,13 @@
|
|||||||
import { getFontString, arrayToMap, isTestEnv } from "../utils";
|
import { getFontString, arrayToMap } from "../utils";
|
||||||
import {
|
import {
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
ExcalidrawTextContainer,
|
ExcalidrawTextContainer,
|
||||||
ExcalidrawTextElement,
|
ExcalidrawTextElement,
|
||||||
ExcalidrawTextElementWithContainer,
|
ExcalidrawTextElementWithContainer,
|
||||||
FontString,
|
|
||||||
NonDeletedExcalidrawElement,
|
NonDeletedExcalidrawElement,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import { mutateElement } from "./mutateElement";
|
import { mutateElement } from "./mutateElement";
|
||||||
import {
|
import { BOUND_TEXT_PADDING, TEXT_ALIGN, VERTICAL_ALIGN } from "../constants";
|
||||||
BOUND_TEXT_PADDING,
|
|
||||||
DEFAULT_FONT_FAMILY,
|
|
||||||
DEFAULT_FONT_SIZE,
|
|
||||||
TEXT_ALIGN,
|
|
||||||
VERTICAL_ALIGN,
|
|
||||||
} from "../constants";
|
|
||||||
import { MaybeTransformHandleType } from "./transformHandles";
|
import { MaybeTransformHandleType } from "./transformHandles";
|
||||||
import Scene from "../scene/Scene";
|
import Scene from "../scene/Scene";
|
||||||
import { isTextElement } from ".";
|
import { isTextElement } from ".";
|
||||||
@@ -30,6 +23,7 @@ import {
|
|||||||
updateOriginalContainerCache,
|
updateOriginalContainerCache,
|
||||||
} from "./textWysiwyg";
|
} from "./textWysiwyg";
|
||||||
import { ExtractSetType } from "../utility-types";
|
import { ExtractSetType } from "../utility-types";
|
||||||
|
import { measureText, wrapText } from "./textMeasurements";
|
||||||
|
|
||||||
export const normalizeText = (text: string) => {
|
export const normalizeText = (text: string) => {
|
||||||
return (
|
return (
|
||||||
@@ -46,7 +40,6 @@ export const redrawTextBoundingBox = (
|
|||||||
container: ExcalidrawElement | null,
|
container: ExcalidrawElement | null,
|
||||||
) => {
|
) => {
|
||||||
let maxWidth = undefined;
|
let maxWidth = undefined;
|
||||||
|
|
||||||
const boundTextUpdates = {
|
const boundTextUpdates = {
|
||||||
x: textElement.x,
|
x: textElement.x,
|
||||||
y: textElement.y,
|
y: textElement.y,
|
||||||
@@ -55,54 +48,48 @@ export const redrawTextBoundingBox = (
|
|||||||
height: textElement.height,
|
height: textElement.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
boundTextUpdates.text = textElement.text;
|
|
||||||
|
|
||||||
if (container) {
|
if (container) {
|
||||||
maxWidth = getMaxContainerWidth(container);
|
maxWidth = getBoundTextMaxWidth(container);
|
||||||
boundTextUpdates.text = wrapText(
|
boundTextUpdates.text = wrapText(
|
||||||
textElement.originalText,
|
textElement.originalText,
|
||||||
getFontString(textElement),
|
getFontString(textElement),
|
||||||
maxWidth,
|
maxWidth,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const metrics = measureText(
|
const metrics = measureText(
|
||||||
boundTextUpdates.text,
|
boundTextUpdates.text,
|
||||||
getFontString(textElement),
|
getFontString(textElement),
|
||||||
|
textElement.lineHeight,
|
||||||
);
|
);
|
||||||
|
|
||||||
boundTextUpdates.width = metrics.width;
|
boundTextUpdates.width = metrics.width;
|
||||||
boundTextUpdates.height = metrics.height;
|
boundTextUpdates.height = metrics.height;
|
||||||
|
|
||||||
if (container) {
|
if (container) {
|
||||||
if (isArrowElement(container)) {
|
const containerDims = getContainerDims(container);
|
||||||
const centerX = textElement.x + textElement.width / 2;
|
const maxContainerHeight = getBoundTextMaxHeight(
|
||||||
const centerY = textElement.y + textElement.height / 2;
|
container,
|
||||||
const diffWidth = metrics.width - textElement.width;
|
textElement as ExcalidrawTextElementWithContainer,
|
||||||
const diffHeight = metrics.height - textElement.height;
|
);
|
||||||
boundTextUpdates.x = centerY - (textElement.height + diffHeight) / 2;
|
let nextHeight = containerDims.height;
|
||||||
boundTextUpdates.y = centerX - (textElement.width + diffWidth) / 2;
|
|
||||||
} else {
|
|
||||||
const containerDims = getContainerDims(container);
|
|
||||||
let maxContainerHeight = getMaxContainerHeight(container);
|
|
||||||
|
|
||||||
let nextHeight = containerDims.height;
|
if (metrics.height > maxContainerHeight) {
|
||||||
if (metrics.height > maxContainerHeight) {
|
nextHeight = computeContainerDimensionForBoundText(
|
||||||
nextHeight = computeContainerDimensionForBoundText(
|
metrics.height,
|
||||||
metrics.height,
|
container.type,
|
||||||
container.type,
|
);
|
||||||
);
|
mutateElement(container, { height: nextHeight });
|
||||||
mutateElement(container, { height: nextHeight });
|
updateOriginalContainerCache(container.id, nextHeight);
|
||||||
maxContainerHeight = getMaxContainerHeight(container);
|
|
||||||
updateOriginalContainerCache(container.id, nextHeight);
|
|
||||||
}
|
|
||||||
const updatedTextElement = {
|
|
||||||
...textElement,
|
|
||||||
...boundTextUpdates,
|
|
||||||
} as ExcalidrawTextElementWithContainer;
|
|
||||||
const { x, y } = computeBoundTextPosition(container, updatedTextElement);
|
|
||||||
boundTextUpdates.x = x;
|
|
||||||
boundTextUpdates.y = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updatedTextElement = {
|
||||||
|
...textElement,
|
||||||
|
...boundTextUpdates,
|
||||||
|
} as ExcalidrawTextElementWithContainer;
|
||||||
|
const { x, y } = computeBoundTextPosition(container, updatedTextElement);
|
||||||
|
boundTextUpdates.x = x;
|
||||||
|
boundTextUpdates.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
mutateElement(textElement, boundTextUpdates);
|
mutateElement(textElement, boundTextUpdates);
|
||||||
@@ -174,8 +161,11 @@ export const handleBindTextResize = (
|
|||||||
let nextHeight = textElement.height;
|
let nextHeight = textElement.height;
|
||||||
let nextWidth = textElement.width;
|
let nextWidth = textElement.width;
|
||||||
const containerDims = getContainerDims(container);
|
const containerDims = getContainerDims(container);
|
||||||
const maxWidth = getMaxContainerWidth(container);
|
const maxWidth = getBoundTextMaxWidth(container);
|
||||||
const maxHeight = getMaxContainerHeight(container);
|
const maxHeight = getBoundTextMaxHeight(
|
||||||
|
container,
|
||||||
|
textElement as ExcalidrawTextElementWithContainer,
|
||||||
|
);
|
||||||
let containerHeight = containerDims.height;
|
let containerHeight = containerDims.height;
|
||||||
if (transformHandleType !== "n" && transformHandleType !== "s") {
|
if (transformHandleType !== "n" && transformHandleType !== "s") {
|
||||||
if (text) {
|
if (text) {
|
||||||
@@ -185,7 +175,11 @@ export const handleBindTextResize = (
|
|||||||
maxWidth,
|
maxWidth,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const dimensions = measureText(text, getFontString(textElement));
|
const dimensions = measureText(
|
||||||
|
text,
|
||||||
|
getFontString(textElement),
|
||||||
|
textElement.lineHeight,
|
||||||
|
);
|
||||||
nextHeight = dimensions.height;
|
nextHeight = dimensions.height;
|
||||||
nextWidth = dimensions.width;
|
nextWidth = dimensions.width;
|
||||||
}
|
}
|
||||||
@@ -229,16 +223,22 @@ export const handleBindTextResize = (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const computeBoundTextPosition = (
|
export const computeBoundTextPosition = (
|
||||||
container: ExcalidrawElement,
|
container: ExcalidrawElement,
|
||||||
boundTextElement: ExcalidrawTextElementWithContainer,
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
||||||
) => {
|
) => {
|
||||||
const containerCoords = getContainerCoords(container);
|
const containerCoords = getContainerCoords(container);
|
||||||
const maxContainerHeight = getMaxContainerHeight(container);
|
const maxContainerHeight = getBoundTextMaxHeight(container, boundTextElement);
|
||||||
const maxContainerWidth = getMaxContainerWidth(container);
|
const maxContainerWidth = getBoundTextMaxWidth(container);
|
||||||
|
|
||||||
let x;
|
let x;
|
||||||
let y;
|
let y;
|
||||||
|
if (isArrowElement(container)) {
|
||||||
|
return LinearElementEditor.getBoundTextElementPosition(
|
||||||
|
container,
|
||||||
|
boundTextElement,
|
||||||
|
);
|
||||||
|
}
|
||||||
if (boundTextElement.verticalAlign === VERTICAL_ALIGN.TOP) {
|
if (boundTextElement.verticalAlign === VERTICAL_ALIGN.TOP) {
|
||||||
y = containerCoords.y;
|
y = containerCoords.y;
|
||||||
} else if (boundTextElement.verticalAlign === VERTICAL_ALIGN.BOTTOM) {
|
} else if (boundTextElement.verticalAlign === VERTICAL_ALIGN.BOTTOM) {
|
||||||
@@ -259,266 +259,6 @@ const computeBoundTextPosition = (
|
|||||||
return { x, y };
|
return { x, y };
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js
|
|
||||||
|
|
||||||
export const measureText = (text: string, font: FontString) => {
|
|
||||||
text = text
|
|
||||||
.split("\n")
|
|
||||||
// replace empty lines with single space because leading/trailing empty
|
|
||||||
// lines would be stripped from computation
|
|
||||||
.map((x) => x || " ")
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
const height = getTextHeight(text, font);
|
|
||||||
const width = getTextWidth(text, font);
|
|
||||||
|
|
||||||
return { width, height };
|
|
||||||
};
|
|
||||||
|
|
||||||
const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase();
|
|
||||||
const cacheApproxLineHeight: { [key: FontString]: number } = {};
|
|
||||||
|
|
||||||
export const getApproxLineHeight = (font: FontString) => {
|
|
||||||
if (cacheApproxLineHeight[font]) {
|
|
||||||
return cacheApproxLineHeight[font];
|
|
||||||
}
|
|
||||||
const fontSize = parseInt(font);
|
|
||||||
|
|
||||||
// Calculate line height relative to font size
|
|
||||||
cacheApproxLineHeight[font] = fontSize * 1.2;
|
|
||||||
return cacheApproxLineHeight[font];
|
|
||||||
};
|
|
||||||
|
|
||||||
let canvas: HTMLCanvasElement | undefined;
|
|
||||||
|
|
||||||
const getLineWidth = (text: string, font: FontString) => {
|
|
||||||
if (!canvas) {
|
|
||||||
canvas = document.createElement("canvas");
|
|
||||||
}
|
|
||||||
const canvas2dContext = canvas.getContext("2d")!;
|
|
||||||
canvas2dContext.font = font;
|
|
||||||
const width = canvas2dContext.measureText(text).width;
|
|
||||||
|
|
||||||
// since in test env the canvas measureText algo
|
|
||||||
// doesn't measure text and instead just returns number of
|
|
||||||
// characters hence we assume that each letteris 10px
|
|
||||||
if (isTestEnv()) {
|
|
||||||
return width * 10;
|
|
||||||
}
|
|
||||||
return width;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getTextWidth = (text: string, font: FontString) => {
|
|
||||||
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
||||||
let width = 0;
|
|
||||||
lines.forEach((line) => {
|
|
||||||
width = Math.max(width, getLineWidth(line, font));
|
|
||||||
});
|
|
||||||
return width;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getTextHeight = (text: string, font: FontString) => {
|
|
||||||
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
||||||
const lineHeight = getApproxLineHeight(font);
|
|
||||||
return lineHeight * lines.length;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const wrapText = (text: string, font: FontString, maxWidth: number) => {
|
|
||||||
const lines: Array<string> = [];
|
|
||||||
const originalLines = text.split("\n");
|
|
||||||
const spaceWidth = getLineWidth(" ", font);
|
|
||||||
|
|
||||||
let currentLine = "";
|
|
||||||
let currentLineWidthTillNow = 0;
|
|
||||||
|
|
||||||
const push = (str: string) => {
|
|
||||||
if (str.trim()) {
|
|
||||||
lines.push(str);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const resetParams = () => {
|
|
||||||
currentLine = "";
|
|
||||||
currentLineWidthTillNow = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
originalLines.forEach((originalLine) => {
|
|
||||||
const currentLineWidth = getTextWidth(originalLine, font);
|
|
||||||
|
|
||||||
//Push the line if its <= maxWidth
|
|
||||||
if (currentLineWidth <= maxWidth) {
|
|
||||||
lines.push(originalLine);
|
|
||||||
return; // continue
|
|
||||||
}
|
|
||||||
const words = originalLine.split(" ");
|
|
||||||
|
|
||||||
resetParams();
|
|
||||||
|
|
||||||
let index = 0;
|
|
||||||
|
|
||||||
while (index < words.length) {
|
|
||||||
const currentWordWidth = getLineWidth(words[index], font);
|
|
||||||
|
|
||||||
// This will only happen when single word takes entire width
|
|
||||||
if (currentWordWidth === maxWidth) {
|
|
||||||
push(words[index]);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start breaking longer words exceeding max width
|
|
||||||
else if (currentWordWidth > maxWidth) {
|
|
||||||
// push current line since the current word exceeds the max width
|
|
||||||
// so will be appended in next line
|
|
||||||
push(currentLine);
|
|
||||||
|
|
||||||
resetParams();
|
|
||||||
|
|
||||||
while (words[index].length > 0) {
|
|
||||||
const currentChar = String.fromCodePoint(
|
|
||||||
words[index].codePointAt(0)!,
|
|
||||||
);
|
|
||||||
const width = charWidth.calculate(currentChar, font);
|
|
||||||
currentLineWidthTillNow += width;
|
|
||||||
words[index] = words[index].slice(currentChar.length);
|
|
||||||
|
|
||||||
if (currentLineWidthTillNow >= maxWidth) {
|
|
||||||
push(currentLine);
|
|
||||||
currentLine = currentChar;
|
|
||||||
currentLineWidthTillNow = width;
|
|
||||||
} else {
|
|
||||||
currentLine += currentChar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// push current line if appending space exceeds max width
|
|
||||||
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
||||||
push(currentLine);
|
|
||||||
resetParams();
|
|
||||||
} else {
|
|
||||||
// space needs to be appended before next word
|
|
||||||
// as currentLine contains chars which couldn't be appended
|
|
||||||
// to previous line
|
|
||||||
currentLine += " ";
|
|
||||||
currentLineWidthTillNow += spaceWidth;
|
|
||||||
}
|
|
||||||
index++;
|
|
||||||
} else {
|
|
||||||
// Start appending words in a line till max width reached
|
|
||||||
while (currentLineWidthTillNow < maxWidth && index < words.length) {
|
|
||||||
const word = words[index];
|
|
||||||
currentLineWidthTillNow = getLineWidth(currentLine + word, font);
|
|
||||||
|
|
||||||
if (currentLineWidthTillNow > maxWidth) {
|
|
||||||
push(currentLine);
|
|
||||||
resetParams();
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
index++;
|
|
||||||
currentLine += `${word} `;
|
|
||||||
|
|
||||||
// Push the word if appending space exceeds max width
|
|
||||||
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
||||||
const word = currentLine.slice(0, -1);
|
|
||||||
push(word);
|
|
||||||
resetParams();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (currentLine.slice(-1) === " ") {
|
|
||||||
// only remove last trailing space which we have added when joining words
|
|
||||||
currentLine = currentLine.slice(0, -1);
|
|
||||||
push(currentLine);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return lines.join("\n");
|
|
||||||
};
|
|
||||||
|
|
||||||
export const charWidth = (() => {
|
|
||||||
const cachedCharWidth: { [key: FontString]: Array<number> } = {};
|
|
||||||
|
|
||||||
const calculate = (char: string, font: FontString) => {
|
|
||||||
const ascii = char.charCodeAt(0);
|
|
||||||
if (!cachedCharWidth[font]) {
|
|
||||||
cachedCharWidth[font] = [];
|
|
||||||
}
|
|
||||||
if (!cachedCharWidth[font][ascii]) {
|
|
||||||
const width = getLineWidth(char, font);
|
|
||||||
cachedCharWidth[font][ascii] = width;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cachedCharWidth[font][ascii];
|
|
||||||
};
|
|
||||||
|
|
||||||
const getCache = (font: FontString) => {
|
|
||||||
return cachedCharWidth[font];
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
calculate,
|
|
||||||
getCache,
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
export const getApproxMinLineWidth = (font: FontString) => {
|
|
||||||
const maxCharWidth = getMaxCharWidth(font);
|
|
||||||
if (maxCharWidth === 0) {
|
|
||||||
return (
|
|
||||||
measureText(DUMMY_TEXT.split("").join("\n"), font).width +
|
|
||||||
BOUND_TEXT_PADDING * 2
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return maxCharWidth + BOUND_TEXT_PADDING * 2;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getApproxMinLineHeight = (font: FontString) => {
|
|
||||||
return getApproxLineHeight(font) + BOUND_TEXT_PADDING * 2;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getMinCharWidth = (font: FontString) => {
|
|
||||||
const cache = charWidth.getCache(font);
|
|
||||||
if (!cache) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
const cacheWithOutEmpty = cache.filter((val) => val !== undefined);
|
|
||||||
|
|
||||||
return Math.min(...cacheWithOutEmpty);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getMaxCharWidth = (font: FontString) => {
|
|
||||||
const cache = charWidth.getCache(font);
|
|
||||||
if (!cache) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
const cacheWithOutEmpty = cache.filter((val) => val !== undefined);
|
|
||||||
return Math.max(...cacheWithOutEmpty);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getApproxCharsToFitInWidth = (font: FontString, width: number) => {
|
|
||||||
// Generally lower case is used so converting to lower case
|
|
||||||
const dummyText = DUMMY_TEXT.toLocaleLowerCase();
|
|
||||||
const batchLength = 6;
|
|
||||||
let index = 0;
|
|
||||||
let widthTillNow = 0;
|
|
||||||
let str = "";
|
|
||||||
while (widthTillNow <= width) {
|
|
||||||
const batch = dummyText.substr(index, index + batchLength);
|
|
||||||
str += batch;
|
|
||||||
widthTillNow += getLineWidth(str, font);
|
|
||||||
if (index === dummyText.length - 1) {
|
|
||||||
index = 0;
|
|
||||||
}
|
|
||||||
index = index + batchLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (widthTillNow > width) {
|
|
||||||
str = str.substr(0, str.length - 1);
|
|
||||||
widthTillNow = getLineWidth(str, font);
|
|
||||||
}
|
|
||||||
return str.length;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getBoundTextElementId = (container: ExcalidrawElement | null) => {
|
export const getBoundTextElementId = (container: ExcalidrawElement | null) => {
|
||||||
return container?.boundElements?.length
|
return container?.boundElements?.length
|
||||||
? container?.boundElements?.filter((ele) => ele.type === "text")[0]?.id ||
|
? container?.boundElements?.filter((ele) => ele.type === "text")[0]?.id ||
|
||||||
@@ -644,18 +384,6 @@ export const getBoundTextElementOffset = (
|
|||||||
return BOUND_TEXT_PADDING;
|
return BOUND_TEXT_PADDING;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBoundTextElementPosition = (
|
|
||||||
container: ExcalidrawElement,
|
|
||||||
boundTextElement: ExcalidrawTextElementWithContainer,
|
|
||||||
) => {
|
|
||||||
if (isArrowElement(container)) {
|
|
||||||
return LinearElementEditor.getBoundTextElementPosition(
|
|
||||||
container,
|
|
||||||
boundTextElement,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const shouldAllowVerticalAlign = (
|
export const shouldAllowVerticalAlign = (
|
||||||
selectedElements: NonDeletedExcalidrawElement[],
|
selectedElements: NonDeletedExcalidrawElement[],
|
||||||
) => {
|
) => {
|
||||||
@@ -756,18 +484,10 @@ export const computeContainerDimensionForBoundText = (
|
|||||||
return dimension + padding;
|
return dimension + padding;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getMaxContainerWidth = (container: ExcalidrawElement) => {
|
export const getBoundTextMaxWidth = (container: ExcalidrawElement) => {
|
||||||
const width = getContainerDims(container).width;
|
const width = getContainerDims(container).width;
|
||||||
if (isArrowElement(container)) {
|
if (isArrowElement(container)) {
|
||||||
const containerWidth = width - BOUND_TEXT_PADDING * 8 * 2;
|
return width - BOUND_TEXT_PADDING * 8 * 2;
|
||||||
if (containerWidth <= 0) {
|
|
||||||
const boundText = getBoundTextElement(container);
|
|
||||||
if (boundText) {
|
|
||||||
return boundText.width;
|
|
||||||
}
|
|
||||||
return BOUND_TEXT_PADDING * 8 * 2;
|
|
||||||
}
|
|
||||||
return containerWidth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (container.type === "ellipse") {
|
if (container.type === "ellipse") {
|
||||||
@@ -784,16 +504,15 @@ export const getMaxContainerWidth = (container: ExcalidrawElement) => {
|
|||||||
return width - BOUND_TEXT_PADDING * 2;
|
return width - BOUND_TEXT_PADDING * 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getMaxContainerHeight = (container: ExcalidrawElement) => {
|
export const getBoundTextMaxHeight = (
|
||||||
|
container: ExcalidrawElement,
|
||||||
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
||||||
|
) => {
|
||||||
const height = getContainerDims(container).height;
|
const height = getContainerDims(container).height;
|
||||||
if (isArrowElement(container)) {
|
if (isArrowElement(container)) {
|
||||||
const containerHeight = height - BOUND_TEXT_PADDING * 8 * 2;
|
const containerHeight = height - BOUND_TEXT_PADDING * 8 * 2;
|
||||||
if (containerHeight <= 0) {
|
if (containerHeight <= 0) {
|
||||||
const boundText = getBoundTextElement(container);
|
return boundTextElement.height;
|
||||||
if (boundText) {
|
|
||||||
return boundText.height;
|
|
||||||
}
|
|
||||||
return BOUND_TEXT_PADDING * 8 * 2;
|
|
||||||
}
|
}
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
@@ -810,14 +529,3 @@ export const getMaxContainerHeight = (container: ExcalidrawElement) => {
|
|||||||
}
|
}
|
||||||
return height - BOUND_TEXT_PADDING * 2;
|
return height - BOUND_TEXT_PADDING * 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isMeasureTextSupported = () => {
|
|
||||||
const width = getTextWidth(
|
|
||||||
DUMMY_TEXT,
|
|
||||||
getFontString({
|
|
||||||
fontSize: DEFAULT_FONT_SIZE,
|
|
||||||
fontFamily: DEFAULT_FONT_FAMILY,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return width > 0;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -0,0 +1,213 @@
|
|||||||
|
import { BOUND_TEXT_PADDING, FONT_FAMILY } from "../constants";
|
||||||
|
import { API } from "../tests/helpers/api";
|
||||||
|
import {
|
||||||
|
detectLineHeight,
|
||||||
|
getDefaultLineHeight,
|
||||||
|
getLineHeightInPx,
|
||||||
|
wrapText,
|
||||||
|
} from "./textMeasurements";
|
||||||
|
import { FontString } from "./types";
|
||||||
|
|
||||||
|
describe("Test wrapText", () => {
|
||||||
|
const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
|
||||||
|
|
||||||
|
it("shouldn't add new lines for trailing spaces", () => {
|
||||||
|
const text = "Hello whats up ";
|
||||||
|
const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
|
||||||
|
const res = wrapText(text, font, maxWidth);
|
||||||
|
expect(res).toBe(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should work with emojis", () => {
|
||||||
|
const text = "😀";
|
||||||
|
const maxWidth = 1;
|
||||||
|
const res = wrapText(text, font, maxWidth);
|
||||||
|
expect(res).toBe("😀");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should show the text correctly when max width reached", () => {
|
||||||
|
const text = "Hello😀";
|
||||||
|
const maxWidth = 10;
|
||||||
|
const res = wrapText(text, font, maxWidth);
|
||||||
|
expect(res).toBe("H\ne\nl\nl\no\n😀");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("When text doesn't contain new lines", () => {
|
||||||
|
const text = "Hello whats up";
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
desc: "break all words when width of each word is less than container width",
|
||||||
|
width: 80,
|
||||||
|
res: `Hello \nwhats \nup`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "break all characters when width of each character is less than container width",
|
||||||
|
width: 25,
|
||||||
|
res: `H
|
||||||
|
e
|
||||||
|
l
|
||||||
|
l
|
||||||
|
o
|
||||||
|
w
|
||||||
|
h
|
||||||
|
a
|
||||||
|
t
|
||||||
|
s
|
||||||
|
u
|
||||||
|
p`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "break words as per the width",
|
||||||
|
|
||||||
|
width: 140,
|
||||||
|
res: `Hello whats \nup`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "fit the container",
|
||||||
|
|
||||||
|
width: 250,
|
||||||
|
res: "Hello whats up",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should push the word if its equal to max width",
|
||||||
|
width: 60,
|
||||||
|
res: `Hello
|
||||||
|
whats
|
||||||
|
up`,
|
||||||
|
},
|
||||||
|
].forEach((data) => {
|
||||||
|
it(`should ${data.desc}`, () => {
|
||||||
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
||||||
|
expect(res).toEqual(data.res);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("When text contain new lines", () => {
|
||||||
|
const text = `Hello
|
||||||
|
whats up`;
|
||||||
|
[
|
||||||
|
{
|
||||||
|
desc: "break all words when width of each word is less than container width",
|
||||||
|
width: 80,
|
||||||
|
res: `Hello\nwhats \nup`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "break all characters when width of each character is less than container width",
|
||||||
|
width: 25,
|
||||||
|
res: `H
|
||||||
|
e
|
||||||
|
l
|
||||||
|
l
|
||||||
|
o
|
||||||
|
w
|
||||||
|
h
|
||||||
|
a
|
||||||
|
t
|
||||||
|
s
|
||||||
|
u
|
||||||
|
p`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "break words as per the width",
|
||||||
|
|
||||||
|
width: 150,
|
||||||
|
res: `Hello
|
||||||
|
whats up`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "fit the container",
|
||||||
|
|
||||||
|
width: 250,
|
||||||
|
res: `Hello
|
||||||
|
whats up`,
|
||||||
|
},
|
||||||
|
].forEach((data) => {
|
||||||
|
it(`should respect new lines and ${data.desc}`, () => {
|
||||||
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
||||||
|
expect(res).toEqual(data.res);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("When text is long", () => {
|
||||||
|
const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
|
||||||
|
[
|
||||||
|
{
|
||||||
|
desc: "fit characters of long string as per container width",
|
||||||
|
width: 170,
|
||||||
|
res: `hellolongtextth\nisiswhatsupwith\nyouIamtypingggg\ngandtypinggg \nbreak it now`,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
desc: "fit characters of long string as per container width and break words as per the width",
|
||||||
|
|
||||||
|
width: 130,
|
||||||
|
res: `hellolongte
|
||||||
|
xtthisiswha
|
||||||
|
tsupwithyou
|
||||||
|
Iamtypinggg
|
||||||
|
ggandtyping
|
||||||
|
gg break it
|
||||||
|
now`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "fit the long text when container width is greater than text length and move the rest to next line",
|
||||||
|
|
||||||
|
width: 600,
|
||||||
|
res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg \nbreak it now`,
|
||||||
|
},
|
||||||
|
].forEach((data) => {
|
||||||
|
it(`should ${data.desc}`, () => {
|
||||||
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
||||||
|
expect(res).toEqual(data.res);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should wrap the text correctly when word length is exactly equal to max width", () => {
|
||||||
|
const text = "Hello Excalidraw";
|
||||||
|
// Length of "Excalidraw" is 100 and exacty equal to max width
|
||||||
|
const res = wrapText(text, font, 100);
|
||||||
|
expect(res).toEqual(`Hello \nExcalidraw`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return the text as is if max width is invalid", () => {
|
||||||
|
const text = "Hello Excalidraw";
|
||||||
|
expect(wrapText(text, font, NaN)).toEqual(text);
|
||||||
|
expect(wrapText(text, font, -1)).toEqual(text);
|
||||||
|
expect(wrapText(text, font, Infinity)).toEqual(text);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const textElement = API.createElement({
|
||||||
|
type: "text",
|
||||||
|
text: "Excalidraw is a\nvirtual \nopensource \nwhiteboard for \nsketching \nhand-drawn like\ndiagrams",
|
||||||
|
fontSize: 20,
|
||||||
|
fontFamily: 1,
|
||||||
|
height: 175,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Test detectLineHeight", () => {
|
||||||
|
it("should return correct line height", () => {
|
||||||
|
expect(detectLineHeight(textElement)).toBe(1.25);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Test getLineHeightInPx", () => {
|
||||||
|
it("should return correct line height", () => {
|
||||||
|
expect(
|
||||||
|
getLineHeightInPx(textElement.fontSize, textElement.lineHeight),
|
||||||
|
).toBe(25);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Test getDefaultLineHeight", () => {
|
||||||
|
it("should return line height using default font family when not passed", () => {
|
||||||
|
//@ts-ignore
|
||||||
|
expect(getDefaultLineHeight()).toBe(1.25);
|
||||||
|
});
|
||||||
|
it("should return correct line height", () => {
|
||||||
|
expect(getDefaultLineHeight(FONT_FAMILY.Cascadia)).toBe(1.2);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,339 @@
|
|||||||
|
import {
|
||||||
|
BOUND_TEXT_PADDING,
|
||||||
|
DEFAULT_FONT_FAMILY,
|
||||||
|
DEFAULT_FONT_SIZE,
|
||||||
|
FONT_FAMILY,
|
||||||
|
} from "../constants";
|
||||||
|
import { getFontString, isTestEnv } from "../utils";
|
||||||
|
import { normalizeText } from "./textElement";
|
||||||
|
import { ExcalidrawTextElement, FontFamilyValues, FontString } from "./types";
|
||||||
|
|
||||||
|
const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase();
|
||||||
|
|
||||||
|
let canvas: HTMLCanvasElement | undefined;
|
||||||
|
|
||||||
|
// since in test env the canvas measureText algo
|
||||||
|
// doesn't measure text and instead just returns number of
|
||||||
|
// characters hence we assume that each letter is 10px
|
||||||
|
const DUMMY_CHAR_WIDTH = 10;
|
||||||
|
|
||||||
|
const getLineWidth = (text: string, font: FontString) => {
|
||||||
|
if (!canvas) {
|
||||||
|
canvas = document.createElement("canvas");
|
||||||
|
}
|
||||||
|
const canvas2dContext = canvas.getContext("2d")!;
|
||||||
|
canvas2dContext.font = font;
|
||||||
|
const width = canvas2dContext.measureText(text).width;
|
||||||
|
|
||||||
|
/* istanbul ignore else */
|
||||||
|
if (isTestEnv()) {
|
||||||
|
return width * DUMMY_CHAR_WIDTH;
|
||||||
|
}
|
||||||
|
/* istanbul ignore next */
|
||||||
|
return width;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getTextWidth = (text: string, font: FontString) => {
|
||||||
|
const lines = splitIntoLines(text);
|
||||||
|
let width = 0;
|
||||||
|
lines.forEach((line) => {
|
||||||
|
width = Math.max(width, getLineWidth(line, font));
|
||||||
|
});
|
||||||
|
return width;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getTextHeight = (
|
||||||
|
text: string,
|
||||||
|
fontSize: number,
|
||||||
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
||||||
|
) => {
|
||||||
|
const lineCount = splitIntoLines(text).length;
|
||||||
|
return getLineHeightInPx(fontSize, lineHeight) * lineCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const splitIntoLines = (text: string) => {
|
||||||
|
return normalizeText(text).split("\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const measureText = (
|
||||||
|
text: string,
|
||||||
|
font: FontString,
|
||||||
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
||||||
|
) => {
|
||||||
|
text = text
|
||||||
|
.split("\n")
|
||||||
|
// replace empty lines with single space because leading/trailing empty
|
||||||
|
// lines would be stripped from computation
|
||||||
|
.map((x) => x || " ")
|
||||||
|
.join("\n");
|
||||||
|
const fontSize = parseFloat(font);
|
||||||
|
const height = getTextHeight(text, fontSize, lineHeight);
|
||||||
|
const width = getTextWidth(text, font);
|
||||||
|
|
||||||
|
return { width, height };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getApproxMinContainerWidth = (
|
||||||
|
font: FontString,
|
||||||
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
||||||
|
) => {
|
||||||
|
const maxCharWidth = getMaxCharWidth(font);
|
||||||
|
if (maxCharWidth === 0) {
|
||||||
|
return (
|
||||||
|
measureText(DUMMY_TEXT.split("").join("\n"), font, lineHeight).width +
|
||||||
|
BOUND_TEXT_PADDING * 2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return maxCharWidth + BOUND_TEXT_PADDING * 2;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getApproxMinContainerHeight = (
|
||||||
|
fontSize: ExcalidrawTextElement["fontSize"],
|
||||||
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
||||||
|
) => {
|
||||||
|
return getLineHeightInPx(fontSize, lineHeight) + BOUND_TEXT_PADDING * 2;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const charWidth = (() => {
|
||||||
|
const cachedCharWidth: { [key: FontString]: Array<number> } = {};
|
||||||
|
|
||||||
|
const calculate = (char: string, font: FontString) => {
|
||||||
|
const ascii = char.charCodeAt(0);
|
||||||
|
if (!cachedCharWidth[font]) {
|
||||||
|
cachedCharWidth[font] = [];
|
||||||
|
}
|
||||||
|
if (!cachedCharWidth[font][ascii]) {
|
||||||
|
const width = getLineWidth(char, font);
|
||||||
|
cachedCharWidth[font][ascii] = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedCharWidth[font][ascii];
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCache = (font: FontString) => {
|
||||||
|
return cachedCharWidth[font];
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
calculate,
|
||||||
|
getCache,
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
export const getMaxCharWidth = (font: FontString) => {
|
||||||
|
const cache = charWidth.getCache(font);
|
||||||
|
if (!cache) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const cacheWithOutEmpty = cache.filter((val) => val !== undefined);
|
||||||
|
return Math.max(...cacheWithOutEmpty);
|
||||||
|
};
|
||||||
|
|
||||||
|
/** this is not used currently but might be useful
|
||||||
|
* in future hence keeping it
|
||||||
|
*/
|
||||||
|
/* istanbul ignore next */
|
||||||
|
export const getApproxCharsToFitInWidth = (font: FontString, width: number) => {
|
||||||
|
// Generally lower case is used so converting to lower case
|
||||||
|
const dummyText = DUMMY_TEXT.toLocaleLowerCase();
|
||||||
|
const batchLength = 6;
|
||||||
|
let index = 0;
|
||||||
|
let widthTillNow = 0;
|
||||||
|
let str = "";
|
||||||
|
while (widthTillNow <= width) {
|
||||||
|
const batch = dummyText.substr(index, index + batchLength);
|
||||||
|
str += batch;
|
||||||
|
widthTillNow += getLineWidth(str, font);
|
||||||
|
if (index === dummyText.length - 1) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
index = index + batchLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (widthTillNow > width) {
|
||||||
|
str = str.substr(0, str.length - 1);
|
||||||
|
widthTillNow = getLineWidth(str, font);
|
||||||
|
}
|
||||||
|
return str.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const wrapText = (text: string, font: FontString, maxWidth: number) => {
|
||||||
|
// if maxWidth is not finite or NaN which can happen in case of bugs in
|
||||||
|
// computation, we need to make sure we don't continue as we'll end up
|
||||||
|
// in an infinite loop
|
||||||
|
if (!Number.isFinite(maxWidth) || maxWidth < 0) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lines: Array<string> = [];
|
||||||
|
const originalLines = text.split("\n");
|
||||||
|
const spaceWidth = getLineWidth(" ", font);
|
||||||
|
|
||||||
|
let currentLine = "";
|
||||||
|
let currentLineWidthTillNow = 0;
|
||||||
|
|
||||||
|
const push = (str: string) => {
|
||||||
|
if (str.trim()) {
|
||||||
|
lines.push(str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetParams = () => {
|
||||||
|
currentLine = "";
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
originalLines.forEach((originalLine) => {
|
||||||
|
const currentLineWidth = getTextWidth(originalLine, font);
|
||||||
|
|
||||||
|
//Push the line if its <= maxWidth
|
||||||
|
if (currentLineWidth <= maxWidth) {
|
||||||
|
lines.push(originalLine);
|
||||||
|
return; // continue
|
||||||
|
}
|
||||||
|
const words = originalLine.split(" ");
|
||||||
|
|
||||||
|
resetParams();
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
while (index < words.length) {
|
||||||
|
const currentWordWidth = getLineWidth(words[index], font);
|
||||||
|
|
||||||
|
// This will only happen when single word takes entire width
|
||||||
|
if (currentWordWidth === maxWidth) {
|
||||||
|
push(words[index]);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start breaking longer words exceeding max width
|
||||||
|
else if (currentWordWidth > maxWidth) {
|
||||||
|
// push current line since the current word exceeds the max width
|
||||||
|
// so will be appended in next line
|
||||||
|
push(currentLine);
|
||||||
|
|
||||||
|
resetParams();
|
||||||
|
|
||||||
|
while (words[index].length > 0) {
|
||||||
|
const currentChar = String.fromCodePoint(
|
||||||
|
words[index].codePointAt(0)!,
|
||||||
|
);
|
||||||
|
const width = charWidth.calculate(currentChar, font);
|
||||||
|
currentLineWidthTillNow += width;
|
||||||
|
words[index] = words[index].slice(currentChar.length);
|
||||||
|
|
||||||
|
if (currentLineWidthTillNow >= maxWidth) {
|
||||||
|
push(currentLine);
|
||||||
|
currentLine = currentChar;
|
||||||
|
currentLineWidthTillNow = width;
|
||||||
|
} else {
|
||||||
|
currentLine += currentChar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// push current line if appending space exceeds max width
|
||||||
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
||||||
|
push(currentLine);
|
||||||
|
resetParams();
|
||||||
|
} else {
|
||||||
|
// space needs to be appended before next word
|
||||||
|
// as currentLine contains chars which couldn't be appended
|
||||||
|
// to previous line
|
||||||
|
currentLine += " ";
|
||||||
|
currentLineWidthTillNow += spaceWidth;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
} else {
|
||||||
|
// Start appending words in a line till max width reached
|
||||||
|
while (currentLineWidthTillNow < maxWidth && index < words.length) {
|
||||||
|
const word = words[index];
|
||||||
|
currentLineWidthTillNow = getLineWidth(currentLine + word, font);
|
||||||
|
|
||||||
|
if (currentLineWidthTillNow > maxWidth) {
|
||||||
|
push(currentLine);
|
||||||
|
resetParams();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
currentLine += `${word} `;
|
||||||
|
|
||||||
|
// Push the word if appending space exceeds max width
|
||||||
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
||||||
|
const word = currentLine.slice(0, -1);
|
||||||
|
push(word);
|
||||||
|
resetParams();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentLine.slice(-1) === " ") {
|
||||||
|
// only remove last trailing space which we have added when joining words
|
||||||
|
currentLine = currentLine.slice(0, -1);
|
||||||
|
push(currentLine);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return lines.join("\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isMeasureTextSupported = () => {
|
||||||
|
const width = getTextWidth(
|
||||||
|
DUMMY_TEXT,
|
||||||
|
getFontString({
|
||||||
|
fontSize: DEFAULT_FONT_SIZE,
|
||||||
|
fontFamily: DEFAULT_FONT_FAMILY,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return width > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We calculate the line height from the font size and the unitless line height,
|
||||||
|
* aligning with the W3C spec.
|
||||||
|
*/
|
||||||
|
export const getLineHeightInPx = (
|
||||||
|
fontSize: ExcalidrawTextElement["fontSize"],
|
||||||
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
||||||
|
) => {
|
||||||
|
return fontSize * lineHeight;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To get unitless line-height (if unknown) we can calculate it by dividing
|
||||||
|
* height-per-line by fontSize.
|
||||||
|
*/
|
||||||
|
export const detectLineHeight = (textElement: ExcalidrawTextElement) => {
|
||||||
|
const lineCount = splitIntoLines(textElement.text).length;
|
||||||
|
return (textElement.height /
|
||||||
|
lineCount /
|
||||||
|
textElement.fontSize) as ExcalidrawTextElement["lineHeight"];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unitless line height
|
||||||
|
*
|
||||||
|
* In previous versions we used `normal` line height, which browsers interpret
|
||||||
|
* differently, and based on font-family and font-size.
|
||||||
|
*
|
||||||
|
* To make line heights consistent across browsers we hardcode the values for
|
||||||
|
* each of our fonts based on most common average line-heights.
|
||||||
|
* See https://github.com/excalidraw/excalidraw/pull/6360#issuecomment-1477635971
|
||||||
|
* where the values come from.
|
||||||
|
*/
|
||||||
|
const DEFAULT_LINE_HEIGHT = {
|
||||||
|
// ~1.25 is the average for Virgil in WebKit and Blink.
|
||||||
|
// Gecko (FF) uses ~1.28.
|
||||||
|
[FONT_FAMILY.Virgil]: 1.25 as ExcalidrawTextElement["lineHeight"],
|
||||||
|
// ~1.15 is the average for Helvetica in WebKit and Blink.
|
||||||
|
// Gecko if all over the place.
|
||||||
|
[FONT_FAMILY.Helvetica]: 1.15 as ExcalidrawTextElement["lineHeight"],
|
||||||
|
// ~1.2 is the average for Cascadia in WebKit and Blink, and kinda Gecko too
|
||||||
|
[FONT_FAMILY.Cascadia]: 1.2 as ExcalidrawTextElement["lineHeight"],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDefaultLineHeight = (fontFamily: FontFamilyValues) => {
|
||||||
|
if (fontFamily) {
|
||||||
|
return DEFAULT_LINE_HEIGHT[fontFamily];
|
||||||
|
}
|
||||||
|
return DEFAULT_LINE_HEIGHT[DEFAULT_FONT_FAMILY];
|
||||||
|
};
|
||||||
@@ -783,7 +783,7 @@ describe("textWysiwyg", () => {
|
|||||||
rectangle.y + h.elements[0].height / 2 - text.height / 2,
|
rectangle.y + h.elements[0].height / 2 - text.height / 2,
|
||||||
);
|
);
|
||||||
expect(text.x).toBe(25);
|
expect(text.x).toBe(25);
|
||||||
expect(text.height).toBe(48);
|
expect(text.height).toBe(50);
|
||||||
expect(text.width).toBe(60);
|
expect(text.width).toBe(60);
|
||||||
|
|
||||||
// Edit and text by removing second line and it should
|
// Edit and text by removing second line and it should
|
||||||
@@ -810,7 +810,7 @@ describe("textWysiwyg", () => {
|
|||||||
|
|
||||||
expect(text.text).toBe("Hello");
|
expect(text.text).toBe("Hello");
|
||||||
expect(text.originalText).toBe("Hello");
|
expect(text.originalText).toBe("Hello");
|
||||||
expect(text.height).toBe(24);
|
expect(text.height).toBe(25);
|
||||||
expect(text.width).toBe(50);
|
expect(text.width).toBe(50);
|
||||||
expect(text.y).toBe(
|
expect(text.y).toBe(
|
||||||
rectangle.y + h.elements[0].height / 2 - text.height / 2,
|
rectangle.y + h.elements[0].height / 2 - text.height / 2,
|
||||||
@@ -903,7 +903,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
85,
|
85,
|
||||||
5,
|
4.5,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
|
|
||||||
@@ -929,7 +929,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
15,
|
15,
|
||||||
66,
|
65,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
|
|
||||||
@@ -1067,9 +1067,9 @@ describe("textWysiwyg", () => {
|
|||||||
mouse.moveTo(rectangle.x + 100, rectangle.y + 50);
|
mouse.moveTo(rectangle.x + 100, rectangle.y + 50);
|
||||||
mouse.up(rectangle.x + 100, rectangle.y + 50);
|
mouse.up(rectangle.x + 100, rectangle.y + 50);
|
||||||
expect(rectangle.x).toBe(80);
|
expect(rectangle.x).toBe(80);
|
||||||
expect(rectangle.y).toBe(-35);
|
expect(rectangle.y).toBe(-40);
|
||||||
expect(text.x).toBe(85);
|
expect(text.x).toBe(85);
|
||||||
expect(text.y).toBe(-30);
|
expect(text.y).toBe(-35);
|
||||||
|
|
||||||
Keyboard.withModifierKeys({ ctrl: true }, () => {
|
Keyboard.withModifierKeys({ ctrl: true }, () => {
|
||||||
Keyboard.keyPress(KEYS.Z);
|
Keyboard.keyPress(KEYS.Z);
|
||||||
@@ -1112,7 +1112,7 @@ describe("textWysiwyg", () => {
|
|||||||
target: { value: "Online whiteboard collaboration made easy" },
|
target: { value: "Online whiteboard collaboration made easy" },
|
||||||
});
|
});
|
||||||
editor.blur();
|
editor.blur();
|
||||||
expect(rectangle.height).toBe(178);
|
expect(rectangle.height).toBe(185);
|
||||||
mouse.select(rectangle);
|
mouse.select(rectangle);
|
||||||
fireEvent.contextMenu(GlobalTestState.canvas, {
|
fireEvent.contextMenu(GlobalTestState.canvas, {
|
||||||
button: 2,
|
button: 2,
|
||||||
@@ -1186,6 +1186,41 @@ describe("textWysiwyg", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should update line height when font family updated", async () => {
|
||||||
|
Keyboard.keyPress(KEYS.ENTER);
|
||||||
|
expect(getOriginalContainerHeightFromCache(rectangle.id)).toBe(75);
|
||||||
|
|
||||||
|
const editor = document.querySelector(
|
||||||
|
".excalidraw-textEditorContainer > textarea",
|
||||||
|
) as HTMLTextAreaElement;
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
fireEvent.change(editor, { target: { value: "Hello World!" } });
|
||||||
|
editor.blur();
|
||||||
|
expect(
|
||||||
|
(h.elements[1] as ExcalidrawTextElementWithContainer).lineHeight,
|
||||||
|
).toEqual(1.25);
|
||||||
|
|
||||||
|
mouse.select(rectangle);
|
||||||
|
Keyboard.keyPress(KEYS.ENTER);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByTitle(/code/i));
|
||||||
|
expect(
|
||||||
|
(h.elements[1] as ExcalidrawTextElementWithContainer).fontFamily,
|
||||||
|
).toEqual(FONT_FAMILY.Cascadia);
|
||||||
|
expect(
|
||||||
|
(h.elements[1] as ExcalidrawTextElementWithContainer).lineHeight,
|
||||||
|
).toEqual(1.2);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByTitle(/normal/i));
|
||||||
|
expect(
|
||||||
|
(h.elements[1] as ExcalidrawTextElementWithContainer).fontFamily,
|
||||||
|
).toEqual(FONT_FAMILY.Helvetica);
|
||||||
|
expect(
|
||||||
|
(h.elements[1] as ExcalidrawTextElementWithContainer).lineHeight,
|
||||||
|
).toEqual(1.15);
|
||||||
|
});
|
||||||
|
|
||||||
describe("should align correctly", () => {
|
describe("should align correctly", () => {
|
||||||
let editor: HTMLTextAreaElement;
|
let editor: HTMLTextAreaElement;
|
||||||
|
|
||||||
@@ -1245,7 +1280,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
15,
|
15,
|
||||||
45.5,
|
45,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1257,7 +1292,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
30,
|
30,
|
||||||
45.5,
|
45,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1269,7 +1304,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
45,
|
45,
|
||||||
45.5,
|
45,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1281,7 +1316,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
15,
|
15,
|
||||||
66,
|
65,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1292,7 +1327,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
30,
|
30,
|
||||||
66,
|
65,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1303,7 +1338,7 @@ describe("textWysiwyg", () => {
|
|||||||
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
expect([h.elements[1].x, h.elements[1].y]).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
45,
|
45,
|
||||||
66,
|
65,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1333,7 +1368,7 @@ describe("textWysiwyg", () => {
|
|||||||
|
|
||||||
const textElement = h.elements[1] as ExcalidrawTextElement;
|
const textElement = h.elements[1] as ExcalidrawTextElement;
|
||||||
expect(textElement.width).toBe(600);
|
expect(textElement.width).toBe(600);
|
||||||
expect(textElement.height).toBe(24);
|
expect(textElement.height).toBe(25);
|
||||||
expect(textElement.textAlign).toBe(TEXT_ALIGN.LEFT);
|
expect(textElement.textAlign).toBe(TEXT_ALIGN.LEFT);
|
||||||
expect((textElement as ExcalidrawTextElement).text).toBe(
|
expect((textElement as ExcalidrawTextElement).text).toBe(
|
||||||
"Excalidraw is an opensource virtual collaborative whiteboard",
|
"Excalidraw is an opensource virtual collaborative whiteboard",
|
||||||
@@ -1365,7 +1400,7 @@ describe("textWysiwyg", () => {
|
|||||||
],
|
],
|
||||||
fillStyle: "hachure",
|
fillStyle: "hachure",
|
||||||
groupIds: [],
|
groupIds: [],
|
||||||
height: 34,
|
height: 35,
|
||||||
isDeleted: false,
|
isDeleted: false,
|
||||||
link: null,
|
link: null,
|
||||||
locked: false,
|
locked: false,
|
||||||
|
|||||||
+38
-48
@@ -11,7 +11,7 @@ import {
|
|||||||
isBoundToContainer,
|
isBoundToContainer,
|
||||||
isTextElement,
|
isTextElement,
|
||||||
} from "./typeChecks";
|
} from "./typeChecks";
|
||||||
import { CLASSES, VERTICAL_ALIGN } from "../constants";
|
import { CLASSES } from "../constants";
|
||||||
import {
|
import {
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
ExcalidrawLinearElement,
|
ExcalidrawLinearElement,
|
||||||
@@ -22,19 +22,15 @@ import {
|
|||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { mutateElement } from "./mutateElement";
|
import { mutateElement } from "./mutateElement";
|
||||||
import {
|
import {
|
||||||
getApproxLineHeight,
|
|
||||||
getBoundTextElementId,
|
getBoundTextElementId,
|
||||||
getContainerCoords,
|
|
||||||
getContainerDims,
|
getContainerDims,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
getTextElementAngle,
|
getTextElementAngle,
|
||||||
getTextWidth,
|
|
||||||
measureText,
|
|
||||||
normalizeText,
|
normalizeText,
|
||||||
redrawTextBoundingBox,
|
redrawTextBoundingBox,
|
||||||
wrapText,
|
getBoundTextMaxHeight,
|
||||||
getMaxContainerHeight,
|
getBoundTextMaxWidth,
|
||||||
getMaxContainerWidth,
|
computeBoundTextPosition,
|
||||||
} from "./textElement";
|
} from "./textElement";
|
||||||
import {
|
import {
|
||||||
actionDecreaseFontSize,
|
actionDecreaseFontSize,
|
||||||
@@ -44,6 +40,12 @@ 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 {
|
||||||
|
getTextWidth,
|
||||||
|
measureText,
|
||||||
|
wrapText,
|
||||||
|
getTextHeight,
|
||||||
|
} from "./textMeasurements";
|
||||||
|
|
||||||
const getTransform = (
|
const getTransform = (
|
||||||
width: number,
|
width: number,
|
||||||
@@ -150,9 +152,7 @@ export const textWysiwyg = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { textAlign, verticalAlign } = updatedTextElement;
|
const { textAlign, verticalAlign } = updatedTextElement;
|
||||||
const approxLineHeight = getApproxLineHeight(
|
|
||||||
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;
|
||||||
@@ -180,15 +180,12 @@ export const textWysiwyg = ({
|
|||||||
editable,
|
editable,
|
||||||
);
|
);
|
||||||
const containerDims = getContainerDims(container);
|
const containerDims = getContainerDims(container);
|
||||||
// using editor.style.height to get the accurate height of text editor
|
|
||||||
const editorHeight = Number(editable.style.height.slice(0, -2));
|
textElementHeight = getTextHeight(
|
||||||
if (editorHeight > 0) {
|
updatedTextElement.text,
|
||||||
textElementHeight = editorHeight;
|
updatedTextElement.fontSize,
|
||||||
}
|
updatedTextElement.lineHeight,
|
||||||
if (propertiesUpdated) {
|
);
|
||||||
// update height of the editor after properties updated
|
|
||||||
textElementHeight = updatedTextElement.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
let originalContainerData;
|
let originalContainerData;
|
||||||
if (propertiesUpdated) {
|
if (propertiesUpdated) {
|
||||||
@@ -206,14 +203,17 @@ export const textWysiwyg = ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
maxWidth = getMaxContainerWidth(container);
|
maxWidth = getBoundTextMaxWidth(container);
|
||||||
maxHeight = getMaxContainerHeight(container);
|
maxHeight = getBoundTextMaxHeight(
|
||||||
|
container,
|
||||||
|
updatedTextElement as ExcalidrawTextElementWithContainer,
|
||||||
|
);
|
||||||
|
|
||||||
// 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,
|
textElementHeight - maxHeight,
|
||||||
approxLineHeight,
|
element.lineHeight,
|
||||||
);
|
);
|
||||||
mutateElement(container, { height: containerDims.height + diff });
|
mutateElement(container, { height: containerDims.height + diff });
|
||||||
return;
|
return;
|
||||||
@@ -226,25 +226,15 @@ export const textWysiwyg = ({
|
|||||||
) {
|
) {
|
||||||
const diff = Math.min(
|
const diff = Math.min(
|
||||||
maxHeight - textElementHeight,
|
maxHeight - textElementHeight,
|
||||||
approxLineHeight,
|
element.lineHeight,
|
||||||
);
|
);
|
||||||
mutateElement(container, { height: containerDims.height - diff });
|
mutateElement(container, { height: containerDims.height - diff });
|
||||||
}
|
} else {
|
||||||
// Start pushing text upward until a diff of 30px (padding)
|
const { y } = computeBoundTextPosition(
|
||||||
// is reached
|
container,
|
||||||
else {
|
updatedTextElement as ExcalidrawTextElementWithContainer,
|
||||||
const containerCoords = getContainerCoords(container);
|
);
|
||||||
|
coordY = y;
|
||||||
// vertically center align the text
|
|
||||||
if (verticalAlign === VERTICAL_ALIGN.MIDDLE) {
|
|
||||||
if (!isArrowElement(container)) {
|
|
||||||
coordY =
|
|
||||||
containerCoords.y + maxHeight / 2 - textElementHeight / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (verticalAlign === VERTICAL_ALIGN.BOTTOM) {
|
|
||||||
coordY = containerCoords.y + (maxHeight - textElementHeight);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const [viewportX, viewportY] = getViewportCoords(coordX, coordY);
|
const [viewportX, viewportY] = getViewportCoords(coordX, coordY);
|
||||||
@@ -266,10 +256,6 @@ export const textWysiwyg = ({
|
|||||||
editable.selectionEnd = editable.value.length - diff;
|
editable.selectionEnd = editable.value.length - diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@@ -282,7 +268,7 @@ export const textWysiwyg = ({
|
|||||||
Object.assign(editable.style, {
|
Object.assign(editable.style, {
|
||||||
font: getFontString(updatedTextElement),
|
font: getFontString(updatedTextElement),
|
||||||
// must be defined *after* font ¯\_(ツ)_/¯
|
// must be defined *after* font ¯\_(ツ)_/¯
|
||||||
lineHeight: `${lineHeight}px`,
|
lineHeight: element.lineHeight,
|
||||||
width: `${textElementWidth}px`,
|
width: `${textElementWidth}px`,
|
||||||
height: `${textElementHeight}px`,
|
height: `${textElementHeight}px`,
|
||||||
left: `${viewportX}px`,
|
left: `${viewportX}px`,
|
||||||
@@ -369,7 +355,7 @@ export const textWysiwyg = ({
|
|||||||
const wrappedText = wrapText(
|
const wrappedText = wrapText(
|
||||||
`${editable.value}${data}`,
|
`${editable.value}${data}`,
|
||||||
font,
|
font,
|
||||||
getMaxContainerWidth(container),
|
getBoundTextMaxWidth(container),
|
||||||
);
|
);
|
||||||
const width = getTextWidth(wrappedText, font);
|
const width = getTextWidth(wrappedText, font);
|
||||||
editable.style.width = `${width}px`;
|
editable.style.width = `${width}px`;
|
||||||
@@ -386,9 +372,13 @@ export const textWysiwyg = ({
|
|||||||
const wrappedText = wrapText(
|
const wrappedText = wrapText(
|
||||||
normalizeText(editable.value),
|
normalizeText(editable.value),
|
||||||
font,
|
font,
|
||||||
getMaxContainerWidth(container!),
|
getBoundTextMaxWidth(container!),
|
||||||
|
);
|
||||||
|
const { width, height } = measureText(
|
||||||
|
wrappedText,
|
||||||
|
font,
|
||||||
|
updatedTextElement.lineHeight,
|
||||||
);
|
);
|
||||||
const { width, height } = measureText(wrappedText, font);
|
|
||||||
editable.style.width = `${width}px`;
|
editable.style.width = `${width}px`;
|
||||||
editable.style.height = `${height}px`;
|
editable.style.height = `${height}px`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,11 @@ export type ExcalidrawTextElement = _ExcalidrawElementBase &
|
|||||||
verticalAlign: VerticalAlign;
|
verticalAlign: VerticalAlign;
|
||||||
containerId: ExcalidrawGenericElement["id"] | null;
|
containerId: ExcalidrawGenericElement["id"] | null;
|
||||||
originalText: string;
|
originalText: string;
|
||||||
|
/**
|
||||||
|
* Unitless line height (aligned to W3C). To get line height in px, multiply
|
||||||
|
* with font size (using `getLineHeightInPx` helper).
|
||||||
|
*/
|
||||||
|
lineHeight: number & { _brand: "unitlessLineHeight" };
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export type ExcalidrawBindableElement =
|
export type ExcalidrawBindableElement =
|
||||||
|
|||||||
@@ -40,14 +40,14 @@ import {
|
|||||||
} from "../constants";
|
} from "../constants";
|
||||||
import { getStroke, StrokeOptions } from "perfect-freehand";
|
import { getStroke, StrokeOptions } from "perfect-freehand";
|
||||||
import {
|
import {
|
||||||
getApproxLineHeight,
|
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getContainerCoords,
|
getContainerCoords,
|
||||||
getContainerElement,
|
getContainerElement,
|
||||||
getMaxContainerHeight,
|
getBoundTextMaxHeight,
|
||||||
getMaxContainerWidth,
|
getBoundTextMaxWidth,
|
||||||
} from "../element/textElement";
|
} from "../element/textElement";
|
||||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||||
|
import { getLineHeightInPx } from "../element/textMeasurements";
|
||||||
|
|
||||||
// using a stronger invert (100% vs our regular 93%) and saturate
|
// using a stronger invert (100% vs our regular 93%) and saturate
|
||||||
// as a temp hack to make images in dark theme look closer to original
|
// as a temp hack to make images in dark theme look closer to original
|
||||||
@@ -279,9 +279,6 @@ 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
|
|
||||||
? getApproxLineHeight(getFontString(element))
|
|
||||||
: element.height / lines.length;
|
|
||||||
const horizontalOffset =
|
const horizontalOffset =
|
||||||
element.textAlign === "center"
|
element.textAlign === "center"
|
||||||
? element.width / 2
|
? element.width / 2
|
||||||
@@ -290,11 +287,16 @@ const drawElementOnCanvas = (
|
|||||||
: 0;
|
: 0;
|
||||||
context.textBaseline = "bottom";
|
context.textBaseline = "bottom";
|
||||||
|
|
||||||
|
const lineHeightPx = getLineHeightInPx(
|
||||||
|
element.fontSize,
|
||||||
|
element.lineHeight,
|
||||||
|
);
|
||||||
|
|
||||||
for (let index = 0; index < lines.length; index++) {
|
for (let index = 0; index < lines.length; index++) {
|
||||||
context.fillText(
|
context.fillText(
|
||||||
lines[index],
|
lines[index],
|
||||||
horizontalOffset,
|
horizontalOffset,
|
||||||
(index + 1) * lineHeight,
|
(index + 1) * lineHeightPx,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
context.restore();
|
context.restore();
|
||||||
@@ -820,14 +822,17 @@ const drawElementFromCanvas = (
|
|||||||
process.env.REACT_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX &&
|
process.env.REACT_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX &&
|
||||||
hasBoundTextElement(element)
|
hasBoundTextElement(element)
|
||||||
) {
|
) {
|
||||||
|
const textElement = getBoundTextElement(
|
||||||
|
element,
|
||||||
|
) as ExcalidrawTextElementWithContainer;
|
||||||
const coords = getContainerCoords(element);
|
const coords = getContainerCoords(element);
|
||||||
context.strokeStyle = "#c92a2a";
|
context.strokeStyle = "#c92a2a";
|
||||||
context.lineWidth = 3;
|
context.lineWidth = 3;
|
||||||
context.strokeRect(
|
context.strokeRect(
|
||||||
(coords.x + renderConfig.scrollX) * window.devicePixelRatio,
|
(coords.x + renderConfig.scrollX) * window.devicePixelRatio,
|
||||||
(coords.y + renderConfig.scrollY) * window.devicePixelRatio,
|
(coords.y + renderConfig.scrollY) * window.devicePixelRatio,
|
||||||
getMaxContainerWidth(element) * window.devicePixelRatio,
|
getBoundTextMaxWidth(element) * window.devicePixelRatio,
|
||||||
getMaxContainerHeight(element) * window.devicePixelRatio,
|
getBoundTextMaxHeight(element, textElement) * window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1313,7 +1318,10 @@ export const renderElementToSvg = (
|
|||||||
}) rotate(${degree} ${cx} ${cy})`,
|
}) rotate(${degree} ${cx} ${cy})`,
|
||||||
);
|
);
|
||||||
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.height / lines.length;
|
const lineHeightPx = getLineHeightInPx(
|
||||||
|
element.fontSize,
|
||||||
|
element.lineHeight,
|
||||||
|
);
|
||||||
const horizontalOffset =
|
const horizontalOffset =
|
||||||
element.textAlign === "center"
|
element.textAlign === "center"
|
||||||
? element.width / 2
|
? element.width / 2
|
||||||
@@ -1331,7 +1339,7 @@ export const renderElementToSvg = (
|
|||||||
const text = svgRoot.ownerDocument!.createElementNS(SVG_NS, "text");
|
const text = svgRoot.ownerDocument!.createElementNS(SVG_NS, "text");
|
||||||
text.textContent = lines[i];
|
text.textContent = lines[i];
|
||||||
text.setAttribute("x", `${horizontalOffset}`);
|
text.setAttribute("x", `${horizontalOffset}`);
|
||||||
text.setAttribute("y", `${i * lineHeight}`);
|
text.setAttribute("y", `${i * lineHeightPx}`);
|
||||||
text.setAttribute("font-family", getFontFamilyString(element));
|
text.setAttribute("font-family", getFontFamilyString(element));
|
||||||
text.setAttribute("font-size", `${element.fontSize}px`);
|
text.setAttribute("font-size", `${element.fontSize}px`);
|
||||||
text.setAttribute("fill", element.strokeColor);
|
text.setAttribute("fill", element.strokeColor);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ exports[`Test Linear Elements Test bound text element should match styles for te
|
|||||||
class="excalidraw-wysiwyg"
|
class="excalidraw-wysiwyg"
|
||||||
data-type="wysiwyg"
|
data-type="wysiwyg"
|
||||||
dir="auto"
|
dir="auto"
|
||||||
style="position: absolute; display: inline-block; min-height: 1em; margin: 0px; padding: 0px; border: 0px; outline: 0; resize: none; background: transparent; overflow: hidden; z-index: var(--zIndex-wysiwyg); word-break: break-word; white-space: pre-wrap; overflow-wrap: break-word; box-sizing: content-box; width: 10.5px; height: 24px; left: 35px; top: 8px; transform: translate(0px, 0px) scale(1) rotate(0deg); text-align: center; vertical-align: middle; color: rgb(0, 0, 0); opacity: 1; filter: var(--theme-filter); max-height: -8px; font: Emoji 20px 20px; line-height: 24px; font-family: Virgil, Segoe UI Emoji;"
|
style="position: absolute; display: inline-block; min-height: 1em; margin: 0px; padding: 0px; border: 0px; outline: 0; resize: none; background: transparent; overflow: hidden; z-index: var(--zIndex-wysiwyg); word-break: break-word; white-space: pre-wrap; overflow-wrap: break-word; box-sizing: content-box; width: 10.5px; height: 25px; left: 35px; top: 7.5px; transform: translate(0px, 0px) scale(1) rotate(0deg); text-align: center; vertical-align: middle; color: rgb(0, 0, 0); opacity: 1; filter: var(--theme-filter); max-height: -7.5px; font: Emoji 20px 20px; line-height: 1.25; font-family: Virgil, Segoe UI Emoji;"
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
wrap="off"
|
wrap="off"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { UI, Pointer, Keyboard } from "./helpers/ui";
|
|||||||
import { getTransformHandles } from "../element/transformHandles";
|
import { getTransformHandles } from "../element/transformHandles";
|
||||||
import { API } from "./helpers/api";
|
import { API } from "./helpers/api";
|
||||||
import { KEYS } from "../keys";
|
import { KEYS } from "../keys";
|
||||||
|
import { actionCreateContainerFromText } from "../actions/actionBoundText";
|
||||||
|
|
||||||
const { h } = window;
|
const { h } = window;
|
||||||
|
|
||||||
@@ -209,4 +210,103 @@ describe("element binding", () => {
|
|||||||
).toBe(null);
|
).toBe(null);
|
||||||
expect(arrow.endBinding?.elementId).toBe(text.id);
|
expect(arrow.endBinding?.elementId).toBe(text.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should update binding when text containerized", async () => {
|
||||||
|
const rectangle1 = API.createElement({
|
||||||
|
type: "rectangle",
|
||||||
|
id: "rectangle1",
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
boundElements: [
|
||||||
|
{ id: "arrow1", type: "arrow" },
|
||||||
|
{ id: "arrow2", type: "arrow" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const arrow1 = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
id: "arrow1",
|
||||||
|
points: [
|
||||||
|
[0, 0],
|
||||||
|
[0, -87.45777932247563],
|
||||||
|
],
|
||||||
|
startBinding: {
|
||||||
|
elementId: "rectangle1",
|
||||||
|
focus: 0.2,
|
||||||
|
gap: 7,
|
||||||
|
},
|
||||||
|
endBinding: {
|
||||||
|
elementId: "text1",
|
||||||
|
focus: 0.2,
|
||||||
|
gap: 7,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const arrow2 = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
id: "arrow2",
|
||||||
|
points: [
|
||||||
|
[0, 0],
|
||||||
|
[0, -87.45777932247563],
|
||||||
|
],
|
||||||
|
startBinding: {
|
||||||
|
elementId: "text1",
|
||||||
|
focus: 0.2,
|
||||||
|
gap: 7,
|
||||||
|
},
|
||||||
|
endBinding: {
|
||||||
|
elementId: "rectangle1",
|
||||||
|
focus: 0.2,
|
||||||
|
gap: 7,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const text1 = API.createElement({
|
||||||
|
type: "text",
|
||||||
|
id: "text1",
|
||||||
|
text: "ola",
|
||||||
|
boundElements: [
|
||||||
|
{ id: "arrow1", type: "arrow" },
|
||||||
|
{ id: "arrow2", type: "arrow" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
h.elements = [rectangle1, arrow1, arrow2, text1];
|
||||||
|
|
||||||
|
API.setSelectedElements([text1]);
|
||||||
|
|
||||||
|
expect(h.state.selectedElementIds[text1.id]).toBe(true);
|
||||||
|
|
||||||
|
h.app.actionManager.executeAction(actionCreateContainerFromText);
|
||||||
|
|
||||||
|
// new text container will be placed before the text element
|
||||||
|
const container = h.elements.at(-2)!;
|
||||||
|
|
||||||
|
expect(container.type).toBe("rectangle");
|
||||||
|
expect(container.id).not.toBe(rectangle1.id);
|
||||||
|
|
||||||
|
expect(container).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
boundElements: expect.arrayContaining([
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
id: text1.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "arrow",
|
||||||
|
id: arrow1.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "arrow",
|
||||||
|
id: arrow2.id,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(arrow1.startBinding?.elementId).toBe(rectangle1.id);
|
||||||
|
expect(arrow1.endBinding?.elementId).toBe(container.id);
|
||||||
|
expect(arrow2.startBinding?.elementId).toBe(container.id);
|
||||||
|
expect(arrow2.endBinding?.elementId).toBe(rectangle1.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,10 +3,13 @@ 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 { getFontString } from "../utils";
|
|
||||||
import { getElementBounds } from "../element";
|
import { getElementBounds } from "../element";
|
||||||
import { NormalizedZoomValue } from "../types";
|
import { NormalizedZoomValue } from "../types";
|
||||||
|
import {
|
||||||
|
getDefaultLineHeight,
|
||||||
|
getLineHeightInPx,
|
||||||
|
} from "../element/textMeasurements";
|
||||||
|
|
||||||
const { h } = window;
|
const { h } = window;
|
||||||
|
|
||||||
@@ -118,12 +121,10 @@ 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 lineHeightPx =
|
||||||
getApproxLineHeight(
|
getLineHeightInPx(
|
||||||
getFontString({
|
h.app.state.currentItemFontSize,
|
||||||
fontSize: h.app.state.currentItemFontSize,
|
getDefaultLineHeight(h.state.currentItemFontFamily),
|
||||||
fontFamily: h.app.state.currentItemFontFamily,
|
|
||||||
}),
|
|
||||||
) +
|
) +
|
||||||
10 / h.app.state.zoom.value;
|
10 / h.app.state.zoom.value;
|
||||||
mouse.moveTo(100, 100);
|
mouse.moveTo(100, 100);
|
||||||
@@ -135,19 +136,17 @@ describe("paste text as single lines", () => {
|
|||||||
for (let i = 1; i < h.elements.length; i++) {
|
for (let i = 1; i < h.elements.length; i++) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const [fx, elY] = getElementBounds(h.elements[i]);
|
const [fx, elY] = getElementBounds(h.elements[i]);
|
||||||
expect(elY).toEqual(firstElY + lineHeight * i);
|
expect(elY).toEqual(firstElY + lineHeightPx * i);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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 lineHeightPx =
|
||||||
getApproxLineHeight(
|
getLineHeightInPx(
|
||||||
getFontString({
|
h.app.state.currentItemFontSize,
|
||||||
fontSize: h.app.state.currentItemFontSize,
|
getDefaultLineHeight(h.state.currentItemFontFamily),
|
||||||
fontFamily: h.app.state.currentItemFontFamily,
|
|
||||||
}),
|
|
||||||
) +
|
) +
|
||||||
10 / h.app.state.zoom.value;
|
10 / h.app.state.zoom.value;
|
||||||
mouse.moveTo(100, 100);
|
mouse.moveTo(100, 100);
|
||||||
@@ -158,7 +157,7 @@ describe("paste text as single lines", () => {
|
|||||||
const [fx, firstElY] = getElementBounds(h.elements[0]);
|
const [fx, firstElY] = getElementBounds(h.elements[0]);
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const [lx, lastElY] = getElementBounds(h.elements[1]);
|
const [lx, lastElY] = getElementBounds(h.elements[1]);
|
||||||
expect(lastElY).toEqual(firstElY + lineHeight * 2);
|
expect(lastElY).toEqual(firstElY + lineHeightPx * 2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -224,7 +223,7 @@ describe("Paste bound text container", () => {
|
|||||||
await sleep(1);
|
await sleep(1);
|
||||||
expect(h.elements.length).toEqual(2);
|
expect(h.elements.length).toEqual(2);
|
||||||
const container = h.elements[0];
|
const container = h.elements[0];
|
||||||
expect(container.height).toBe(354);
|
expect(container.height).toBe(368);
|
||||||
expect(container.width).toBe(166);
|
expect(container.width).toBe(166);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -247,7 +246,7 @@ describe("Paste bound text container", () => {
|
|||||||
await sleep(1);
|
await sleep(1);
|
||||||
expect(h.elements.length).toEqual(2);
|
expect(h.elements.length).toEqual(2);
|
||||||
const container = h.elements[0];
|
const container = h.elements[0];
|
||||||
expect(container.height).toBe(740);
|
expect(container.height).toBe(770);
|
||||||
expect(container.width).toBe(166);
|
expect(container.width).toBe(166);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -291,6 +291,7 @@ Object {
|
|||||||
"height": 100,
|
"height": 100,
|
||||||
"id": "id-text01",
|
"id": "id-text01",
|
||||||
"isDeleted": false,
|
"isDeleted": false,
|
||||||
|
"lineHeight": 1.25,
|
||||||
"link": null,
|
"link": null,
|
||||||
"locked": false,
|
"locked": false,
|
||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
@@ -312,7 +313,7 @@ Object {
|
|||||||
"verticalAlign": "middle",
|
"verticalAlign": "middle",
|
||||||
"width": 100,
|
"width": 100,
|
||||||
"x": -20,
|
"x": -20,
|
||||||
"y": -8.4,
|
"y": -8.75,
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
@@ -329,6 +330,7 @@ Object {
|
|||||||
"height": 100,
|
"height": 100,
|
||||||
"id": "id-text01",
|
"id": "id-text01",
|
||||||
"isDeleted": false,
|
"isDeleted": false,
|
||||||
|
"lineHeight": 1.25,
|
||||||
"link": null,
|
"link": null,
|
||||||
"locked": false,
|
"locked": false,
|
||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
|
|||||||
@@ -111,6 +111,9 @@ export class API {
|
|||||||
fileId?: T extends "image" ? string : never;
|
fileId?: T extends "image" ? string : never;
|
||||||
scale?: T extends "image" ? ExcalidrawImageElement["scale"] : never;
|
scale?: T extends "image" ? ExcalidrawImageElement["scale"] : never;
|
||||||
status?: T extends "image" ? ExcalidrawImageElement["status"] : never;
|
status?: T extends "image" ? ExcalidrawImageElement["status"] : never;
|
||||||
|
startBinding?: T extends "arrow"
|
||||||
|
? ExcalidrawLinearElement["startBinding"]
|
||||||
|
: never;
|
||||||
endBinding?: T extends "arrow"
|
endBinding?: T extends "arrow"
|
||||||
? ExcalidrawLinearElement["endBinding"]
|
? ExcalidrawLinearElement["endBinding"]
|
||||||
: never;
|
: never;
|
||||||
@@ -178,11 +181,13 @@ export class API {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "text":
|
case "text":
|
||||||
|
const fontSize = rest.fontSize ?? appState.currentItemFontSize;
|
||||||
|
const fontFamily = rest.fontFamily ?? appState.currentItemFontFamily;
|
||||||
element = newTextElement({
|
element = newTextElement({
|
||||||
...base,
|
...base,
|
||||||
text: rest.text || "test",
|
text: rest.text || "test",
|
||||||
fontSize: rest.fontSize ?? appState.currentItemFontSize,
|
fontSize,
|
||||||
fontFamily: rest.fontFamily ?? appState.currentItemFontFamily,
|
fontFamily,
|
||||||
textAlign: rest.textAlign ?? appState.currentItemTextAlign,
|
textAlign: rest.textAlign ?? appState.currentItemTextAlign,
|
||||||
verticalAlign: rest.verticalAlign ?? DEFAULT_VERTICAL_ALIGN,
|
verticalAlign: rest.verticalAlign ?? DEFAULT_VERTICAL_ALIGN,
|
||||||
containerId: rest.containerId ?? undefined,
|
containerId: rest.containerId ?? undefined,
|
||||||
@@ -221,6 +226,10 @@ export class API {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (element.type === "arrow") {
|
||||||
|
element.startBinding = rest.startBinding ?? null;
|
||||||
|
element.endBinding = rest.endBinding ?? null;
|
||||||
|
}
|
||||||
if (id) {
|
if (id) {
|
||||||
element.id = id;
|
element.id = id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,8 @@ import { KEYS } from "../keys";
|
|||||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||||
import { queryByTestId, queryByText } from "@testing-library/react";
|
import { queryByTestId, queryByText } from "@testing-library/react";
|
||||||
import { resize, rotate } from "./utils";
|
import { resize, rotate } from "./utils";
|
||||||
import {
|
import { getBoundTextMaxWidth } from "../element/textElement";
|
||||||
getBoundTextElementPosition,
|
import { wrapText } from "../element/textMeasurements";
|
||||||
wrapText,
|
|
||||||
getMaxContainerWidth,
|
|
||||||
} from "../element/textElement";
|
|
||||||
import * as textElementUtils from "../element/textElement";
|
import * as textElementUtils from "../element/textElement";
|
||||||
import { ROUNDNESS } from "../constants";
|
import { ROUNDNESS } from "../constants";
|
||||||
|
|
||||||
@@ -729,7 +726,7 @@ describe("Test Linear Elements", () => {
|
|||||||
type: "text",
|
type: "text",
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
text: wrapText(text, font, getMaxContainerWidth(container)),
|
text: wrapText(text, font, getBoundTextMaxWidth(container)),
|
||||||
containerId: container.id,
|
containerId: container.id,
|
||||||
width: 30,
|
width: 30,
|
||||||
height: 20,
|
height: 20,
|
||||||
@@ -937,8 +934,9 @@ describe("Test Linear Elements", () => {
|
|||||||
|
|
||||||
expect(container.angle).toBe(0);
|
expect(container.angle).toBe(0);
|
||||||
expect(textElement.angle).toBe(0);
|
expect(textElement.angle).toBe(0);
|
||||||
expect(getBoundTextElementPosition(arrow, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(arrow, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 75,
|
"x": 75,
|
||||||
"y": 60,
|
"y": 60,
|
||||||
@@ -964,8 +962,9 @@ describe("Test Linear Elements", () => {
|
|||||||
rotate(container, -35, 55);
|
rotate(container, -35, 55);
|
||||||
expect(container.angle).toMatchInlineSnapshot(`1.3988061968364685`);
|
expect(container.angle).toMatchInlineSnapshot(`1.3988061968364685`);
|
||||||
expect(textElement.angle).toBe(0);
|
expect(textElement.angle).toBe(0);
|
||||||
expect(getBoundTextElementPosition(container, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(container, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 21.73926141863671,
|
"x": 21.73926141863671,
|
||||||
"y": 73.31003398390868,
|
"y": 73.31003398390868,
|
||||||
@@ -1002,8 +1001,9 @@ describe("Test Linear Elements", () => {
|
|||||||
);
|
);
|
||||||
expect(container.width).toBe(70);
|
expect(container.width).toBe(70);
|
||||||
expect(container.height).toBe(50);
|
expect(container.height).toBe(50);
|
||||||
expect(getBoundTextElementPosition(container, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(container, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 75,
|
"x": 75,
|
||||||
"y": 60,
|
"y": 60,
|
||||||
@@ -1031,16 +1031,17 @@ describe("Test Linear Elements", () => {
|
|||||||
expect({ width: container.width, height: container.height })
|
expect({ width: container.width, height: container.height })
|
||||||
.toMatchInlineSnapshot(`
|
.toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"height": 128,
|
"height": 130,
|
||||||
"width": 367,
|
"width": 367,
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
expect(getBoundTextElementPosition(container, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(container, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 272,
|
"x": 272,
|
||||||
"y": 46,
|
"y": 45,
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
expect((h.elements[1] as ExcalidrawTextElementWithContainer).text)
|
expect((h.elements[1] as ExcalidrawTextElementWithContainer).text)
|
||||||
@@ -1052,11 +1053,11 @@ describe("Test Linear Elements", () => {
|
|||||||
.toMatchInlineSnapshot(`
|
.toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
20,
|
20,
|
||||||
36,
|
35,
|
||||||
502,
|
502,
|
||||||
94,
|
95,
|
||||||
205.9061448421403,
|
205.9061448421403,
|
||||||
53,
|
52.5,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1070,8 +1071,9 @@ describe("Test Linear Elements", () => {
|
|||||||
arrow,
|
arrow,
|
||||||
);
|
);
|
||||||
expect(container.width).toBe(40);
|
expect(container.width).toBe(40);
|
||||||
expect(getBoundTextElementPosition(container, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(container, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 25,
|
"x": 25,
|
||||||
"y": 10,
|
"y": 10,
|
||||||
@@ -1090,16 +1092,17 @@ describe("Test Linear Elements", () => {
|
|||||||
expect({ width: container.width, height: container.height })
|
expect({ width: container.width, height: container.height })
|
||||||
.toMatchInlineSnapshot(`
|
.toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"height": 128,
|
"height": 130,
|
||||||
"width": 340,
|
"width": 340,
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
expect(getBoundTextElementPosition(container, textElement))
|
expect(
|
||||||
.toMatchInlineSnapshot(`
|
LinearElementEditor.getBoundTextElementPosition(container, textElement),
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
Object {
|
Object {
|
||||||
"x": 75,
|
"x": 75,
|
||||||
"y": -4,
|
"y": -5,
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
expect(textElement.text).toMatchInlineSnapshot(`
|
expect(textElement.text).toMatchInlineSnapshot(`
|
||||||
@@ -1149,7 +1152,7 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(rect.x).toBe(400);
|
expect(rect.x).toBe(400);
|
||||||
expect(rect.y).toBe(0);
|
expect(rect.y).toBe(0);
|
||||||
expect(
|
expect(
|
||||||
wrapText(textElement.originalText, font, getMaxContainerWidth(arrow)),
|
wrapText(textElement.originalText, font, getBoundTextMaxWidth(arrow)),
|
||||||
).toMatchInlineSnapshot(`
|
).toMatchInlineSnapshot(`
|
||||||
"Online whiteboard collaboration
|
"Online whiteboard collaboration
|
||||||
made easy"
|
made easy"
|
||||||
@@ -1172,7 +1175,7 @@ describe("Test Linear Elements", () => {
|
|||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
wrapText(textElement.originalText, font, getMaxContainerWidth(arrow)),
|
wrapText(textElement.originalText, font, getBoundTextMaxWidth(arrow)),
|
||||||
).toMatchInlineSnapshot(`
|
).toMatchInlineSnapshot(`
|
||||||
"Online whiteboard
|
"Online whiteboard
|
||||||
collaboration made
|
collaboration made
|
||||||
|
|||||||
Reference in New Issue
Block a user