Fix groups

This commit is contained in:
Marcel Mraz 2025-03-19 15:50:51 +01:00
parent ccbd004f22
commit 65d5595c79
No known key found for this signature in database
GPG key ID: 4EBD6E62DC830CD2
3 changed files with 45 additions and 6 deletions

View file

@ -2,15 +2,13 @@
* Create and link between shapes.
*/
import {
ELEMENT_LINK_KEY,
normalizeLink,
elementsAreInSameGroup,
} from "@excalidraw/common";
import { ELEMENT_LINK_KEY, normalizeLink } from "@excalidraw/common";
import type { AppProps, AppState } from "@excalidraw/excalidraw/types";
import type { ExcalidrawElement } from "@excalidraw/element/types";
import { elementsAreInSameGroup } from "./groups";
export const defaultGetElementLinkFromSelection: Exclude<
AppProps["generateLinkForSelection"],
undefined

View file

@ -360,3 +360,42 @@ export const getNonDeletedGroupIds = (elements: ElementsMap) => {
return nonDeletedGroupIds;
};
export const elementsAreInSameGroup = (
elements: readonly ExcalidrawElement[],
) => {
const allGroups = elements.flatMap((element) => element.groupIds);
const groupCount = new Map<string, number>();
let maxGroup = 0;
for (const group of allGroups) {
groupCount.set(group, (groupCount.get(group) ?? 0) + 1);
if (groupCount.get(group)! > maxGroup) {
maxGroup = groupCount.get(group)!;
}
}
return maxGroup === elements.length;
};
export const isInGroup = (element: NonDeletedExcalidrawElement) => {
return element.groupIds.length > 0;
};
export const getNewGroupIdsForDuplication = (
groupIds: ExcalidrawElement["groupIds"],
editingGroupId: AppState["editingGroupId"],
mapper: (groupId: GroupId) => GroupId,
) => {
const copy = [...groupIds];
const positionOfEditingGroupId = editingGroupId
? groupIds.indexOf(editingGroupId)
: -1;
const endIndex =
positionOfEditingGroupId > -1 ? positionOfEditingGroupId : groupIds.length;
for (let index = 0; index < endIndex; index++) {
copy[index] = mapper(copy[index]);
}
return copy;
};

View file

@ -3,13 +3,15 @@ import clsx from "clsx";
import throttle from "lodash.throttle";
import { useEffect, useMemo, useState, memo } from "react";
import { STATS_PANELS, elementsAreInSameGroup } from "@excalidraw/common";
import { STATS_PANELS } from "@excalidraw/common";
import { getCommonBounds } from "@excalidraw/element/bounds";
import { getUncroppedWidthAndHeight } from "@excalidraw/element/cropElement";
import { isElbowArrow, isImageElement } from "@excalidraw/element/typeChecks";
import { frameAndChildrenSelectedTogether } from "@excalidraw/element/frame";
import { elementsAreInSameGroup } from "@excalidraw/element/groups";
import type { NonDeletedExcalidrawElement } from "@excalidraw/element/types";
import { t } from "../../i18n";