feat: 页面和编辑器高宽度自适应

This commit is contained in:
Frankie Huang 2025-04-08 02:04:18 +08:00
parent 75314c2aed
commit 95529f6111

View File

@ -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
}
// <SelectFolder> folderTreeData <FolderTree>
@ -67,11 +71,12 @@ function showFileTree(treeData) {
// <FolderTree> 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() {
<Split v-model="split">
<template #left>
<div class="split-left">
<SelectFolder :class="hiddenSplitRight" :rootPath="rootPath" @update:rootPath="changeRootPath"
<SelectFolder :class="hiddenSplitRight" :rootPath="rootPathOfFolderTree" @update:rootPath="changeRootPath"
@folder-selected="showFileTree" />
<FolderTree :class="hiddenSplitRight" :treeData="folderTreeData" :expandLevel="1"
:specifyFileSuffix="['md']" @file-selected="loadFileContent" />
@ -108,7 +117,7 @@ async function writeFileContent() {
<template #right>
<div ref="splitRight" class="split-right">
<MarkdownEditor ref="markdownRef" width="100%" height="100%" markdownCode="# hello tauri"
:onload="reloadEditorHeight" @update:markdownCode="writeFileContent" />
:onload="reloadEditorHeight" :onfullscreenExit="handleWindowResize" @update:markdownCode="writeFileContent" />
</div>
</template>
</Split>