remove more AppState assertions

This commit is contained in:
dwelle 2023-08-05 12:06:58 +02:00
parent 40cc901d56
commit 2f940fefc0
4 changed files with 82 additions and 68 deletions

View File

@ -259,7 +259,9 @@ const duplicateElements = (
return {
elements: finalElements,
appState: selectGroupsForSelectedElements(
appState: {
...appState,
...selectGroupsForSelectedElements(
{
...appState,
selectedGroupIds: {},
@ -276,6 +278,7 @@ const duplicateElements = (
getNonDeletedElements(finalElements),
appState,
null,
) as AppState,
),
},
};
};

View File

@ -6,7 +6,6 @@ import { ExcalidrawElement } from "../element/types";
import { isLinearElement } from "../element/typeChecks";
import { LinearElementEditor } from "../element/linearElementEditor";
import { excludeElementsInFramesFromSelection } from "../scene/selection";
import { AppState } from "../types";
export const actionSelectAll = register({
name: "selectAll",
@ -29,7 +28,9 @@ export const actionSelectAll = register({
}, {});
return {
appState: selectGroupsForSelectedElements(
appState: {
...appState,
...selectGroupsForSelectedElements(
{
...appState,
selectedLinearElement:
@ -44,7 +45,8 @@ export const actionSelectAll = register({
getNonDeletedElements(elements),
appState,
app,
) as AppState,
),
},
commitToHistory: true,
};
},

View File

@ -7591,7 +7591,9 @@ class App extends React.Component<AppProps, AppState> {
this.setState(
{
...(element && !this.state.selectedElementIds[element.id]
? selectGroupsForSelectedElements(
? {
...this.state,
...selectGroupsForSelectedElements(
{
...this.state,
selectedElementIds: { [element.id]: true },
@ -7602,10 +7604,11 @@ class App extends React.Component<AppProps, AppState> {
this.scene.getNonDeletedElements(),
this.state,
this,
)
),
}
: this.state),
showHyperlinkPopup: false,
} as AppState,
},
() => {
this.setState({
contextMenu: { top, left, items: this.getContextMenuItems(type) },

View File

@ -21,12 +21,15 @@ export const selectGroup = (
InteractiveCanvasAppState,
"selectedGroupIds" | "selectedElementIds" | "editingGroupId"
> => {
const elementsInGroup = elements.reduce((acc, element) => {
const elementsInGroup = elements.reduce(
(acc: Record<string, true>, element) => {
if (element.groupIds.includes(groupId)) {
acc[element.id] = true;
}
return acc;
}, {} as Record<string, boolean>);
},
{},
);
if (Object.keys(elementsInGroup).length < 2) {
if (
@ -48,7 +51,7 @@ export const selectGroup = (
selectedElementIds: {
...appState.selectedElementIds,
...elementsInGroup,
} as AppState["selectedElementIds"],
},
};
};
@ -92,7 +95,8 @@ export const selectGroups = (function () {
// Gather all the elements within selected groups
const groupElementsIndex: Record<GroupId, string[]> = {};
const selectedElementIdsInGroups = elements.reduce((acc, element) => {
const selectedElementIdsInGroups = elements.reduce(
(acc: Record<string, true>, element) => {
const groupId = element.groupIds.find((id) => selectedGroupIds[id]);
if (groupId) {
@ -106,7 +110,9 @@ export const selectGroups = (function () {
}
}
return acc;
}, {} as Record<string, boolean>);
},
{},
);
for (const groupId of Object.keys(groupElementsIndex)) {
// If there is one element in the group, and the group is selected or it's being edited, it's not a group
@ -126,7 +132,7 @@ export const selectGroups = (function () {
selectedElementIds: {
...appState.selectedElementIds,
...selectedElementIdsInGroups,
} as AppState["selectedElementIds"],
},
};
return lastAppState;