feat: added undo and redo functionality
This commit is contained in:
committed by
JobvAlewijk
parent
608bd54617
commit
5e22ed8806
@@ -1,4 +1,4 @@
|
|||||||
import {useState} from "react";
|
import {useEffect, useState} from "react";
|
||||||
import styles from "./TextField.module.css";
|
import styles from "./TextField.module.css";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,6 +105,10 @@ export function TextField({
|
|||||||
}) {
|
}) {
|
||||||
const [inputValue, setInputValue] = useState(value);
|
const [inputValue, setInputValue] = useState(value);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setInputValue(value);
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
const onCommit = () => setValue(inputValue);
|
const onCommit = () => setValue(inputValue);
|
||||||
|
|
||||||
return <RealtimeTextField
|
return <RealtimeTextField
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
MarkerType,
|
MarkerType,
|
||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import '@xyflow/react/dist/style.css';
|
import '@xyflow/react/dist/style.css';
|
||||||
|
import {useEffect} from "react";
|
||||||
import {useShallow} from 'zustand/react/shallow';
|
import {useShallow} from 'zustand/react/shallow';
|
||||||
import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx';
|
import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx';
|
||||||
import useFlowStore from './visualProgrammingUI/VisProgStores.tsx';
|
import useFlowStore from './visualProgrammingUI/VisProgStores.tsx';
|
||||||
@@ -41,7 +42,11 @@ const selector = (state: FlowState) => ({
|
|||||||
onConnect: state.onConnect,
|
onConnect: state.onConnect,
|
||||||
onReconnectStart: state.onReconnectStart,
|
onReconnectStart: state.onReconnectStart,
|
||||||
onReconnectEnd: state.onReconnectEnd,
|
onReconnectEnd: state.onReconnectEnd,
|
||||||
onReconnect: state.onReconnect
|
onReconnect: state.onReconnect,
|
||||||
|
undo: state.undo,
|
||||||
|
redo: state.redo,
|
||||||
|
beginBatchAction: state.beginBatchAction,
|
||||||
|
endBatchAction: state.endBatchAction
|
||||||
});
|
});
|
||||||
|
|
||||||
// --| define ReactFlow editor |--
|
// --| define ReactFlow editor |--
|
||||||
@@ -60,9 +65,23 @@ const VisProgUI = () => {
|
|||||||
onConnect,
|
onConnect,
|
||||||
onReconnect,
|
onReconnect,
|
||||||
onReconnectStart,
|
onReconnectStart,
|
||||||
onReconnectEnd
|
onReconnectEnd,
|
||||||
|
undo,
|
||||||
|
redo,
|
||||||
|
beginBatchAction,
|
||||||
|
endBatchAction
|
||||||
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
||||||
|
|
||||||
|
// adds ctrl+z and ctrl+y support to respectively undo and redo actions
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = (e: KeyboardEvent) => {
|
||||||
|
if (e.ctrlKey && e.key === 'z') undo();
|
||||||
|
if (e.ctrlKey && e.key === 'y') redo();
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handler);
|
||||||
|
return () => window.removeEventListener('keydown', handler);
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`${styles.innerEditorContainer} round-lg border-lg`}>
|
<div className={`${styles.innerEditorContainer} round-lg border-lg`}>
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
@@ -76,6 +95,8 @@ const VisProgUI = () => {
|
|||||||
onReconnectStart={onReconnectStart}
|
onReconnectStart={onReconnectStart}
|
||||||
onReconnectEnd={onReconnectEnd}
|
onReconnectEnd={onReconnectEnd}
|
||||||
onConnect={onConnect}
|
onConnect={onConnect}
|
||||||
|
onNodeDragStart={beginBatchAction}
|
||||||
|
onNodeDragStop={endBatchAction}
|
||||||
snapToGrid
|
snapToGrid
|
||||||
fitView
|
fitView
|
||||||
proOptions={{hideAttribution: true}}
|
proOptions={{hideAttribution: true}}
|
||||||
@@ -83,6 +104,10 @@ const VisProgUI = () => {
|
|||||||
<Panel position="top-center" className={styles.dndPanel}>
|
<Panel position="top-center" className={styles.dndPanel}>
|
||||||
<DndToolbar/> {/* contains the drag and drop panel for nodes */}
|
<DndToolbar/> {/* contains the drag and drop panel for nodes */}
|
||||||
</Panel>
|
</Panel>
|
||||||
|
<Panel position="bottom-center">
|
||||||
|
<button onClick={() => undo()}>undo</button>
|
||||||
|
<button onClick={() => redo()}>Redo</button>
|
||||||
|
</Panel>
|
||||||
<Controls/>
|
<Controls/>
|
||||||
<Background/>
|
<Background/>
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
@@ -90,8 +115,6 @@ const VisProgUI = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Places the VisProgUI component inside a ReactFlowProvider
|
* Places the VisProgUI component inside a ReactFlowProvider
|
||||||
*
|
*
|
||||||
|
|||||||
129
src/pages/VisProgPage/visualProgrammingUI/EditorUndoRedo.ts
Normal file
129
src/pages/VisProgPage/visualProgrammingUI/EditorUndoRedo.ts
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
import type {Edge, Node} from "@xyflow/react";
|
||||||
|
import type {StateCreator, StoreApi } from 'zustand/vanilla';
|
||||||
|
import type {FlowState} from "./VisProgTypes.tsx";
|
||||||
|
|
||||||
|
export type FlowSnapshot = {
|
||||||
|
nodes: Node[];
|
||||||
|
edges: Edge[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reduced version of the flowState type,
|
||||||
|
* This removes the functions that are provided by UndoRedo from the expected input type
|
||||||
|
*/
|
||||||
|
type BaseFlowState = Omit<FlowState, 'undo' | 'redo' | 'pushSnapshot' | 'beginBatchAction' | 'endBatchAction'>;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UndoRedo is implemented as a middleware for the FlowState store,
|
||||||
|
* this allows us to keep the undo redo logic separate from the flowState,
|
||||||
|
* and thus from the internal editor logic
|
||||||
|
*
|
||||||
|
* Allows users to undo and redo actions in the visual programming editor
|
||||||
|
*
|
||||||
|
* @param {(set: StoreApi<FlowState>["setState"], get: () => FlowState, api: StoreApi<FlowState>) => BaseFlowState} config
|
||||||
|
* @returns {StateCreator<FlowState>}
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
export const UndoRedo = (
|
||||||
|
config: (
|
||||||
|
set: StoreApi<FlowState>['setState'],
|
||||||
|
get: () => FlowState,
|
||||||
|
api: StoreApi<FlowState>
|
||||||
|
) => BaseFlowState ) : StateCreator<FlowState> => (set, get, api) => {
|
||||||
|
let batchTimeout: number | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Captures the current state for
|
||||||
|
*
|
||||||
|
* @param {BaseFlowState} state - the current state of the editor
|
||||||
|
* @returns {FlowSnapshot} - returns a snapshot of the current editor state
|
||||||
|
*/
|
||||||
|
const getSnapshot = (state : BaseFlowState) : FlowSnapshot => ({
|
||||||
|
nodes: state.nodes,
|
||||||
|
edges: state.edges
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialState = config(set, get, api);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...initialState,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a snapshot of the current state to the undo history
|
||||||
|
*/
|
||||||
|
pushSnapshot: () => {
|
||||||
|
const state = get();
|
||||||
|
// we don't add new snapshots during an ongoing batch action
|
||||||
|
if (!state.isBatchAction) {
|
||||||
|
set({
|
||||||
|
past: [...state.past, getSnapshot(state)],
|
||||||
|
future: []
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undoes the last action from the editor,
|
||||||
|
* The state before undoing is added to the future for potential redoing
|
||||||
|
*/
|
||||||
|
undo: () => {
|
||||||
|
const state = get();
|
||||||
|
if (!state.past.length) return;
|
||||||
|
|
||||||
|
const snapshot = state.past.pop()!; // pop last snapshot
|
||||||
|
const currentSnapshot: FlowSnapshot = getSnapshot(state);
|
||||||
|
|
||||||
|
set({
|
||||||
|
nodes: snapshot.nodes,
|
||||||
|
edges: snapshot.edges,
|
||||||
|
});
|
||||||
|
|
||||||
|
state.future.push(currentSnapshot); // push current to redo
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redoes the last undone action,
|
||||||
|
* The state before redoing is added to the past for potential undoing
|
||||||
|
*/
|
||||||
|
redo: () => {
|
||||||
|
const state = get();
|
||||||
|
if (!state.future.length) return;
|
||||||
|
|
||||||
|
const snapshot = state.future.pop()!; // pop last redo
|
||||||
|
const currentSnapshot: FlowSnapshot = getSnapshot(state);
|
||||||
|
|
||||||
|
set({
|
||||||
|
nodes: snapshot.nodes,
|
||||||
|
edges: snapshot.edges,
|
||||||
|
});
|
||||||
|
|
||||||
|
state.past.push(currentSnapshot); // push current to undo
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begins a batched action
|
||||||
|
*
|
||||||
|
* An example of a batched action is dragging a node in the editor,
|
||||||
|
* where we want the entire action of moving a node to a different position
|
||||||
|
* to be covered by one undoable snapshot
|
||||||
|
*/
|
||||||
|
beginBatchAction: () => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
set({ isBatchAction: true });
|
||||||
|
if (batchTimeout) clearTimeout(batchTimeout);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends a batched action,
|
||||||
|
* a very short timeout is used to prevent new snapshots from being added
|
||||||
|
* until we are certain that the batch event is finished
|
||||||
|
*/
|
||||||
|
endBatchAction: () => {
|
||||||
|
batchTimeout = window.setTimeout(() => {
|
||||||
|
set({ isBatchAction: false });
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import type { FlowState } from './VisProgTypes';
|
import type { FlowState } from './VisProgTypes';
|
||||||
import { NodeDefaults, NodeConnects, NodeDeletes } from './NodeRegistry';
|
import { NodeDefaults, NodeConnects, NodeDeletes } from './NodeRegistry';
|
||||||
|
import { UndoRedo } from "./EditorUndoRedo.ts";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,7 +35,7 @@ function createNode(id: string, type: string, position: XYPosition, data: Record
|
|||||||
return {...defaultData, ...newData}
|
return {...defaultData, ...newData}
|
||||||
}
|
}
|
||||||
|
|
||||||
//* Initial nodes to populate the flow at startup.
|
//* Initial nodes, created by using createNode. */
|
||||||
const initialNodes : Node[] = [
|
const initialNodes : Node[] = [
|
||||||
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}, false),
|
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}, false),
|
||||||
createNode('end', 'end', {x: 500, y: 100}, {label: "End"}, false),
|
createNode('end', 'end', {x: 500, y: 100}, {label: "End"}, false),
|
||||||
@@ -42,7 +43,7 @@ const initialNodes : Node[] = [
|
|||||||
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
||||||
];
|
];
|
||||||
|
|
||||||
//* Initial edges to connect the startup nodes.
|
// * Initial edges * /
|
||||||
const initialEdges: Edge[] = [
|
const initialEdges: Edge[] = [
|
||||||
{ id: 'start-phase-1', source: 'start', target: 'phase-1' },
|
{ id: 'start-phase-1', source: 'start', target: 'phase-1' },
|
||||||
{ id: 'phase-1-end', source: 'phase-1', target: 'end' },
|
{ id: 'phase-1-end', source: 'phase-1', target: 'end' },
|
||||||
@@ -50,17 +51,17 @@ const initialEdges: Edge[] = [
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How we have defined the functions for our FlowState.
|
* useFlowStore contains the implementation for all editor functionality
|
||||||
* We have the normal functionality of a default FlowState with some exceptions to account for extra functionality.
|
* and stores the current state of the visual programming editor
|
||||||
* The biggest changes are in onConnect and onDelete, which we have given extra functionality based on the nodes defined functions.
|
|
||||||
*
|
*
|
||||||
* * Provides:
|
* * Provides:
|
||||||
* - Node and edge state management
|
* - Node and edge state management
|
||||||
* - Node creation, deletion, and updates
|
* - Node creation, deletion, and updates
|
||||||
* - Custom connection handling via NodeConnects
|
* - Custom connection handling via NodeConnects
|
||||||
* - Edge reconnection handling
|
* - Edge reconnection handling
|
||||||
|
* - Undo Redo functionality through custom middleware
|
||||||
*/
|
*/
|
||||||
const useFlowStore = create<FlowState>((set, get) => ({
|
const useFlowStore = create<FlowState>(UndoRedo((set, get) => ({
|
||||||
nodes: initialNodes,
|
nodes: initialNodes,
|
||||||
edges: initialEdges,
|
edges: initialEdges,
|
||||||
edgeReconnectSuccessful: true,
|
edgeReconnectSuccessful: true,
|
||||||
@@ -68,8 +69,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
/**
|
/**
|
||||||
* Handles changes to nodes triggered by ReactFlow.
|
* Handles changes to nodes triggered by ReactFlow.
|
||||||
*/
|
*/
|
||||||
onNodesChange: (changes) =>
|
onNodesChange: (changes) => set({nodes: applyNodeChanges(changes, get().nodes)}),
|
||||||
set({nodes: applyNodeChanges(changes, get().nodes)}),
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles changes to edges triggered by ReactFlow.
|
* Handles changes to edges triggered by ReactFlow.
|
||||||
@@ -81,28 +81,34 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Updates edges and calls the node-specific connection functions.
|
* Updates edges and calls the node-specific connection functions.
|
||||||
*/
|
*/
|
||||||
onConnect: (connection) => {
|
onConnect: (connection) => {
|
||||||
const edges = addEdge(connection, get().edges);
|
get().pushSnapshot();
|
||||||
const nodes = get().nodes;
|
|
||||||
// connection has: { source, sourceHandle, target, targetHandle }
|
|
||||||
// Let's find the source and target ID's.
|
|
||||||
const sourceNode = nodes.find((n) => n.id == connection.source);
|
|
||||||
const targetNode = nodes.find((n) => n.id == connection.target);
|
|
||||||
|
|
||||||
// In case the nodes weren't found, return basic functionality.
|
const edges = addEdge(connection, get().edges);
|
||||||
if (sourceNode == undefined || targetNode == undefined || sourceNode.type == undefined || targetNode.type == undefined) {
|
const nodes = get().nodes;
|
||||||
|
// connection has: { source, sourceHandle, target, targetHandle }
|
||||||
|
// Let's find the source and target ID's.
|
||||||
|
const sourceNode = nodes.find((n) => n.id == connection.source);
|
||||||
|
const targetNode = nodes.find((n) => n.id == connection.target);
|
||||||
|
|
||||||
|
// In case the nodes weren't found, return basic functionality.
|
||||||
|
if ( sourceNode == undefined
|
||||||
|
|| targetNode == undefined
|
||||||
|
|| sourceNode.type == undefined
|
||||||
|
|| targetNode.type == undefined
|
||||||
|
){
|
||||||
|
set({ nodes, edges });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We should find out how their data changes by calling their respective functions.
|
||||||
|
const sourceConnectFunction = NodeConnects[sourceNode.type as keyof typeof NodeConnects]
|
||||||
|
const targetConnectFunction = NodeConnects[targetNode.type as keyof typeof NodeConnects]
|
||||||
|
|
||||||
|
// We're going to have to update their data based on how they want to update it.
|
||||||
|
sourceConnectFunction(sourceNode, targetNode, true)
|
||||||
|
targetConnectFunction(targetNode, sourceNode, false)
|
||||||
set({ nodes, edges });
|
set({ nodes, edges });
|
||||||
return;
|
},
|
||||||
}
|
|
||||||
|
|
||||||
// We should find out how their data changes by calling their respective functions.
|
|
||||||
const sourceConnectFunction = NodeConnects[sourceNode.type as keyof typeof NodeConnects]
|
|
||||||
const targetConnectFunction = NodeConnects[targetNode.type as keyof typeof NodeConnects]
|
|
||||||
|
|
||||||
// We're going to have to update their data based on how they want to update it.
|
|
||||||
sourceConnectFunction(sourceNode, targetNode, true)
|
|
||||||
targetConnectFunction(targetNode, sourceNode, false)
|
|
||||||
set({ nodes, edges });
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles reconnecting an edge between nodes.
|
* Handles reconnecting an edge between nodes.
|
||||||
@@ -112,7 +118,18 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
set({ edges: reconnectEdge(oldEdge, newConnection, get().edges) });
|
set({ edges: reconnectEdge(oldEdge, newConnection, get().edges) });
|
||||||
},
|
},
|
||||||
|
|
||||||
onReconnectStart: () => set({ edgeReconnectSuccessful: false }),
|
onReconnectStart: () => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
set({ edgeReconnectSuccessful: false })
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles potential dropping (deleting) of an edge
|
||||||
|
* if it is not reconnected to a node after detaching it
|
||||||
|
*
|
||||||
|
* @param _evt - the event
|
||||||
|
* @param {{id: string}} edge - the described edge
|
||||||
|
*/
|
||||||
onReconnectEnd: (_evt, edge) => {
|
onReconnectEnd: (_evt, edge) => {
|
||||||
if (!get().edgeReconnectSuccessful) {
|
if (!get().edgeReconnectSuccessful) {
|
||||||
set({ edges: get().edges.filter((e) => e.id !== edge.id) });
|
set({ edges: get().edges.filter((e) => e.id !== edge.id) });
|
||||||
@@ -125,6 +142,8 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Also removes all edges connected to that node.
|
* Also removes all edges connected to that node.
|
||||||
*/
|
*/
|
||||||
deleteNode: (nodeId) => {
|
deleteNode: (nodeId) => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
|
||||||
// Let's find our node to check if they have a special deletion function
|
// Let's find our node to check if they have a special deletion function
|
||||||
const ourNode = get().nodes.find((n)=>n.id==nodeId);
|
const ourNode = get().nodes.find((n)=>n.id==nodeId);
|
||||||
const ourFunction = Object.entries(NodeDeletes).find(([t])=>t==ourNode?.type)?.[1]
|
const ourFunction = Object.entries(NodeDeletes).find(([t])=>t==ourNode?.type)?.[1]
|
||||||
@@ -135,7 +154,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
||||||
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
||||||
})}
|
})}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the entire nodes array in the store.
|
* Replaces the entire nodes array in the store.
|
||||||
@@ -151,6 +170,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Updates the data of a node by merging new data with existing data.
|
* Updates the data of a node by merging new data with existing data.
|
||||||
*/
|
*/
|
||||||
updateNodeData: (nodeId, data) => {
|
updateNodeData: (nodeId, data) => {
|
||||||
|
get().pushSnapshot();
|
||||||
set({
|
set({
|
||||||
nodes: get().nodes.map((node) => {
|
nodes: get().nodes.map((node) => {
|
||||||
if (node.id === nodeId) {
|
if (node.id === nodeId) {
|
||||||
@@ -165,8 +185,15 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Adds a new node to the flow store.
|
* Adds a new node to the flow store.
|
||||||
*/
|
*/
|
||||||
addNode: (node: Node) => {
|
addNode: (node: Node) => {
|
||||||
|
get().pushSnapshot();
|
||||||
set({ nodes: [...get().nodes, node] });
|
set({ nodes: [...get().nodes, node] });
|
||||||
},
|
},
|
||||||
}));
|
|
||||||
|
// undo redo default values
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
export default useFlowStore;
|
export default useFlowStore;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// VisProgTypes.ts
|
// VisProgTypes.ts
|
||||||
import type { Edge, OnNodesChange, OnEdgesChange, OnConnect, OnReconnect, Node } from '@xyflow/react';
|
import type { Edge, OnNodesChange, OnEdgesChange, OnConnect, OnReconnect, Node } from '@xyflow/react';
|
||||||
import type { NodeTypes } from './NodeRegistry';
|
import type { NodeTypes } from './NodeRegistry';
|
||||||
|
import type {FlowSnapshot} from "./EditorUndoRedo.ts";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type representing all registered node types.
|
* Type representing all registered node types.
|
||||||
@@ -74,4 +76,14 @@ export type FlowState = {
|
|||||||
* @param node - the Node object to add
|
* @param node - the Node object to add
|
||||||
*/
|
*/
|
||||||
addNode: (node: Node) => void;
|
addNode: (node: Node) => void;
|
||||||
|
|
||||||
|
// UndoRedo Types
|
||||||
|
past: FlowSnapshot[];
|
||||||
|
future: FlowSnapshot[];
|
||||||
|
pushSnapshot: () => void;
|
||||||
|
isBatchAction: boolean;
|
||||||
|
beginBatchAction: () => void;
|
||||||
|
endBatchAction: () => void;
|
||||||
|
undo: () => void;
|
||||||
|
redo: () => void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
|
|||||||
* @param nodeType - The type of node to create (from `NodeTypes`).
|
* @param nodeType - The type of node to create (from `NodeTypes`).
|
||||||
* @param position - The XY position in the flow canvas where the node will appear.
|
* @param position - The XY position in the flow canvas where the node will appear.
|
||||||
*/
|
*/
|
||||||
function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
function addNodeToFlow(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
||||||
const { nodes, setNodes } = useFlowStore.getState();
|
const { nodes, addNode } = useFlowStore.getState();
|
||||||
|
|
||||||
// Load any predefined data for this node type.
|
// Load any predefined data for this node type.
|
||||||
const defaultData = NodeDefaults[nodeType] ?? {}
|
const defaultData = NodeDefaults[nodeType] ?? {}
|
||||||
@@ -90,7 +90,7 @@ function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
|||||||
position,
|
position,
|
||||||
data: {...defaultData}
|
data: {...defaultData}
|
||||||
}
|
}
|
||||||
setNodes([...nodes, newNode]);
|
addNode(newNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,7 +125,7 @@ export function DndToolbar() {
|
|||||||
|
|
||||||
if (isInFlow) {
|
if (isInFlow) {
|
||||||
const position = screenToFlowPosition(screenPosition);
|
const position = screenToFlowPosition(screenPosition);
|
||||||
addNode(nodeType, position);
|
addNodeToFlow(nodeType, position);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[screenToFlowPosition],
|
[screenToFlowPosition],
|
||||||
|
|||||||
@@ -32,23 +32,22 @@ export type GoalNode = Node<GoalNodeData>
|
|||||||
* @param props NodeProps, like id, label, children
|
* @param props NodeProps, like id, label, children
|
||||||
* @returns React.JSX.Element
|
* @returns React.JSX.Element
|
||||||
*/
|
*/
|
||||||
export default function GoalNode(props: NodeProps<GoalNode>) {
|
export default function GoalNode({id, data}: NodeProps<GoalNode>) {
|
||||||
const data = props.data
|
|
||||||
const {updateNodeData} = useFlowStore();
|
const {updateNodeData} = useFlowStore();
|
||||||
|
|
||||||
const text_input_id = `goal_${props.id}_text_input`;
|
const text_input_id = `goal_${id}_text_input`;
|
||||||
const checkbox_id = `goal_${props.id}_checkbox`;
|
const checkbox_id = `goal_${id}_checkbox`;
|
||||||
|
|
||||||
const setDescription = (value: string) => {
|
const setDescription = (value: string) => {
|
||||||
updateNodeData(props.id, {...data, description: value});
|
updateNodeData(id, {...data, description: value});
|
||||||
}
|
}
|
||||||
|
|
||||||
const setAchieved = (value: boolean) => {
|
const setAchieved = (value: boolean) => {
|
||||||
updateNodeData(props.id, {...data, achieved: value});
|
updateNodeData(id, {...data, achieved: value});
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
<Toolbar nodeId={id} allowDelete={true}/>
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeGoal} flex-col gap-sm`}>
|
<div className={`${styles.defaultNode} ${styles.nodeGoal} flex-col gap-sm`}>
|
||||||
<div className={"flex-row gap-md"}>
|
<div className={"flex-row gap-md"}>
|
||||||
<label htmlFor={text_input_id}>Goal:</label>
|
<label htmlFor={text_input_id}>Goal:</label>
|
||||||
@@ -64,7 +63,7 @@ export default function GoalNode(props: NodeProps<GoalNode>) {
|
|||||||
<input
|
<input
|
||||||
id={checkbox_id}
|
id={checkbox_id}
|
||||||
type={"checkbox"}
|
type={"checkbox"}
|
||||||
value={data.achieved ? "checked" : ""}
|
checked={data.achieved || false}
|
||||||
onChange={(e) => setAchieved(e.target.checked)}
|
onChange={(e) => setAchieved(e.target.checked)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,239 @@
|
|||||||
|
import {act} from '@testing-library/react';
|
||||||
|
import useFlowStore from '../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx';
|
||||||
|
import { mockReactFlow } from '../../../setupFlowTests.ts';
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
mockReactFlow();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("UndoRedo Middleware", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("pushSnapshot adds a snapshot to past and clears future", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [{
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
edges: []
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
})
|
||||||
|
|
||||||
|
const state = store.getState();
|
||||||
|
expect(state.past.length).toBe(1);
|
||||||
|
expect(state.past[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
expect(state.future).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("pushSnapshot does nothing during batch action", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.setState({ isBatchAction: true });
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().past.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("undo restores last snapshot and pushes current snapshot to future", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
// initial state
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
|
||||||
|
// modified state
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
store.getState().undo();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}]);
|
||||||
|
expect(store.getState().future.length).toBe(1);
|
||||||
|
expect(store.getState().future[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("undo does nothing when past is empty", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({past: []});
|
||||||
|
|
||||||
|
act(() => { store.getState().undo(); });
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([]);
|
||||||
|
expect(store.getState().future).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("redo restores last future snapshot and pushes current to past", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
// initial
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
store.getState().undo();
|
||||||
|
|
||||||
|
// redo should restore node with id 'B'
|
||||||
|
store.getState().redo();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}]);
|
||||||
|
expect(store.getState().past.length).toBe(1); // snapshot A stored
|
||||||
|
expect(store.getState().past[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("redo does nothing when future is empty", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({past: []});
|
||||||
|
act(() => { store.getState().redo(); });
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("beginBatchAction pushes snapshot and sets isBatchAction=true", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => { store.getState().beginBatchAction(); });
|
||||||
|
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
expect(store.getState().past.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("endBatchAction sets isBatchAction=false after timeout", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({ isBatchAction: true });
|
||||||
|
act(() => { store.getState().endBatchAction(); });
|
||||||
|
|
||||||
|
// isBatchAction should remain true before the timer has advanced
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(10);
|
||||||
|
|
||||||
|
// it should now be set to false as the timer has advanced enough
|
||||||
|
expect(store.getState().isBatchAction).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("multiple beginBatchAction calls clear the timeout", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().beginBatchAction();
|
||||||
|
store.getState().endBatchAction(); // starts timeout
|
||||||
|
store.getState().beginBatchAction(); // should clear previous timeout
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(10);
|
||||||
|
|
||||||
|
// After advancing the timers, isBatchAction should still be true,
|
||||||
|
// as the timeout should have been cleared
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -69,6 +69,9 @@ beforeAll(() => {
|
|||||||
useFlowStore.setState({
|
useFlowStore.setState({
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
edgeReconnectSuccessful: true
|
edgeReconnectSuccessful: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -78,6 +81,9 @@ afterEach(() => {
|
|||||||
useFlowStore.setState({
|
useFlowStore.setState({
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
edgeReconnectSuccessful: true
|
edgeReconnectSuccessful: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user