Skip to content

Commit a10cfcc

Browse files
committed
add tinymce upload demo
1 parent c8e632f commit a10cfcc

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<template>
2+
<div class="upload-container">
3+
<el-button icon='upload' :style="{background:color,borderColor:color}" @click=" dialogVisible=true" type="primary">上传图片
4+
</el-button>
5+
<el-dialog v-model="dialogVisible">
6+
<el-upload
7+
class="editor-slide-upload"
8+
action="https://httpbin.org/post"
9+
:multiple="true"
10+
:file-list="fileList"
11+
:show-file-list="true"
12+
list-type="picture-card"
13+
:on-remove="handleRemove"
14+
:on-success="handleSuccess"
15+
:before-upload="beforeUpload">
16+
<el-button size="small" type="primary">点击上传</el-button>
17+
</el-upload>
18+
<el-button @click="dialogVisible = false">取 消</el-button>
19+
<el-button type="primary" @click="handleSubmit">确 定</el-button>
20+
</el-dialog>
21+
</div>
22+
</template>
23+
<script>
24+
// import { getToken } from 'api/qiniu'
25+
26+
export default {
27+
name: 'editorSlideUpload',
28+
props: {
29+
color: {
30+
type: String,
31+
default: '#20a0ff'
32+
}
33+
},
34+
data() {
35+
return {
36+
dialogVisible: false,
37+
listObj: {},
38+
fileList: []
39+
}
40+
},
41+
methods: {
42+
checkAllSuccess() {
43+
return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
44+
},
45+
handleSubmit() {
46+
const arr = Object.keys(this.listObj).map(v => this.listObj[v])
47+
if (!this.checkAllSuccess()) {
48+
this.$message('请等待所有图片上传成功 或 出现了网络问题,请刷新页面重新上传!')
49+
return
50+
}
51+
console.log(arr)
52+
this.$emit('successCBK', arr)
53+
this.listObj = {}
54+
this.fileList = []
55+
this.dialogVisible = false
56+
},
57+
handleSuccess(response, file) {
58+
const uid = file.uid
59+
const objKeyArr = Object.keys(this.listObj)
60+
for (let i = 0, len = objKeyArr.length; i < len; i++) {
61+
if (this.listObj[objKeyArr[i]].uid === uid) {
62+
this.listObj[objKeyArr[i]].url = response.files.file
63+
this.listObj[objKeyArr[i]].hasSuccess = true
64+
return
65+
}
66+
}
67+
},
68+
handleRemove(file) {
69+
const uid = file.uid
70+
const objKeyArr = Object.keys(this.listObj)
71+
for (let i = 0, len = objKeyArr.length; i < len; i++) {
72+
if (this.listObj[objKeyArr[i]].uid === uid) {
73+
delete this.listObj[objKeyArr[i]]
74+
return
75+
}
76+
}
77+
},
78+
beforeUpload(file) {
79+
const _self = this
80+
const _URL = window.URL || window.webkitURL
81+
const fileName = file.uid
82+
this.listObj[fileName] = {}
83+
return new Promise((resolve, reject) => {
84+
const img = new Image()
85+
img.src = _URL.createObjectURL(file)
86+
img.onload = function() {
87+
_self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
88+
}
89+
resolve(true)
90+
})
91+
}
92+
}
93+
}
94+
</script>
95+
96+
<style rel="stylesheet/scss" lang="scss" scoped>
97+
.upload-container {
98+
.editor-slide-upload {
99+
margin-bottom: 20px;
100+
}
101+
}
102+
</style>

src/components/Tinymce/index.vue

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<template>
22
<div class='tinymce-container editor-container'>
33
<textarea class='tinymce-textarea' :id="id"></textarea>
4+
<div class="editor-custom-btn-container">
5+
<editorImage color="#20a0ff" class="editor-upload-btn" @successCBK="imageSuccessCBK"></editorImage>
6+
</div>
47
</div>
58
</template>
69

710
<script>
11+
import editorImage from './components/editorImage'
12+
813
export default {
914
name: 'tinymce',
15+
components: { editorImage },
1016
props: {
1117
id: {
1218
type: String,
@@ -143,6 +149,14 @@ export default {
143149
}
144150
})
145151
},
152+
methods: {
153+
imageSuccessCBK(arr) {
154+
const _this = this
155+
arr.forEach(v => {
156+
window.tinymce.get(_this.id).insertContent(`<img class="wscnph" src="${v.url}" >`)
157+
})
158+
}
159+
},
146160
destroyed() {
147161
window.tinymce.get(this.id).destroy()
148162
}
@@ -153,9 +167,17 @@ export default {
153167
.tinymce-container {
154168
position: relative
155169
}
156-
157170
.tinymce-textarea {
158171
visibility: hidden;
159172
z-index: -1;
160173
}
174+
.editor-custom-btn-container {
175+
position: absolute;
176+
right: 15px;
177+
/*z-index: 2005;*/
178+
top: 18px;
179+
}
180+
.editor-upload-btn {
181+
display: inline-block;
182+
}
161183
</style>

0 commit comments

Comments
 (0)