diff --git a/packages/element/src/groups.ts b/packages/element/src/groups.ts index f241abec60..550f9f51e2 100644 --- a/packages/element/src/groups.ts +++ b/packages/element/src/groups.ts @@ -110,6 +110,9 @@ export const selectGroupsFromGivenElements = ( } if (groupIds.length > 0) { const groupId = groupIds[groupIds.length - 1]; + if (nextAppState.selectedGroupIds[groupId] === true) { + continue; + } nextAppState = { ...nextAppState, ...selectGroup(groupId, nextAppState, elements), diff --git a/packages/element/tests/groups.test.ts b/packages/element/tests/groups.test.ts new file mode 100644 index 0000000000..acfbc7c10b --- /dev/null +++ b/packages/element/tests/groups.test.ts @@ -0,0 +1,89 @@ +import { arrayToMap } from "@excalidraw/common"; + +import { getDefaultAppState } from "@excalidraw/excalidraw/appState"; + +import { getElementsInResizingFrame } from "../src/frame"; +import { selectGroupsFromGivenElements } from "../src/groups"; +import { newElement, newFrameElement } from "../src/newElement"; + +const appState = () => ({ + ...getDefaultAppState(), + width: 1000, + height: 1000, + offsetLeft: 0, + offsetTop: 0, +}); + +const rectangle = (id: string, groupIds: string[]) => ({ + ...newElement({ + type: "rectangle", + x: 10, + y: 10, + width: 10, + height: 10, + groupIds, + }), + id, +}); + +describe("selectGroupsFromGivenElements", () => { + it("does not repeatedly scan already selected groups during frame dragging", () => { + let groupReads = 0; + const elements = Array.from({ length: 400 }, (_, index) => { + const element = rectangle(`element-${index}`, [`group-${index % 4}`]); + const groupIds = element.groupIds; + Object.defineProperty(element, "groupIds", { + get: () => { + groupReads++; + return groupIds; + }, + }); + return element; + }); + const frame = newFrameElement({ x: 0, y: 0, width: 100, height: 100 }); + const scene = [...elements, frame]; + const highlighted = getElementsInResizingFrame( + scene, + frame, + appState(), + arrayToMap(scene), + ); + + expect(highlighted.map((element) => element.id).sort()).toEqual( + elements.map((element) => element.id).sort(), + ); + // Bound the work rather than elapsed time, which varies with machine load. + expect(groupReads).toBeLessThan(elements.length * 40); + }); + + it("preserves nested group selection while editing a group", () => { + const elements = [ + rectangle("a", ["inner", "outer"]), + rectangle("b", ["inner", "outer"]), + rectangle("c", ["other", "outer"]), + rectangle("d", ["other", "outer"]), + ]; + const state = appState(); + + expect(selectGroupsFromGivenElements(elements, state)).toEqual({ + outer: true, + }); + expect( + selectGroupsFromGivenElements(elements, { + ...state, + editingGroupId: "outer", + }), + ).toEqual({ inner: true, other: true }); + expect(state.selectedElementIds).toEqual({}); + expect(state.selectedGroupIds).toEqual({}); + }); + + it("does not select singleton or ungrouped candidates", () => { + expect( + selectGroupsFromGivenElements( + [rectangle("a", ["singleton"]), rectangle("b", [])], + appState(), + ), + ).toEqual({}); + }); +});