|
@@ -10,6 +10,8 @@ import {
|
|
} from "./types";
|
|
} from "./types";
|
|
import { mutateElement } from "./mutateElement";
|
|
import { mutateElement } from "./mutateElement";
|
|
import {
|
|
import {
|
|
|
|
+ ARROW_LABEL_FONT_SIZE_TO_MIN_WIDTH_RATIO,
|
|
|
|
+ ARROW_LABEL_WIDTH_FRACTION,
|
|
BOUND_TEXT_PADDING,
|
|
BOUND_TEXT_PADDING,
|
|
DEFAULT_FONT_FAMILY,
|
|
DEFAULT_FONT_FAMILY,
|
|
DEFAULT_FONT_SIZE,
|
|
DEFAULT_FONT_SIZE,
|
|
@@ -65,7 +67,7 @@ export const redrawTextBoundingBox = (
|
|
boundTextUpdates.text = textElement.text;
|
|
boundTextUpdates.text = textElement.text;
|
|
|
|
|
|
if (container) {
|
|
if (container) {
|
|
- maxWidth = getBoundTextMaxWidth(container);
|
|
|
|
|
|
+ maxWidth = getBoundTextMaxWidth(container, textElement);
|
|
boundTextUpdates.text = wrapText(
|
|
boundTextUpdates.text = wrapText(
|
|
textElement.originalText,
|
|
textElement.originalText,
|
|
getFontString(textElement),
|
|
getFontString(textElement),
|
|
@@ -83,13 +85,12 @@ export const redrawTextBoundingBox = (
|
|
boundTextUpdates.baseline = metrics.baseline;
|
|
boundTextUpdates.baseline = metrics.baseline;
|
|
|
|
|
|
if (container) {
|
|
if (container) {
|
|
- const containerDims = getContainerDims(container);
|
|
|
|
const maxContainerHeight = getBoundTextMaxHeight(
|
|
const maxContainerHeight = getBoundTextMaxHeight(
|
|
container,
|
|
container,
|
|
textElement as ExcalidrawTextElementWithContainer,
|
|
textElement as ExcalidrawTextElementWithContainer,
|
|
);
|
|
);
|
|
|
|
|
|
- let nextHeight = containerDims.height;
|
|
|
|
|
|
+ let nextHeight = container.height;
|
|
if (metrics.height > maxContainerHeight) {
|
|
if (metrics.height > maxContainerHeight) {
|
|
nextHeight = computeContainerDimensionForBoundText(
|
|
nextHeight = computeContainerDimensionForBoundText(
|
|
metrics.height,
|
|
metrics.height,
|
|
@@ -155,6 +156,7 @@ export const bindTextToShapeAfterDuplication = (
|
|
export const handleBindTextResize = (
|
|
export const handleBindTextResize = (
|
|
container: NonDeletedExcalidrawElement,
|
|
container: NonDeletedExcalidrawElement,
|
|
transformHandleType: MaybeTransformHandleType,
|
|
transformHandleType: MaybeTransformHandleType,
|
|
|
|
+ shouldMaintainAspectRatio = false,
|
|
) => {
|
|
) => {
|
|
const boundTextElementId = getBoundTextElementId(container);
|
|
const boundTextElementId = getBoundTextElementId(container);
|
|
if (!boundTextElementId) {
|
|
if (!boundTextElementId) {
|
|
@@ -175,15 +177,17 @@ export const handleBindTextResize = (
|
|
let text = textElement.text;
|
|
let text = textElement.text;
|
|
let nextHeight = textElement.height;
|
|
let nextHeight = textElement.height;
|
|
let nextWidth = textElement.width;
|
|
let nextWidth = textElement.width;
|
|
- const containerDims = getContainerDims(container);
|
|
|
|
const maxWidth = getBoundTextMaxWidth(container);
|
|
const maxWidth = getBoundTextMaxWidth(container);
|
|
const maxHeight = getBoundTextMaxHeight(
|
|
const maxHeight = getBoundTextMaxHeight(
|
|
container,
|
|
container,
|
|
textElement as ExcalidrawTextElementWithContainer,
|
|
textElement as ExcalidrawTextElementWithContainer,
|
|
);
|
|
);
|
|
- let containerHeight = containerDims.height;
|
|
|
|
|
|
+ let containerHeight = container.height;
|
|
let nextBaseLine = textElement.baseline;
|
|
let nextBaseLine = textElement.baseline;
|
|
- if (transformHandleType !== "n" && transformHandleType !== "s") {
|
|
|
|
|
|
+ if (
|
|
|
|
+ shouldMaintainAspectRatio ||
|
|
|
|
+ (transformHandleType !== "n" && transformHandleType !== "s")
|
|
|
|
+ ) {
|
|
if (text) {
|
|
if (text) {
|
|
text = wrapText(
|
|
text = wrapText(
|
|
textElement.originalText,
|
|
textElement.originalText,
|
|
@@ -207,7 +211,7 @@ export const handleBindTextResize = (
|
|
container.type,
|
|
container.type,
|
|
);
|
|
);
|
|
|
|
|
|
- const diff = containerHeight - containerDims.height;
|
|
|
|
|
|
+ const diff = containerHeight - container.height;
|
|
// fix the y coord when resizing from ne/nw/n
|
|
// fix the y coord when resizing from ne/nw/n
|
|
const updatedY =
|
|
const updatedY =
|
|
!isArrowElement(container) &&
|
|
!isArrowElement(container) &&
|
|
@@ -687,16 +691,6 @@ export const getContainerElement = (
|
|
return null;
|
|
return null;
|
|
};
|
|
};
|
|
|
|
|
|
-export const getContainerDims = (element: ExcalidrawElement) => {
|
|
|
|
- const MIN_WIDTH = 300;
|
|
|
|
- if (isArrowElement(element)) {
|
|
|
|
- const width = Math.max(element.width, MIN_WIDTH);
|
|
|
|
- const height = element.height;
|
|
|
|
- return { width, height };
|
|
|
|
- }
|
|
|
|
- return { width: element.width, height: element.height };
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
export const getContainerCenter = (
|
|
export const getContainerCenter = (
|
|
container: ExcalidrawElement,
|
|
container: ExcalidrawElement,
|
|
appState: AppState,
|
|
appState: AppState,
|
|
@@ -887,12 +881,19 @@ export const computeContainerDimensionForBoundText = (
|
|
return dimension + padding;
|
|
return dimension + padding;
|
|
};
|
|
};
|
|
|
|
|
|
-export const getBoundTextMaxWidth = (container: ExcalidrawElement) => {
|
|
|
|
- const width = getContainerDims(container).width;
|
|
|
|
|
|
+export const getBoundTextMaxWidth = (
|
|
|
|
+ container: ExcalidrawElement,
|
|
|
|
+ boundTextElement: ExcalidrawTextElement | null = getBoundTextElement(
|
|
|
|
+ container,
|
|
|
|
+ ),
|
|
|
|
+) => {
|
|
|
|
+ const { width } = container;
|
|
if (isArrowElement(container)) {
|
|
if (isArrowElement(container)) {
|
|
- return width - BOUND_TEXT_PADDING * 8 * 2;
|
|
|
|
|
|
+ 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") {
|
|
if (container.type === "ellipse") {
|
|
// The width of the largest rectangle inscribed inside an ellipse is
|
|
// The width of the largest rectangle inscribed inside an ellipse is
|
|
// Math.round((ellipse.width / 2) * Math.sqrt(2)) which is derived from
|
|
// Math.round((ellipse.width / 2) * Math.sqrt(2)) which is derived from
|
|
@@ -911,7 +912,7 @@ export const getBoundTextMaxHeight = (
|
|
container: ExcalidrawElement,
|
|
container: ExcalidrawElement,
|
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
|
) => {
|
|
) => {
|
|
- const height = getContainerDims(container).height;
|
|
|
|
|
|
+ const { height } = container;
|
|
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) {
|