tidy(ui): more succinct syntax for edge and node updates

This commit is contained in:
psychedelicious 2024-05-19 14:57:54 +10:00
parent b8b671c0db
commit a51142674a
4 changed files with 23 additions and 23 deletions

View File

@ -143,11 +143,11 @@ const AddNodePopover = () => {
// Deselect all other nodes and edges
const nodeChanges: NodeChange[] = [{ type: 'add', item: node }];
const edgeChanges: EdgeChange[] = [];
nodes.forEach((n) => {
nodeChanges.push({ id: n.id, type: 'select', selected: false });
nodes.forEach(({ id }) => {
nodeChanges.push({ type: 'select', id, selected: false });
});
edges.forEach((e) => {
edgeChanges.push({ id: e.id, type: 'select', selected: false });
edges.forEach(({ id }) => {
edgeChanges.push({ type: 'select', id, selected: false });
});
// Onwards!

View File

@ -207,11 +207,11 @@ export const Flow = memo(() => {
const { nodes, edges } = store.getState().nodes.present;
const nodeChanges: NodeChange[] = [];
const edgeChanges: EdgeChange[] = [];
nodes.forEach((n) => {
nodeChanges.push({ id: n.id, type: 'select', selected: true });
nodes.forEach(({ id }) => {
nodeChanges.push({ type: 'select', id, selected: true });
});
edges.forEach((e) => {
edgeChanges.push({ id: e.id, type: 'select', selected: true });
edges.forEach(({ id }) => {
edgeChanges.push({ type: 'select', id, selected: true });
});
dispatch(nodesChanged(nodeChanges));
dispatch(edgesChanged(edgeChanges));

View File

@ -73,33 +73,33 @@ const pasteSelection = (withEdgesToCopiedNodes?: boolean) => {
const nodeChanges: NodeChange[] = [];
const edgeChanges: EdgeChange[] = [];
// Deselect existing nodes
nodes.forEach((n) => {
nodes.forEach(({ id }) => {
nodeChanges.push({
id: n.data.id,
type: 'select',
id,
selected: false,
});
});
// Add new nodes
copiedNodes.forEach((n) => {
nodeChanges.push({
item: n,
type: 'add',
item: n,
});
});
// Deselect existing edges
edges.forEach((e) => {
edges.forEach(({ id }) => {
edgeChanges.push({
id: e.id,
type: 'select',
id,
selected: false,
});
});
// Add new edges
copiedEdges.forEach((e) => {
edgeChanges.push({
item: e,
type: 'add',
item: e,
});
});
dispatch(nodesChanged(nodeChanges));

View File

@ -103,13 +103,13 @@ export const nodesSlice = createSlice({
if (edge && edge.type === 'collapsed') {
const hiddenEdges = state.edges.filter((e) => e.source === edge.source && e.target === edge.target);
if (change.type === 'remove') {
hiddenEdges.forEach((e) => {
changes.push({ type: 'remove', id: e.id });
hiddenEdges.forEach(({ id }) => {
changes.push({ type: 'remove', id });
});
}
if (change.type === 'select') {
hiddenEdges.forEach((e) => {
changes.push({ type: 'select', id: e.id, selected: change.selected });
hiddenEdges.forEach(({ id }) => {
changes.push({ type: 'select', id, selected: change.selected });
});
}
}
@ -275,10 +275,10 @@ export const nodesSlice = createSlice({
nodeExclusivelySelected: (state, action: PayloadAction<string>) => {
const nodeId = action.payload;
state.nodes = applyNodeChanges(
state.nodes.map((n) => ({
id: n.id,
state.nodes.map(({ id }) => ({
type: 'select',
selected: n.id === nodeId ? true : false,
id,
selected: id === nodeId ? true : false,
})),
state.nodes
);
@ -355,13 +355,13 @@ export const nodesSlice = createSlice({
const { nodes, edges } = action.payload;
state.nodes = applyNodeChanges(
nodes.map((node) => ({
item: { ...node, ...SHARED_NODE_PROPERTIES },
type: 'add',
item: { ...node, ...SHARED_NODE_PROPERTIES },
})),
[]
);
state.edges = applyEdgeChanges(
edges.map((edge) => ({ item: edge, type: 'add' })),
edges.map((edge) => ({ type: 'add', item: edge })),
[]
);
});