mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-09 03:27:32 +08:00
refactor: centralize Vditor init
This commit is contained in:
@@ -19,13 +19,14 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { ref, onMounted, computed, watch } from 'vue'
|
import { ref, onMounted, computed, watch } from 'vue'
|
||||||
import Vditor from 'vditor'
|
|
||||||
import { themeState } from '../utils/theme'
|
import { themeState } from '../utils/theme'
|
||||||
import 'vditor/dist/index.css'
|
import {
|
||||||
|
createVditor,
|
||||||
|
getEditorTheme as getEditorThemeUtil,
|
||||||
|
getPreviewTheme as getPreviewThemeUtil
|
||||||
|
} from '../utils/vditor'
|
||||||
import LoginOverlay from './LoginOverlay.vue'
|
import LoginOverlay from './LoginOverlay.vue'
|
||||||
import { isMobile } from '../utils/screen'
|
import { isMobile } from '../utils/screen'
|
||||||
import { API_BASE_URL } from '../main'
|
|
||||||
import { getToken } from '../utils/auth'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'CommentEditor',
|
name: 'CommentEditor',
|
||||||
@@ -52,10 +53,8 @@ export default {
|
|||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
const vditorInstance = ref(null)
|
const vditorInstance = ref(null)
|
||||||
const text = ref('')
|
const text = ref('')
|
||||||
const getEditorTheme = () =>
|
const getEditorTheme = getEditorThemeUtil
|
||||||
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
|
const getPreviewTheme = getPreviewThemeUtil
|
||||||
const getPreviewTheme = () =>
|
|
||||||
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'
|
|
||||||
const applyTheme = () => {
|
const applyTheme = () => {
|
||||||
if (vditorInstance.value) {
|
if (vditorInstance.value) {
|
||||||
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
|
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
|
||||||
@@ -73,61 +72,13 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
vditorInstance.value = new Vditor(props.editorId, {
|
vditorInstance.value = createVditor(props.editorId, {
|
||||||
placeholder: '说点什么...',
|
placeholder: '说点什么...',
|
||||||
height: isMobile.value ? 'auto' : 200,
|
height: isMobile.value ? 'auto' : 200,
|
||||||
theme: getEditorTheme(),
|
|
||||||
preview: {
|
preview: {
|
||||||
actions: [],
|
actions: [],
|
||||||
markdown: { toc: false },
|
markdown: { toc: false }
|
||||||
theme: { current: getPreviewTheme() }
|
|
||||||
},
|
},
|
||||||
cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor',
|
|
||||||
toolbar: [
|
|
||||||
'emoji',
|
|
||||||
'bold',
|
|
||||||
'italic',
|
|
||||||
'strike',
|
|
||||||
'|',
|
|
||||||
'list',
|
|
||||||
'line',
|
|
||||||
'quote',
|
|
||||||
'code',
|
|
||||||
'inline-code',
|
|
||||||
'|',
|
|
||||||
'undo',
|
|
||||||
'redo',
|
|
||||||
'|',
|
|
||||||
'link',
|
|
||||||
'upload'
|
|
||||||
],
|
|
||||||
upload: {
|
|
||||||
fieldName: 'file',
|
|
||||||
url: `${API_BASE_URL}/api/upload`,
|
|
||||||
accept: 'image/*,video/*',
|
|
||||||
multiple: false,
|
|
||||||
headers: { Authorization: `Bearer ${getToken()}` },
|
|
||||||
format(files, responseText) {
|
|
||||||
const res = JSON.parse(responseText)
|
|
||||||
if (res.code === 0) {
|
|
||||||
return JSON.stringify({
|
|
||||||
code: 0,
|
|
||||||
msg: '',
|
|
||||||
data: {
|
|
||||||
errFiles: [],
|
|
||||||
succMap: { [files[0].name]: res.data.url }
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return JSON.stringify({
|
|
||||||
code: 1,
|
|
||||||
msg: '上传失败',
|
|
||||||
data: { errFiles: files.map(f => f.name), succMap: {} }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
toolbarConfig: { pin: true },
|
|
||||||
input(value) {
|
input(value) {
|
||||||
text.value = value
|
text.value = value
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -10,10 +10,11 @@
|
|||||||
<script>
|
<script>
|
||||||
import { ref, onMounted, watch } from 'vue'
|
import { ref, onMounted, watch } from 'vue'
|
||||||
import { themeState } from '../utils/theme'
|
import { themeState } from '../utils/theme'
|
||||||
import Vditor from 'vditor'
|
import {
|
||||||
import 'vditor/dist/index.css'
|
createVditor,
|
||||||
import { API_BASE_URL } from '../main'
|
getEditorTheme as getEditorThemeUtil,
|
||||||
import { getToken } from '../utils/auth'
|
getPreviewTheme as getPreviewThemeUtil
|
||||||
|
} from '../utils/vditor'
|
||||||
import { hatch } from 'ldrs'
|
import { hatch } from 'ldrs'
|
||||||
hatch.register()
|
hatch.register()
|
||||||
|
|
||||||
@@ -41,10 +42,8 @@ export default {
|
|||||||
},
|
},
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
const vditorInstance = ref(null)
|
const vditorInstance = ref(null)
|
||||||
const getEditorTheme = () =>
|
const getEditorTheme = getEditorThemeUtil
|
||||||
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
|
const getPreviewTheme = getPreviewThemeUtil
|
||||||
const getPreviewTheme = () =>
|
|
||||||
document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'
|
|
||||||
const applyTheme = () => {
|
const applyTheme = () => {
|
||||||
if (vditorInstance.value) {
|
if (vditorInstance.value) {
|
||||||
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
|
vditorInstance.value.setTheme(getEditorTheme(), getPreviewTheme())
|
||||||
@@ -91,59 +90,8 @@ export default {
|
|||||||
)
|
)
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
vditorInstance.value = new Vditor(props.editorId, {
|
vditorInstance.value = createVditor(props.editorId, {
|
||||||
placeholder: '请输入正文...',
|
placeholder: '请输入正文...',
|
||||||
theme: getEditorTheme(),
|
|
||||||
preview: {
|
|
||||||
theme: { current: getPreviewTheme() },
|
|
||||||
},
|
|
||||||
cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor',
|
|
||||||
toolbar: [
|
|
||||||
'emoji',
|
|
||||||
'bold',
|
|
||||||
'italic',
|
|
||||||
'strike',
|
|
||||||
'|',
|
|
||||||
'list',
|
|
||||||
'line',
|
|
||||||
'quote',
|
|
||||||
'code',
|
|
||||||
'inline-code',
|
|
||||||
'|',
|
|
||||||
'undo',
|
|
||||||
'redo',
|
|
||||||
'|',
|
|
||||||
'link',
|
|
||||||
'upload'
|
|
||||||
],
|
|
||||||
upload: {
|
|
||||||
fieldName: 'file',
|
|
||||||
url: `${API_BASE_URL}/api/upload`,
|
|
||||||
accept: 'image/*,video/*',
|
|
||||||
multiple: false,
|
|
||||||
headers: { Authorization: `Bearer ${getToken()}` },
|
|
||||||
format(files, responseText) {
|
|
||||||
const res = JSON.parse(responseText)
|
|
||||||
if (res.code === 0) {
|
|
||||||
return JSON.stringify({
|
|
||||||
code: 0,
|
|
||||||
msg: '',
|
|
||||||
data: {
|
|
||||||
errFiles: [],
|
|
||||||
succMap: { [files[0].name]: res.data.url }
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return JSON.stringify({
|
|
||||||
code: 1,
|
|
||||||
msg: '上传失败',
|
|
||||||
data: { errFiles: files.map(f => f.name), succMap: {} }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
toolbarConfig: { pin: true },
|
|
||||||
cache: { enable: false },
|
|
||||||
input(value) {
|
input(value) {
|
||||||
emit('update:modelValue', value)
|
emit('update:modelValue', value)
|
||||||
},
|
},
|
||||||
|
|||||||
78
open-isle-cli/src/utils/vditor.js
Normal file
78
open-isle-cli/src/utils/vditor.js
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import Vditor from 'vditor'
|
||||||
|
import 'vditor/dist/index.css'
|
||||||
|
import { API_BASE_URL } from '../main'
|
||||||
|
import { getToken } from './auth'
|
||||||
|
|
||||||
|
export function getEditorTheme() {
|
||||||
|
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPreviewTheme() {
|
||||||
|
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createVditor(editorId, options = {}) {
|
||||||
|
const {
|
||||||
|
placeholder = '',
|
||||||
|
height,
|
||||||
|
preview = {},
|
||||||
|
input,
|
||||||
|
after
|
||||||
|
} = options
|
||||||
|
|
||||||
|
return new Vditor(editorId, {
|
||||||
|
placeholder,
|
||||||
|
height,
|
||||||
|
theme: getEditorTheme(),
|
||||||
|
preview: Object.assign({ theme: { current: getPreviewTheme() } }, preview),
|
||||||
|
cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor',
|
||||||
|
toolbar: [
|
||||||
|
'emoji',
|
||||||
|
'bold',
|
||||||
|
'italic',
|
||||||
|
'strike',
|
||||||
|
'|',
|
||||||
|
'list',
|
||||||
|
'line',
|
||||||
|
'quote',
|
||||||
|
'code',
|
||||||
|
'inline-code',
|
||||||
|
'|',
|
||||||
|
'undo',
|
||||||
|
'redo',
|
||||||
|
'|',
|
||||||
|
'link',
|
||||||
|
'upload'
|
||||||
|
],
|
||||||
|
upload: {
|
||||||
|
fieldName: 'file',
|
||||||
|
url: `${API_BASE_URL}/api/upload`,
|
||||||
|
accept: 'image/*,video/*',
|
||||||
|
multiple: false,
|
||||||
|
headers: { Authorization: `Bearer ${getToken()}` },
|
||||||
|
format(files, responseText) {
|
||||||
|
const res = JSON.parse(responseText)
|
||||||
|
if (res.code === 0) {
|
||||||
|
return JSON.stringify({
|
||||||
|
code: 0,
|
||||||
|
msg: '',
|
||||||
|
data: {
|
||||||
|
errFiles: [],
|
||||||
|
succMap: { [files[0].name]: res.data.url }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return JSON.stringify({
|
||||||
|
code: 1,
|
||||||
|
msg: '上传失败',
|
||||||
|
data: { errFiles: files.map(f => f.name), succMap: {} }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
toolbarConfig: { pin: true },
|
||||||
|
cache: { enable: false },
|
||||||
|
input,
|
||||||
|
after
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user