1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2025-04-06 03:57:53 +08:00

Merge c4dbb8897dc0ceae5dc40eedccc273f0c0c682b7 into 6858a9ad67483025f6a9432a926beb9327037be3

This commit is contained in:
show110695 2024-11-28 00:30:15 +00:00 committed by GitHub
commit a3b54addc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 98 deletions

View File

@ -15,6 +15,7 @@
"test:ci": "npm run lint && npm run test:unit"
},
"dependencies": {
"@toast-ui/vue-editor": "^3.2.2",
"axios": "0.18.1",
"clipboard": "2.0.4",
"codemirror": "5.45.0",
@ -34,7 +35,6 @@
"screenfull": "4.2.0",
"script-loader": "0.7.2",
"sortablejs": "1.8.4",
"tui-editor": "1.3.3",
"vue": "2.6.10",
"vue-count-to": "1.0.13",
"vue-router": "3.0.2",

View File

@ -1,117 +1,40 @@
<template>
<div :id="id" />
<div>
<editor ref="toastuiEditor" :options="editorOptions" height="500px" @load="onEditorLoad" @change="onEditorChange" />
</div>
</template>
<script>
// deps for editor
import 'codemirror/lib/codemirror.css' // codemirror
import 'tui-editor/dist/tui-editor.css' // editor ui
import 'tui-editor/dist/tui-editor-contents.css' // editor content
import '@toast-ui/editor/dist/toastui-editor.css'
import Editor from 'tui-editor'
import defaultOptions from './default-options'
import { Editor } from '@toast-ui/vue-editor'
export default {
name: 'MarkdownEditor',
props: {
value: {
type: String,
default: ''
},
id: {
type: String,
required: false,
default() {
return 'markdown-editor-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
}
},
options: {
type: Object,
default() {
return defaultOptions
}
},
mode: {
type: String,
default: 'markdown'
},
height: {
type: String,
required: false,
default: '300px'
},
language: {
type: String,
required: false,
default: 'en_US' // https://github.com/nhnent/tui.editor/tree/master/src/js/langs
}
components: {
editor: Editor
},
data() {
return {
editor: null
}
},
computed: {
editorOptions() {
const options = Object.assign({}, defaultOptions, this.options)
options.initialEditType = this.mode
options.height = this.height
options.language = this.language
return options
}
},
watch: {
value(newValue, preValue) {
if (newValue !== preValue && newValue !== this.editor.getValue()) {
this.editor.setValue(newValue)
editorText: '',
editorOptions: {
hideModeSwitch: true
}
},
language(val) {
this.destroyEditor()
this.initEditor()
},
height(newValue) {
this.editor.height(newValue)
},
mode(newValue) {
this.editor.changeMode(newValue)
}
},
mounted() {
this.initEditor()
console.log(this.$route.query.editorText)
},
destroyed() {
this.destroyEditor()
beforeDestroy() {
},
methods: {
initEditor() {
this.editor = new Editor({
el: document.getElementById(this.id),
...this.editorOptions
onEditorLoad(editor) {
editor.setMarkdown(this.$route.query.editorText, true)
},
onEditorChange() {
const query = JSON.parse(JSON.stringify(this.$route.query)) //
this.$nextTick(() => {
query.editorText = this.$refs.toastuiEditor.invoke('getMarkdown') //
this.$router.push({ path: this.$route.path, query }) //
})
if (this.value) {
this.editor.setValue(this.value)
}
this.editor.on('change', () => {
this.$emit('input', this.editor.getValue())
})
},
destroyEditor() {
if (!this.editor) return
this.editor.off('change')
this.editor.remove()
},
setValue(value) {
this.editor.setValue(value)
},
getValue() {
return this.editor.getValue()
},
setHtml(value) {
this.editor.setHtml(value)
},
getHtml() {
return this.editor.getHtml()
}
}
}