Files
excalidraw/packages/element/src/textElement.ts
T

756 lines
22 KiB
TypeScript

import {
ARROW_LABEL_FONT_SIZE_TO_MIN_WIDTH_RATIO,
ARROW_LABEL_WIDTH_FRACTION,
BOUND_TEXT_PADDING,
DEFAULT_FONT_SIZE,
STICKY_NOTE_BODY_INSET_Y,
STICKY_NOTE_PADDING,
TEXT_ALIGN,
VERTICAL_ALIGN,
getFontString,
isProdEnv,
invariant,
} from "@excalidraw/common";
import {
distanceToLineSegment,
lineSegment,
pointFrom,
pointRotateRads,
polygonFromPoints,
polygonIncludesPoint,
type GlobalPoint,
type Radians,
} from "@excalidraw/math";
import type { ExtractSetType } from "@excalidraw/common/utility-types";
import {
resetOriginalContainerCache,
updateOriginalContainerCache,
} from "./containerCache";
import { LinearElementEditor } from "./linearElementEditor";
import { getPositionAfterHeightChange } from "./sizeHelpers";
import { updateStickyNoteLayout } from "./stickyNote";
import { measureText } from "./textMeasurements";
import { wrapText } from "./textWrapping";
import {
isBoundToContainer,
isArrowElement,
isEllipseArcElement,
isStickyNoteElement,
isTextElement,
} from "./typeChecks";
import {
getBoundTextElementId,
getBoundTextElement,
getContainerElement,
} from "./boundText";
import type { RenderEnvironment } from "./renderEnvironment";
import type { Scene } from "./Scene";
import type { MaybeTransformHandleType } from "./transformHandles";
import type {
ElementsMap,
ExcalidrawElement,
ExcalidrawElementType,
ExcalidrawEllipseArcElement,
ExcalidrawTextContainer,
ExcalidrawTextElement,
ExcalidrawTextElementWithContainer,
} from "./types";
/** the space between a wedge's ellipse and its label */
const ELLIPSE_ARC_LABEL_GAP = 8;
/**
* How far from straight up or down a wedge's center line is for its label to
* be all to the side of it (11.25°)
*/
const ELLIPSE_ARC_LABEL_SIDE_ANGLE = Math.PI / 16;
export const redrawTextBoundingBox = (
textElement: ExcalidrawTextElement,
container: ExcalidrawElement | null,
scene: Scene,
renderEnvironment?: RenderEnvironment,
) => {
const elementsMap = scene.getNonDeletedElementsMap();
if (container && isStickyNoteElement(container)) {
// the sticky fit owns both halves (label + note geometry). `textElement`
// may be an uncommitted clone (font actions clone before install), so it
// is passed explicitly instead of looked up in the scene
updateStickyNoteLayout(container, scene, {
text: textElement,
renderEnvironment,
});
return;
}
let maxWidth = undefined;
if (!isProdEnv()) {
invariant(
!container || !isArrowElement(container) || textElement.angle === 0,
"text element angle must be 0 if bound to arrow container",
);
}
const boundTextUpdates = {
x: textElement.x,
y: textElement.y,
text: textElement.text,
width: textElement.width,
height: textElement.height,
angle: container ? getBoundTextAngle(container) : textElement.angle,
};
boundTextUpdates.text = textElement.text;
if (container || !textElement.autoResize) {
maxWidth = container
? getBoundTextMaxWidth(container, textElement)
: textElement.width;
boundTextUpdates.text = wrapText(
textElement.originalText,
getFontString(textElement),
maxWidth,
renderEnvironment,
);
}
const metrics = measureText(
boundTextUpdates.text,
getFontString(textElement),
textElement.lineHeight,
renderEnvironment,
);
// Note: only update width for unwrapped text and bound texts (which always have autoResize set to true)
if (textElement.autoResize) {
boundTextUpdates.width = metrics.width;
}
boundTextUpdates.height = metrics.height;
if (container) {
const maxContainerHeight = getBoundTextMaxHeight(
container,
textElement as ExcalidrawTextElementWithContainer,
);
const maxContainerWidth = getBoundTextMaxWidth(container, textElement);
if (
containerGrowsWithBoundText(container) &&
metrics.height > maxContainerHeight
) {
const nextHeight = computeContainerDimensionForBoundText(
metrics.height,
container.type,
);
scene.mutateElement(container, { height: nextHeight });
updateOriginalContainerCache(container.id, nextHeight);
}
if (!isEllipseArcElement(container) && metrics.width > maxContainerWidth) {
const nextWidth = computeContainerDimensionForBoundText(
metrics.width,
container.type,
);
scene.mutateElement(container, { width: nextWidth });
}
const updatedTextElement = {
...textElement,
...boundTextUpdates,
} as ExcalidrawTextElementWithContainer;
const { x, y } = computeBoundTextPosition(
container,
updatedTextElement,
elementsMap,
);
boundTextUpdates.x = x;
boundTextUpdates.y = y;
}
scene.mutateElement(textElement, boundTextUpdates);
};
export const handleBindTextResize = (
container: ExcalidrawElement,
scene: Scene,
transformHandleType: MaybeTransformHandleType,
shouldMaintainAspectRatio = false,
shouldResizeFromCenter = false,
flipByY = false,
renderEnvironment?: RenderEnvironment,
) => {
if (isStickyNoteElement(container)) {
// resize callers pass their intents to `updateStickyNoteLayout` directly
// and own the bound-arrow pass; this is the fallback for generic callers
updateStickyNoteLayout(container, scene, {
bindings: false,
renderEnvironment,
});
return;
}
const elementsMap = scene.getNonDeletedElementsMap();
const boundTextElementId = getBoundTextElementId(container);
if (!boundTextElementId) {
return;
}
resetOriginalContainerCache(container.id);
const textElement = getBoundTextElement(container, elementsMap);
if (textElement && textElement.text) {
if (!container) {
return;
}
let text = textElement.text;
let nextHeight = textElement.height;
let nextWidth = textElement.width;
const maxWidth = getBoundTextMaxWidth(container, textElement);
const maxHeight = getBoundTextMaxHeight(container, textElement);
let containerHeight = container.height;
if (
shouldMaintainAspectRatio ||
(transformHandleType !== "n" && transformHandleType !== "s")
) {
if (text) {
text = wrapText(
textElement.originalText,
getFontString(textElement),
maxWidth,
renderEnvironment,
);
}
const metrics = measureText(
text,
getFontString(textElement),
textElement.lineHeight,
renderEnvironment,
);
nextHeight = metrics.height;
nextWidth = metrics.width;
}
// increase height in case text element height exceeds (but not an
// arrow's, which its points determine, and its label doesn't grow)
if (containerGrowsWithBoundText(container) && nextHeight > maxHeight) {
containerHeight = computeContainerDimensionForBoundText(
nextHeight,
container.type,
);
// Crossing the opposite edge swaps the anchor for text-driven growth.
const shouldResizeFromTop =
(transformHandleType === "n" ||
transformHandleType === "ne" ||
transformHandleType === "nw") !== flipByY;
scene.mutateElement(container, {
height: containerHeight,
...getPositionAfterHeightChange(
container,
containerHeight,
shouldResizeFromCenter
? "center"
: shouldResizeFromTop
? "bottom"
: "top",
),
});
}
scene.mutateElement(textElement, {
text,
width: nextWidth,
height: nextHeight,
});
if (!isArrowElement(container)) {
scene.mutateElement(
textElement,
computeBoundTextPosition(container, textElement, elementsMap),
);
}
}
};
export const computeBoundTextPosition = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
elementsMap: ElementsMap,
) => {
if (isArrowElement(container)) {
return LinearElementEditor.getBoundTextElementPosition(
container,
boundTextElement,
elementsMap,
);
}
if (isEllipseArcElement(container)) {
return computeEllipseArcLabelPosition(container, boundTextElement);
}
const containerCoords = getContainerCoords(container);
const maxContainerHeight = getBoundTextMaxHeight(container, boundTextElement);
const maxContainerWidth = getBoundTextMaxWidth(container, boundTextElement);
let x;
let y;
if (boundTextElement.verticalAlign === VERTICAL_ALIGN.TOP) {
y = containerCoords.y;
} else if (boundTextElement.verticalAlign === VERTICAL_ALIGN.BOTTOM) {
y = containerCoords.y + (maxContainerHeight - boundTextElement.height);
} else if (isStickyNoteElement(container)) {
// a note's label body ends above the creation-date footer, but a label
// centered in that body sits visibly high — center it in the whole
// padded note while it stays clear of the footer, and only push it up
// against the body's bottom once it would overlap
const paddedHeight = container.height - STICKY_NOTE_PADDING * 2;
y =
containerCoords.y +
Math.min(
(paddedHeight - boundTextElement.height) / 2,
maxContainerHeight - boundTextElement.height,
);
} else {
y =
containerCoords.y +
(maxContainerHeight / 2 - boundTextElement.height / 2);
}
if (boundTextElement.textAlign === TEXT_ALIGN.LEFT) {
x = containerCoords.x;
} else if (boundTextElement.textAlign === TEXT_ALIGN.RIGHT) {
x = containerCoords.x + (maxContainerWidth - boundTextElement.width);
} else {
x =
containerCoords.x + (maxContainerWidth / 2 - boundTextElement.width / 2);
}
const angle = (container.angle ?? 0) as Radians;
if (angle !== 0) {
// A sticky's footer makes its body asymmetric. The body still rotates
// about the note's center, rather than about its own (higher) center.
const contentCenter = isStickyNoteElement(container)
? pointFrom(
container.x + container.width / 2,
container.y + container.height / 2,
)
: pointFrom(
containerCoords.x + maxContainerWidth / 2,
containerCoords.y + maxContainerHeight / 2,
);
const textCenter = pointFrom(
x + boundTextElement.width / 2,
y + boundTextElement.height / 2,
);
const [rx, ry] = pointRotateRads(textCenter, contentCenter, angle);
return {
x: rx - boundTextElement.width / 2,
y: ry - boundTextElement.height / 2,
};
}
return { x, y };
};
/**
* A wedge's label sits outside its ellipse (as the labels of a pie chart do):
* the wedge itself narrows to a point at the ellipse's center, where a
* centered label would sit.
*
* The label is on the wedge's center line, which runs from the ellipse's
* center through the middle of the arc. It's to the side of the wedge, with
* the middle of its closer side on the line, as far out as it takes for it to
* clear the ellipse. Only above and below the ellipse it's centered on the
* line, which it slides to as the line nears the vertical.
*
* The label stays upright when the ellipse is rotated (see
* `getBoundTextAngle`), and moves around with the arc's middle instead.
*/
const computeEllipseArcLabelPosition = (
container: ExcalidrawEllipseArcElement,
boundTextElement: ExcalidrawTextElementWithContainer,
) => {
const { start, sweep } = container.arc;
const { width, height } = boundTextElement;
const middle = start + sweep / 2;
const rx = container.width / 2;
const ry = container.height / 2;
const origin = pointFrom<GlobalPoint>(0, 0);
// the direction of the center line, as the ellipse is turned
const [lineX, lineY] = pointRotateRads(
rx || ry
? pointFrom<GlobalPoint>(rx * Math.cos(middle), ry * Math.sin(middle))
: pointFrom<GlobalPoint>(Math.cos(middle), Math.sin(middle)),
origin,
container.angle,
);
const lineLength = Math.hypot(lineX, lineY);
const directionX = lineX / lineLength;
const directionY = lineY / lineLength;
// where the label's center is, off the point of the line the label is at:
// half its width to the side, less so close to the vertical
const fromVertical = Math.atan2(Math.abs(directionX), Math.abs(directionY));
const offsetX =
((Math.sign(directionX) * width) / 2) *
Math.min(1, fromVertical / ELLIPSE_ARC_LABEL_SIDE_ANGLE);
// Whether the label would be in the ellipse (or closer to it than the gap)
// that far along the line. It's told where that padded ellipse is a circle
// of radius 1 around the origin.
const overlapsEllipse = (distance: number) => {
const corners = [
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
].map(([horizontal, vertical]) => {
const [x, y] = pointRotateRads(
pointFrom<GlobalPoint>(
directionX * distance + offsetX + (horizontal * width) / 2,
directionY * distance + (vertical * height) / 2,
),
origin,
-container.angle as Radians,
);
return pointFrom<GlobalPoint>(
x / (rx + ELLIPSE_ARC_LABEL_GAP),
y / (ry + ELLIPSE_ARC_LABEL_GAP),
);
});
return (
polygonIncludesPoint(origin, polygonFromPoints(corners)) ||
corners.some(
(corner, index) =>
distanceToLineSegment(
origin,
lineSegment(corner, corners[(index + 1) % corners.length]),
) < 1,
)
);
};
// The label holds the ellipse's center at the line's start, and leaves the
// ellipse once along the line, both of them being convex.
let overlapping = 0;
let clear = rx + ry + ELLIPSE_ARC_LABEL_GAP + width + height;
for (let step = 0; step < 32; step++) {
const distance = (overlapping + clear) / 2;
if (overlapsEllipse(distance)) {
overlapping = distance;
} else {
clear = distance;
}
}
return {
x: container.x + rx + directionX * clear + offsetX - width / 2,
y: container.y + ry + directionY * clear - height / 2,
};
};
/**
* The point a text bound to this container centers on — and, for arrows, the
* point the text tool snaps a new label to.
*/
export const getContainerCenter = (
container: ExcalidrawElement,
elementsMap: ElementsMap,
) => {
if (!isArrowElement(container)) {
return {
x: container.x + container.width / 2,
y: container.y + container.height / 2,
};
}
const center = LinearElementEditor.getBoundTextElementCenter(
container,
elementsMap,
);
return { x: center[0], y: center[1] };
};
export const getContainerCoords = (container: ExcalidrawElement) => {
const padding = isStickyNoteElement(container)
? STICKY_NOTE_PADDING
: BOUND_TEXT_PADDING;
let offsetX = padding;
let offsetY = padding;
if (container.type === "ellipse") {
// The derivation of coordinates is explained in https://github.com/excalidraw/excalidraw/pull/6172
offsetX += (container.width / 2) * (1 - Math.sqrt(2) / 2);
offsetY += (container.height / 2) * (1 - Math.sqrt(2) / 2);
}
// The derivation of coordinates is explained in https://github.com/excalidraw/excalidraw/pull/6265
if (container.type === "diamond") {
offsetX += container.width / 4;
offsetY += container.height / 4;
}
return {
x: container.x + offsetX,
y: container.y + offsetY,
};
};
/**
* Whether a container is as large as its label needs it to be, at least. Not
* when the label is outside of it: an arrow's, or a wedge's.
*/
export const containerGrowsWithBoundText = (container: ExcalidrawElement) =>
!isArrowElement(container) && !isEllipseArcElement(container);
/**
* The angle a container's label is drawn at: the container's, unless the
* label is outside of it. An arrow's label and a wedge's stay upright: turning
* a wedge's ellipse is no different from moving its arc's start.
*/
export const getBoundTextAngle = (container: ExcalidrawElement): Radians =>
isArrowElement(container) || isEllipseArcElement(container)
? (0 as Radians)
: container.angle;
/**
* Brings a container's label in line with the container's angle, after that
* changed.
*/
export const updateBoundTextAngle = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
scene: Scene,
) => {
scene.mutateElement(boundTextElement, {
angle: getBoundTextAngle(container),
// (a label in its container turns about the center they share)
...(isEllipseArcElement(container) &&
computeEllipseArcLabelPosition(container, boundTextElement)),
});
};
export const getTextElementAngle = (
textElement: ExcalidrawTextElement,
container: ExcalidrawTextContainer | null,
) => {
if (!container) {
return textElement.angle;
}
return getBoundTextAngle(container);
};
export const getBoundTextElementPosition = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
elementsMap: ElementsMap,
) => {
if (isArrowElement(container)) {
return LinearElementEditor.getBoundTextElementPosition(
container,
boundTextElement,
elementsMap,
);
}
};
/**
* Returns the text element with its accurate coords. An arrow label's
* position is derived from the arrow at render time rather than kept in
* sync on the element itself (see getBoundTextElementPosition), so
* consumers reading a label's coords directly must go through this helper.
*/
export const getTextElementWithAccuratePosition = <
T extends ExcalidrawTextElement,
>(
textElement: T,
elementsMap: ElementsMap,
): T => {
if (!textElement.containerId) {
return textElement;
}
const container = elementsMap.get(textElement.containerId);
if (!container || !isArrowElement(container)) {
return textElement;
}
return {
...textElement,
...LinearElementEditor.getBoundTextElementPosition(
container,
textElement as ExcalidrawTextElementWithContainer,
elementsMap,
),
};
};
export const shouldAllowVerticalAlign = (
selectedElements: readonly ExcalidrawElement[],
elementsMap: ElementsMap,
) => {
return selectedElements.some((element) => {
if (isBoundToContainer(element)) {
const container = getContainerElement(element, elementsMap);
if (isArrowElement(container)) {
return false;
}
return true;
}
return false;
});
};
export const suppportsHorizontalAlign = (
selectedElements: readonly ExcalidrawElement[],
elementsMap: ElementsMap,
) => {
return selectedElements.some((element) => {
if (isBoundToContainer(element)) {
const container = getContainerElement(element, elementsMap);
if (isArrowElement(container)) {
return false;
}
return true;
}
return isTextElement(element);
});
};
const VALID_CONTAINER_TYPES = new Set([
"rectangle",
"stickynote",
"ellipse",
"diamond",
"arrow",
]);
export const isValidTextContainer = (element: {
type: ExcalidrawElementType;
}): element is ExcalidrawTextContainer =>
VALID_CONTAINER_TYPES.has(element.type);
export const computeContainerDimensionForBoundText = (
dimension: number,
containerType: ExtractSetType<typeof VALID_CONTAINER_TYPES>,
) => {
dimension = Math.ceil(dimension);
const padding = BOUND_TEXT_PADDING * 2;
if (containerType === "ellipse") {
return Math.round(((dimension + padding) / Math.sqrt(2)) * 2);
}
if (containerType === "arrow") {
return dimension + padding * 8;
}
if (containerType === "diamond") {
return 2 * (dimension + padding);
}
return dimension + padding;
};
export const getBoundTextMaxWidth = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElement | null,
) => {
const { width } = container;
if (isArrowElement(container)) {
const minWidth =
(boundTextElement?.fontSize ?? DEFAULT_FONT_SIZE) *
ARROW_LABEL_FONT_SIZE_TO_MIN_WIDTH_RATIO;
return Math.max(ARROW_LABEL_WIDTH_FRACTION * width, minWidth);
}
if (container.type === "ellipse") {
// The width of the largest rectangle inscribed inside an ellipse is
// Math.round((ellipse.width / 2) * Math.sqrt(2)) which is derived from
// equation of an ellipse -https://github.com/excalidraw/excalidraw/pull/6172
const inscribedWidth =
Math.round((width / 2) * Math.sqrt(2)) - BOUND_TEXT_PADDING * 2;
if (isEllipseArcElement(container)) {
// a wedge's label is outside the ellipse, so that a small pie doesn't
// have to break its labels' words (the same floor as an arrow's label)
return Math.max(
inscribedWidth,
(boundTextElement?.fontSize ?? DEFAULT_FONT_SIZE) *
ARROW_LABEL_FONT_SIZE_TO_MIN_WIDTH_RATIO,
);
}
return inscribedWidth;
}
if (container.type === "diamond") {
// The width of the largest rectangle inscribed inside a rhombus is
// Math.round(width / 2) - https://github.com/excalidraw/excalidraw/pull/6265
return Math.round(width / 2) - BOUND_TEXT_PADDING * 2;
}
return (
width -
(isStickyNoteElement(container)
? STICKY_NOTE_PADDING
: BOUND_TEXT_PADDING) *
2
);
};
export const getBoundTextMaxHeight = (
container: ExcalidrawElement,
boundTextElement: ExcalidrawTextElementWithContainer,
) => {
const { height } = container;
if (isStickyNoteElement(container)) {
// the label body ends above the creation-date footer
return Math.max(0, height - STICKY_NOTE_BODY_INSET_Y);
}
if (isArrowElement(container)) {
const containerHeight = height - BOUND_TEXT_PADDING * 8 * 2;
if (containerHeight <= 0) {
return boundTextElement.height;
}
return height;
}
if (isEllipseArcElement(container)) {
// a wedge's label is outside the ellipse (see
// `containerGrowsWithBoundText`)
return boundTextElement.height;
}
if (container.type === "ellipse") {
// The height of the largest rectangle inscribed inside an ellipse is
// Math.round((ellipse.height / 2) * Math.sqrt(2)) which is derived from
// equation of an ellipse - https://github.com/excalidraw/excalidraw/pull/6172
return Math.round((height / 2) * Math.sqrt(2)) - BOUND_TEXT_PADDING * 2;
}
if (container.type === "diamond") {
// The height of the largest rectangle inscribed inside a rhombus is
// Math.round(height / 2) - https://github.com/excalidraw/excalidraw/pull/6265
return Math.round(height / 2) - BOUND_TEXT_PADDING * 2;
}
return height - BOUND_TEXT_PADDING * 2;
};
/** retrieves text from text elements and concatenates to a single string */
export const getTextFromElements = (
elements: readonly ExcalidrawElement[],
separator = "\n\n",
) => {
const text = elements
.reduce((acc: string[], element) => {
if (isTextElement(element)) {
acc.push(element.text);
}
return acc;
}, [])
.join(separator);
return text;
};
export const DEFAULT_BOUND_TEXT_LABEL_POSITION = 0.5;