优化和复用代码
This commit is contained in:
parent
700f6980b9
commit
23cadb8da4
20
src/App.vue
20
src/App.vue
@ -22,7 +22,7 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import LeftSidebar from './components/LeftSidebar.vue'
|
import LeftSidebar from './components/LeftSidebar.vue'
|
||||||
import MainEditor from './components/MainEditor.vue'
|
import MainEditor from './components/MainEditor.vue'
|
||||||
import { ref, watch, useTemplateRef, 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 { readTextFile, writeTextFile, exists } from '@tauri-apps/plugin-fs';
|
import { readTextFile, writeTextFile, exists } from '@tauri-apps/plugin-fs';
|
||||||
import { Message } from 'view-ui-plus'
|
import { Message } from 'view-ui-plus'
|
||||||
@ -79,7 +79,12 @@ watch(markdownCode, (newMarkdownCode) => {
|
|||||||
})
|
})
|
||||||
async function readFileContent(filePath) {
|
async function readFileContent(filePath) {
|
||||||
try {
|
try {
|
||||||
mainEditor.value.setMarkdownCode(await readTextFile(filePath));
|
const fileContent = await readTextFile(filePath);
|
||||||
|
if (mainEditor.value.isEditorLoadingCompleted()) {
|
||||||
|
mainEditor.value.setMarkdownCode(fileContent);
|
||||||
|
} else {
|
||||||
|
markdownCode.value = fileContent;
|
||||||
|
}
|
||||||
currentFilePath.value = filePath;
|
currentFilePath.value = filePath;
|
||||||
Message.success('已读取文件内容,并加载到编辑器中:' + filePath);
|
Message.success('已读取文件内容,并加载到编辑器中:' + filePath);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -97,25 +102,22 @@ async function writeFileContent(markdownCode) {
|
|||||||
if (!exist) {
|
if (!exist) {
|
||||||
throw new Error(currentFilePath.value + " 文件不存在.");
|
throw new Error(currentFilePath.value + " 文件不存在.");
|
||||||
}
|
}
|
||||||
await writeTextFile(currentFilePath.value, markdownCode)
|
await writeTextFile(currentFilePath.value, markdownCode);
|
||||||
Message.success('文件更新成功:' + currentFilePath.value);
|
Message.success('文件更新成功:' + currentFilePath.value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Message.error('文件更新失败:' + err);
|
Message.error('文件更新失败:' + err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
// 使用 nextTick 确保页面组件已完成挂载和渲染
|
||||||
|
await nextTick();
|
||||||
// splitRight 的宽度为视口宽度右边百分比,再减去 splitTrigger 的宽度
|
// splitRight 的宽度为视口宽度右边百分比,再减去 splitTrigger 的宽度
|
||||||
splitRightWidth.value = window.innerWidth * (1 - split.value) - splitTrigger.value.offsetWidth;
|
splitRightWidth.value = window.innerWidth * (1 - split.value) - splitTrigger.value.offsetWidth;
|
||||||
// 监听页面宽度和高度,调整 markdown 编辑器的高宽度
|
// 监听页面宽度和高度,调整 markdown 编辑器的高宽度
|
||||||
window.addEventListener('resize', mainEditor.value.resizeEditorWindow);
|
window.addEventListener('resize', mainEditor.value.resizeEditorWindow);
|
||||||
// 打开上一次选择的文件的内容
|
// 打开上一次选择的文件的内容
|
||||||
if (currentFilePath.value.length > 0) {
|
if (currentFilePath.value.length > 0) {
|
||||||
try {
|
await readFileContent(currentFilePath.value);
|
||||||
markdownCode.value = await readTextFile(currentFilePath.value);
|
|
||||||
Message.success('已读取文件内容,并加载到编辑器中:' + currentFilePath.value);
|
|
||||||
} catch (err) {
|
|
||||||
Message.error('文件读取失败:' + err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mainEditor.value.initMarkdownEditor();
|
mainEditor.value.initMarkdownEditor();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -65,6 +65,9 @@ const markdownRef = ref(null);
|
|||||||
const initMarkdownEditor = () => {
|
const initMarkdownEditor = () => {
|
||||||
markdownRef.value.initEditor();
|
markdownRef.value.initEditor();
|
||||||
}
|
}
|
||||||
|
const isEditorLoadingCompleted = () => {
|
||||||
|
return markdownRef.value.isEditorLoadingCompleted();
|
||||||
|
}
|
||||||
const getMarkdownCode = () => {
|
const getMarkdownCode = () => {
|
||||||
return markdownRef.value.getMarkdownCode();
|
return markdownRef.value.getMarkdownCode();
|
||||||
}
|
}
|
||||||
@ -158,6 +161,7 @@ const resizeEditorWindow = () => {
|
|||||||
// 对父组件暴露数据或方法
|
// 对父组件暴露数据或方法
|
||||||
defineExpose({
|
defineExpose({
|
||||||
initMarkdownEditor,
|
initMarkdownEditor,
|
||||||
|
isEditorLoadingCompleted,
|
||||||
getMarkdownCode,
|
getMarkdownCode,
|
||||||
setMarkdownCode,
|
setMarkdownCode,
|
||||||
resizeEditorWindow,
|
resizeEditorWindow,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user