Fix group element removing (#1676)

This commit is contained in:
David Luzar 2020-05-30 22:48:57 +02:00 committed by GitHub
parent 17e9cc4506
commit f413bab3de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 478 additions and 43 deletions

View file

@ -7,15 +7,31 @@ export function selectGroup(
appState: AppState,
elements: readonly NonDeleted<ExcalidrawElement>[],
): AppState {
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;
}
return {
...appState,
selectedGroupIds: { ...appState.selectedGroupIds, [groupId]: true },
selectedElementIds: {
...appState.selectedElementIds,
...Object.fromEntries(
elements
.filter((element) => element.groupIds.includes(groupId))
.map((element) => [element.id, true]),
elementsInGroup.map((element) => [element.id, true]),
),
},
};
@ -89,8 +105,8 @@ export function getSelectedGroupIdForElement(
}
export function getNewGroupIdsForDuplication(
groupIds: GroupId[],
editingGroupId: GroupId | null,
groupIds: ExcalidrawElement["groupIds"],
editingGroupId: AppState["editingGroupId"],
mapper: (groupId: GroupId) => GroupId,
) {
const copy = [...groupIds];
@ -107,9 +123,9 @@ export function getNewGroupIdsForDuplication(
}
export function addToGroup(
prevGroupIds: GroupId[],
prevGroupIds: ExcalidrawElement["groupIds"],
newGroupId: GroupId,
editingGroupId: GroupId | null,
editingGroupId: AppState["editingGroupId"],
) {
// insert before the editingGroupId, or push to the end.
const groupIds = [...prevGroupIds];
@ -123,7 +139,7 @@ export function addToGroup(
}
export function removeFromSelectedGroups(
groupIds: GroupId[],
groupIds: ExcalidrawElement["groupIds"],
selectedGroupIds: { [groupId: string]: boolean },
) {
return groupIds.filter((groupId) => !selectedGroupIds[groupId]);