feat(ui): remove selectionPasted action

This commit is contained in:
psychedelicious 2024-05-19 14:52:11 +10:00
parent cbe32b647a
commit 7cceafe0dd
2 changed files with 40 additions and 48 deletions

View File

@ -5,11 +5,13 @@ import {
$copiedNodes, $copiedNodes,
$cursorPos, $cursorPos,
$edgesToCopiedNodes, $edgesToCopiedNodes,
selectionPasted, edgesChanged,
nodesChanged,
selectNodesSlice, selectNodesSlice,
} from 'features/nodes/store/nodesSlice'; } from 'features/nodes/store/nodesSlice';
import { findUnoccupiedPosition } from 'features/nodes/store/util/findUnoccupiedPosition'; import { findUnoccupiedPosition } from 'features/nodes/store/util/findUnoccupiedPosition';
import { isEqual, uniqWith } from 'lodash-es'; import { isEqual, uniqWith } from 'lodash-es';
import type { EdgeChange, NodeChange } from 'reactflow';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
const copySelection = () => { const copySelection = () => {
@ -26,7 +28,7 @@ const copySelection = () => {
const pasteSelection = (withEdgesToCopiedNodes?: boolean) => { const pasteSelection = (withEdgesToCopiedNodes?: boolean) => {
const { getState, dispatch } = getStore(); const { getState, dispatch } = getStore();
const currentNodes = selectNodesSlice(getState()).nodes; const { nodes, edges } = selectNodesSlice(getState());
const cursorPos = $cursorPos.get(); const cursorPos = $cursorPos.get();
const copiedNodes = deepClone($copiedNodes.get()); const copiedNodes = deepClone($copiedNodes.get());
@ -46,7 +48,7 @@ const pasteSelection = (withEdgesToCopiedNodes?: boolean) => {
const offsetY = cursorPos ? cursorPos.y - minY : 50; const offsetY = cursorPos ? cursorPos.y - minY : 50;
copiedNodes.forEach((node) => { copiedNodes.forEach((node) => {
const { x, y } = findUnoccupiedPosition(currentNodes, node.position.x + offsetX, node.position.y + offsetY); const { x, y } = findUnoccupiedPosition(nodes, node.position.x + offsetX, node.position.y + offsetY);
node.position.x = x; node.position.x = x;
node.position.y = y; node.position.y = y;
// Pasted nodes are selected // Pasted nodes are selected
@ -68,7 +70,40 @@ const pasteSelection = (withEdgesToCopiedNodes?: boolean) => {
node.data.id = id; node.data.id = id;
}); });
dispatch(selectionPasted({ nodes: copiedNodes, edges: copiedEdges })); const nodeChanges: NodeChange[] = [];
const edgeChanges: EdgeChange[] = [];
// Deselect existing nodes
nodes.forEach((n) => {
nodeChanges.push({
id: n.data.id,
type: 'select',
selected: false,
});
});
// Add new nodes
copiedNodes.forEach((n) => {
nodeChanges.push({
item: n,
type: 'add',
});
});
// Deselect existing edges
edges.forEach((e) => {
edgeChanges.push({
id: e.id,
type: 'select',
selected: false,
});
});
// Add new edges
copiedEdges.forEach((e) => {
edgeChanges.push({
item: e,
type: 'add',
});
});
dispatch(nodesChanged(nodeChanges));
dispatch(edgesChanged(edgeChanges));
}; };
const api = { copySelection, pasteSelection }; const api = { copySelection, pasteSelection };

View File

@ -347,47 +347,6 @@ export const nodesSlice = createSlice({
state.nodes = []; state.nodes = [];
state.edges = []; state.edges = [];
}, },
selectionPasted: (state, action: PayloadAction<{ nodes: AnyNode[]; edges: InvocationNodeEdge[] }>) => {
const { nodes, edges } = action.payload;
const nodeChanges: NodeChange[] = [];
// Deselect existing nodes
state.nodes.forEach((n) => {
nodeChanges.push({
id: n.data.id,
type: 'select',
selected: false,
});
});
// Add new nodes
nodes.forEach((n) => {
nodeChanges.push({
item: n,
type: 'add',
});
});
const edgeChanges: EdgeChange[] = [];
// Deselect existing edges
state.edges.forEach((e) => {
edgeChanges.push({
id: e.id,
type: 'select',
selected: false,
});
});
// Add new edges
edges.forEach((e) => {
edgeChanges.push({
item: e,
type: 'add',
});
});
state.nodes = applyNodeChanges(nodeChanges, state.nodes);
state.edges = applyEdgeChanges(edgeChanges, state.edges);
},
selectionDeleted: (state) => { selectionDeleted: (state) => {
const selectedNodes = state.nodes.filter((n) => n.selected); const selectedNodes = state.nodes.filter((n) => n.selected);
const selectedEdges = state.edges.filter((e) => e.selected); const selectedEdges = state.edges.filter((e) => e.selected);
@ -455,7 +414,6 @@ export const {
nodesChanged, nodesChanged,
nodeUseCacheChanged, nodeUseCacheChanged,
notesNodeValueChanged, notesNodeValueChanged,
selectionPasted,
selectionDeleted, selectionDeleted,
undo, undo,
redo, redo,
@ -498,7 +456,7 @@ export const nodesPersistConfig: PersistConfig<NodesState> = {
persistDenylist: [], persistDenylist: [],
}; };
const selectionMatcher = isAnyOf(selectionPasted, nodeExclusivelySelected); const selectionMatcher = isAnyOf(nodeExclusivelySelected);
const isSelectionAction = (action: UnknownAction) => { const isSelectionAction = (action: UnknownAction) => {
if (selectionMatcher(action)) { if (selectionMatcher(action)) {
@ -573,6 +531,5 @@ export const isAnyNodeOrEdgeMutation = isAnyOf(
nodeNotesChanged, nodeNotesChanged,
nodeUseCacheChanged, nodeUseCacheChanged,
notesNodeValueChanged, notesNodeValueChanged,
selectionPasted,
selectionDeleted selectionDeleted
); );