2022-03-02 17:04:09 +01:00
|
|
|
import { GroupId, ExcalidrawElement, NonDeleted } from "./element/types";
|
2020-05-26 13:07:46 -07:00
|
|
|
import { AppState } from "./types";
|
|
|
|
import { getSelectedElements } from "./scene";
|
2022-03-02 17:04:09 +01:00
|
|
|
import { getBoundTextElement } from "./element/textElement";
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const selectGroup = (
|
2020-05-26 13:07:46 -07:00
|
|
|
groupId: GroupId,
|
|
|
|
appState: AppState,
|
|
|
|
elements: readonly NonDeleted<ExcalidrawElement>[],
|
2020-11-06 22:06:39 +02:00
|
|
|
): AppState => {
|
2020-05-30 22:48:57 +02:00
|
|
|
const elementsInGroup = elements.filter((element) =>
|
|
|
|
element.groupIds.includes(groupId),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (elementsInGroup.length < 2) {
|
|
|
|
if (
|
|
|
|
appState.selectedGroupIds[groupId] ||
|
|
|
|
appState.editingGroupId === groupId
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
...appState,
|
|
|
|
selectedGroupIds: { ...appState.selectedGroupIds, [groupId]: false },
|
|
|
|
editingGroupId: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return appState;
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:07:46 -07:00
|
|
|
return {
|
|
|
|
...appState,
|
|
|
|
selectedGroupIds: { ...appState.selectedGroupIds, [groupId]: true },
|
|
|
|
selectedElementIds: {
|
|
|
|
...appState.selectedElementIds,
|
|
|
|
...Object.fromEntries(
|
2020-05-30 22:48:57 +02:00
|
|
|
elementsInGroup.map((element) => [element.id, true]),
|
2020-05-26 13:07:46 -07:00
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-05-26 13:07:46 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the element's group is selected, don't render an individual
|
|
|
|
* selection border around it.
|
|
|
|
*/
|
2020-11-06 22:06:39 +02:00
|
|
|
export const isSelectedViaGroup = (
|
2020-05-26 13:07:46 -07:00
|
|
|
appState: AppState,
|
|
|
|
element: ExcalidrawElement,
|
2020-11-06 22:06:39 +02:00
|
|
|
) => getSelectedGroupForElement(appState, element) != null;
|
2020-09-11 17:06:07 +02:00
|
|
|
|
|
|
|
export const getSelectedGroupForElement = (
|
|
|
|
appState: AppState,
|
|
|
|
element: ExcalidrawElement,
|
2020-11-06 22:06:39 +02:00
|
|
|
) =>
|
|
|
|
element.groupIds
|
2020-05-26 13:07:46 -07:00
|
|
|
.filter((groupId) => groupId !== appState.editingGroupId)
|
|
|
|
.find((groupId) => appState.selectedGroupIds[groupId]);
|
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const getSelectedGroupIds = (appState: AppState): GroupId[] =>
|
|
|
|
Object.entries(appState.selectedGroupIds)
|
2020-05-26 13:07:46 -07:00
|
|
|
.filter(([groupId, isSelected]) => isSelected)
|
|
|
|
.map(([groupId, isSelected]) => groupId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When you select an element, you often want to actually select the whole group it's in, unless
|
|
|
|
* you're currently editing that group.
|
|
|
|
*/
|
2020-11-06 22:06:39 +02:00
|
|
|
export const selectGroupsForSelectedElements = (
|
2020-05-26 13:07:46 -07:00
|
|
|
appState: AppState,
|
|
|
|
elements: readonly NonDeleted<ExcalidrawElement>[],
|
2020-11-06 22:06:39 +02:00
|
|
|
): AppState => {
|
2021-05-20 22:28:34 +02:00
|
|
|
let nextAppState: AppState = { ...appState, selectedGroupIds: {} };
|
2020-05-26 13:07:46 -07:00
|
|
|
|
|
|
|
const selectedElements = getSelectedElements(elements, appState);
|
|
|
|
|
2021-05-20 22:28:34 +02:00
|
|
|
if (!selectedElements.length) {
|
|
|
|
return { ...nextAppState, editingGroupId: null };
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:07:46 -07:00
|
|
|
for (const selectedElement of selectedElements) {
|
|
|
|
let groupIds = selectedElement.groupIds;
|
|
|
|
if (appState.editingGroupId) {
|
|
|
|
// handle the case where a group is nested within a group
|
|
|
|
const indexOfEditingGroup = groupIds.indexOf(appState.editingGroupId);
|
|
|
|
if (indexOfEditingGroup > -1) {
|
|
|
|
groupIds = groupIds.slice(0, indexOfEditingGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (groupIds.length > 0) {
|
|
|
|
const groupId = groupIds[groupIds.length - 1];
|
|
|
|
nextAppState = selectGroup(groupId, nextAppState, elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextAppState;
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2023-06-15 00:42:01 +08:00
|
|
|
// given a list of elements, return the the actual group ids that should be selected
|
|
|
|
// or used to update the elements
|
|
|
|
export const selectGroupsFromGivenElements = (
|
|
|
|
elements: readonly NonDeleted<ExcalidrawElement>[],
|
|
|
|
appState: AppState,
|
|
|
|
) => {
|
|
|
|
let nextAppState: AppState = { ...appState, selectedGroupIds: {} };
|
|
|
|
|
|
|
|
for (const element of elements) {
|
|
|
|
let groupIds = element.groupIds;
|
|
|
|
if (appState.editingGroupId) {
|
|
|
|
const indexOfEditingGroup = groupIds.indexOf(appState.editingGroupId);
|
|
|
|
if (indexOfEditingGroup > -1) {
|
|
|
|
groupIds = groupIds.slice(0, indexOfEditingGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (groupIds.length > 0) {
|
|
|
|
const groupId = groupIds[groupIds.length - 1];
|
|
|
|
nextAppState = selectGroup(groupId, nextAppState, elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextAppState.selectedGroupIds;
|
|
|
|
};
|
|
|
|
|
2020-08-27 20:59:46 +02:00
|
|
|
export const editGroupForSelectedElement = (
|
|
|
|
appState: AppState,
|
|
|
|
element: NonDeleted<ExcalidrawElement>,
|
|
|
|
): AppState => {
|
|
|
|
return {
|
|
|
|
...appState,
|
|
|
|
editingGroupId: element.groupIds.length ? element.groupIds[0] : null,
|
|
|
|
selectedGroupIds: {},
|
|
|
|
selectedElementIds: {
|
|
|
|
[element.id]: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const isElementInGroup = (element: ExcalidrawElement, groupId: string) =>
|
|
|
|
element.groupIds.includes(groupId);
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const getElementsInGroup = (
|
2020-05-26 13:07:46 -07:00
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
groupId: string,
|
2020-11-06 22:06:39 +02:00
|
|
|
) => elements.filter((element) => isElementInGroup(element, groupId));
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const getSelectedGroupIdForElement = (
|
2020-05-26 13:07:46 -07:00
|
|
|
element: ExcalidrawElement,
|
|
|
|
selectedGroupIds: { [groupId: string]: boolean },
|
2020-11-06 22:06:39 +02:00
|
|
|
) => element.groupIds.find((groupId) => selectedGroupIds[groupId]);
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const getNewGroupIdsForDuplication = (
|
2020-05-30 22:48:57 +02:00
|
|
|
groupIds: ExcalidrawElement["groupIds"],
|
|
|
|
editingGroupId: AppState["editingGroupId"],
|
2020-05-26 13:07:46 -07:00
|
|
|
mapper: (groupId: GroupId) => GroupId,
|
2020-11-06 22:06:39 +02:00
|
|
|
) => {
|
2020-05-26 13:07:46 -07:00
|
|
|
const copy = [...groupIds];
|
|
|
|
const positionOfEditingGroupId = editingGroupId
|
|
|
|
? groupIds.indexOf(editingGroupId)
|
|
|
|
: -1;
|
|
|
|
const endIndex =
|
|
|
|
positionOfEditingGroupId > -1 ? positionOfEditingGroupId : groupIds.length;
|
2020-11-29 18:32:51 +02:00
|
|
|
for (let index = 0; index < endIndex; index++) {
|
|
|
|
copy[index] = mapper(copy[index]);
|
2020-05-26 13:07:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const addToGroup = (
|
2020-05-30 22:48:57 +02:00
|
|
|
prevGroupIds: ExcalidrawElement["groupIds"],
|
2020-05-26 13:07:46 -07:00
|
|
|
newGroupId: GroupId,
|
2020-05-30 22:48:57 +02:00
|
|
|
editingGroupId: AppState["editingGroupId"],
|
2020-11-06 22:06:39 +02:00
|
|
|
) => {
|
2020-05-26 13:07:46 -07:00
|
|
|
// insert before the editingGroupId, or push to the end.
|
|
|
|
const groupIds = [...prevGroupIds];
|
|
|
|
const positionOfEditingGroupId = editingGroupId
|
|
|
|
? groupIds.indexOf(editingGroupId)
|
|
|
|
: -1;
|
|
|
|
const positionToInsert =
|
|
|
|
positionOfEditingGroupId > -1 ? positionOfEditingGroupId : groupIds.length;
|
|
|
|
groupIds.splice(positionToInsert, 0, newGroupId);
|
|
|
|
return groupIds;
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const removeFromSelectedGroups = (
|
2020-05-30 22:48:57 +02:00
|
|
|
groupIds: ExcalidrawElement["groupIds"],
|
2020-05-26 13:07:46 -07:00
|
|
|
selectedGroupIds: { [groupId: string]: boolean },
|
2020-11-06 22:06:39 +02:00
|
|
|
) => groupIds.filter((groupId) => !selectedGroupIds[groupId]);
|
2021-12-23 17:02:13 +05:30
|
|
|
|
|
|
|
export const getMaximumGroups = (
|
|
|
|
elements: ExcalidrawElement[],
|
|
|
|
): ExcalidrawElement[][] => {
|
|
|
|
const groups: Map<String, ExcalidrawElement[]> = new Map<
|
|
|
|
String,
|
|
|
|
ExcalidrawElement[]
|
|
|
|
>();
|
|
|
|
|
|
|
|
elements.forEach((element: ExcalidrawElement) => {
|
|
|
|
const groupId =
|
|
|
|
element.groupIds.length === 0
|
|
|
|
? element.id
|
|
|
|
: element.groupIds[element.groupIds.length - 1];
|
|
|
|
|
|
|
|
const currentGroupMembers = groups.get(groupId) || [];
|
|
|
|
|
2022-03-02 17:04:09 +01:00
|
|
|
// Include bound text if present when grouping
|
|
|
|
const boundTextElement = getBoundTextElement(element);
|
|
|
|
if (boundTextElement) {
|
|
|
|
currentGroupMembers.push(boundTextElement);
|
2021-12-23 17:02:13 +05:30
|
|
|
}
|
|
|
|
groups.set(groupId, [...currentGroupMembers, element]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Array.from(groups.values());
|
|
|
|
};
|
2023-06-15 00:42:01 +08:00
|
|
|
|
|
|
|
export const elementsAreInSameGroup = (elements: 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;
|
|
|
|
};
|