nebula/components/ContentEditor.vue

170 lines
3.7 KiB
Vue
Raw Permalink Normal View History

2024-02-23 15:41:41 +08:00
<template>
<div>
<div id="contentEditor"></div>
</div>
</template>
<script>
import Vue from "vue";
import apiConfig from "@/config/api.config";
export default {
name: "contentEditor",
data() {
return {
contentEditor: null,
2024-02-26 14:56:56 +08:00
tokenURL: {}
2024-02-23 15:41:41 +08:00
}
},
props: {
initValue: {
type: String,
required: true
2024-02-26 14:56:56 +08:00
},
cacheId: {
type: String,
required: true
},
mode: {
type: String,
required: true
2024-02-23 15:41:41 +08:00
}
},
methods: {
_initEditor(data) {
//初始化
let _ts = this;
let toolbar = [
'emoji',
'headings',
'bold',
'italic',
'strike',
'link',
'|',
'list',
'ordered-list',
'check',
'outdent',
'indent',
'|',
'quote',
'line',
'code',
'inline-code',
'insert-before',
'insert-after',
'|',
2024-02-26 14:56:56 +08:00
'upload',
2024-02-23 15:41:41 +08:00
// 'record',
'table',
'|',
'undo',
'redo',
'|',
'edit-mode',
{
name: 'more',
toolbar: [
'fullscreen',
'both',
'preview',
'info'
],
}]
return new Vue.Vditor(data.id, {
toolbar,
2024-02-26 14:56:56 +08:00
mode: _ts.mode,
2024-02-23 15:41:41 +08:00
tab: '\t',
cdn: apiConfig.VDITOR,
cache: {
2024-02-26 14:56:56 +08:00
enable: !_ts.cacheId,
id: _ts.cacheId,
2024-02-23 15:41:41 +08:00
},
after() {
_ts.contentEditor.setValue(data.value ? data.value : '');
},
typewriterMode: true,
hint: {
emoji: Vue.emoji
},
preview: {
2024-02-26 14:56:56 +08:00
hljs: {
enable: true,
lineNumber: true,
style: 'github'
},
2024-02-23 15:41:41 +08:00
markdown: {
toc: true,
2024-02-26 14:56:56 +08:00
autoSpace: true
2024-02-23 15:41:41 +08:00
},
math: {
inlineDigit: true
},
delay: 500,
mode: data.mode,
/*url: `${process.env.Server}/api/console/markdown`,*/
parse: (element) => {
if (element.style.display === 'none') {
return false
}
// LazyLoadImage();
// Vue.Vditor.highlightRender({style: 'github'}, element, this.contentEditor);
},
theme: {
cdn: apiConfig.VDITOR_CSS
}
},
2024-02-26 14:56:56 +08:00
upload: {
max: 10 * 1024 * 1024,
url: _ts.tokenURL.URL,
linkToImgUrl: _ts.tokenURL.linkToImageURL,
token: _ts.tokenURL.token,
filename: name => name.replace(/[^(a-zA-Z0-9\u4e00-\u9fa5\.)]/g, '').
replace(/[\?\\/:|<>\*\[\]\(\)\$%\{\}@~]/g, '').
replace('/\\s/g', '')
},
2024-02-23 15:41:41 +08:00
height: data.height,
counter: 102400,
resize: {
enable: data.resize,
},
lang: this.$store.state.locale,
2024-02-26 14:56:56 +08:00
placeholder: data.placeholder,
2024-02-23 15:41:41 +08:00
})
},
2024-02-26 14:56:56 +08:00
contentValue() {
return this.contentEditor.getValue();
2024-02-23 15:41:41 +08:00
},
2024-02-26 14:56:56 +08:00
async contentHtml() {
return await this.contentEditor.getHTML()
2024-02-23 15:41:41 +08:00
}
},
2024-02-26 14:56:56 +08:00
async mounted() {
let _ts = this;
const responseData = await _ts.$axios.$get('/api/upload/token');
if (responseData) {
_ts.$store.commit('setUploadHeaders', responseData.uploadToken);
_ts.$set(_ts, 'tokenURL', {
token: responseData.uploadToken || '',
URL: responseData.uploadURL || '',
linkToImageURL: responseData.linkToImageURL || ''
})
}
_ts.contentEditor = _ts._initEditor({
2024-02-23 15:41:41 +08:00
id: 'contentEditor',
mode: 'both',
height: 480,
placeholder: '', //this.$t('inputContent', this.$store.state.locale)
resize: false,
2024-02-26 14:56:56 +08:00
value: _ts.initValue
2024-02-23 15:41:41 +08:00
});
}
}
</script>
2024-02-26 14:56:56 +08:00
<style lang="less">
@import "~vditor/src/assets/less/index.less";
2024-02-23 15:41:41 +08:00
</style>