Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/rnbwdev/rnbw into node_api_s…
Browse files Browse the repository at this point in the history
…tructure
  • Loading branch information
atulbhatt-system32 committed Dec 25, 2023
2 parents 7bc2fb1 + d1d82b5 commit 90265a6
Show file tree
Hide file tree
Showing 16 changed files with 520 additions and 243 deletions.
38 changes: 20 additions & 18 deletions src/_node/file/FileSystemApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { FileSystemFileHandle } from "file-system-access";
import { verifyFileHandlerPermission } from "@_services/main";

import {
getFileNameAndExtensionFromFullname,
TFileHandlerCollection,
TFileNodeData,
TFileNodeTreeData,
TNodeUid,
getTargetHandler,
} from "../";
import {
_createIDBDirectory,
Expand Down Expand Up @@ -89,7 +89,7 @@ const removeSingleLocalDirectoryOrFile = async ({
const node = fileTree[uid];
if (!node) return false;

const parentNode = fileTree[node.parentUid as TNodeUid];
const parentNode = fileTree[node.parentUid!];
if (!parentNode) return false;

const parentHandler = fileHandlers[
Expand Down Expand Up @@ -119,7 +119,7 @@ const removeSingleIDBDirectoryOrFile = async ({
const node = fileTree[uid];
if (!node) return false;

const parentNode = fileTree[node.parentUid as TNodeUid];
const parentNode = fileTree[node.parentUid!];
if (!parentNode) return false;

const nodeData = node.data;
Expand Down Expand Up @@ -222,7 +222,7 @@ const moveLocalSingleDirectoryOrFile = async ({
const node = fileTree[uid];
if (!node) return false;

const parentNode = fileTree[node.parentUid as TNodeUid];
const parentNode = fileTree[node.parentUid!];
if (!parentNode) return false;

const targetNode = fileTree[targetUid];
Expand All @@ -232,7 +232,7 @@ const moveLocalSingleDirectoryOrFile = async ({
const parentHandler = fileHandlers[
parentNode.uid
] as FileSystemDirectoryHandle;
const targetHandler = fileHandlers[targetUid] as FileSystemDirectoryHandle;
const targetHandler = getTargetHandler({ targetUid, fileTree, fileHandlers });
if (
!(await verifyFileHandlerPermission(handler)) ||
!(await verifyFileHandlerPermission(parentHandler)) ||
Expand Down Expand Up @@ -364,16 +364,18 @@ const generateNewNameForLocalDirectoryOrFile = async ({
nodeData: TFileNodeData;
targetHandler: FileSystemDirectoryHandle;
}): Promise<string> => {
let newName = nodeData.name;
const { baseName, ext } = getFileNameAndExtensionFromFullname(nodeData.name);
const { name, ext, kind } = nodeData;
let newName = kind === "directory" ? name : `${name}.${ext}`;
let exists = true;
let index = -1;
while (exists) {
try {
if (nodeData.kind === "directory") {
await targetHandler.getDirectoryHandle(newName, { create: false });
} else {
await targetHandler.getFileHandle(newName, { create: false });
await targetHandler.getFileHandle(newName, {
create: false,
});
}
} catch (err) {
exists = false;
Expand All @@ -384,11 +386,11 @@ const generateNewNameForLocalDirectoryOrFile = async ({
newName =
nodeData.kind === "directory"
? index === 0
? `${baseName} copy`
: `${baseName} copy (${index})`
? `${name} copy`
: `${name} copy (${index})`
: index === 0
? `${baseName} copy.${ext}`
: `${baseName} copy (${index}).${ext}`;
? `${name} copy.${ext}`
: `${name} copy (${index}).${ext}`;
}
}
return newName;
Expand All @@ -400,8 +402,8 @@ const generateNewNameForIDBDirectoryOrFile = async ({
nodeData: TFileNodeData;
targetNodeData: TFileNodeData;
}): Promise<string> => {
let newName = nodeData.name;
const { baseName, ext } = getFileNameAndExtensionFromFullname(nodeData.name);
const { name, ext, kind } = nodeData;
let newName = kind === "directory" ? name : `${name}.${ext}`;
let exists = true;
let index = -1;
while (exists) {
Expand All @@ -418,11 +420,11 @@ const generateNewNameForIDBDirectoryOrFile = async ({
newName =
nodeData.kind === "directory"
? index === 0
? `${baseName} copy`
: `${baseName} copy (${index})`
? `${name} copy`
: `${name} copy (${index})`
: index === 0
? `${baseName} copy.${ext}`
: `${baseName} copy (${index}).${ext}`;
? `${name} copy.${ext}`
: `${name} copy (${index}).${ext}`;
}
}
return newName;
Expand Down
81 changes: 80 additions & 1 deletion src/_node/file/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { TProjectContext } from "@_redux/main/fileTree";

import { TFileHandlerCollection, TFileNodeTreeData, TNodeUid } from "../";
import { FileSystemApis } from "./FileSystemApis";
import { setClipboardData } from "@_redux/main/processor";
import { Dispatch } from "react";
import { AnyAction } from "@reduxjs/toolkit";

const create = async ({
projectContext,
Expand Down Expand Up @@ -110,9 +113,85 @@ const rename = async ({
fb && fb();
}
};

const cut = ({
uids,
dispatch,
}: {
uids: TNodeUid[];
dispatch: Dispatch<AnyAction>;
}) => {
dispatch(
setClipboardData({
panel: "file",
type: "cut",
uids,
}),
);
};
const copy = ({
uids,
dispatch,
}: {
uids: TNodeUid[];
dispatch: Dispatch<AnyAction>;
}) => {
dispatch(
setClipboardData({
panel: "file",
type: "copy",
uids,
}),
);
};
const move = async ({
projectContext,
fileTree,
fileHandlers,
uids,
targetUids,
newNames,
isCopy,
fb,
cb,
}: {
projectContext: TProjectContext;
fileTree: TFileNodeTreeData;
fileHandlers: TFileHandlerCollection;
uids: TNodeUid[];
targetUids: TNodeUid[];
newNames: string[];
isCopy: boolean;
fb?: (...params: any[]) => void;
cb?: (...params: any[]) => void;
}) => {
try {
let allDone = true;
await Promise.all(
uids.map(async (uid, index) => {
const done = await FileSystemApis[
projectContext
].moveSingleDirectoryOrFile({
fileTree,
fileHandlers,
uid,
targetUid: targetUids[index],
newName: newNames[index],
isCopy,
});
if (!done) allDone = false;
}),
);
cb && cb(allDone);
} catch (err) {
LogAllow && console.error(err);
fb && fb();
}
};
export const FileActions = {
create,
remove,
rename,
cut,
copy,
move,
};
62 changes: 47 additions & 15 deletions src/_node/file/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
_readIDBFile,
_removeIDBDirectoryOrFile,
_writeIDBFile,
TFileHandlerCollection,
TFileHandlerInfoObj,
TFileNodeData,
TFileNodeTreeData,
Expand Down Expand Up @@ -56,15 +57,29 @@ export const getInitialFileUidToOpen = (handlerObj: TFileHandlerInfoObj) => {

return initialFileUidToOpen;
};
export const isUnsavedProject = (fileTree: TFileNodeTreeData) => {
for (const uid in fileTree) {
const file = fileTree[uid];
const fileData = file.data as TFileNodeData;
if (fileData && fileData.changed) {
return true;
export const isUnsavedProject = (
fileTree: TFileNodeTreeData,
uids?: TNodeUid[],
) => {
if (uids) {
for (const uid of uids) {
const file = fileTree[uid];
const fileData = file.data as TFileNodeData;
if (fileData && fileData.changed) {
return true;
}
}
return false;
} else {
for (const uid in fileTree) {
const file = fileTree[uid];
const fileData = file.data as TFileNodeData;
if (fileData && fileData.changed) {
return true;
}
}
return false;
}
return false;
};
export const confirmAlert = (msg: string): boolean => {
if (!window.confirm(msg)) {
Expand All @@ -77,14 +92,6 @@ export const confirmFileChanges = (fileTree: TFileNodeTreeData): boolean => {
? confirmAlert(FileChangeAlertMessage)
: true;
};
export const getFileNameAndExtensionFromFullname = (
name: string,
): { baseName: string; ext: string } => {
const nameArr = name.split(".");
const ext = nameArr.length > 1 ? (nameArr.pop() as string) : "";
const baseName = nameArr.join(".");
return { baseName, ext };
};
export const getNormalizedPath = (
path: string,
): { isAbsolutePath: boolean; normalizedPath: string } => {
Expand Down Expand Up @@ -116,3 +123,28 @@ export const getFullnameFromUid = (uid: TNodeUid): string => {
const uidArr = uid.split(_path.sep);
return uidArr.pop() || "";
};
export const getParentUidFromUid = (uid: TNodeUid): TNodeUid => {
const uidArr = uid.split(_path.sep);
uidArr.pop();
return uidArr.join(_path.sep);
};
export const getTargetHandler = ({
fileHandlers,
targetUid,
fileTree,
}: {
targetUid: TNodeUid;
fileTree: TFileNodeTreeData;
fileHandlers: TFileHandlerCollection;
}) => {
const targetNode = fileTree[targetUid];
let targetHandler = null;
if (targetNode.data.kind === "file" && targetNode.parentUid) {
targetHandler = fileHandlers[
targetNode.parentUid
] as FileSystemDirectoryHandle;
} else {
targetHandler = fileHandlers[targetUid] as FileSystemDirectoryHandle;
}
return targetHandler;
};
14 changes: 7 additions & 7 deletions src/_node/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export const getValidNodeUids = (
// remove parent uids
let parentNode = tree[uid];
while (parentNode.uid !== RootNodeUid) {
parentNode = tree[parentNode.parentUid as TNodeUid];
parentNode = tree[parentNode.parentUid!];
delete validatedUids[parentNode.uid];
}

// remove nested uids
Object.keys(validatedUids).map((validatedUid) => {
let validatedNode = tree[validatedUid];
while (validatedNode.uid !== RootNodeUid) {
validatedNode = tree[validatedNode.parentUid as TNodeUid];
validatedNode = tree[validatedNode.parentUid!];
if (validatedNode.uid === uid) {
delete validatedUids[validatedUid];
break;
Expand All @@ -125,7 +125,7 @@ export const getValidNodeUids = (
let targetNode = tree[targetUid];
while (targetNode.uid !== RootNodeUid) {
delete validatedUids[targetNode.uid];
targetNode = tree[targetNode.parentUid as TNodeUid];
targetNode = tree[targetNode.parentUid!];
}

if (treeType === "html") {
Expand Down Expand Up @@ -183,7 +183,7 @@ export const getPrevSiblingNodeUid = (
): TNodeUid => {
let beforeUid = "" as TNodeUid;

const parentNode = tree[node.parentUid as TNodeUid];
const parentNode = tree[node.parentUid!];
for (const uid of parentNode.children) {
if (uid === node.uid) break;
beforeUid = uid;
Expand All @@ -197,7 +197,7 @@ export const getValidPrevNodeUid = (
): TNodeUid => {
let prevNodeUid = "" as TNodeUid;

const parentNode = tree[node.parentUid as TNodeUid];
const parentNode = tree[node.parentUid!];
for (const uid of parentNode.children) {
if (uid === node.uid) break;

Expand All @@ -215,7 +215,7 @@ export const getNodeDepth = (tree: TNodeTreeData, uid: TNodeUid): number => {
node = tree[uid];

while (node.uid !== RootNodeUid) {
node = tree[node.parentUid as TNodeUid];
node = tree[node.parentUid!];
++nodeDepth;
}

Expand All @@ -229,7 +229,7 @@ export const getNodeDepthExternal = (
node = tree[uid];

while (node.uid !== RootNodeUid) {
node = tree[node.parentUid as TNodeUid];
node = tree[node.parentUid!];
++nodeDepth;
}

Expand Down
10 changes: 10 additions & 0 deletions src/_redux/main/fileTree/event/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export type TFileAction = { action: TFileActionType } & (
newUid: string;
};
}
| {
action: Extract<TFileActionType, "move">;
payload: {
uids: {
orgUid: string;
newUid: string;
}[];
isCopy: boolean;
};
}
| {
action: null;
payload?: never;
Expand Down
7 changes: 2 additions & 5 deletions src/_redux/main/processor/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TNode, TNodeTreeData, TNodeUid } from "@_node/types";
import { TNodeUid } from "@_node/index";

export type TProcessorReducerState = {
doingAction: boolean;
Expand Down Expand Up @@ -31,12 +31,9 @@ export type TPanelContext =
| "processor"
| "hms"
| "none";

export type TClipboardData = {
panel: TPanelContext;
type: "cut" | "copy" | null;
uids: TNodeUid[];
fileType: "html" | "unknown";
data: TNode[];
fileUid: TNodeUid;
prevNodeTree: TNodeTreeData;
};
Loading

0 comments on commit 90265a6

Please sign in to comment.