chore: 优化 components 组件,抽象简化代码

This commit is contained in:
Frankie Huang 2025-04-21 00:47:37 +08:00
parent ad813ffe75
commit d336938e78
6 changed files with 148 additions and 82 deletions

View File

@ -12,11 +12,8 @@
</template> </template>
<template #right> <template #right>
<div ref="splitRight" class="split-right"> <div ref="splitRight" class="split-right">
<MarkdownEditor ref="markdownRef" width="100%" height="100%" :autoInit="false" <MainEditor ref="mainEditor" v-model:markdownCode="markdownCode" :save="writeFileContent">
:markdownCode="markdownCode" :imageUpload="true" :imageUploadURL="imageUploadURLOfEditor" </MainEditor>
:imageUploadURLChange="changeImageUploadURL" :onload="reloadEditorHeight"
:onFullScreenExit="handleWindowResize" @update:markdownCode="newCode => markdownCode = newCode"
:appendToolbar="appendToolbar" />
</div> </div>
</template> </template>
</Split> </Split>
@ -24,8 +21,8 @@
</template> </template>
<script setup> <script setup>
import MarkdownEditor from './components/MarkdownEditor.vue'
import LeftSidebar from './components/LeftSidebar.vue' import LeftSidebar from './components/LeftSidebar.vue'
import MainEditor from './components/MainEditor.vue'
import { ref, watch, useTemplateRef, onMounted, onUnmounted } from "vue"; import { ref, watch, useTemplateRef, 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';
@ -57,7 +54,7 @@ watch(split, (newSplit) => {
} }
// MarkdownEditor reset CodeMirror // MarkdownEditor reset CodeMirror
reloadEditorWidth() mainEditor.value.resizeEditorWindow();
}) })
// //
@ -74,80 +71,21 @@ watch(currentFilePath, async (newFilePath) => {
await readFileContent(newFilePath); await readFileContent(newFilePath);
}) })
// Markdown const mainEditor = ref(null);
const markdownRef = ref(null);
const markdownCode = ref("# Hello Markdown"); const markdownCode = ref("# Hello Markdown");
const appendToolbar = [ watch(markdownCode, (newMarkdownCode) => {
{ console.log("code be updated");
name: "|", })
},
{
name: "save-markdown-code",
icon: "fa-save", // font-awesome icon. editormd.css
title: "保存当前内容",
shortcut: [
navigator.userAgent.toUpperCase().indexOf('MAC') >= 0 ? "Cmd-S" : "Ctrl-S"
],
handler: () => {
writeFileContent();
},
nofocus: true,
},
{
name: "publish",
icon: "fa-cloud-upload",
title: "发布文章",
handler: () => {
// TODO markdown HTML
Message.warning('抱歉,该功能暂未开放');
},
nofocus: true,
},
];
// localStorage imageUploadURL
const imageUploadURLOfEditor = ref(localStorage.getItem('imageUploadURLOfEditor') || "");
function changeImageUploadURL(newImageUploadURL) {
console.log("new imageUploadURL: ", newImageUploadURL);
localStorage.setItem('imageUploadURLOfEditor', newImageUploadURL);
}
async function initMarkdownEditor() {
//
if (currentFilePath.value.length > 0) {
try {
markdownCode.value = await readTextFile(currentFilePath.value);
Message.success('已读取文件内容,并加载到编辑器中:' + currentFilePath.value);
} catch (err) {
Message.error('文件读取失败:' + err);
}
}
markdownRef.value.initEditor()
}
// 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();
}
async function readFileContent(filePath) { async function readFileContent(filePath) {
try { try {
markdownRef.value.setMarkdownCode(await readTextFile(filePath)); mainEditor.value.setMarkdownCode(await readTextFile(filePath));
currentFilePath.value = filePath; currentFilePath.value = filePath;
Message.success('已读取文件内容,并加载到编辑器中:' + filePath); Message.success('已读取文件内容,并加载到编辑器中:' + filePath);
} catch (err) { } catch (err) {
Message.error('文件读取失败:' + err); Message.error('文件读取失败:' + err);
} }
} }
async function writeFileContent() { async function writeFileContent(markdownCode) {
// //
if (currentFilePath.value.length == 0) { if (currentFilePath.value.length == 0) {
Message.warning('当前编辑器暂未关联目标文件,请在左侧打开目录并选择一个文件') Message.warning('当前编辑器暂未关联目标文件,请在左侧打开目录并选择一个文件')
@ -158,20 +96,29 @@ async function writeFileContent() {
if (!exist) { if (!exist) {
throw new Error(currentFilePath.value + " 文件不存在."); throw new Error(currentFilePath.value + " 文件不存在.");
} }
await writeTextFile(currentFilePath.value, markdownRef.value.getMarkdownCode()) 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(() => { // markdown
window.addEventListener('resize', handleWindowResize); window.addEventListener('resize', mainEditor.value.resizeEditorWindow);
initMarkdownEditor(); //
}) if (currentFilePath.value.length > 0) {
try {
markdownCode.value = await readTextFile(currentFilePath.value);
Message.success('已读取文件内容,并加载到编辑器中:' + currentFilePath.value);
} catch (err) {
Message.error('文件读取失败:' + err);
}
}
mainEditor.value.initMarkdownEditor();
});
onUnmounted(() => { onUnmounted(() => {
window.removeEventListener('resize', handleWindowResize); window.removeEventListener('resize', mainEditor.value.resizeEditorWindow);
}) });
</script> </script>
<style scoped> <style scoped>

View File

@ -6,8 +6,8 @@
</template> </template>
<script setup> <script setup>
import SelectFolder from './SelectFolder.vue'; import SelectFolder from './UI/SelectFolder.vue';
import FolderTree from './FolderTree.vue'; import FolderTree from './UI/FolderTree.vue';
import { ref } from 'vue'; import { ref } from 'vue';
import { mkdir, remove, create, exists, rename } from '@tauri-apps/plugin-fs'; import { mkdir, remove, create, exists, rename } from '@tauri-apps/plugin-fs';
import { Message } from 'view-ui-plus' import { Message } from 'view-ui-plus'

View File

@ -0,0 +1,119 @@
<template>
<MarkdownEditor ref="markdownRef" width="100%" height="100%" :appendToolbar="appendToolbar"
:autoInit="false" :markdownCode="markdownCode" @update:markdownCode="updateMarkdownCode"
:imageUpload="true" :imageUploadURL="imageUploadURLOfEditor" :imageUploadURLChange="changeImageUploadURL"
:onload="reloadEditorHeight" :onFullScreenExit="resizeEditorWindow" />
</template>
<script setup>
import MarkdownEditor from './UI/MarkdownEditor.vue';
import { ref } from "vue";
import { Message } from 'view-ui-plus'
const props = defineProps({
markdownCode: {
type: String,
required: false,
default: ''
},
save: {
type: Function,
required: false,
default: (markdownCode) => {
console.log("save markdown code: " + markdownCode);
}
},
});
const emit = defineEmits([
'update:markdownCode',
]);
// Markdown
const markdownRef = ref(null);
const initMarkdownEditor = () => {
markdownRef.value.initEditor();
}
const getMarkdownCode = () => {
return markdownRef.value.getMarkdownCode();
}
const setMarkdownCode = (newMarkdownCode) => {
return markdownRef.value.setMarkdownCode(newMarkdownCode);
}
const updateMarkdownCode = (newMarkdownCode) => {
emit('update:markdownCode', newMarkdownCode); //
}
// localStorage imageUploadURL
const imageUploadURLOfEditor = ref(localStorage.getItem('imageUploadURLOfEditor') || "");
function changeImageUploadURL(newImageUploadURL) {
console.log("new imageUploadURL: ", newImageUploadURL);
localStorage.setItem('imageUploadURLOfEditor', newImageUploadURL);
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text)
.then(() => Message.success('复制成功'))
.catch(err => Message.error('复制失败:', err));
}
const appendToolbar = [
{
name: "|",
},
{
name: "save-markdown-code",
icon: "fa-save", // font-awesome icon. editormd.css
title: "保存当前内容",
shortcut: [
navigator.userAgent.toUpperCase().indexOf('MAC') >= 0 ? "Cmd-S" : "Ctrl-S"
],
handler: () => {
props.save(markdownRef.value.getMarkdownCode());
},
nofocus: true,
},
{
name: "get-html-code",
icon: "fa-copy", // font-awesome icon. editormd.css
title: "获取 HTML 代码(复制到粘贴板)",
handler: () => {
copyToClipboard(markdownRef.value.getPreviewedHTML())
},
nofocus: true,
},
{
name: "publish",
icon: "fa-cloud-upload",
title: "发布文章",
handler: () => {
// TODO markdown HTML
Message.warning('抱歉,该功能暂未开放');
},
nofocus: true,
},
];
// 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 resizeEditorWindow = () => {
reloadEditorHeight();
reloadEditorWidth();
}
//
defineExpose({
initMarkdownEditor,
getMarkdownCode,
setMarkdownCode,
resizeEditorWindow,
})
</script>