fix: stable editor id on SSR

This commit is contained in:
Tim
2025-08-07 21:28:17 +08:00
parent 9c59277023
commit 752d288e3b
2 changed files with 16 additions and 8 deletions

View File

@@ -8,7 +8,7 @@
</template>
<script>
import { ref, onMounted, watch, onUnmounted } from 'vue'
import { ref, onMounted, watch, onUnmounted, useId } from 'vue'
import { themeState } from '../utils/theme'
import {
createVditor,
@@ -27,7 +27,7 @@ export default {
},
editorId: {
type: String,
default: () => 'post-editor-' + Math.random().toString(36).slice(2)
default: ''
},
loading: {
type: Boolean,
@@ -41,6 +41,10 @@ export default {
setup(props, { emit }) {
const vditorInstance = ref(null)
let vditorRender = false
const editorId = ref(props.editorId)
if (!editorId.value) {
editorId.value = 'post-editor-' + useId()
}
const getEditorTheme = getEditorThemeUtil
const getPreviewTheme = getPreviewThemeUtil
@@ -92,7 +96,7 @@ export default {
onMounted(() => {
emit('update:loading', true)
vditorInstance.value = createVditor(props.editorId, {
vditorInstance.value = createVditor(editorId.value, {
placeholder: '请输入正文...',
input(value) {
emit('update:modelValue', value)
@@ -114,7 +118,7 @@ export default {
clearVditorStorage()
})
return {}
return { editorId }
}
}
</script>