= ({
});
const newHierarchy = updateNodeName(structuredClone(hierarchy));
- pushHistory(newHierarchy); // record rename
+ if (onRename) onRename(id, newName);
+ addAction({
+ do: () => setHierarchy(newHierarchy),
+ undo: () => setHierarchy(prevHierarchy),
+ });
};
-
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.ctrlKey && e.key.toLowerCase() === "z") {
e.preventDefault();
- handleUndo();
+ undo();
} else if (e.ctrlKey && e.key.toLowerCase() === "y") {
e.preventDefault();
- handleRedo();
+ redo();
}
};
-
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
- }, [hierarchy, undoStack, redoStack, handleUndo, handleRedo]);
+ }, [undo, redo]);
return (
= ({
-
- {/* Children */}
{isExpanded && item.children?.length ? (
{item.children.map((child) => (
@@ -345,7 +344,7 @@ const TreeNode: React.FC
= ({
onRename={onRename}
draggedItems={draggedItems}
setDraggedItems={setDraggedItems}
- pushHistory={pushHistory}
+ // pushHistory={pushHistory}
setHierarchy={setHierarchy}
hierarchy={hierarchy}
/>
diff --git a/src/pages/SceneView.tsx b/src/pages/SceneView.tsx
index 077df40..c64fda6 100644
--- a/src/pages/SceneView.tsx
+++ b/src/pages/SceneView.tsx
@@ -1,18 +1,48 @@
+import { useState } from "react";
import OutlinePanel from "../components/ui/OutlinePanel";
-import { OutlineListData } from "../data/OutlineListData";
+import { OutlineListData, type AssetGroupChild } from "../data/OutlineListData";
const SceneView = () => {
+ const [outlineData, setOutlineData] = useState(OutlineListData);
+
+ const handleDragStart = () => {};
+
+ const handleDragOver = () => {};
+
+ const handleDrop = (updatedData: AssetGroupChild[]) => {
+ setOutlineData(updatedData);
+ };
+
+ const handleRename = (id: string, newName: string) => {
+ const renameItem = (items: AssetGroupChild[]): AssetGroupChild[] => {
+ return items.map((item) => {
+ if (item.modelUuid === id) {
+ return { ...item, modelName: newName };
+ } else if (item.children && item.children.length > 0) {
+ return { ...item, children: renameItem(item.children) };
+ }
+ return item;
+ });
+ };
+ const updatedData = renameItem(outlineData);
+ setOutlineData(updatedData);
+ };
+
return (
);
};
diff --git a/src/useUndoRedo.tsx b/src/useUndoRedo.tsx
new file mode 100644
index 0000000..f6be770
--- /dev/null
+++ b/src/useUndoRedo.tsx
@@ -0,0 +1,26 @@
+import { createContext, useContext } from "react";
+
+export type UndoRedoAction = {
+ undo: () => void;
+ do: () => void;
+};
+
+export type UndoRedoContextType = {
+ addAction: (action: UndoRedoAction) => void;
+ undo: () => void;
+ redo: () => void;
+ canUndo: boolean;
+ canRedo: boolean;
+ stackCount: { undo: number; redo: number };
+ setShortcutKeys?: (undoKey?: string, redoKey?: string) => void;
+};
+
+export const UndoRedoContext = createContext(null);
+
+export const useUndoRedo = () => {
+ const ctx = useContext(UndoRedoContext);
+ if (!ctx) {
+ throw new Error("useUndoRedo must be used inside an UndoRedoProvider");
+ }
+ return ctx;
+};