Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: align and distribute binded text in container and cleanup (#4468)

Aakansha Doshibf2bca221ed9
diff --git a/src/align.ts b/src/align.ts
index 622e5c4826..b9971c242e 100644
--- a/src/align.ts
+++ b/src/align.ts
@@ -1,6 +1,7 @@
import { ExcalidrawElement } from "./element/types";
import { newElementWith } from "./element/mutateElement";
import { Box, getCommonBoundingBox } from "./element/bounds";
+import { getMaximumGroups } from "./groups";
export interface Alignment {
position: "start" | "center" | "end";
@@ -30,28 +31,6 @@ export const alignElements = (
});
};
-export const getMaximumGroups = (
- elements: ExcalidrawElement[],
-): ExcalidrawElement[][] => {
- const groups: Map<String, ExcalidrawElement[]> = new Map<
- String,
- ExcalidrawElement[]
- >();
-
- elements.forEach((element: ExcalidrawElement) => {
- const groupId =
- element.groupIds.length === 0
- ? element.id
- : element.groupIds[element.groupIds.length - 1];
-
- const currentGroupMembers = groups.get(groupId) || [];
-
- groups.set(groupId, [...currentGroupMembers, element]);
- });
-
- return Array.from(groups.values());
-};
-
const calculateTranslation = (
group: ExcalidrawElement[],
selectionBoundingBox: Box,
diff --git a/src/disitrubte.ts b/src/disitrubte.ts
index 8335f5c536..acad09b2de 100644
--- a/src/disitrubte.ts
+++ b/src/disitrubte.ts
@@ -1,17 +1,7 @@
import { ExcalidrawElement } from "./element/types";
import { newElementWith } from "./element/mutateElement";
-import { getCommonBounds } from "./element";
-
-interface Box {
- minX: number;
- minY: number;
- maxX: number;
- maxY: number;
- midX: number;
- midY: number;
- width: number;
- height: number;
-}
+import { getMaximumGroups } from "./groups";
+import { getCommonBoundingBox } from "./element/bounds";
export interface Distribution {
space: "between";
@@ -98,39 +88,3 @@ export const distributeElements = (
);
});
};
-
-export const getMaximumGroups = (
- elements: ExcalidrawElement[],
-): ExcalidrawElement[][] => {
- const groups: Map<String, ExcalidrawElement[]> = new Map<
- String,
- ExcalidrawElement[]
- >();
-
- elements.forEach((element: ExcalidrawElement) => {
- const groupId =
- element.groupIds.length === 0
- ? element.id
- : element.groupIds[element.groupIds.length - 1];
-
- const currentGroupMembers = groups.get(groupId) || [];
-
- groups.set(groupId, [...currentGroupMembers, element]);
- });
-
- return Array.from(groups.values());
-};
-
-const getCommonBoundingBox = (elements: ExcalidrawElement[]): Box => {
- const [minX, minY, maxX, maxY] = getCommonBounds(elements);
- return {
- minX,
- minY,
- maxX,
- maxY,
- width: maxX - minX,
- height: maxY - minY,
- midX: (minX + maxX) / 2,
- midY: (minY + maxY) / 2,
- };
-};
diff --git a/src/element/bounds.ts b/src/element/bounds.ts
index 59be00a9e3..84fa608bf0 100644
--- a/src/element/bounds.ts
+++ b/src/element/bounds.ts
@@ -520,11 +520,24 @@ export interface Box {
minY: number;
maxX: number;
maxY: number;
+ midX: number;
+ midY: number;
+ width: number;
+ height: number;
}
export const getCommonBoundingBox = (
elements: ExcalidrawElement[] | readonly NonDeleted<ExcalidrawElement>[],
): Box => {
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
- return { minX, minY, maxX, maxY };
+ return {
+ minX,
+ minY,
+ maxX,
+ maxY,
+ width: maxX - minX,
+ height: maxY - minY,
+ midX: (minX + maxX) / 2,
+ midY: (minY + maxY) / 2,
+ };
};
diff --git a/src/groups.ts b/src/groups.ts
index f374e816b0..4bbf2bc4c9 100644
--- a/src/groups.ts
+++ b/src/groups.ts
@@ -1,6 +1,13 @@
-import { GroupId, ExcalidrawElement, NonDeleted } from "./element/types";
+import {
+ GroupId,
+ ExcalidrawElement,
+ NonDeleted,
+ ExcalidrawTextElementWithContainer,
+} from "./element/types";
import { AppState } from "./types";
import { getSelectedElements } from "./scene";
+import { getBoundTextElementId } from "./element/textElement";
+import Scene from "./scene/Scene";
export const selectGroup = (
groupId: GroupId,
@@ -158,3 +165,33 @@ export const removeFromSelectedGroups = (
groupIds: ExcalidrawElement["groupIds"],
selectedGroupIds: { [groupId: string]: boolean },
) => groupIds.filter((groupId) => !selectedGroupIds[groupId]);
+
+export const getMaximumGroups = (
+ elements: ExcalidrawElement[],
+): ExcalidrawElement[][] => {
+ const groups: Map<String, ExcalidrawElement[]> = new Map<
+ String,
+ ExcalidrawElement[]
+ >();
+
+ elements.forEach((element: ExcalidrawElement) => {
+ const groupId =
+ element.groupIds.length === 0
+ ? element.id
+ : element.groupIds[element.groupIds.length - 1];
+
+ const currentGroupMembers = groups.get(groupId) || [];
+
+ // Include bounded text if present when grouping
+ const boundTextElementId = getBoundTextElementId(element);
+ if (boundTextElementId) {
+ const textElement = Scene.getScene(element)!.getElement(
+ boundTextElementId,
+ ) as ExcalidrawTextElementWithContainer;
+ currentGroupMembers.push(textElement);
+ }
+ groups.set(groupId, [...currentGroupMembers, element]);
+ });
+
+ return Array.from(groups.values());
+};