fix(editor): prevent frame-drag lag when selecting large groups (#12692)

fix: avoid repeatedly selecting the same group during frame dragging
This commit is contained in:
zsviczian authored and GitHub committed 2026-11-19 20:01:22 +01:00
1 parent 091bc0e9d8
commit 2c3dcc06ec
2 files changed
+92

No files matched your search

+3
View File
@@ -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),
+89
View File
@@ -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({});
});
});