feat: 文件内容未保存时,阻塞窗口关闭事件
This commit is contained in:
parent
95d533f3bf
commit
1226991c7a
@ -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": [
|
||||||
|
|||||||
26
src/App.vue
26
src/App.vue
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user