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,23 +259,26 @@ const duplicateElements = (
return { return {
elements: finalElements, elements: finalElements,
appState: selectGroupsForSelectedElements( appState: {
{ ...appState,
...appState, ...selectGroupsForSelectedElements(
selectedGroupIds: {}, {
selectedElementIds: nextElementsToSelect.reduce( ...appState,
(acc: Record<ExcalidrawElement["id"], true>, element) => { selectedGroupIds: {},
if (!isBoundToContainer(element)) { selectedElementIds: nextElementsToSelect.reduce(
acc[element.id] = true; (acc: Record<ExcalidrawElement["id"], true>, element) => {
} if (!isBoundToContainer(element)) {
return acc; acc[element.id] = true;
}, }
{}, return acc;
), },
}, {},
getNonDeletedElements(finalElements), ),
appState, },
null, getNonDeletedElements(finalElements),
) as AppState, appState,
null,
),
},
}; };
}; };

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,22 +28,25 @@ export const actionSelectAll = register({
}, {}); }, {});
return { return {
appState: selectGroupsForSelectedElements( appState: {
{ ...appState,
...appState, ...selectGroupsForSelectedElements(
selectedLinearElement: {
// single linear element selected ...appState,
Object.keys(selectedElementIds).length === 1 && selectedLinearElement:
isLinearElement(elements[0]) // single linear element selected
? new LinearElementEditor(elements[0], app.scene) Object.keys(selectedElementIds).length === 1 &&
: null, isLinearElement(elements[0])
editingGroupId: null, ? new LinearElementEditor(elements[0], app.scene)
selectedElementIds, : null,
}, editingGroupId: null,
getNonDeletedElements(elements), selectedElementIds,
appState, },
app, getNonDeletedElements(elements),
) as AppState, appState,
app,
),
},
commitToHistory: true, commitToHistory: true,
}; };
}, },

View File

@ -7591,21 +7591,24 @@ 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,
...this.state, ...selectGroupsForSelectedElements(
selectedElementIds: { [element.id]: true }, {
selectedLinearElement: isLinearElement(element) ...this.state,
? new LinearElementEditor(element, this.scene) selectedElementIds: { [element.id]: true },
: null, selectedLinearElement: isLinearElement(element)
}, ? new LinearElementEditor(element, this.scene)
this.scene.getNonDeletedElements(), : null,
this.state, },
this, this.scene.getNonDeletedElements(),
) this.state,
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(
if (element.groupIds.includes(groupId)) { (acc: Record<string, true>, element) => {
acc[element.id] = true; if (element.groupIds.includes(groupId)) {
} acc[element.id] = true;
return acc; }
}, {} as Record<string, boolean>); return acc;
},
{},
);
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,21 +95,24 @@ 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(
const groupId = element.groupIds.find((id) => selectedGroupIds[id]); (acc: Record<string, true>, element) => {
const groupId = element.groupIds.find((id) => selectedGroupIds[id]);
if (groupId) { if (groupId) {
acc[element.id] = true; acc[element.id] = true;
// Populate the index // Populate the index
if (!Array.isArray(groupElementsIndex[groupId])) { if (!Array.isArray(groupElementsIndex[groupId])) {
groupElementsIndex[groupId] = [element.id]; groupElementsIndex[groupId] = [element.id];
} else { } else {
groupElementsIndex[groupId].push(element.id); groupElementsIndex[groupId].push(element.id);
}
} }
} 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;