Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy, Cut , Paste, Duplicate #520

Merged
merged 9 commits into from
Dec 20, 2023
178 changes: 167 additions & 11 deletions src/_node/file/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import { LogAllow } from "@_constants/global";
import {
TFileApiPayload,
TFileHandlerCollection,
TFileNodeData,
TFileNodeTreeData,
TNodeTreeData,
TNodeUid,
moveIDBFF,
moveLocalFF,
} from "../";
import { TProjectContext } from "@_redux/main/fileTree";
import { FileSystemApis } from "./FileSystemApis";
import { TClipboardData, setClipboardData } from "@_redux/main/processor";
import { AnyAction } from "@reduxjs/toolkit";
import { Dispatch } from "react";
import { generateNewNameMoveNode } from "@_components/main/actionsPanel/workspaceTreeView/helpers";
import { verifyFileHandlerPermission } from "@_services/main";

const create = () => {};
const remove = async ({
Expand Down Expand Up @@ -40,10 +49,142 @@ const remove = async ({
}
});
};
const cut = () => {};
const copy = () => {};
const duplicate = () => {};
const move = () => {};

const move = async ({
projectContext,
fileHandlers,
uids,
clipboardData,
fileTree,
targetNode,
}: {
projectContext: TProjectContext;
fileHandlers: any;
uids: string[];
clipboardData: TClipboardData | null;
fileTree: TFileNodeTreeData;
targetNode: any;
}) => {
return new Promise<boolean>((resolve, reject) => {
uids.map(async (uid) => {
const node = fileTree[uid];
if (node === undefined) {
return false;
}

const nodeData = node.data as TFileNodeData;
const parentNode = fileTree[node.parentUid as TNodeUid];
if (parentNode === undefined) {
return false;
}

const handler = fileHandlers[uid];

const parentHandler = fileHandlers[
parentNode.uid
] as FileSystemDirectoryHandle;
let targetHandler = null;

if (targetNode.data.kind === "file") {
targetHandler = fileHandlers[
targetNode.parentUid
] as FileSystemDirectoryHandle;
} else {
targetHandler = fileHandlers[
targetNode.uid
] as FileSystemDirectoryHandle;
}
if (
!(await verifyFileHandlerPermission(handler)) ||
!(await verifyFileHandlerPermission(parentHandler)) ||
!(await verifyFileHandlerPermission(targetHandler))
) {
return false;
}
const newFileName = await generateNewNameMoveNode(
nodeData,
targetHandler,
);

// move
try {
if (projectContext === "local") {
await moveLocalFF(
handler,
parentHandler,
targetHandler,
newFileName,
clipboardData?.type === "copy",
);
} else if (projectContext === "idb") {
const targetNodeData = fileTree[targetNode.uid].data as TFileNodeData;
await moveIDBFF(
nodeData,
targetNodeData,
newFileName,
clipboardData?.type === "copy",
);
}
resolve(true);
} catch (err) {
reject(err);
}
});
});
};

const cut = ({
dispatch,
uids,
fileTree,
currentFileUid,
nodeTree,
}: {
dispatch: Dispatch<AnyAction>;
uids: TNodeUid[];
fileTree: TFileNodeTreeData;
currentFileUid: string;
nodeTree: TNodeTreeData;
}) => {
dispatch(
setClipboardData({
panel: "file",
type: "cut",
uids,
fileType: fileTree[currentFileUid].data.type,
data: [],
fileUid: currentFileUid,
prevNodeTree: nodeTree,
}),
);
};

const copy = ({
dispatch,
uids,
fileTree,
currentFileUid,
nodeTree,
}: {
dispatch: Dispatch<AnyAction>;
uids: TNodeUid[];
fileTree: TFileNodeTreeData;
currentFileUid: string;
nodeTree: TNodeTreeData;
}) => {
dispatch(
setClipboardData({
panel: "file",
type: "copy",
uids,
fileType: fileTree[currentFileUid].data.type,
data: [],
fileUid: currentFileUid,
prevNodeTree: nodeTree,
}),
);
};

const rename = () => {};

export const doFileActions = async (
Expand All @@ -58,7 +199,11 @@ export const doFileActions = async (
uids,
fileTree,
fileHandlers,
osType = "Windows",
dispatch,
currentFileUid,
nodeTree,
clipboardData,
targetNode,
} = params;

let allDone = true;
Expand All @@ -75,16 +220,27 @@ export const doFileActions = async (
});
break;
case "cut":
cut();
cut({
dispatch,
uids,
fileTree,
currentFileUid,
nodeTree,
});
break;
case "copy":
copy();
break;
case "duplicate":
duplicate();
copy({ dispatch, uids, fileTree, currentFileUid, nodeTree });
break;

case "move":
move();
allDone = await move({
projectContext,
fileHandlers,
uids,
clipboardData,
fileTree,
targetNode,
});
break;
case "rename":
rename();
Expand Down
Loading
Loading