Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrnoch committed May 17, 2022
1 parent 9fbd67e commit 6451e2d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
7 changes: 3 additions & 4 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, useEffect, useState } from "react";
import CodeEditor from "@uiw/react-textarea-code-editor";
import * as pathExt from "path";
import { extname } from "../utils";
import { useStore } from "../hooks/store";

type EditorProps = {
Expand All @@ -11,8 +11,7 @@ type EditorProps = {

export const Editor: FC<EditorProps> = ({ onSave, initialValue = "" }) => {
const filePaths = useStore((s) => s.currentFilePaths);
const ext = pathExt.extname(filePaths[0]).slice(1);
console.log(ext);
const ext = extname(filePaths[0]);

const [state, setState] = useState(initialValue);

Expand All @@ -31,7 +30,7 @@ export const Editor: FC<EditorProps> = ({ onSave, initialValue = "" }) => {
return (
<CodeEditor
value={initialValue}
language="js"
language={ext}
placeholder="Please enter JS code."
onChange={(evn) => setState(evn.target.value)}
padding={15}
Expand Down
24 changes: 11 additions & 13 deletions src/components/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import { useStore } from "../hooks/store";
import { useDirectoryConfig } from "../hooks/useDirectoryConfig";
import { useDirectoryWatch } from "../hooks/useDirectoryWatch";
import { keyBy } from "lodash";
// import { basename, dirname, join } from "path";
import * as pathExt from "path";
import { FC, useCallback, useEffect, useState } from "react";
import { useDrop } from "react-dnd";
import { FiArrowLeft } from "react-icons/fi";
import { VscAdd, VscFile, VscFiles } from "react-icons/vsc";
import { keyframes, styled } from "../theme";
import { FsElement } from "types";
import { useDebouncedCallback } from "use-debounce";
import { orderWith } from "../utils";
import { orderWith, basename, join, dirname } from "../utils";
import { DraggableList } from "./DraggableList";
import { FileItem } from "./FileItem";
import { Content, Item, ItemIcon, Menu, Trigger } from "./ui/Menu";
Expand Down Expand Up @@ -124,8 +122,8 @@ export const SideBar: FC = () => {
};

const onMove = async (from: string, pathTo: string) => {
const name = pathExt.basename(from);
const to = pathExt.join(pathTo, name);
const name = basename(from);
const to = join(pathTo, name);
await copyFile(from, to);
await removeFile(from);
};
Expand All @@ -134,7 +132,7 @@ export const SideBar: FC = () => {
const type = showAddItem;

if (type === "file") {
const path = pathExt.join(directoryPath, name);
const path = join(directoryPath, name);

await writeFile({
contents: "",
Expand All @@ -144,7 +142,7 @@ export const SideBar: FC = () => {
}

if (type === "folder") {
await createDir(pathExt.join(directoryPath, name));
await createDir(join(directoryPath, name));
set({ showAddItem: false });
}
};
Expand All @@ -161,8 +159,8 @@ export const SideBar: FC = () => {
};

const onRename = async (name: string, path: string) => {
const newPath = pathExt.join(pathExt.dirname(path), name);
await renameFile(path, pathExt.join(pathExt.dirname(path), name));
const newPath = join(dirname(path), name);
await renameFile(path, join(dirname(path), name));
// update present selected document
if (filePaths.includes(path))
set({
Expand Down Expand Up @@ -196,7 +194,7 @@ export const SideBar: FC = () => {
accept: "file",
drop: async (item: any) => {
const path = item.id;
await onMove(path, pathExt.dirname(directoryPath));
await onMove(path, dirname(directoryPath));
},
});

Expand All @@ -220,21 +218,21 @@ export const SideBar: FC = () => {
cursor: "pointer",
}}
onClick={() => {
set({ currentDirectoryPath: pathExt.dirname(directoryPath) });
set({ currentDirectoryPath: dirname(directoryPath) });
}}
>
<FiArrowLeft />
</div>
)}
<div>{pathExt.basename(directoryPath)}</div>
<div>{basename(directoryPath)}</div>
<div
style={{
position: "absolute",
right: 10,
cursor: "pointer",
}}
onClick={() => {
set({ currentDirectoryPath: pathExt.dirname(directoryPath) });
set({ currentDirectoryPath: dirname(directoryPath) });
}}
>
<Menu>
Expand Down
11 changes: 0 additions & 11 deletions src/components/ui/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ export type BoxProps = {
style?: any;
children?: any;

padding?: number | string;
pointer?: string;
flex?: number;

mb?: number | string;
mt?: number | string;
ml?: number | string;
mr?: number | string;

minHeight?: string | number;

className?: string;
};

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useDirectoryWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const useDirectoryWatch = (path: string, cb: (path: string) => void) => {
useEffect(() => {
if (!path) return;
invoke("watch", { path });
console.log("watching" + path);
return () => {
console.log("unwatch");
invoke("unwatch", { path });
};
}, [path]);
Expand All @@ -18,6 +20,7 @@ export const useDirectoryWatch = (path: string, cb: (path: string) => void) => {

let stop: any;
listen<string>("file_changed", (event) => {
console.log("payload change")
cb(event.payload);
}).then((fn) => {
stop = fn;
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export const useFs = () => {
multiple: false,
directory: true,
})) as string;
console.log(path);
set({ currentProjectPath: path, currentDirectoryPath: path });
};

const openFile = async () => {
const path = (await open({
multiple: false,
defaultPath: currentDirectoryPath,
})) as string;
console.log(path);
set({ currentFilePaths: [path] });
};

Expand Down
20 changes: 17 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import * as path from "path";

export const removeExt = (name: string) =>
path.basename(name).replace(path.extname(name), "");
basename(name).replace(extname(name), "");

export const orderWith = (array: string[], guideArray: string[]) => {
return [
...guideArray.filter((e) => array.includes(e)),
...array.filter((e) => !guideArray.includes(e)),
];
};

export const basename = (path: String) => {
return path.split("/").reverse()[0];
};

export const extname = (path: String) => {
return path.split(".").reverse()[0];
};

export const join = (...paths: string[]) => {
return paths.join("/");
};

export const dirname = (path: String) => {
return path.split("/").slice(0, -1).join("/");
};

0 comments on commit 6451e2d

Please sign in to comment.