@setoelkahfi / svara / commits / a4c77a4

feat: added abililty to rename files/folders

mellbacon committed Jun 25, 2023 at 15:45 UTC a4c77a4831095320423fa035c527d6b06d435a9a
4 files changed +35 -6
src/lib/EditorTabList.svelte
+7
@@ -43,6 +43,13 @@
43 export function closeTab(tabid: number) {
44 editorTab.closeTab(tabid);
45 }
46 + export function renameTab(tab, label, path) {
47 + if (tab) {
48 + tab.label = label;
49 + tab.path = path;
50 + editorTab.setActive(tab.id);
51 + }
52 + }
53 function closeAllTabs() {
54 editorTab.closeAllTabs();
55 }
src/lib/File.ts
+23 -1
@@ -1,6 +1,6 @@
1 import { dialog, fs, path, invoke, window } from "@tauri-apps/api";
2 import { get, writable } from 'svelte/store';
3 -import { tabs, addEditorTab } from "./EditorTabList.svelte";
3 +import { tabs, addEditorTab, renameTab } from "./EditorTabList.svelte";
4 import { filetree } from "./FileTree.svelte";
5 import { watch } from "tauri-plugin-fs-watch-api";
6
@@ -162,3 +162,25 @@ export async function createFile(p) {
162 }
163 addEditorTab(p, p.split(path.sep).pop());
164 }
165 +
166 +export async function renameFile(filename: string, oldpath: string) {
167 + if (filename.length === 0) {
168 + console.warn("filename length 0")
169 + return false;
170 + }
171 + if (await invoke("is_file", {path: oldpath}) && filename.includes(path.sep)) {
172 + console.warn("invalid filename")
173 + return false;
174 + }
175 + let newpath = oldpath.replace(oldpath.split(path.sep).pop(), filename);
176 +
177 + try {
178 + await fs.renameFile(oldpath, newpath);
179 + } catch (error) {
180 + console.error(error);
181 + return false;
182 + }
183 + let tab = get(tabs).find(t => t.active && t.isfile);
184 + renameTab(tab, filename, newpath);
185 + return true;
186 +}
src/lib/FileTree/FileTreeList.svelte
+2 -2
@@ -10,7 +10,7 @@
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 } from "../File";
13 + import { createFile, createFolder, moveFile, moveToTrash, openInExplorer, renameFile } from "../File";
14 import { clipboard } from "@tauri-apps/api";
15 import { openInputModal, openRenameModal } from "../../App.svelte";
16 import { path as p } from "@tauri-apps/api";
@@ -51,7 +51,7 @@
51 {name: "Copy Absolute Path", shortcut: "", action: async () => {await clipboard.writeText(path)}},
52 {name: "Rename...", shortcut: "F2", action: () => {openRenameModal(`Rename ${name}`,
53 `Give a new name to ${name}/`, [
54 - {name: "Rename", action: () => {}},
54 + {name: "Rename", action: async (filename) => {await renameFile(filename, path)}},
55 {name: "Cancel", action: () => {}}
56 ])}},
57 {name: "Delete", disabled: isroot, shortcut: "Delete", action: async () => {await moveToTrash(path)}}
src/lib/FileTree/FileTreeNode.svelte
+3 -3
@@ -18,7 +18,7 @@
18 import { createEventDispatcher } from "svelte";
19 import File from "../../util/icons/File.svelte";
20 import ContextMenu from "../utility/ContextMenu.svelte";
21 - import { moveToTrash, openInExplorer } from "../File";
21 + import { moveToTrash, openInExplorer, renameFile } from "../File";
22 import { addEditorTab } from "../EditorTabList.svelte";
23 import { clipboard } from "@tauri-apps/api";
24 import { openRenameModal } from "../../App.svelte";
@@ -41,9 +41,9 @@
41 {name: "Copy Filename", shortcut: "", action: async () => {await clipboard.writeText(name)}},
42 {name: "Copy Absolute Path", shortcut: "", action: async () => {await clipboard.writeText(path)}},
43 {name: "Edit", shortcut: "", action: () => {addEditorTab(path, name)}},
44 - {name: "Rename...", shortcut: "F2", disabled: true, action: () => {openRenameModal(`Rename ${name}`,
44 + {name: "Rename...", shortcut: "F2", action: () => {openRenameModal(`Rename ${name}`,
45 `Give a new name to ${name}`, [
46 - {name: "Rename", action: () => {}},
46 + {name: "Rename", action: async (filename) => {await renameFile(filename, path)}},
47 {name: "Cancel", action: () => {}}
48 ])}},
49 {name: "Delete", shortcut: "Delete", action: async () => {await moveToTrash(path)}}