feat: 实现保存,显示隐藏目录等功能

This commit is contained in:
Frankie Huang 2025-04-29 01:47:28 +08:00
parent f514f885df
commit b14151517f
3 changed files with 49 additions and 4 deletions

View File

@ -60,6 +60,7 @@
1. `npm install md-editor-v3 --save`
2. 在 src/components/MainEditor.vue 组件里使用 md-editor-v3 组件
3. 实现保存功能;自定义工具栏实现显示/隐藏目录以及发布等功能
### 调试应用

View File

@ -36,7 +36,7 @@ async function greet() {
}
// 使 Split
const split = ref(0.2);
const split = ref(0.15);
const splitTrigger = useTemplateRef('splitTrigger');
const splitRight = useTemplateRef('splitRight');
const splitRightWidth = ref(0);

View File

@ -2,7 +2,20 @@
<Split v-model="split">
<template #left>
<MdEditor ref="editorRef" v-model="editorState.text" :id="editorState.id" :theme="editorState.theme"
:style="{ height: editorState.height }" :toolbars="editorState.toolbars" :toolbarsExclude="['github']">
:style="{ height: editorState.height }" :toolbars="editorState.toolbars"
:toolbarsExclude="['github', 'catalog']" @onSave="onSave" @onHtmlChanged="getPreviewedHTML">
<template #defToolbars>
<NormalToolbar title="显示/隐藏目录" @onClick="toggleCatalog">
<template #trigger>
<Icon class="md-editor-icon" type="ios-list-box-outline" size="22" />
</template>
</NormalToolbar>
<NormalToolbar title="发布文章" @onClick="publish">
<template #trigger>
<Icon class="md-editor-icon" type="md-cloud-upload" size="22" />
</template>
</NormalToolbar>
</template>
</MdEditor>
</template>
<template #trigger>
@ -18,7 +31,7 @@
<script setup>
import 'md-editor-v3/lib/style.css';
import { MdEditor, MdCatalog } from 'md-editor-v3';
import { MdEditor, MdCatalog, NormalToolbar } from 'md-editor-v3';
import { ref, reactive, watch } from "vue";
import { Message } from 'view-ui-plus'
@ -42,7 +55,8 @@ const emit = defineEmits([
]);
//
const split = ref(0.8);
const split = ref(1);
const defaultCatalogSplit = 0.2; // 8:2
const hiddenSplitRight = split.value == 1 ? ref('hidden') : ref('');
watch(split, (newSplit) => {
if (newSplit > 0.95) {
@ -65,6 +79,8 @@ const editorState = reactive({
height: '100vh',
text: props.markdownCode,
toolbars: [
0,
'-',
'bold',
'underline',
'italic',
@ -89,6 +105,7 @@ const editorState = reactive({
'revoke',
'next',
'save',
1,
'=',
'prettier',
'pageFullscreen',
@ -110,6 +127,33 @@ watch(() => editorState.text, (newCode) => {
emit('update:markdownCode', newCode);
})
const onSave = (v, h) => {
// save v markdown code
props.save(v);
// 使 h html
// h.then((html) => {
// console.log(html);
// });
};
const getPreviewedHTML = (html) => {
console.log(html);
}
const toggleCatalog = () => {
if (split.value < 1) {
split.value = 1;
} else {
split.value = 1 - defaultCatalogSplit;
}
}
const publish = () => {
// TODO markdown HTML
Message.warning('抱歉,该功能暂未开放');
}
//
defineExpose({})
</script>