Skip to content

Commit 2c2e2e2

Browse files
author
小さい猫
authored
playground: add a rename button (#669)
Signed-off-by: Ookiineko <[email protected]>
1 parent 496f420 commit 2c2e2e2

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

apps/website/src/components/Playground/Workspace/FileSystemManager.tsx

+51-8
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ import { Node } from "./types";
2525
interface FileSystemManagerProps {
2626
path: string;
2727
nodes: Node[];
28+
oldName: string;
29+
newName: string;
30+
renameOpen: boolean;
31+
onNewNameChange: () => (event: ChangeEvent<HTMLInputElement>) => Promise<void>;
32+
onCloseRenameModal: () => () => Promise<void>;
2833
onFileUpload: (
2934
isText: boolean
3035
) => (event: ChangeEvent<HTMLInputElement>) => Promise<void>;
3136
onDirClick: (name: string) => () => Promise<void>;
3237
onFileClick: (name: string) => (option: string) => Promise<void>;
3338
onDirCreate: (name: string) => () => Promise<void>;
39+
onRename: (old_name: string, new_name: string) => () => Promise<void>;
3440
onRefresh: () => Promise<void>;
3541
onLoadSamples: () => Promise<void>;
3642
}
@@ -46,6 +52,7 @@ const modalStyle = {
4652
};
4753

4854
export const options = [
55+
{ text: "Rename", key: "rename" },
4956
{ text: "Download", key: "download" },
5057
{ text: "Download as Text File", key: "download-text" },
5158
{ text: "Delete", key: "delete" },
@@ -54,17 +61,23 @@ export const options = [
5461
export default function FileSystemManager({
5562
path = "/",
5663
nodes = [],
64+
oldName = "",
65+
newName = "",
66+
renameOpen = false,
67+
onNewNameChange = () => () => Promise.resolve(),
68+
onCloseRenameModal = () => () => Promise.resolve(),
5769
onFileUpload = () => () => Promise.resolve(),
5870
onFileClick = () => () => Promise.resolve(),
5971
onDirClick = () => () => Promise.resolve(),
6072
onDirCreate = () => () => Promise.resolve(),
73+
onRename = () => () => Promise.resolve(),
6174
onRefresh = () => Promise.resolve(),
6275
onLoadSamples = () => Promise.resolve(),
6376
}: FileSystemManagerProps) {
64-
const [open, setOpen] = useState(false);
77+
const [newFolderOpen, setNewFolderOpen] = useState(false);
6578
const [dirName, setDirName] = useState("");
66-
const handleModalOpen = () => setOpen(true);
67-
const handleModalClose = () => setOpen(false);
79+
const handleNewFolderModalOpen = () => setNewFolderOpen(true);
80+
const handleNewFolderModalClose = () => setNewFolderOpen(false);
6881

6982
return (
7083
<>
@@ -113,7 +126,7 @@ export default function FileSystemManager({
113126
<IconButton
114127
onClick={() => {
115128
setDirName("");
116-
handleModalOpen();
129+
handleNewFolderModalOpen();
117130
}}
118131
aria-label="create-a-new-folder"
119132
size="small"
@@ -165,8 +178,8 @@ export default function FileSystemManager({
165178
</Stack>
166179
</Paper>
167180
<Modal
168-
open={open}
169-
onClose={handleModalClose}
181+
open={newFolderOpen}
182+
onClose={handleNewFolderModalClose}
170183
aria-labelledby="new-folder-name"
171184
aria-describedby="new-folder-name-description"
172185
>
@@ -183,12 +196,12 @@ export default function FileSystemManager({
183196
onChange={(event) => setDirName(event.target.value)}
184197
/>
185198
<Stack direction="row" justifyContent="flex-end">
186-
<Button onClick={handleModalClose}>Cancel</Button>
199+
<Button onClick={handleNewFolderModalClose}>Cancel</Button>
187200
<Button
188201
variant="contained"
189202
onClick={() => {
190203
onDirCreate(dirName);
191-
handleModalClose();
204+
handleNewFolderModalClose();
192205
}}
193206
>
194207
Create
@@ -197,6 +210,36 @@ export default function FileSystemManager({
197210
</Stack>
198211
</Box>
199212
</Modal>
213+
<Modal
214+
open={renameOpen}
215+
onClose={onCloseRenameModal()}
216+
aria-labelledby="new-name"
217+
aria-describedby="new-name-description"
218+
>
219+
<Box sx={modalStyle}>
220+
<Stack spacing={4}>
221+
<Typography id="modal-modal-title" variant="h6" component="h2">
222+
New Name:
223+
</Typography>
224+
<TextField
225+
id="outlined-basic"
226+
label="my-file"
227+
variant="outlined"
228+
value={newName}
229+
onChange={onNewNameChange()}
230+
/>
231+
<Stack direction="row" justifyContent="flex-end">
232+
<Button onClick={onCloseRenameModal()}>Cancel</Button>
233+
<Button
234+
variant="contained"
235+
onClick={onRename(oldName, newName)}
236+
>
237+
Rename
238+
</Button>
239+
</Stack>
240+
</Stack>
241+
</Box>
242+
</Modal>
200243
</>
201244
);
202245
}

apps/website/src/components/Playground/Workspace/index.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ interface WorkspaceProps {
2323
export default function Workspace({ ffmpeg: _ffmpeg }: WorkspaceProps) {
2424
const [path, setPath] = useState("/");
2525
const [nodes, setNodes] = useState<Node[]>([]);
26+
const [oldName, setOldName] = useState("");
27+
const [newName, setNewName] = useState("");
28+
const [renameOpen, setRenameOpen] = useState(false);
2629
const [args, setArgs] = useState(defaultArgs);
2730
const [progress, setProgress] = useState(0);
2831
const [time, setTime] = useState(0);
@@ -38,6 +41,14 @@ export default function Workspace({ ffmpeg: _ffmpeg }: WorkspaceProps) {
3841
}
3942
};
4043

44+
const onNewNameChange = () => async (event: ChangeEvent<HTMLInputElement>) => {
45+
setNewName(event.target.value);
46+
};
47+
48+
const onCloseRenameModal = () => async () => {
49+
setRenameOpen(false);
50+
};
51+
4152
const onFileUpload =
4253
(isText: boolean) =>
4354
async ({ target: { files } }: ChangeEvent<HTMLInputElement>) => {
@@ -53,6 +64,11 @@ export default function Workspace({ ffmpeg: _ffmpeg }: WorkspaceProps) {
5364
const onFileClick = (name: string) => async (option: string) => {
5465
const fullPath = `${path}/${name}`;
5566
switch (option) {
67+
case "rename":
68+
setOldName(name);
69+
setNewName("");
70+
setRenameOpen(true);
71+
break;
5672
case "download":
5773
downloadFile(
5874
name,
@@ -93,6 +109,14 @@ export default function Workspace({ ffmpeg: _ffmpeg }: WorkspaceProps) {
93109
refreshDir(path);
94110
};
95111

112+
const onRename = (old_name: string, new_name: string) => async () => {
113+
if (old_name !== "" && new_name !== "") {
114+
await ffmpeg.rename(`${path}/${old_name}`, `${path}/${new_name}`);
115+
}
116+
setRenameOpen(false);
117+
refreshDir(path);
118+
};
119+
96120
const onLoadSamples = async () => {
97121
for (const name of Object.keys(SAMPLE_FILES)) {
98122
await ffmpeg.writeFile(name, await fetchFile(SAMPLE_FILES[name]));
@@ -130,10 +154,16 @@ export default function Workspace({ ffmpeg: _ffmpeg }: WorkspaceProps) {
130154
<FileSystemManager
131155
path={path}
132156
nodes={nodes}
157+
oldName={oldName}
158+
newName={newName}
159+
renameOpen={renameOpen}
160+
onNewNameChange={onNewNameChange}
161+
onCloseRenameModal={onCloseRenameModal}
133162
onFileUpload={onFileUpload}
134163
onFileClick={onFileClick}
135164
onDirClick={onDirClick}
136165
onDirCreate={onDirCreate}
166+
onRename={onRename}
137167
onLoadSamples={onLoadSamples}
138168
onRefresh={() => refreshDir(path)}
139169
/>

0 commit comments

Comments
 (0)