From 95529f6111b91b8cbb7fdbeb65cca452e9bb92da Mon Sep 17 00:00:00 2001 From: Frankie Huang Date: Tue, 8 Apr 2025 02:04:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A1=B5=E9=9D=A2=E5=92=8C=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=99=A8=E9=AB=98=E5=AE=BD=E5=BA=A6=E8=87=AA=E9=80=82?= =?UTF-8?q?=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 57 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/App.vue b/src/App.vue index 2ceb880..ebcba2e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -15,6 +15,25 @@ async function greet() { greetMsg.value = await invoke("greet", { name: name.value }); } +const markdownRef = ref(null); +// 矫正 markdown 编辑器的高度 +function reloadEditorHeight() { + markdownRef.value.resetHeight(window.innerHeight) +} +// 矫正 markdown 编辑器的宽度 +function reloadEditorWidth() { + // 可以使用 splitRight.value.offsetWidth 也可以使用 100% 来调整 + const width = splitRight.value.offsetWidth + markdownRef.value.resetWidth("100%") +} +// 监听页面宽度和高度,调整 markdown 编辑器的高宽度 +const handleWindowResize = () => { + reloadEditorHeight() + reloadEditorWidth() +} +onMounted(() => window.addEventListener('resize', handleWindowResize)) +onUnmounted(() => window.removeEventListener('resize', handleWindowResize)) + // 动态控制页面左右布局 const split = ref(0.2) const hiddenSplitRight = ref("") @@ -22,6 +41,7 @@ const splitRight = useTemplateRef('splitRight') watch(split, (newSplit, oldSplit) => { // 当 split 小于某个值时,隐藏左边布局 if (newSplit < 0.05) { + split.value = 0.01 if (hiddenSplitRight.value == "") { hiddenSplitRight.value = "hidden" } @@ -31,31 +51,15 @@ watch(split, (newSplit, oldSplit) => { } } - // 动态调整 MarkdownEditor 的宽度 - const width = splitRight.value.offsetWidth - // 必须重新 reset 宽度。如果不执行 reset 的话,CodeMirror 内部编辑器不会自适应宽度 - // 可以使用 splitRight.value.offsetWidth 也可以使用 100% 来调整 - markdownRef.value.resetWidth("100%") + // 动态调整 MarkdownEditor 的宽度。如果不执行 reset 的话,CodeMirror 内部编辑器不会自适应宽度 + reloadEditorWidth() }) -// 当编辑器初始化完成后,矫正为当前窗口的高度 -function reloadEditorHeight() { - markdownRef.value.resetHeight(window.innerHeight) -} - -// 监听页面宽度和高度,调整 markdown 编辑器的高宽度 -const handleWindowResize = () => { - markdownRef.value.resetWidth("100%") - markdownRef.value.resetHeight(window.innerHeight) -} -onMounted(() => window.addEventListener('resize', handleWindowResize)) -onUnmounted(() => window.removeEventListener('resize', handleWindowResize)) - // 如果之前已经选过目录,那么直接加载该目录 -const rootPath = ref(localStorage.getItem('rootPathOfFolderTree') || "") +const rootPathOfFolderTree = ref(localStorage.getItem('rootPathOfFolderTree') || "") function changeRootPath(newRootPath) { localStorage.setItem('rootPathOfFolderTree', newRootPath) - rootPath.value = newRootPath + rootPathOfFolderTree.value = newRootPath } // 选择目录后赋值给 folderTreeData,然后再传递给 @@ -67,11 +71,12 @@ function showFileTree(treeData) { // 完成文件树渲染后,点击文件得到 currentFilePath // 读取 currentFilePath 的文件内容,填充到 MarkdownEditor 之中 const currentFilePath = ref(""); -const markdownRef = ref(null); +const loadingCurrentFile = ref(true); async function loadFileContent(fileNodeData) { console.log(fileNodeData) const filePath = fileNodeData.path currentFilePath.value = filePath + loadingCurrentFile.value = true await readFileContent(filePath) } async function readFileContent(filePath) { @@ -84,7 +89,11 @@ async function readFileContent(filePath) { } async function writeFileContent() { try { - await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode()) + // 如果触发当前事件,是因为刚点击并加载了某个文件内容,则不执行写文件操作 + if (!loadingCurrentFile.value) { + await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode()) + } + loadingCurrentFile.value = false } catch (err) { Message.error('文件更新失败:' + err); } @@ -96,7 +105,7 @@ async function writeFileContent() {