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 { return {
elements: finalElements, elements: finalElements,
appState: selectGroupsForSelectedElements( appState: {
...appState,
...selectGroupsForSelectedElements(
{ {
...appState, ...appState,
selectedGroupIds: {}, selectedGroupIds: {},
@ -276,6 +278,7 @@ const duplicateElements = (
getNonDeletedElements(finalElements), getNonDeletedElements(finalElements),
appState, appState,
null, null,
) as AppState, ),
},
}; };
}; };

View File

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

View File

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

View File

@ -21,12 +21,15 @@ export const selectGroup = (
InteractiveCanvasAppState, InteractiveCanvasAppState,
"selectedGroupIds" | "selectedElementIds" | "editingGroupId" "selectedGroupIds" | "selectedElementIds" | "editingGroupId"
> => { > => {
const elementsInGroup = elements.reduce((acc, element) => { const elementsInGroup = elements.reduce(
(acc: Record<string, true>, element) => {
if (element.groupIds.includes(groupId)) { if (element.groupIds.includes(groupId)) {
acc[element.id] = true; acc[element.id] = true;
} }
return acc; return acc;
}, {} as Record<string, boolean>); },
{},
);
if (Object.keys(elementsInGroup).length < 2) { if (Object.keys(elementsInGroup).length < 2) {
if ( if (
@ -48,7 +51,7 @@ export const selectGroup = (
selectedElementIds: { selectedElementIds: {
...appState.selectedElementIds, ...appState.selectedElementIds,
...elementsInGroup, ...elementsInGroup,
} as AppState["selectedElementIds"], },
}; };
}; };
@ -92,7 +95,8 @@ export const selectGroups = (function () {
// Gather all the elements within selected groups // Gather all the elements within selected groups
const groupElementsIndex: Record<GroupId, string[]> = {}; 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]); const groupId = element.groupIds.find((id) => selectedGroupIds[id]);
if (groupId) { if (groupId) {
@ -106,7 +110,9 @@ export const selectGroups = (function () {
} }
} }
return acc; return acc;
}, {} as Record<string, boolean>); },
{},
);
for (const groupId of Object.keys(groupElementsIndex)) { 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 // 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: { selectedElementIds: {
...appState.selectedElementIds, ...appState.selectedElementIds,
...selectedElementIdsInGroups, ...selectedElementIdsInGroups,
} as AppState["selectedElementIds"], },
}; };
return lastAppState; return lastAppState;