mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-05 18:55:28 +03:00
feat(editor): put a wedge's label outside its ellipse, by its centre line
This commit is contained in:
10 files changed
+357
-44
No files matched your search
@@ -37,6 +37,7 @@ import {
|
||||
handleBindTextResize,
|
||||
getBoundTextMaxWidth,
|
||||
computeBoundTextPosition,
|
||||
getBoundTextAngle,
|
||||
} from "./textElement";
|
||||
import {
|
||||
getBoundTextElement,
|
||||
@@ -276,7 +277,7 @@ const rotateSingleElement = (
|
||||
scene.getNonDeletedElementsMap(),
|
||||
);
|
||||
scene.mutateElement(textElement, {
|
||||
angle,
|
||||
angle: getBoundTextAngle(element),
|
||||
x,
|
||||
y,
|
||||
});
|
||||
@@ -512,7 +513,7 @@ const rotateMultipleElements = (
|
||||
scene.mutateElement(boundText, {
|
||||
x,
|
||||
y,
|
||||
angle: normalizeRadians((centerAngle + origAngle) as Radians),
|
||||
angle: getBoundTextAngle(element),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1586,8 +1587,6 @@ export const resizeMultipleElements = (
|
||||
element,
|
||||
update: { boundTextFontSize, ...update },
|
||||
} of elementsAndUpdates) {
|
||||
const { angle } = update;
|
||||
|
||||
scene.mutateElement(element, update);
|
||||
|
||||
if (isStickyNoteElement(element)) {
|
||||
@@ -1644,7 +1643,9 @@ export const resizeMultipleElements = (
|
||||
) {
|
||||
scene.mutateElement(boundTextElement, {
|
||||
fontSize: boundTextFontSize,
|
||||
angle: isLinearElement(element) ? undefined : angle,
|
||||
angle: isLinearElement(element)
|
||||
? undefined
|
||||
: getBoundTextAngle(element),
|
||||
});
|
||||
handleBindTextResize(
|
||||
element,
|
||||
|
||||
@@ -12,7 +12,16 @@ import {
|
||||
invariant,
|
||||
} from "@excalidraw/common";
|
||||
|
||||
import { pointFrom, pointRotateRads, type Radians } from "@excalidraw/math";
|
||||
import {
|
||||
distanceToLineSegment,
|
||||
lineSegment,
|
||||
pointFrom,
|
||||
pointRotateRads,
|
||||
polygonFromPoints,
|
||||
polygonIncludesPoint,
|
||||
type GlobalPoint,
|
||||
type Radians,
|
||||
} from "@excalidraw/math";
|
||||
|
||||
import type { ExtractSetType } from "@excalidraw/common/utility-types";
|
||||
|
||||
@@ -30,6 +39,7 @@ import { wrapText } from "./textWrapping";
|
||||
import {
|
||||
isBoundToContainer,
|
||||
isArrowElement,
|
||||
isEllipseArcElement,
|
||||
isStickyNoteElement,
|
||||
isTextElement,
|
||||
} from "./typeChecks";
|
||||
@@ -49,11 +59,20 @@ 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,
|
||||
@@ -88,11 +107,7 @@ export const redrawTextBoundingBox = (
|
||||
text: textElement.text,
|
||||
width: textElement.width,
|
||||
height: textElement.height,
|
||||
angle: (container
|
||||
? isArrowElement(container)
|
||||
? 0
|
||||
: container.angle
|
||||
: textElement.angle) as Radians,
|
||||
angle: container ? getBoundTextAngle(container) : textElement.angle,
|
||||
};
|
||||
|
||||
boundTextUpdates.text = textElement.text;
|
||||
@@ -129,7 +144,10 @@ export const redrawTextBoundingBox = (
|
||||
);
|
||||
const maxContainerWidth = getBoundTextMaxWidth(container, textElement);
|
||||
|
||||
if (!isArrowElement(container) && metrics.height > maxContainerHeight) {
|
||||
if (
|
||||
containerGrowsWithBoundText(container) &&
|
||||
metrics.height > maxContainerHeight
|
||||
) {
|
||||
const nextHeight = computeContainerDimensionForBoundText(
|
||||
metrics.height,
|
||||
container.type,
|
||||
@@ -138,7 +156,7 @@ export const redrawTextBoundingBox = (
|
||||
updateOriginalContainerCache(container.id, nextHeight);
|
||||
}
|
||||
|
||||
if (metrics.width > maxContainerWidth) {
|
||||
if (!isEllipseArcElement(container) && metrics.width > maxContainerWidth) {
|
||||
const nextWidth = computeContainerDimensionForBoundText(
|
||||
metrics.width,
|
||||
container.type,
|
||||
@@ -223,7 +241,7 @@ export const handleBindTextResize = (
|
||||
}
|
||||
// increase height in case text element height exceeds (but not an
|
||||
// arrow's, which its points determine, and its label doesn't grow)
|
||||
if (!isArrowElement(container) && nextHeight > maxHeight) {
|
||||
if (containerGrowsWithBoundText(container) && nextHeight > maxHeight) {
|
||||
containerHeight = computeContainerDimensionForBoundText(
|
||||
nextHeight,
|
||||
container.type,
|
||||
@@ -276,6 +294,9 @@ export const computeBoundTextPosition = (
|
||||
elementsMap,
|
||||
);
|
||||
}
|
||||
if (isEllipseArcElement(container)) {
|
||||
return computeEllipseArcLabelPosition(container, boundTextElement);
|
||||
}
|
||||
const containerCoords = getContainerCoords(container);
|
||||
const maxContainerHeight = getBoundTextMaxHeight(container, boundTextElement);
|
||||
const maxContainerWidth = getBoundTextMaxWidth(container, boundTextElement);
|
||||
@@ -341,6 +362,105 @@ export const computeBoundTextPosition = (
|
||||
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.
|
||||
@@ -387,17 +507,48 @@ export const getContainerCoords = (container: ExcalidrawElement) => {
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 (isArrowElement(container)) {
|
||||
return 0;
|
||||
}
|
||||
if (!container) {
|
||||
return textElement.angle;
|
||||
}
|
||||
return container.angle;
|
||||
return getBoundTextAngle(container);
|
||||
};
|
||||
|
||||
export const getBoundTextElementPosition = (
|
||||
@@ -523,7 +674,18 @@ export const getBoundTextMaxWidth = (
|
||||
// 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
|
||||
return Math.round((width / 2) * Math.sqrt(2)) - BOUND_TEXT_PADDING * 2;
|
||||
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
|
||||
@@ -555,6 +717,11 @@ export const getBoundTextMaxHeight = (
|
||||
}
|
||||
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
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { ORIG_ID } from "@excalidraw/common";
|
||||
|
||||
import { convertToExcalidrawElements } from "@excalidraw/element";
|
||||
|
||||
import type { Radians } from "@excalidraw/math";
|
||||
import type { ExcalidrawEllipseElement } from "@excalidraw/element/types";
|
||||
|
||||
@@ -45,6 +47,35 @@ describe("actionDuplicateSelection", () => {
|
||||
expect(h.state.selectedElementIds).toEqual({ [copy.id]: true });
|
||||
});
|
||||
|
||||
it("moves the copy's label on with its arc", () => {
|
||||
// the bottom right quarter, whose copy is the bottom left one
|
||||
const [wedge, label] = convertToExcalidrawElements([
|
||||
{
|
||||
type: "ellipse",
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 200,
|
||||
height: 200,
|
||||
arc: { start: 0 as Radians, sweep: (Math.PI / 2) as Radians },
|
||||
label: { text: "A label" },
|
||||
},
|
||||
]);
|
||||
API.setElements([wedge, label]);
|
||||
API.setSelectedElements([wedge]);
|
||||
|
||||
act(() => {
|
||||
h.app.actionManager.executeAction(actionDuplicateSelection);
|
||||
});
|
||||
|
||||
const copy = h.elements[3];
|
||||
expect(copy.type).toBe("text");
|
||||
// mirrored to the other side of the pie
|
||||
expect(copy.x + copy.width / 2).toBeCloseTo(
|
||||
200 - (label.x + label.width / 2),
|
||||
);
|
||||
expect(copy.y).toBeCloseTo(label.y);
|
||||
});
|
||||
|
||||
it("offsets the copy of a whole ellipse, whatever its start", () => {
|
||||
const copy = duplicate({
|
||||
start: 5 as Radians,
|
||||
|
||||
@@ -14,7 +14,9 @@ import { getSelectedElements } from "@excalidraw/element";
|
||||
import { syncMovedIndices } from "@excalidraw/element";
|
||||
|
||||
import {
|
||||
computeBoundTextPosition,
|
||||
duplicateElements,
|
||||
isBoundToContainer,
|
||||
isEllipseArcElement,
|
||||
normalizeEllipseArc,
|
||||
} from "@excalidraw/element";
|
||||
@@ -87,15 +89,28 @@ export const actionDuplicateSelection = register({
|
||||
origElement.frameId && origIdToDuplicateId.get(origElement.frameId);
|
||||
const frameId = duplicateFrameId ?? origElement.frameId;
|
||||
if (pieSlice) {
|
||||
return origElement.id === pieSlice.id
|
||||
? {
|
||||
frameId,
|
||||
arc: normalizeEllipseArc({
|
||||
start: pieSlice.arc.start + pieSlice.arc.sweep,
|
||||
sweep: pieSlice.arc.sweep,
|
||||
}),
|
||||
}
|
||||
: { frameId };
|
||||
const arc = normalizeEllipseArc({
|
||||
start: pieSlice.arc.start + pieSlice.arc.sweep,
|
||||
sweep: pieSlice.arc.sweep,
|
||||
});
|
||||
if (origElement.id === pieSlice.id) {
|
||||
return { frameId, arc };
|
||||
}
|
||||
// the slice's label moves on with its arc
|
||||
if (
|
||||
isBoundToContainer(origElement) &&
|
||||
origElement.containerId === pieSlice.id
|
||||
) {
|
||||
return {
|
||||
frameId,
|
||||
...computeBoundTextPosition(
|
||||
{ ...pieSlice, arc },
|
||||
origElement,
|
||||
app.scene.getNonDeletedElementsMap(),
|
||||
),
|
||||
};
|
||||
}
|
||||
return { frameId };
|
||||
}
|
||||
return {
|
||||
x: origElement.x + DEFAULT_GRID_SIZE / 2,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { degreesToRadians, radiansToDegrees } from "@excalidraw/math";
|
||||
|
||||
import { getBoundTextElement } from "@excalidraw/element";
|
||||
import { getBoundTextElement, updateBoundTextAngle } from "@excalidraw/element";
|
||||
import { isArrowElement, isElbowArrow } from "@excalidraw/element";
|
||||
|
||||
import { updateBindings } from "@excalidraw/element";
|
||||
@@ -55,7 +55,7 @@ const handleDegreeChange: DragInputCallbackType<AngleProps["property"]> = ({
|
||||
|
||||
const boundTextElement = getBoundTextElement(latestElement, elementsMap);
|
||||
if (boundTextElement && !isArrowElement(latestElement)) {
|
||||
scene.mutateElement(boundTextElement, { angle: nextAngle });
|
||||
updateBoundTextAngle(latestElement, boundTextElement, scene);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -83,7 +83,7 @@ const handleDegreeChange: DragInputCallbackType<AngleProps["property"]> = ({
|
||||
|
||||
const boundTextElement = getBoundTextElement(latestElement, elementsMap);
|
||||
if (boundTextElement && !isArrowElement(latestElement)) {
|
||||
scene.mutateElement(boundTextElement, { angle: nextAngle });
|
||||
updateBoundTextAngle(latestElement, boundTextElement, scene);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import {
|
||||
DEFAULT_ELLIPSE_ARC_START,
|
||||
getBoundTextElement,
|
||||
isArrowElement,
|
||||
normalizeEllipseArc,
|
||||
reanchorBindingsToOutline,
|
||||
redrawTextBoundingBox,
|
||||
updateBindings,
|
||||
} from "@excalidraw/element";
|
||||
|
||||
@@ -111,6 +113,20 @@ const handleArcChange: DragInputCallbackType<ArcProps["property"]> = ({
|
||||
},
|
||||
),
|
||||
});
|
||||
// a wedge's label follows the middle of its arc, and is back inside once
|
||||
// the ellipse is whole
|
||||
const boundText = getBoundTextElement(
|
||||
latestElement,
|
||||
scene.getNonDeletedElementsMap(),
|
||||
);
|
||||
if (boundText) {
|
||||
redrawTextBoundingBox(
|
||||
boundText,
|
||||
latestElement,
|
||||
scene,
|
||||
app.renderEnvironment,
|
||||
);
|
||||
}
|
||||
// Arrows bound to a part of the outline that's now gone move to what's
|
||||
// drawn. They do from where they were bound as the change began, for them
|
||||
// to be back there when a drag returns to where it started.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { degreesToRadians, radiansToDegrees } from "@excalidraw/math";
|
||||
|
||||
import { getBoundTextElement } from "@excalidraw/element";
|
||||
import { getBoundTextElement, updateBoundTextAngle } from "@excalidraw/element";
|
||||
import { isArrowElement } from "@excalidraw/element";
|
||||
|
||||
import { isInGroup } from "@excalidraw/element";
|
||||
@@ -59,7 +59,7 @@ const handleDegreeChange: DragInputCallbackType<
|
||||
|
||||
const boundTextElement = getBoundTextElement(element, elementsMap);
|
||||
if (boundTextElement && !isArrowElement(element)) {
|
||||
scene.mutateElement(boundTextElement, { angle: nextAngle });
|
||||
updateBoundTextAngle(element, boundTextElement, scene);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ const handleDegreeChange: DragInputCallbackType<
|
||||
|
||||
const boundTextElement = getBoundTextElement(latestElement, elementsMap);
|
||||
if (boundTextElement && !isArrowElement(latestElement)) {
|
||||
scene.mutateElement(boundTextElement, { angle: nextAngle });
|
||||
updateBoundTextAngle(latestElement, boundTextElement, scene);
|
||||
}
|
||||
}
|
||||
scene.triggerUpdate();
|
||||
|
||||
@@ -5,7 +5,7 @@ import { vi } from "vitest";
|
||||
|
||||
import { setDateTimeForTests, reseed } from "@excalidraw/common";
|
||||
|
||||
import { isInGroup } from "@excalidraw/element";
|
||||
import { convertToExcalidrawElements, isInGroup } from "@excalidraw/element";
|
||||
import { distanceToElement, LinearElementEditor } from "@excalidraw/element";
|
||||
|
||||
import { isTextElement } from "@excalidraw/element";
|
||||
@@ -431,6 +431,53 @@ describe("stats for a non-generic element", () => {
|
||||
expect(arc()).toBe(null);
|
||||
});
|
||||
|
||||
it("ellipse arc: a label follows the arc, and is inside a whole ellipse", () => {
|
||||
const [ellipse, label] = convertToExcalidrawElements([
|
||||
{
|
||||
type: "ellipse",
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 200,
|
||||
height: 200,
|
||||
label: { text: "A label" },
|
||||
},
|
||||
]);
|
||||
API.setElements([ellipse, label]);
|
||||
API.setSelectedElements([ellipse]);
|
||||
|
||||
const input = (name: string) =>
|
||||
UI.queryStatsProperty(name)!.querySelector(
|
||||
".drag-input",
|
||||
) as HTMLInputElement;
|
||||
const center = () => {
|
||||
const text = h.elements[1];
|
||||
return [text.x + text.width / 2, text.y + text.height / 2];
|
||||
};
|
||||
|
||||
// the right half: its label is to the right of the ellipse
|
||||
UI.updateInput(input("Sweep"), "50");
|
||||
expect(center()[0] - h.elements[1].width / 2).toBeGreaterThan(200);
|
||||
expect(center()[1]).toBeCloseTo(100);
|
||||
|
||||
// the bottom half: under it
|
||||
UI.updateInput(input("Start"), "25");
|
||||
expect(center()[0]).toBeCloseTo(100);
|
||||
expect(h.elements[1].y).toBeGreaterThan(200);
|
||||
|
||||
// turned by a quarter, the bottom half is the left one. The label goes
|
||||
// there with it, and stays upright
|
||||
UI.updateInput(input("A"), "90");
|
||||
expect(h.elements[1].x + h.elements[1].width).toBeLessThan(0);
|
||||
expect(center()[1]).toBeCloseTo(100);
|
||||
expect(h.elements[1].angle).toBe(0);
|
||||
|
||||
// it turns with a whole ellipse, which it's in
|
||||
UI.updateInput(input("Sweep"), "100");
|
||||
expect(center()[0]).toBeCloseTo(100, 0);
|
||||
expect(center()[1]).toBeCloseTo(100, 0);
|
||||
expect(h.elements[1].angle).toBeCloseTo(Math.PI / 2);
|
||||
});
|
||||
|
||||
it("ellipse arc: bound arrows move to what's left of the outline", () => {
|
||||
const ellipse = API.createElement({
|
||||
type: "ellipse",
|
||||
|
||||
@@ -47,7 +47,7 @@ describe("ellipse arcs in the editor", () => {
|
||||
expect(h.app.hitElement(50, 50, selected)).toBe(false);
|
||||
});
|
||||
|
||||
it("hits a selected wedge's label where it reaches out of the wedge", () => {
|
||||
it("puts a wedge's label outside its ellipse, at its centre line", () => {
|
||||
const [wedge, label] = convertToExcalidrawElements([
|
||||
{
|
||||
type: "ellipse",
|
||||
@@ -56,17 +56,52 @@ describe("ellipse arcs in the editor", () => {
|
||||
width: 200,
|
||||
height: 200,
|
||||
arc: { start: 0 as Radians, sweep: QUARTER },
|
||||
label: { text: "A label" },
|
||||
// (one that takes two lines)
|
||||
label: { text: "A label that is rather long" },
|
||||
},
|
||||
]);
|
||||
API.setElements([wedge, label]);
|
||||
API.setSelectedElements([wedge]);
|
||||
// in the label, left of the centre the wedge starts at
|
||||
const x = label.x + 5;
|
||||
const y = label.y + label.height / 2;
|
||||
|
||||
expect(x).toBeLessThan(100);
|
||||
expect(h.app.hitElement(x, y, wedge)).toBe(true);
|
||||
// the ellipse stays as it is, rather than growing to fit the label
|
||||
expect(label).toEqual(
|
||||
expect.objectContaining({ text: expect.stringContaining("\n") }),
|
||||
);
|
||||
expect(wedge).toEqual(expect.objectContaining({ width: 200, height: 200 }));
|
||||
// to the right of the wedge's centre line, which runs down and right
|
||||
// from the centre: the middle of its left side is on the line
|
||||
const centerX = label.x + label.width / 2;
|
||||
const centerY = label.y + label.height / 2;
|
||||
expect(label.x - 100).toBeGreaterThan(0);
|
||||
expect(label.x - 100).toBeCloseTo(centerY - 100);
|
||||
// as close to the circle as the gap lets its nearest corner be
|
||||
expect(Math.hypot(label.x - 100, label.y - 100)).toBeCloseTo(108);
|
||||
// and it's a part of the selected wedge still
|
||||
expect(h.app.hitElement(centerX, centerY, wedge)).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps the label of a turned wedge upright", () => {
|
||||
const labeled = (angle: number, start: number) =>
|
||||
convertToExcalidrawElements([
|
||||
{
|
||||
type: "ellipse",
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 200,
|
||||
height: 200,
|
||||
angle: angle as Radians,
|
||||
arc: { start: start as Radians, sweep: QUARTER },
|
||||
label: { text: "A label" },
|
||||
},
|
||||
])[1];
|
||||
|
||||
// turning a circle is no different from moving its arc's start
|
||||
const turned = labeled(1, 0);
|
||||
const moved = labeled(0, 1);
|
||||
|
||||
expect(turned.angle).toBe(0);
|
||||
expect(turned.x).toBeCloseTo(moved.x);
|
||||
expect(turned.y).toBeCloseTo(moved.y);
|
||||
});
|
||||
|
||||
it("keeps an arrow on a wedge that's flipped", () => {
|
||||
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
getBoundTextMaxHeight,
|
||||
getBoundTextMaxWidth,
|
||||
computeContainerDimensionForBoundText,
|
||||
containerGrowsWithBoundText,
|
||||
computeBoundTextPosition,
|
||||
getBoundTextElement,
|
||||
} from "@excalidraw/element";
|
||||
@@ -450,7 +451,7 @@ export const textWysiwyg = ({
|
||||
}
|
||||
|
||||
// autogrow container height if text exceeds
|
||||
if (!isArrowElement(container) && height > maxHeight) {
|
||||
if (containerGrowsWithBoundText(container) && height > maxHeight) {
|
||||
const targetContainerHeight = computeContainerDimensionForBoundText(
|
||||
height,
|
||||
container.type,
|
||||
@@ -466,7 +467,7 @@ export const textWysiwyg = ({
|
||||
} else if (
|
||||
// autoshrink container height until original container height
|
||||
// is reached when text is removed
|
||||
!isArrowElement(container) &&
|
||||
containerGrowsWithBoundText(container) &&
|
||||
container.height > originalContainerData.height &&
|
||||
height < maxHeight
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user