diff --git a/src/App.vue b/src/App.vue index 1dcf1b4..1892bfc 100644 --- a/src/App.vue +++ b/src/App.vue @@ -79,7 +79,7 @@ watch(rootFolderPath, (newRootPath) => { // 完成文件树渲染后,点击文件得到 currentFilePath const currentFilePath = ref(localStorage.getItem('currentSelectedFilePath') || ""); -let switchFilePath = false; // 切换文件时,避免触发 markdownCode 的 watch 事件 +let switchFilePath = true; // 切换文件时,避免触发 markdownCode 的 watch 事件 // 读取 currentFilePath 的文件内容,填充到 MarkdownEditor 之中 watch(currentFilePath, async (newFilePath) => { switchFilePath = true; @@ -95,11 +95,14 @@ const getFileNameFromFilePath = (filePath) => { const mainEditor = ref(null); const markdownCode = ref("# Hello Markdown"); +const lastSaveTime = new Map(); // 记录每个文件的最后保存时间 +const lastUpdateTime = ref(0); // 记录当前文件的最后更新时间 watch(markdownCode, (newMarkdownCode) => { if (switchFilePath === true) { switchFilePath = false; return; } + lastUpdateTime.value = new Date().getTime(); console.log("code be updated"); }) async function readFileContent(filePath) { @@ -110,6 +113,7 @@ async function readFileContent(filePath) { GetSingleArticle(articleID).then((data) => { markdownCode.value = data.mdCode; currentFilePath.value = filePath; + lastSaveTime.set(filePath, new Date().getTime()); Message.success('已读取文件内容,并加载到编辑器中:' + filePath); }).catch((error) => { Message.error(`调用异常: ${error}`); @@ -120,6 +124,7 @@ async function readFileContent(filePath) { const fileContent = await readTextFile(filePath); markdownCode.value = fileContent; currentFilePath.value = filePath; + lastSaveTime.set(filePath, new Date().getTime()); Message.success('已读取文件内容,并加载到编辑器中:' + filePath); } catch (err) { Message.error('文件读取失败:' + err); @@ -149,6 +154,7 @@ async function writeFileContent(markdownCode) { await writeTextFile(currentFilePath.value, markdownCode); Message.success('文件更新成功:' + currentFilePath.value); } + lastSaveTime.set(currentFilePath.value, new Date().getTime()); } catch (err) { Message.error('文件更新失败:' + err); }