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 }); 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 split = ref(0.2)
const hiddenSplitRight = ref("") const hiddenSplitRight = ref("")
@ -22,6 +41,7 @@ const splitRight = useTemplateRef('splitRight')
watch(split, (newSplit, oldSplit) => { watch(split, (newSplit, oldSplit) => {
// split // split
if (newSplit < 0.05) { if (newSplit < 0.05) {
split.value = 0.01
if (hiddenSplitRight.value == "") { if (hiddenSplitRight.value == "") {
hiddenSplitRight.value = "hidden" hiddenSplitRight.value = "hidden"
} }
@ -31,31 +51,15 @@ watch(split, (newSplit, oldSplit) => {
} }
} }
// MarkdownEditor // MarkdownEditor reset CodeMirror
const width = splitRight.value.offsetWidth reloadEditorWidth()
// reset reset CodeMirror
// 使 splitRight.value.offsetWidth 使 100%
markdownRef.value.resetWidth("100%")
}) })
//
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) { function changeRootPath(newRootPath) {
localStorage.setItem('rootPathOfFolderTree', newRootPath) localStorage.setItem('rootPathOfFolderTree', newRootPath)
rootPath.value = newRootPath rootPathOfFolderTree.value = newRootPath
} }
// <SelectFolder> folderTreeData <FolderTree> // <SelectFolder> folderTreeData <FolderTree>
@ -67,11 +71,12 @@ function showFileTree(treeData) {
// <FolderTree> currentFilePath // <FolderTree> currentFilePath
// currentFilePath MarkdownEditor // currentFilePath MarkdownEditor
const currentFilePath = ref(""); const currentFilePath = ref("");
const markdownRef = ref(null); const loadingCurrentFile = ref(true);
async function loadFileContent(fileNodeData) { async function loadFileContent(fileNodeData) {
console.log(fileNodeData) console.log(fileNodeData)
const filePath = fileNodeData.path const filePath = fileNodeData.path
currentFilePath.value = filePath currentFilePath.value = filePath
loadingCurrentFile.value = true
await readFileContent(filePath) await readFileContent(filePath)
} }
async function readFileContent(filePath) { async function readFileContent(filePath) {
@ -84,7 +89,11 @@ async function readFileContent(filePath) {
} }
async function writeFileContent() { async function writeFileContent() {
try { try {
//
if (!loadingCurrentFile.value) {
await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode()) await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode())
}
loadingCurrentFile.value = false
} catch (err) { } catch (err) {
Message.error('文件更新失败:' + err); Message.error('文件更新失败:' + err);
} }
@ -96,7 +105,7 @@ async function writeFileContent() {
<Split v-model="split"> <Split v-model="split">
<template #left> <template #left>
<div class="split-left"> <div class="split-left">
<SelectFolder :class="hiddenSplitRight" :rootPath="rootPath" @update:rootPath="changeRootPath" <SelectFolder :class="hiddenSplitRight" :rootPath="rootPathOfFolderTree" @update:rootPath="changeRootPath"
@folder-selected="showFileTree" /> @folder-selected="showFileTree" />
<FolderTree :class="hiddenSplitRight" :treeData="folderTreeData" :expandLevel="1" <FolderTree :class="hiddenSplitRight" :treeData="folderTreeData" :expandLevel="1"
:specifyFileSuffix="['md']" @file-selected="loadFileContent" /> :specifyFileSuffix="['md']" @file-selected="loadFileContent" />
@ -108,7 +117,7 @@ async function writeFileContent() {
<template #right> <template #right>
<div ref="splitRight" class="split-right"> <div ref="splitRight" class="split-right">
<MarkdownEditor ref="markdownRef" width="100%" height="100%" markdownCode="# hello tauri" <MarkdownEditor ref="markdownRef" width="100%" height="100%" markdownCode="# hello tauri"
:onload="reloadEditorHeight" @update:markdownCode="writeFileContent" /> :onload="reloadEditorHeight" :onfullscreenExit="handleWindowResize" @update:markdownCode="writeFileContent" />
</div> </div>
</template> </template>
</Split> </Split>