diff --git a/packages/element/src/elementLink.ts b/packages/element/src/elementLink.ts index 559aab962..fe242666c 100644 --- a/packages/element/src/elementLink.ts +++ b/packages/element/src/elementLink.ts @@ -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 diff --git a/packages/element/src/groups.ts b/packages/element/src/groups.ts index ce95f1e29..db0edef2c 100644 --- a/packages/element/src/groups.ts +++ b/packages/element/src/groups.ts @@ -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(); + 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; +}; diff --git a/packages/excalidraw/components/Stats/index.tsx b/packages/excalidraw/components/Stats/index.tsx index 6a38f4713..889f78971 100644 --- a/packages/excalidraw/components/Stats/index.tsx +++ b/packages/excalidraw/components/Stats/index.tsx @@ -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";