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

@@ -18,7 +18,7 @@
</template> </template>
<script> <script>
import { ref, onMounted, computed, watch, onUnmounted } from 'vue' import { ref, onMounted, computed, watch, onUnmounted, useId } from 'vue'
import { themeState } from '../utils/theme' import { themeState } from '../utils/theme'
import { import {
createVditor, createVditor,
@@ -34,7 +34,7 @@ export default {
props: { props: {
editorId: { editorId: {
type: String, type: String,
default: () => 'editor-' + Math.random().toString(36).slice(2) default: ''
}, },
loading: { loading: {
type: Boolean, type: Boolean,
@@ -53,6 +53,10 @@ export default {
setup(props, { emit }) { setup(props, { emit }) {
const vditorInstance = ref(null) const vditorInstance = ref(null)
const text = ref('') const text = ref('')
const editorId = ref(props.editorId)
if (!editorId.value) {
editorId.value = 'editor-' + useId()
}
const getEditorTheme = getEditorThemeUtil const getEditorTheme = getEditorThemeUtil
const getPreviewTheme = getPreviewThemeUtil const getPreviewTheme = getPreviewThemeUtil
const applyTheme = () => { const applyTheme = () => {
@@ -75,7 +79,7 @@ export default {
} }
onMounted(() => { onMounted(() => {
vditorInstance.value = createVditor(props.editorId, { vditorInstance.value = createVditor(editorId.value, {
placeholder: '说点什么...', placeholder: '说点什么...',
preview: { preview: {
actions: [], actions: [],
@@ -129,7 +133,7 @@ export default {
} }
) )
return { submit, isDisabled } return { submit, isDisabled, editorId }
} }
} }
</script> </script>

View File

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