feat: support Vditor theme switching

This commit is contained in:
Tim
2025-07-17 13:22:51 +08:00
parent 8761d828f9
commit 25bbb07297
2 changed files with 44 additions and 1 deletions

View File

@@ -17,6 +17,7 @@
<script> <script>
import { ref, onMounted, computed, watch } from 'vue' import { ref, onMounted, computed, watch } from 'vue'
import Vditor from 'vditor' import Vditor from 'vditor'
import { themeState } from '../utils/theme'
import 'vditor/dist/index.css' import 'vditor/dist/index.css'
export default { export default {
@@ -39,6 +40,15 @@ export default {
setup(props, { emit }) { setup(props, { emit }) {
const vditorInstance = ref(null) const vditorInstance = ref(null)
const text = ref('') const text = ref('')
const getEditorTheme = () =>
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
const getPreviewTheme = () =>
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'
const applyTheme = () => {
if (vditorInstance.value) {
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
}
}
const isDisabled = computed(() => props.loading || props.disabled || !text.value.trim()) const isDisabled = computed(() => props.loading || props.disabled || !text.value.trim())
@@ -54,9 +64,11 @@ export default {
vditorInstance.value = new Vditor(props.editorId, { vditorInstance.value = new Vditor(props.editorId, {
placeholder: '说点什么...', placeholder: '说点什么...',
height: 120, height: 120,
theme: getEditorTheme(),
preview: { preview: {
actions: [], actions: [],
markdown: { toc: false } markdown: { toc: false },
theme: { current: getPreviewTheme() }
}, },
toolbar: [ toolbar: [
'emoji', 'emoji',
@@ -81,6 +93,7 @@ export default {
text.value = value text.value = value
} }
}) })
applyTheme()
}) })
watch( watch(
@@ -107,6 +120,13 @@ export default {
} }
) )
watch(
() => themeState.mode,
() => {
applyTheme()
}
)
return { submit, isDisabled } return { submit, isDisabled }
} }
} }

View File

@@ -9,6 +9,7 @@
<script> <script>
import { ref, onMounted, watch } from 'vue' import { ref, onMounted, watch } from 'vue'
import { themeState } from '../utils/theme'
import Vditor from 'vditor' import Vditor from 'vditor'
import 'vditor/dist/index.css' import 'vditor/dist/index.css'
import { API_BASE_URL } from '../main' import { API_BASE_URL } from '../main'
@@ -40,6 +41,15 @@ export default {
}, },
setup(props, { emit }) { setup(props, { emit }) {
const vditorInstance = ref(null) const vditorInstance = ref(null)
const getEditorTheme = () =>
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
const getPreviewTheme = () =>
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'
const applyTheme = () => {
if (vditorInstance.value) {
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
}
}
watch( watch(
() => props.loading, () => props.loading,
@@ -73,10 +83,21 @@ export default {
} }
) )
watch(
() => themeState.mode,
() => {
applyTheme()
}
)
onMounted(() => { onMounted(() => {
vditorInstance.value = new Vditor(props.editorId, { vditorInstance.value = new Vditor(props.editorId, {
placeholder: '请输入正文...', placeholder: '请输入正文...',
height: 450, height: 450,
theme: getEditorTheme(),
preview: {
theme: { current: getPreviewTheme() },
},
toolbar: [ toolbar: [
'emoji', 'emoji',
'bold', 'bold',
@@ -131,8 +152,10 @@ export default {
if (props.loading || props.disabled) { if (props.loading || props.disabled) {
vditorInstance.value.disabled() vditorInstance.value.disabled()
} }
applyTheme()
} }
}) })
applyTheme()
}) })
return {} return {}