Skip to content

Commit

Permalink
Merge pull request #520 from rnbwdev/copy_paste
Browse files Browse the repository at this point in the history
Copy, Cut , Paste, Duplicate
  • Loading branch information
atulbhatt-system32 authored Dec 20, 2023
2 parents 66742de + 6cefb40 commit 582f7fa
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 439 deletions.
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

0 comments on commit 582f7fa

Please sign in to comment.