Browse Source

fix:修复文件夹重命名后会被删除问题

master
秋田弘 5 months ago
parent
commit
beaeedec39
  1. 160
      frontend/src/components/builtin/FileList.vue

160
frontend/src/components/builtin/FileList.vue

@ -17,7 +17,10 @@
</div> </div>
</div> </div>
</template> </template>
<div draggable="true" class="file-item" :class="{ <div
draggable="true"
class="file-item"
:class="{
chosen: chosenIndexs.includes(index), chosen: chosenIndexs.includes(index),
'no-chosen': !chosenIndexs.includes(index), 'no-chosen': !chosenIndexs.includes(index),
'mode-icon': mode === 'icon', 'mode-icon': mode === 'icon',
@ -26,27 +29,52 @@
'mode-middle': mode === 'middle', 'mode-middle': mode === 'middle',
'mode-detail': mode === 'detail', 'mode-detail': mode === 'detail',
'drag-over': hoverIndex === index, 'drag-over': hoverIndex === index,
}" :style="{ }"
:style="{
'--theme-color': theme === 'light' ? '#ffffff6b' : '#3bdbff3d', '--theme-color': theme === 'light' ? '#ffffff6b' : '#3bdbff3d',
}" v-for="(item, index) in fileList" :key="item.path" @dblclick="handleOnOpen(item)" }"
v-for="(item, index) in fileList"
:key="item.path"
@dblclick="handleOnOpen(item)"
@touchstart.passive="doubleTouch($event, item)" @touchstart.passive="doubleTouch($event, item)"
@contextmenu.stop.prevent="handleRightClick($event, item, index)" @drop="hadnleDrop($event, item.path)" @contextmenu.stop.prevent="handleRightClick($event, item, index)"
@dragenter.prevent="handleDragEnter(index)" @dragover.prevent @dragleave="handleDragLeave()" @drop="hadnleDrop($event, item.path)"
@dragstart.stop="startDragApp($event, item)" @click="handleClick(index, item)" @mousedown.stop :ref="(ref: any) => { @dragenter.prevent="handleDragEnter(index)"
@dragover.prevent
@dragleave="handleDragLeave()"
@dragstart.stop="startDragApp($event, item)"
@click="handleClick(index, item)"
@mousedown.stop
:ref="(ref: any) => {
if (ref) { if (ref) {
appPositions[index] = markRaw(ref as Element); appPositions[index] = markRaw(ref as Element);
} }
} }
"> "
>
<div class="file-item_img"> <div class="file-item_img">
<FileIcon :file="item" /> <FileIcon :file="item" />
</div> </div>
<span v-if="editIndex !== index" class="file-item_title"> <span
v-if="editIndex !== index"
class="file-item_title"
>
{{ getName(item) }} {{ getName(item) }}
</span> </span>
<textarea autofocus draggable="false" @dragover.stop @dragstart.stop @dragenter.stop @mousedown.stop <textarea
@dblclick.stop @click.stop @blur="onEditNameEnd" v-if="editIndex === index" autofocus
class="file-item_title file-item_editing" v-model="editName"></textarea> draggable="false"
@dragover.stop
@dragstart.stop
@dragenter.stop
@mousedown.stop
@dblclick.stop
@click.stop
@keydown.enter.prevent="onEditNameEnd"
v-if="editIndex === index"
class="file-item_title file-item_editing"
v-model="editName"
></textarea>
<template v-if="mode === 'detail'"> <template v-if="mode === 'detail'">
<div class="file-item_type"> <div class="file-item_type">
<span>{{ item.isDirectory ? "-" : dealSize(item.size) }}</span> <span>{{ item.isDirectory ? "-" : dealSize(item.size) }}</span>
@ -84,7 +112,7 @@ import { Menu } from "@/system/menu/Menu";
import { throttle } from "@/util/debounce"; import { throttle } from "@/util/debounce";
import { dealSize } from "@/util/file"; import { dealSize } from "@/util/file";
import { markRaw, onMounted, ref } from "vue"; import { markRaw, onMounted, ref } from "vue";
import { notifyError, notifySuccess } from "@/util/msg";
const { openPropsWindow, copyFile, createLink, deleteFile } = const { openPropsWindow, copyFile, createLink, deleteFile } =
useContextMenu(); useContextMenu();
const sys = useSystem(); const sys = useSystem();
@ -171,28 +199,19 @@ function doubleTouch(e: TouchEvent, item: OsFileWithoutContent) {
const editIndex = ref<number>(-1); const editIndex = ref<number>(-1);
const editName = ref<string>(""); const editName = ref<string>("");
async function onEditNameEnd() { async function onEditNameEnd() {
const editEndName = editName.value.trim(); const editEndName = editName.value.trim();
if (editEndName && editIndex.value >= 0) { if (editIndex.value >= 0) {
const editpath: any = const currentItem = props.fileList[editIndex.value];
props.fileList[editIndex.value].path.toString(); const success = await renameFile(currentItem, editEndName);
let newPath: any; if (!success) {
let sp = "/"; editName.value = basename(currentItem.path);
if (editpath.indexOf("/") === -1) {
sp = "\\";
}
newPath = editpath?.split(sp);
newPath.pop();
newPath.push(editEndName);
newPath = newPath.join(sp);
await sys?.fs.rename(editpath, newPath);
props.onRefresh();
if (newPath.indexOf("Desktop") !== -1) {
sys.refershAppList();
} }
} }
editIndex.value = -1; editIndex.value = -1;
} }
mountEvent("edit.end", () => { mountEvent("edit.end", () => {
onEditNameEnd(); onEditNameEnd();
}); });
@ -206,7 +225,7 @@ const isMobile = ref<boolean>(false);
function handleClick(index: number, item: OsFileWithoutContent) { function handleClick(index: number, item: OsFileWithoutContent) {
chosenIndexs.value = [index]; chosenIndexs.value = [index];
if (isMobile.value) { if (isMobile.value) {
handleOnOpen(item) handleOnOpen(item);
} }
} }
onMounted(() => { onMounted(() => {
@ -252,15 +271,12 @@ function startDragApp(mouse: DragEvent, item: OsFileWithoutContent) {
}); });
} }
} }
document.addEventListener('copy', function () { document.addEventListener("copy", function () {
const files = chosenIndexs.value.map( const files = chosenIndexs.value.map((index) => props.fileList[index]);
(index) => props.fileList[index]
)
if (files.length > 0) { if (files.length > 0) {
copyFile(files); copyFile(files);
chosenIndexs.value = []; chosenIndexs.value = [];
} }
}); });
// document.addEventListener('keydown', function (event) { // document.addEventListener('keydown', function (event) {
// // Control + C // // Control + C
@ -369,9 +385,9 @@ function handleRightClick(
menuArr.push({ menuArr.push({
label: "加入知识库", label: "加入知识库",
click: () => { click: () => {
console.log(item) console.log(item);
addKnowledge(item.path).then((res: any) => { addKnowledge(item.path).then((res: any) => {
console.log(res) console.log(res);
if (res.code != 0) { if (res.code != 0) {
new Notify({ new Notify({
title: t("tips"), title: t("tips"),
@ -384,7 +400,7 @@ function handleRightClick(
}); });
props.onRefresh(); props.onRefresh();
} }
}) });
}, },
}); });
} else { } else {
@ -406,7 +422,6 @@ function handleRightClick(
}, },
}); });
} }
} }
if (choose.ifShow) { if (choose.ifShow) {
menuArr.push({ menuArr.push({
@ -551,6 +566,49 @@ function handleDragLeave() {
// function dealtName(name: string) { // function dealtName(name: string) {
// return name; // return name;
// } // }
function getPathSeparator(path: string): string {
return path.indexOf("/") === -1 ? "\\" : "/";
}
function buildNewPath(editpath: string, editEndName: string): string {
const sp = getPathSeparator(editpath);
const pathParts = editpath.split(sp);
pathParts.pop();
pathParts.push(editEndName);
return pathParts.join(sp);
}
async function renameFile(
currentItem: OsFileWithoutContent,
newName: string
) {
const currentName = basename(currentItem.path);
if (newName && newName !== currentName) {
const editpath = currentItem.path.toString();
const newPath = buildNewPath(editpath, newName);
if (await sys?.fs.exists(newPath)) {
notifyError("文件名已存在,请选择其他名称。");
return false;
}
try {
await sys?.fs.rename(editpath, newPath);
props.onRefresh();
if (newPath.includes("Desktop")) {
sys.refershAppList();
}
notifySuccess("重命名成功");
return true;
} catch (error) {
console.error("重命名失败:", error);
notifyError("重命名失败,请重试。");
return false;
}
}
return false;
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.file-item { .file-item {
@ -585,13 +643,28 @@ function handleDragLeave() {
display: inline-block !important; display: inline-block !important;
outline: none; outline: none;
pointer-events: all; pointer-events: all;
padding: 0; padding: 6px 10px;
margin: 0; margin: 0;
min-width: 0; min-width: 120px;
height: min-content !important; max-width: 100%;
width: min-content !important; height: auto !important;
width: auto !important;
resize: none; resize: none;
border-radius: 0; border-radius: 6px;
border: 1px solid var(--border-color, #ccc);
background-color: var(--input-bg-color, #f9f9f9);
color: var(--input-text-color, #333);
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
transition: border-color 0.3s, box-shadow 0.3s,
background-color 0.3s;
overflow-x: auto;
white-space: nowrap;
}
.file-item_editing:focus {
border-color: var(--focus-border-color, #0056b3);
box-shadow: 0 3px 8px rgba(0, 86, 179, 0.3);
background-color: var(--focus-bg-color, #e6f7ff);
} }
} }
@ -751,6 +824,5 @@ function handleDragLeave() {
background-color: transparent; background-color: transparent;
border: 1px solid transparent; border: 1px solid transparent;
} }
} }
</style> </style>

Loading…
Cancel
Save