@setoelkahfi / svara / commits / b62bcfe

fix: handled trying to rename/create file that already exists

Signed-off-by: mellobacon <mellodev@outlook.com>

mellobacon committed Nov 2, 2023 at 20:34 UTC b62bcfe512e0269091e49f9a56a42c4abbcc8f6c
6 files changed +42 -13
src/App.svelte
+4 -4
@@ -73,14 +73,14 @@
73 const popup = writable(null);
74 export const fullscreen = writable(false);
75
76 - export function openInputModal(title: string, description: string, buttons: any[], options = undefined) {
76 + export function openInputModal(title: string, description: string, buttons: any[], options = undefined, path = "") {
77 popup.set(InputModal);
78 - popupProps.set({title: title, description: description, buttons: buttons, options: options, open: true});
78 + popupProps.set({title: title, description: description, buttons: buttons, options: options, open: true, path: path});
79 _openPopup.set(true);
80 }
81 - export function openRenameModal(title: string, description: string, buttons: any[]) {
81 + export function openRenameModal(title: string, description: string, buttons: any[], path: string) {
82 popup.set(RenameModal);
83 - popupProps.set({title: title, description: description, buttons: buttons, open: true});
83 + popupProps.set({title: title, description: description, buttons: buttons, open: true, path: path});
84 _openPopup.set(true);
85 }
86 </script>
src/config/commands.ts
+5 -2
@@ -194,10 +194,13 @@ export const commands = {
194 return;
195 }
196 openRenameModal(`Rename ${filename}`,
197 - `Give a new name to ${filename}`, [
197 + `Give a new name to ${filename}`,
198 + [
199 {name: "Rename", action: async (name) => {await renameFile(name, oldpath)}},
200 {name: "Cancel", cancel: true, action: () => {}}
200 - ])
201 + ],
202 + oldpath
203 + )
204 }
205 },
206 "openInExplorer": {
src/lib/FileTree.svelte
+2 -2
@@ -31,12 +31,12 @@
31 `Create a new folder in ${path}`, [
32 {name: "Create Folder", action: async (name) => { await createFolder(`${path}${p.sep}${name}`)}},
33 {name: "Cancel", cancel: true, action: () => {}}
34 - ], {label: "Folder Name"})}},
34 + ], {label: "Folder Name"}, path)}},
35 {name: "New File...", shortcut: "", action: () => {openInputModal("Create New File",
36 `Create a new file in ${path}`, [
37 {name: "Create File", action: (name) => {createFile(`${path}${p.sep}${name}`)}},
38 {name: "Cancel", cancel: true, action: () => {}}
39 - ], {label: "File Name"})}},
39 + ], {label: "File Name"}, path)}},
40 {name: "Copy", shortcut: "Ctrl + C", action: () => {console.warn("Feature not implemented yet.")}},
41 {name: "Cut", disabled: true, shortcut: "Ctrl + X", action: () => {console.warn("Feature not implemented yet.")}},
42 {name: "Paste", shortcut: "Ctrl + X", disabled: true, action: () => {console.warn("Feature not implemented yet.")}},
src/lib/FileTree/FileTreeList.svelte
+11 -5
@@ -10,9 +10,9 @@
10 import Directory from "../../util/icons/Directory.svelte";
11 import {filetree} from "../FileTree.svelte";
12 import ContextMenu from "../utility/ContextMenu.svelte";
13 - import { createFile, createFolder, moveFile, moveToTrash, openInExplorer, renameFile } from "../File";
13 + import { createFile, createFolder, moveFile, moveToTrash } from "../File";
14 import { clipboard } from "@tauri-apps/api";
15 - import { openInputModal, openRenameModal } from "../../App.svelte";
15 + import { openInputModal } from "../../App.svelte";
16 import { path as p } from "@tauri-apps/api";
17 import { onMount } from "svelte";
18 import { commands } from "../../config/commands";
@@ -43,18 +43,18 @@
43 `Create a new folder in ${path}`, [
44 {name: "Create Folder", action: async (name) => { await createFolder(`${path}${p.sep}${name}`)}},
45 {name: "Cancel", cancel: true, action: () => {}}
46 - ], {label: "Folder Name"})}},
46 + ], {label: "Folder Name"}, path)}},
47 {name: "New File...", shortcut: "", action: () => {openInputModal("Create New File",
48 `Create a new file in ${path}`, [
49 {name: "Create File", action: (name) => {createFile(`${path}${p.sep}${name}`)}},
50 {name: "Cancel", cancel: true, action: () => {}}
51 - ], {label: "File Name"})}},
51 + ], {label: "File Name"}, path)}},
52 {name: "Copy", shortcut: "Ctrl + C", action: () => {console.warn("Feature not implemented yet.")}},
53 {name: "Cut", disabled: isroot, shortcut: "Ctrl + X", action: () => {console.warn("Feature not implemented yet.")}},
54 {name: "Paste", shortcut: "Ctrl + X", disabled: true, action: () => {console.warn("Feature not implemented yet.")}},
55 {name: "Copy Filename", shortcut: "", action: async () => {await clipboard.writeText(name)}},
56 {name: "Copy Absolute Path", shortcut: "", action: async () => {await clipboard.writeText(path)}},
57 - {name: "Rename...", shortcut: commands.renameFile.keybind, action: () => {commands.renameFile.command(name, path)}},
57 + {name: "Rename...", disabled: isroot, shortcut: commands.renameFile.keybind, action: () => {commands.renameFile.command(name, path)}},
58 {name: "Delete", disabled: isroot, shortcut: "Delete", action: async () => {await moveToTrash(path)}}
59 ]
60
@@ -163,6 +163,12 @@
163 }
164 </script>
165
166 +<svelte:window on:click={(e) => {
167 + if (e.target !== refLabel) {
168 + contextmenu = false;
169 + }
170 +}}></svelte:window>
171 +
172 {#if root}
173 {#each children as child}
174 {#if Array.isArray(child.children)}
src/lib/Modals/InputModal.svelte
+10
@@ -4,11 +4,13 @@
4 import Popup from "../utility/Popup.svelte";
5 import { _openPopup } from "../../App.svelte";
6 import { checkValidFileName } from "../File";
7 + import { fs, invoke, path as p } from "@tauri-apps/api";
8
9 export let title;
10 export let buttons;
11 export let description = "";
12 export let options = {label: null, placeholder: "Enter text..."};
13 + export let path = "";
14 let invalid = false;
15 let helpText = ""
16 let invalidText = "";
@@ -38,6 +40,14 @@
40 invalidText = `{${value === "" ? "null" : value}} is not a valid file or directory name`;
41 return;
42 }
43 + if (!button.cancel && await fs.exists(`${path}${p.sep}${value}`)) {
44 + invalid = true;
45 + if (await invoke("is_file", {path: path})) {
46 + path = path.slice(0, path.indexOf(value));
47 + }
48 + invalidText = `${value} in ${path} already exists`;
49 + return;
50 + }
51 await handleButtonClick(button.action, button.cancel);
52 }}>{button.name}</button>
53 {/each}
src/lib/Modals/RenameModal.svelte
+10
@@ -4,11 +4,13 @@
4 import Popup from "../utility/Popup.svelte";
5 import { _openPopup } from "../../App.svelte";
6 import { checkValidFileName } from "../File";
7 + import { fs, path as p, invoke } from "@tauri-apps/api";
8
9 export let title;
10 export let buttons;
11 export let description = "";
12 export let options = {label: "Name: ", placeholder: "Enter text..."};
13 + export let path = "";
14 let invalid = false;
15 let helpText = ""
16 let invalidText = "";
@@ -42,6 +44,14 @@
44 invalidText = `{${value === "" ? "null" : value}} is not a valid file name`;
45 return;
46 }
47 + if (!button.cancel && await fs.exists(`${path}${p.sep}${value}`)) {
48 + invalid = true;
49 + if (await invoke("is_file", {path: path})) {
50 + path = path.slice(0, path.indexOf(value));
51 + }
52 + invalidText = `${value} in ${path} already exists`;
53 + return;
54 + }
55 await handleButtonClick(button.action, button.cancel);
56 }}>{button.name}</button>
57 {/each}