feat: 切换节点时,当前文件内容变动未保存,提醒先保存文件

This commit is contained in:
Frankie Huang 2025-06-28 19:38:02 +08:00
parent f2ba92530f
commit 95d533f3bf
3 changed files with 58 additions and 11 deletions

View File

@ -2,7 +2,8 @@
<Split v-model="split" min="5px"> <Split v-model="split" min="5px">
<template #left> <template #left>
<div class="split-left" :class="hiddenSplitLeft"> <div class="split-left" :class="hiddenSplitLeft">
<LeftSidebar v-model:rootFolderPath="rootFolderPath" v-model:currentFilePath="currentFilePath"> <LeftSidebar v-model:rootFolderPath="rootFolderPath" v-model:currentFilePath="currentFilePath"
:confirmSwitchingFile="confirmSwitchingFile">
</LeftSidebar> </LeftSidebar>
</div> </div>
</template> </template>
@ -26,7 +27,7 @@ import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vu
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
import { readTextFile, writeTextFile, writeFile, exists } from '@tauri-apps/plugin-fs'; import { readTextFile, writeTextFile, writeFile, exists } from '@tauri-apps/plugin-fs';
import { save } from '@tauri-apps/plugin-dialog'; import { save } from '@tauri-apps/plugin-dialog';
import { Message, Notice } from 'view-ui-plus' import { Message, Notice, Modal } from 'view-ui-plus'
import { GetSingleArticle, UpdateArticle } from '/src/utils/userHandler'; import { GetSingleArticle, UpdateArticle } from '/src/utils/userHandler';
// //
@ -86,6 +87,32 @@ watch(currentFilePath, async (newFilePath) => {
localStorage.setItem('currentSelectedFilePath', newFilePath); localStorage.setItem('currentSelectedFilePath', newFilePath);
await readFileContent(newFilePath); await readFileContent(newFilePath);
}) })
//
const confirmSwitchingFile = () => {
//
const lastSaveTimeValue = lastSaveTime.get(currentFilePath.value);
const isUpdated = lastSaveTimeValue && lastUpdateTime.value > lastSaveTimeValue;
return new Promise((resolve) => {
//
if (!isUpdated) {
resolve(true);
return;
}
//
Modal.confirm({
title: '当前文件的内容变动未保存',
content: `<p><b>${currentFilePath.value}</b> 内容被修改但仍未保存,确认切换文件?<br/><br/>注:确认切换之后,内容变动会丢失</p>`,
okText: "仍然切换",
cancelText: "取消切换",
onOk: () => {
resolve(true);
},
onCancel: () => {
resolve(false);
}
});
});
}
const getFileNameFromFilePath = (filePath) => { const getFileNameFromFilePath = (filePath) => {
const fullFileName = filePath.split('/').pop(); const fullFileName = filePath.split('/').pop();
const splitResult = fullFileName.split("."); const splitResult = fullFileName.split(".");

View File

@ -8,7 +8,7 @@
@update:rootPath="changeRootPath" @folder-selected="showFileTree" /> @update:rootPath="changeRootPath" @folder-selected="showFileTree" />
</Space> </Space>
<FolderTree ref="folderTreeRef" :treeData="folderTreeData" :expandLevel="1" :selectedNode="[currentFilePath]" <FolderTree ref="folderTreeRef" :treeData="folderTreeData" :expandLevel="1" :selectedNode="[currentFilePath]"
:specifyFileSuffix="['md']" @file-selected="fileSelected" /> :confirmSwitchingNode="confirmSwitchingFile" :specifyFileSuffix="['md']" @file-selected="fileSelected" />
</Space> </Space>
<UserLogin ref="loginRef" v-model:visible="showLoginModal" v-model:loginStatus="loginStatus"></UserLogin> <UserLogin ref="loginRef" v-model:visible="showLoginModal" v-model:loginStatus="loginStatus"></UserLogin>
</template> </template>
@ -32,6 +32,11 @@ defineProps({
required: false, required: false,
default: '' default: ''
}, },
confirmSwitchingFile: {
type: Function,
required: false,
default: undefined,
}
}); });
const emit = defineEmits([ const emit = defineEmits([

View File

@ -60,6 +60,14 @@ export default {
required: false, required: false,
default: [], default: [],
}, },
// Promise resolve(true) resolve(false)
confirmSwitchingNode: {
type: Function,
required: false,
default: () => new Promise((resolve) => {
resolve(true);
}),
},
}, },
data() { data() {
return { return {
@ -220,15 +228,22 @@ export default {
return return
} }
// this.confirmSwitchingNode().then((result) => {
let selectedNodes = this.$refs.tree.getSelectedNodes() //
selectedNodes.forEach(element => { if (result === false) {
element.selected = false return;
}); }
nodeRenderData.selected = true
// //
this.$emit('file-selected', nodeRenderData); let selectedNodes = this.$refs.tree.getSelectedNodes();
selectedNodes.forEach(element => {
element.selected = false;
});
nodeRenderData.selected = true;
//
this.$emit('file-selected', nodeRenderData);
});
}, },
// //
insertTreeNode(parentRenderData, nodeData, index) { insertTreeNode(parentRenderData, nodeData, index) {