|
@@ -10,7 +10,6 @@ import tinymce from 'tinymce/tinymce'
|
|
|
import Editor from '@tinymce/tinymce-vue'
|
|
|
import 'tinymce/themes/silver/theme'
|
|
|
import 'tinymce/icons/default/icons'
|
|
|
-import axios from 'axios'
|
|
|
|
|
|
export default {
|
|
|
components: {
|
|
@@ -35,7 +34,6 @@ export default {
|
|
|
},
|
|
|
data() {
|
|
|
return {
|
|
|
- fileUpload: false,
|
|
|
inputValue: '',
|
|
|
edit: false,
|
|
|
init: {
|
|
@@ -61,8 +59,6 @@ export default {
|
|
|
watch: {
|
|
|
value: {
|
|
|
handler(newV, oldV) {
|
|
|
- // console.log(newV)
|
|
|
- this.getImages(newV)
|
|
|
this.inputValue = newV
|
|
|
},
|
|
|
immediate: true
|
|
@@ -71,93 +67,11 @@ export default {
|
|
|
mounted() {
|
|
|
tinymce.init({ selector: `#tinymce_${this.id}` })
|
|
|
},
|
|
|
- beforeDestroy() {
|
|
|
- // 销毁编辑器
|
|
|
- this.editor.remove()
|
|
|
- },
|
|
|
methods: {
|
|
|
changeText(e) { // 富文本内容改变
|
|
|
this.inputValue = e
|
|
|
this.$emit('update:value', this.inputValue)
|
|
|
this.$emit('change', this.inputValue)
|
|
|
- },
|
|
|
- async getImages(node) { // 获取图片
|
|
|
- const reg = /<img.*?(?:>|\/>)/gi
|
|
|
- const imgArr = node.match(reg) // 获取图片数组
|
|
|
- if (imgArr && imgArr.length > 0) {
|
|
|
- for (const value of imgArr) {
|
|
|
- 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')
|
|
|
- await this.beforeUpload(file)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- 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 })
|
|
|
- },
|
|
|
- async beforeUpload(file) { // 上传前
|
|
|
- try {
|
|
|
- const res = await this.updateFile(file)
|
|
|
- // const res = { data: { url: '//pt-starimg.didistatic.com/static/starimg/node/elczkYKpzb1598698408779.png' }}
|
|
|
- let url = ''
|
|
|
- if (res.data) {
|
|
|
- url = 'http:' + res.data.url
|
|
|
- }
|
|
|
- const img = `<img src="${url}" />`
|
|
|
- console.log(img)
|
|
|
- this.successUpload(img)
|
|
|
- } catch (error) {
|
|
|
- this.successUpload('')
|
|
|
- }
|
|
|
- return new Promise((resolve, reject) => { resolve() })
|
|
|
- },
|
|
|
- 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) { // 图片上传成功
|
|
|
- const reg = /<img.*?(?:>|\/>)/gi
|
|
|
- let newStr = ''
|
|
|
- this.inputValue.replace(reg, function(match, ...rest) {
|
|
|
- newStr = rest[1].substring(0, rest[0]) + file + rest[1].substring(rest[0] + match.length, rest[1].length)
|
|
|
- return rest[1]
|
|
|
- })
|
|
|
- this.$nextTick(() => {
|
|
|
- this.inputValue = newStr
|
|
|
- })
|
|
|
}
|
|
|
}
|
|
|
}
|