feat: 支持切换编辑器主题

This commit is contained in:
Frankie Huang 2025-04-30 01:27:48 +08:00
parent 8895ddf9a4
commit 3d326fcfc0
2 changed files with 51 additions and 9 deletions

View File

@ -158,9 +158,9 @@ body {
} }
.split-trigger { .split-trigger {
width: 5px; width: 2px;
height: 100vh; height: 100vh;
background-color: gray; background-color: #e6e6e6;
} }
.split-trigger:hover { .split-trigger:hover {
@ -168,7 +168,7 @@ body {
} }
.split-right { .split-right {
margin-left: 5px; margin-left: 2px;
} }
.hidden { .hidden {

View File

@ -2,8 +2,9 @@
<Split v-model="split"> <Split v-model="split">
<template #left> <template #left>
<MdEditor ref="editorRef" v-model="editorState.text" :id="editorState.id" :theme="editorState.theme" <MdEditor ref="editorRef" v-model="editorState.text" :id="editorState.id" :theme="editorState.theme"
:style="{ height: editorState.height }" :toolbars="editorState.toolbars" :toolbarsExclude="['github']" :previewTheme="editorState.previewTheme" :style="{ height: editorState.height }"
@onSave="onSave" @onHtmlChanged="getPreviewedHTML"> :toolbars="editorState.toolbars" :toolbarsExclude="editorState.toolbarsExclude" @onSave="onSave"
@onHtmlChanged="getPreviewedHTML">
<template #defToolbars> <template #defToolbars>
<NormalToolbar :title="leftSidebarState == 'open' ? '隐藏文件树' : '显示文件树'" @onClick="toggleLeftSideBar"> <NormalToolbar :title="leftSidebarState == 'open' ? '隐藏文件树' : '显示文件树'" @onClick="toggleLeftSideBar">
<template #trigger> <template #trigger>
@ -22,6 +23,28 @@
<Icon class="md-editor-icon" type="md-cloud-upload" size="22" /> <Icon class="md-editor-icon" type="md-cloud-upload" size="22" />
</template> </template>
</NormalToolbar> </NormalToolbar>
<NormalToolbar title="切换编辑器主题" @onClick="toggleEditorTheme">
<template #trigger>
<Icon v-if="editorState.theme == 'dark'" type="ios-sunny-outline" size="22" />
<Icon v-else type="md-moon" size="22" />
</template>
</NormalToolbar>
<DropdownToolbar title="切换预览主题" :visible="editorState.previewThemeListVisible"
:onChange="showPreviewThemeList">
<template #overlay>
<div>
<List border size="small">
<ListItem v-for="theme in editorState.previewThemeList" :key="theme"
:class="theme == editorState.previewTheme ? 'md-editor-toolbar-active' : ''"
@click="togglePreviewTheme(theme)">{{ theme }}
</ListItem>
</List>
</div>
</template>
<template #trigger>
<Icon class="md-editor-icon" type="ios-color-filter-outline" :size="22" />
</template>
</DropdownToolbar>
</template> </template>
</MdEditor> </MdEditor>
</template> </template>
@ -29,7 +52,7 @@
<div class="split-trigger"></div> <div class="split-trigger"></div>
</template> </template>
<template #right> <template #right>
<div class="md-catalog"> <div class="split-right md-catalog">
<MdCatalog :class="hiddenSplitRight" :editorId="editorState.id" :theme="editorState.theme" /> <MdCatalog :class="hiddenSplitRight" :editorId="editorState.id" :theme="editorState.theme" />
</div> </div>
</template> </template>
@ -38,7 +61,7 @@
<script setup> <script setup>
import 'md-editor-v3/lib/style.css'; import 'md-editor-v3/lib/style.css';
import { MdEditor, MdCatalog, NormalToolbar } from 'md-editor-v3'; import { MdEditor, MdCatalog, NormalToolbar, DropdownToolbar } from 'md-editor-v3';
import { ref, reactive, watch } from "vue"; import { ref, reactive, watch } from "vue";
import { Message } from 'view-ui-plus' import { Message } from 'view-ui-plus'
@ -88,7 +111,10 @@ watch(split, (newSplit) => {
const editorRef = ref(null); const editorRef = ref(null);
const editorState = reactive({ const editorState = reactive({
id: 'markdown-editor', id: 'markdown-editor',
theme: 'light', theme: '',
previewTheme: 'default',
previewThemeList: ['default', 'github', 'vuepress', 'mk-cute', 'smart-blue', 'cyanosis'],
previewThemeListVisible: false,
height: '100vh', height: '100vh',
text: props.markdownCode, text: props.markdownCode,
toolbars: [ toolbars: [
@ -129,7 +155,10 @@ const editorState = reactive({
'htmlPreview', 'htmlPreview',
'catalog', 'catalog',
'github', 'github',
] 3,
4,
],
toolbarsExclude: ['fullscreen', 'github'], // github
}); });
// markdownCode text // markdownCode text
watch(() => props.markdownCode, (newCode) => { watch(() => props.markdownCode, (newCode) => {
@ -157,6 +186,19 @@ const toggleCatalog = () => {
} }
} }
const toggleEditorTheme = () => {
editorState.theme = editorState.theme == '' ? 'dark' : '';
}
const showPreviewThemeList = (visible) => {
editorState.previewThemeListVisible = visible;
}
const togglePreviewTheme = (theme) => {
editorState.previewTheme = theme;
editorState.previewThemeListVisible = false;
}
const onSave = (v, h) => { const onSave = (v, h) => {
// save v markdown code // save v markdown code
props.save(v); props.save(v);