Virtual whiteboard for sketching hand-drawn like diagrams
chore(editor): streamline getSelectedElementsByGroup (#11636)
Christopher Tangonane9c856d262a1
diff --git a/packages/element/src/groups.ts b/packages/element/src/groups.tsindex 401b4d4569..c090a53d54 100644--- a/packages/element/src/groups.ts+++ b/packages/element/src/groups.ts@@ -415,76 +415,52 @@ export const getNewGroupIdsForDuplication = (// given a list of selected elements, return the element grouped by their immediate group selected state// in the case if only one group is selected and all elements selected are within the group, it will respect group hierarchy in accordance to their nested grouping orderexport const getSelectedElementsByGroup = (- selectedElements: ExcalidrawElement[],+ selectedElements: NonDeletedExcalidrawElement[],elementsMap: ElementsMap,appState: Readonly<Pick<AppState, "selectedGroupIds" | "editingGroupId">>,): NonDeletedExcalidrawElement[][] => {+ const buckets: Map<string, NonDeletedExcalidrawElement[]> = new Map();const selectedGroupIds = getSelectedGroupIds(appState);- const unboundElements = selectedElements.filter(- (element) => !isBoundToContainer(element),- );- const groups: Map<string, ExcalidrawElement[]> = new Map();- const elements: Map<string, ExcalidrawElement[]> = new Map();-- // helper function to add an element to the elements map- const addToElementsMap = (element: ExcalidrawElement) => {- // elements- const currentElementMembers = elements.get(element.id) || [];- const boundTextElement = getBoundTextElement(element, elementsMap);-- if (boundTextElement) {- currentElementMembers.push(boundTextElement);+ const isSingleSelectedGroupCase =+ selectedGroupIds.length === 1 &&+ selectedElements.every((element) => isSelectedViaGroup(appState, element));++ selectedElements.forEach((element) => {+ // skip dependent boundTextElements, they are appended after their container+ if (isBoundToContainer(element)) {+ return;}- elements.set(element.id, [...currentElementMembers, element]);- };- // helper function to add an element to the groups map- const addToGroupsMap = (element: ExcalidrawElement, groupId: string) => {- // groups- const currentGroupMembers = groups.get(groupId) || [];- const boundTextElement = getBoundTextElement(element, elementsMap);-- if (boundTextElement) {- currentGroupMembers.push(boundTextElement);- }- groups.set(groupId, [...currentGroupMembers, element]);- };-- // helper function to handle the case where a single group is selected- // and all elements selected are within the group, it will respect group hierarchy in accordance to- // their nested grouping order- const handleSingleSelectedGroupCase = (- element: ExcalidrawElement,- selectedGroupId: GroupId,- ) => {- const indexOfSelectedGroupId = element.groupIds.indexOf(selectedGroupId, 0);- const nestedGroupCount = element.groupIds.slice(- 0,- indexOfSelectedGroupId,- ).length;- return nestedGroupCount > 0- ? addToGroupsMap(element, element.groupIds[indexOfSelectedGroupId - 1])- : addToElementsMap(element);- };-- const isAllInSameGroup = selectedElements.every((element) =>- isSelectedViaGroup(appState, element),- );-- unboundElements.forEach((element) => {+ let bucketKey: string;const selectedGroupId = getSelectedGroupIdForElement(element,appState.selectedGroupIds,);+if (!selectedGroupId) {- addToElementsMap(element);- } else if (selectedGroupIds.length === 1 && isAllInSameGroup) {- handleSingleSelectedGroupCase(element, selectedGroupId);+ bucketKey = `${element.id}_element`;} else {- addToGroupsMap(element, selectedGroupId);+ // if only one group is selected, grouping is based on inner hierarchy+ const keyIndex = isSingleSelectedGroupCase+ ? element.groupIds.indexOf(selectedGroupId) - 1+ : element.groupIds.indexOf(selectedGroupId);++ // edge case: single selected group where element is non member of inner group+ bucketKey =+ keyIndex < 0+ ? `${element.id}_element`+ : `${element.groupIds[keyIndex]}_group`;}++ const currentBucketMembers = buckets.get(bucketKey) ?? [];+ const boundTextElement = getBoundTextElement(element, elementsMap);++ // preserve boundtext after container ordering+ buckets.set(bucketKey, [+ ...currentBucketMembers,+ element,+ ...(boundTextElement ? [boundTextElement] : []),+ ]);});- return Array.from(groups.values()).concat(- Array.from(elements.values()),- ) as NonDeletedExcalidrawElement[][];+ return [...buckets.values()];};diff --git a/packages/element/tests/align.test.tsx b/packages/element/tests/align.test.tsxindex b796793690..899b13da2b 100644--- a/packages/element/tests/align.test.tsx+++ b/packages/element/tests/align.test.tsx@@ -1009,4 +1009,89 @@ describe("aligning", () => {expect(API.getSelectedElements()[1].x).toEqual(150);expect(API.getSelectedElements()[2].x).toEqual(100);});++ const createAndSelectGroupWithBoundTextAndRectangle = () => {+ const groupId = "group-with-bound-text";++ // container is vertically/horizontally centered around its bound text+ const container = API.createElement({+ type: "rectangle",+ id: "container",+ x: 100,+ y: 100,+ width: 100,+ height: 100,+ groupIds: [groupId],+ boundElements: [{ type: "text", id: "bound-text" }],+ });++ const boundText = API.createElement({+ type: "text",+ id: "bound-text",+ x: 120,+ y: 140,+ width: 60,+ height: 20,+ containerId: container.id,+ groupIds: [groupId],+ });++ const groupedRectangle = API.createElement({+ type: "rectangle",+ id: "grouped-rectangle",+ x: 100,+ y: 200,+ width: 100,+ height: 100,+ groupIds: [groupId],+ });++ const standaloneRectangle = API.createElement({+ type: "rectangle",+ id: "standalone-rectangle",+ x: 0,+ y: 0,+ width: 100,+ height: 100,+ });++ API.setElements([+ container,+ boundText,+ groupedRectangle,+ standaloneRectangle,+ ]);++ API.setSelectedElements([container, groupedRectangle, standaloneRectangle]);++ return { container, boundText, groupedRectangle, standaloneRectangle };+ };++ it("keeps a bound text within its container when aligning a group to the top", () => {+ const { container, boundText, groupedRectangle, standaloneRectangle } =+ createAndSelectGroupWithBoundTextAndRectangle();++ API.executeAction(actionAlignTop);++ expect(API.getElement(standaloneRectangle).y).toEqual(0);+ expect(API.getElement(container).y).toEqual(0);+ expect(API.getElement(groupedRectangle).y).toEqual(100);++ expect(API.getElement(boundText).y).toEqual(40);+ expect(API.getElement(boundText).x).toEqual(120);+ });++ it("keeps a bound text within its container when aligning a group to the left", () => {+ const { container, boundText, groupedRectangle, standaloneRectangle } =+ createAndSelectGroupWithBoundTextAndRectangle();++ API.executeAction(actionAlignLeft);++ expect(API.getElement(standaloneRectangle).x).toEqual(0);+ expect(API.getElement(container).x).toEqual(0);+ expect(API.getElement(groupedRectangle).x).toEqual(0);++ expect(API.getElement(boundText).x).toEqual(20);+ expect(API.getElement(boundText).y).toEqual(140);+ });});