feat: 文件内容未保存时,阻塞窗口关闭事件

This commit is contained in:
Frankie Huang 2025-06-28 20:38:49 +08:00
parent 95d533f3bf
commit 1226991c7a
2 changed files with 24 additions and 5 deletions

View File

@ -10,6 +10,9 @@
"opener:default", "opener:default",
"fs:default", "fs:default",
"dialog:default", "dialog:default",
{
"identifier": "core:window:allow-destroy"
},
{ {
"identifier": "fs:scope", "identifier": "fs:scope",
"allow": [ "allow": [

View File

@ -25,8 +25,9 @@ import LeftSidebar from './components/LeftSidebar.vue'
import MainEditor from './components/MainEditor.vue' import MainEditor from './components/MainEditor.vue'
import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vue"; import { ref, watch, useTemplateRef, nextTick, onMounted, onUnmounted } from "vue";
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
import { getCurrentWindow } from "@tauri-apps/api/window";
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, confirm } from '@tauri-apps/plugin-dialog';
import { Message, Notice, Modal } 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';
@ -89,12 +90,9 @@ watch(currentFilePath, async (newFilePath) => {
}) })
// //
const confirmSwitchingFile = () => { const confirmSwitchingFile = () => {
//
const lastSaveTimeValue = lastSaveTime.get(currentFilePath.value);
const isUpdated = lastSaveTimeValue && lastUpdateTime.value > lastSaveTimeValue;
return new Promise((resolve) => { return new Promise((resolve) => {
// //
if (!isUpdated) { if (currentFileSaved()) {
resolve(true); resolve(true);
return; return;
} }
@ -113,6 +111,12 @@ const confirmSwitchingFile = () => {
}); });
}); });
} }
const currentFileSaved = () => {
// true
const lastSaveTimeValue = lastSaveTime.get(currentFilePath.value);
const isUpdated = lastSaveTimeValue && lastUpdateTime.value > lastSaveTimeValue;
return !isUpdated;
}
const getFileNameFromFilePath = (filePath) => { const getFileNameFromFilePath = (filePath) => {
const fullFileName = filePath.split('/').pop(); const fullFileName = filePath.split('/').pop();
const splitResult = fullFileName.split("."); const splitResult = fullFileName.split(".");
@ -230,6 +234,18 @@ onMounted(async () => {
if (currentFilePath.value.length > 0) { if (currentFilePath.value.length > 0) {
await readFileContent(currentFilePath.value); await readFileContent(currentFilePath.value);
} }
//
await getCurrentWindow().onCloseRequested(async (event) => {
if (currentFileSaved()) {
return; //
}
const confirmed = await confirm('当前文件内容未保存,是否确认关闭窗口?');
if (!confirmed) {
// user did not confirm closing the window; let's prevent it
event.preventDefault();
}
});
}); });
onUnmounted(() => { onUnmounted(() => {
window.removeEventListener('resize', resizeHandler); window.removeEventListener('resize', resizeHandler);