Skip to content

Commit 24a634b

Browse files
wip move item
1 parent a347acc commit 24a634b

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

editor-packages/editor-dashboard/core/action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type Action =
22
| NewSectionAction
33
| MakeDirAction
4+
| MoveAction
45
| FilterAction
56
| FoldUnfoldAction
67
| FoldUnfoldAllAction;
@@ -26,6 +27,12 @@ export type MakeDirAction = {
2627
name?: string;
2728
};
2829

30+
export type MoveAction = {
31+
type: "hierarchy/mv";
32+
source: string[];
33+
dest: string;
34+
};
35+
2936
export type FoldUnfoldAllAction = FoldAllAction | UnfoldAllAction;
3037

3138
export type FoldAllAction = {

editor-packages/editor-dashboard/core/provider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ export function useDashboard() {
147147
[dispatch]
148148
);
149149

150+
const mv = useCallback(
151+
(src: string[], dst: string) => {
152+
dispatch({
153+
type: "hierarchy/mv",
154+
source: src,
155+
dest: dst,
156+
});
157+
},
158+
[dispatch]
159+
);
160+
150161
return {
151162
...state,
152163
selection: editorState.selectedNodes,
@@ -159,5 +170,6 @@ export function useDashboard() {
159170
fold,
160171
unfold,
161172
mkdir,
173+
mv,
162174
};
163175
}

editor-packages/editor-dashboard/core/reducer.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import produce from "immer";
2+
import path from "path";
23
import type {
34
Action,
45
FilterAction,
56
FoldAction,
67
FoldAllAction,
78
MakeDirAction,
9+
MoveAction,
810
NewSectionAction,
911
UnfoldAction,
1012
UnfoldAllAction,
@@ -59,6 +61,43 @@ export function reducer(state: DashboardState, action: Action): DashboardState {
5961
});
6062
}
6163

64+
case "hierarchy/mv": {
65+
const { source, dest } = <MoveAction>action;
66+
67+
console.log("move", source, dest);
68+
return produce(state, (draft) => {
69+
// // 1. check if dest is a valid directory
70+
// const destFolder = findFolder(dest, {
71+
// directories: state.hierarchy.sections,
72+
// });
73+
// console.log("destDir", destFolder);
74+
// if (!destFolder) {
75+
// return;
76+
// }
77+
// // 2. move each source under dest
78+
// for (const src of source) {
79+
// console.log("moving", src, "to", dest);
80+
// const srcFolder = draft.hierarchy.sections.find(
81+
// (s) => s.path === src
82+
// );
83+
// const srcParent = draft.hierarchy.sections.find((s) =>
84+
// s.contents.find((c) => c.path === src)
85+
// );
86+
// if (!srcParent) {
87+
// continue;
88+
// }
89+
// // 2.1 remove src from srcParent
90+
// srcParent.contents = srcParent.contents.filter(
91+
// (c) => c.path !== srcFolder.path
92+
// );
93+
// // 2.2 add src to dest
94+
// destFolder.contents.push(srcFolder);
95+
// console.log("srcParent", srcParent);
96+
// console.log("destDir", destFolder);
97+
// }
98+
});
99+
}
100+
62101
case "hierarchy/fold": {
63102
const { path } = <FoldAction>action;
64103
return produce(state, (draft) => {
@@ -121,3 +160,41 @@ function newDirName({
121160
i++;
122161
}
123162
}
163+
164+
/**
165+
* find item under hierarchy
166+
*
167+
* @param directories the root directories to begin search with. it will loop recursively through all directories
168+
*/
169+
function findFolder(
170+
path: string,
171+
{ directories }: { directories: Array<DashboardFolderItem> }
172+
) {
173+
for (const dir of directories) {
174+
if (dir.path === path) {
175+
return dir;
176+
}
177+
178+
if (dir.$type === "folder") {
179+
const found = findFolder(path, {
180+
directories: dir.contents.filter(
181+
(d) => d.$type === "folder"
182+
) as Array<DashboardFolderItem>,
183+
});
184+
if (found) {
185+
return found;
186+
}
187+
}
188+
}
189+
}
190+
191+
// Visual File system
192+
// mv
193+
// mkdir
194+
// rm
195+
// rename
196+
// fold
197+
// unfold
198+
// order
199+
// tag
200+
// search

editor-packages/editor-dashboard/scaffold/editor-dashboard.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ function FolderCard(
288288
props: DashboardFolderItem &
289289
Omit<DashboardItemCardProps, "label" | "preview" | "icon">
290290
) {
291+
const { mv } = useDashboard();
291292
const [{ isActive }, drop] = useDrop(() => ({
292293
accept: ["scene", "folder"],
293294
collect: (monitor) => ({
@@ -297,8 +298,8 @@ function FolderCard(
297298
return item.id !== props.id;
298299
},
299300
drop(item, monitor) {
300-
console.log("drop", item, monitor);
301-
// todo:
301+
// @ts-ignore
302+
mv([item.path], props.path);
302303
},
303304
}));
304305

0 commit comments

Comments
 (0)