|
@@ -1,7 +1,7 @@
|
|
<template>
|
|
<template>
|
|
<div>
|
|
<div>
|
|
<article :id="id">
|
|
<article :id="id">
|
|
- <editor v-model="inputValue" :class="'tinymce_'+id" :init="init" @input="changeText" />
|
|
|
|
|
|
+ <editor :id="'tinymce_'+id" ref="editor" v-model="inputValue" :init="init" @input="changeText" />
|
|
</article>
|
|
</article>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
@@ -10,6 +10,7 @@ import tinymce from 'tinymce/tinymce'
|
|
import Editor from '@tinymce/tinymce-vue'
|
|
import Editor from '@tinymce/tinymce-vue'
|
|
import 'tinymce/themes/silver/theme'
|
|
import 'tinymce/themes/silver/theme'
|
|
import 'tinymce/icons/default/icons'
|
|
import 'tinymce/icons/default/icons'
|
|
|
|
+import axios from 'axios'
|
|
|
|
|
|
export default {
|
|
export default {
|
|
components: {
|
|
components: {
|
|
@@ -34,6 +35,7 @@ export default {
|
|
},
|
|
},
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
|
|
+ fileUpload: false,
|
|
inputValue: '',
|
|
inputValue: '',
|
|
edit: false,
|
|
edit: false,
|
|
init: {
|
|
init: {
|
|
@@ -59,20 +61,90 @@ export default {
|
|
watch: {
|
|
watch: {
|
|
value: {
|
|
value: {
|
|
handler(newV, oldV) {
|
|
handler(newV, oldV) {
|
|
- console.log(newV)
|
|
|
|
|
|
+ // console.log(newV)
|
|
|
|
+ this.getImages(newV)
|
|
this.inputValue = newV
|
|
this.inputValue = newV
|
|
},
|
|
},
|
|
immediate: true
|
|
immediate: true
|
|
}
|
|
}
|
|
},
|
|
},
|
|
mounted() {
|
|
mounted() {
|
|
- tinymce.init({ selector: `.tinymce_${this.id}` })
|
|
|
|
|
|
+ tinymce.init({ selector: `#tinymce_${this.id}` })
|
|
|
|
+ },
|
|
|
|
+ beforeDestroy() {
|
|
|
|
+ // 销毁编辑器
|
|
|
|
+ this.editor.remove()
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
changeText(e) { // 富文本内容改变
|
|
changeText(e) { // 富文本内容改变
|
|
this.inputValue = e
|
|
this.inputValue = e
|
|
this.$emit('update:value', this.inputValue)
|
|
this.$emit('update:value', this.inputValue)
|
|
this.$emit('change', this.inputValue)
|
|
this.$emit('change', this.inputValue)
|
|
|
|
+ },
|
|
|
|
+ getImages(node) {
|
|
|
|
+ const reg = /<img.*?(?:>|\/>)/gi
|
|
|
|
+ const imgArr = node.match(reg) // 获取图片数组
|
|
|
|
+ if (imgArr && imgArr.length > 0) {
|
|
|
|
+ imgArr.forEach((value, key) => {
|
|
|
|
+ const regSrc = /<img.*?src="(.*?)".*?\/?>/i
|
|
|
|
+ const src = value.match(regSrc)
|
|
|
|
+ if (src[1].indexOf('base64') > 0) {
|
|
|
|
+ const file = this.dataURLtoFile(src[1], this.generateMixed(15) + '.png', 'image/png')
|
|
|
|
+ this.successUpload(`<img src="" />`, key)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ generateMixed(len) { // 图片随机名字生成
|
|
|
|
+ const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
|
|
|
+ let res = ''
|
|
|
|
+ for (let i = 0; i < len; i++) {
|
|
|
|
+ const id = Math.ceil(Math.random() * 35)
|
|
|
|
+ res += chars[id]
|
|
|
|
+ }
|
|
|
|
+ return res
|
|
|
|
+ },
|
|
|
|
+ dataURLtoFile(base64, filename, contentType) { // base64 -> file
|
|
|
|
+ var arr = base64.split(',') // 去掉base64格式图片的头部
|
|
|
|
+ var bstr = atob(arr[1]) // atob()方法将数据解码
|
|
|
|
+ var leng = bstr.length
|
|
|
|
+ var u8arr = new Uint8Array(leng)
|
|
|
|
+ while (leng--) {
|
|
|
|
+ u8arr[leng] = bstr.charCodeAt(leng) // 返回指定位置的字符的 Unicode 编码
|
|
|
|
+ }
|
|
|
|
+ return new File([u8arr], filename, { type: contentType })
|
|
|
|
+ },
|
|
|
|
+ updateFile(file) { // 上传图片
|
|
|
|
+ const param = new FormData() // 创建form对象
|
|
|
|
+ param.append('file', file)// 通过append向form对象添加数据
|
|
|
|
+ const config = {
|
|
|
|
+ headers: {
|
|
|
|
+ 'Content-Type': 'multipart/form-data'
|
|
|
|
+ },
|
|
|
|
+ withCredentials: false
|
|
|
|
+ } // 添加请求头
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ axios.post('http://star.xiaojukeji.com/upload/img.node', param, config)
|
|
|
|
+ .then(response => {
|
|
|
|
+ resolve(response)
|
|
|
|
+ }).catch(err => {
|
|
|
|
+ reject(err)
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ async successUpload(file, index) { // 图片上传成功
|
|
|
|
+ const reg = /<img.*?(?:>|\/>)/gi
|
|
|
|
+ let newStr = ''
|
|
|
|
+ this.inputValue.replace(reg, function(match, ...rest) {
|
|
|
|
+ // console.log(match, rest)
|
|
|
|
+ // console.log(rest[1].substring(0, rest[0]))
|
|
|
|
+ // console.log(rest[1].substring(rest[0] + match.length, rest[1].length))
|
|
|
|
+ // debugger
|
|
|
|
+ newStr = rest[1].substring(0, rest[0]) + rest[1].substring(rest[0] + match.length, rest[1].length)
|
|
|
|
+ return rest[1].substring(0, rest[0]) + rest[1].substring(rest[0] + match.length, rest[1].length)
|
|
|
|
+ })
|
|
|
|
+ // this.inputValue = newStr
|
|
|
|
+ tinymce.activeEditor.setContent(newStr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|