750aee67 by 蔡俊立

实现粘贴图片

1 parent 68c6b9ef
1 <template> 1 <template>
2 <dialogBox title="新增系统通知" @submitForm="submitForm" saveButton="保存" :isFullscreen="false" width="60%" 2 <dialogBox
3 @closeDialog="closeDialog" v-model="value"> 3 title="新增系统通知"
4 @submitForm="submitForm"
5 saveButton="保存"
6 :isFullscreen="false"
7 width="60%"
8 @closeDialog="closeDialog"
9 v-model="value"
10 >
4 <el-form ref="ruleForm" :model="ruleForm" label-width="100px" :rules="rules"> 11 <el-form ref="ruleForm" :model="ruleForm" label-width="100px" :rules="rules">
5 <el-row> 12 <el-row>
6 <el-col :span="12"> 13 <el-col :span="12">
...@@ -21,14 +28,14 @@ ...@@ -21,14 +28,14 @@
21 <el-form-item label="通知内容:" prop="noticeContent"> 28 <el-form-item label="通知内容:" prop="noticeContent">
22 <quill-editor 29 <quill-editor
23 v-model="ruleForm.noticeContent" 30 v-model="ruleForm.noticeContent"
31 class="editor ql-editor"
24 ref="myQuillEditor" 32 ref="myQuillEditor"
25 :options="editorOption" 33 :options="editorOption"
26 @blur="onEditorBlur($event)" 34 @blur="onEditorBlur($event)"
27 @focus="onEditorFocus($event)" 35 @focus="onEditorFocus($event)"
28 @change="onEditorChange($event)" 36 @change="onEditorChange($event)"
29 @ready="onEditorReady($event)" 37 @ready="onEditorReady($event)"
30 > 38 ></quill-editor>
31 </quill-editor>
32 <!-- <el-input v-model="ruleForm.noticeContent" type="textarea" :rows="16"></el-input> --> 39 <!-- <el-input v-model="ruleForm.noticeContent" type="textarea" :rows="16"></el-input> -->
33 </el-form-item> 40 </el-form-item>
34 </el-col> 41 </el-col>
...@@ -36,8 +43,14 @@ ...@@ -36,8 +43,14 @@
36 <el-row> 43 <el-row>
37 <el-col :span="8"> 44 <el-col :span="8">
38 <el-form-item label="附件:" prop="noticeFileUrl"> 45 <el-form-item label="附件:" prop="noticeFileUrl">
39 <el-upload action="" multiple :auto-upload="false" :limit="1" 46 <el-upload
40 :on-change="handleChange" :before-upload="beforeUpload"> 47 action
48 multiple
49 :auto-upload="false"
50 :limit="1"
51 :on-change="handleChange"
52 :before-upload="beforeUpload"
53 >
41 <el-button icon="el-icon-upload" type="primary">上传</el-button> 54 <el-button icon="el-icon-upload" type="primary">上传</el-button>
42 <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div> 55 <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div>
43 </el-upload> 56 </el-upload>
...@@ -45,10 +58,16 @@ ...@@ -45,10 +58,16 @@
45 </el-col> 58 </el-col>
46 </el-row> 59 </el-row>
47 <div v-show="false"> 60 <div v-show="false">
48 <el-upload action="" multiple :auto-upload="false" 61 <el-upload
49 :on-change="RichTexthandleChange" :before-upload="beforeUpload" class="richUpload"> 62 action
50 <el-button icon="el-icon-upload" type="primary">上传</el-button> 63 multiple
51 <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div> 64 :auto-upload="false"
65 :on-change="RichTexthandleChange"
66 :before-upload="beforeUpload"
67 class="richUpload"
68 >
69 <el-button icon="el-icon-upload" type="primary">上传</el-button>
70 <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div>
52 </el-upload> 71 </el-upload>
53 </div> 72 </div>
54 </el-form> 73 </el-form>
...@@ -56,35 +75,35 @@ ...@@ -56,35 +75,35 @@
56 </template> 75 </template>
57 76
58 <script> 77 <script>
59 import { addSysNotice } from "@/api/notice.js" 78 import { addSysNotice } from "@/api/notice.js";
60 import { upload } from "@/api/system.js" 79 import { upload } from "@/api/system.js";
61 import { quillEditor } from "vue-quill-editor"; 80 import { quillEditor } from "vue-quill-editor";
62 export default { 81 export default {
63 props: { 82 props: {
64 value: { type: Boolean, default: false }, 83 value: { type: Boolean, default: false }
65 }, 84 },
66 components: { quillEditor }, 85 components: { quillEditor },
67 data () { 86 data() {
68 return { 87 return {
69 ruleForm: { 88 ruleForm: {
70 noticeTitle: '', 89 noticeTitle: "",
71 noticeContent: '', 90 noticeContent: "",
72 noticeFileUrl: '', 91 noticeFileUrl: "",
73 noticeSource: '', 92 noticeSource: "",
74 noticeType: '1' 93 noticeType: "1"
75 }, 94 },
76 rules: { 95 rules: {
77 noticeTitle: [ 96 noticeTitle: [
78 { required: true, message: '请输入通知标题', trigger: 'blur' } 97 { required: true, message: "请输入通知标题", trigger: "blur" }
79 ], 98 ],
80 noticeContent: [ 99 noticeContent: [
81 { required: true, message: '请输入通知内容', trigger: 'blur' } 100 { required: true, message: "请输入通知内容", trigger: "blur" }
82 ], 101 ],
83 noticeSource: [ 102 noticeSource: [
84 { required: true, message: '请输入通知来源', trigger: 'blur' } 103 { required: true, message: "请输入通知来源", trigger: "blur" }
85 ] 104 ]
86 }, 105 },
87 // 富文本编辑器配置 106 // 富文本编辑器配置
88 editorOption: { 107 editorOption: {
89 theme: "snow", // or 'bubble' 108 theme: "snow", // or 'bubble'
90 modules: { 109 modules: {
...@@ -97,66 +116,76 @@ export default { ...@@ -97,66 +116,76 @@ export default {
97 [{ script: "sub" }, { script: "super" }], // 上标/下标 116 [{ script: "sub" }, { script: "super" }], // 上标/下标
98 [{ indent: "-1" }, { indent: "+1" }], // 缩进 117 [{ indent: "-1" }, { indent: "+1" }], // 缩进
99 [{ direction: "rtl" }], // 文本方向 118 [{ direction: "rtl" }], // 文本方向
100 [{ size: ['small', false, 'large', 'huge'] }], // 字体大小 119 [{ size: ["small", false, "large", "huge"] }], // 字体大小
101 [{ header: [1, 2, 3, 4, 5, 6] }], // 标题 120 [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
102 [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色 121 [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
103 // [{ font: ['songti'] }], // 字体种类 122 // [{ font: ['songti'] }], // 字体种类
104 [{ align: [] }], // 对齐方式 123 [{ align: [] }], // 对齐方式
105 ["clean"], // 清除文本格式 124 ["clean"], // 清除文本格式
106 ["image", "video"], // 链接、图片、视频 125 ["image", "video"] // 链接、图片、视频
107 ], 126 ],
108 handlers: { 127 handlers: {
109 image: function(value) { 128 image: function(value) {
110 if (value) { 129 if (value) {
111 // 用upload的点击事件代替文本编辑器的图片上传事件 130 // 用upload的点击事件代替文本编辑器的图片上传事件
112 document.querySelector('.richUpload input').click(); 131 document.querySelector(".richUpload input").click();
113 } else { 132 } else {
114 this.quill.format('image', false); 133 this.quill.format("image", false);
115 }
116 } 134 }
135 }
117 } 136 }
118 } 137 }
119 }, 138 },
120 placeholder: "请输入正文", 139 placeholder: "请输入正文"
121 }, 140 }
122 } 141 };
123 }, 142 },
124 mounted() { 143 watch: {
125 // 自定义粘贴图片功能 144 value(val) {
126 let quill = this.$refs.myQuillEditor.quill; 145 if (val) {
127 quill.root.addEventListener("paste",evt => { 146 let that = this
128 console.log(1111); 147 this.$nextTick(() => {
129 if (evt.clipboardData && evt.clipboardData.files &&evt.clipboardData.files.length) { 148 let quill = this.$refs.myQuillEditor.quill;
130 console.log(2222); 149 this.$forceUpdate();
131 evt.preventDefault(); 150 quill.root.addEventListener(
132 [].forEach.call(evt.clipboardData.files, file => { 151 "paste",
133 console.log(33333); 152 evt => {
134 if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) { 153 if (
135 return; 154 evt.clipboardData &&
136 } 155 evt.clipboardData.files &&
137 console.log(4444); 156 evt.clipboardData.files.length
138 this.RichTexthandleChange(File); 157 ) {
139 }); 158 evt.preventDefault();
140 } 159 [].forEach.call(evt.clipboardData.files, file => {
141 }, 160 if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
142 false 161 return;
143 ); 162 }
163 that.clipboardPictureChange(file);
164 });
165 }
166 },
167 false
168 );
169 });
170 }
171 }
144 }, 172 },
173 mounted() {},
145 methods: { 174 methods: {
146 submitForm () { 175 submitForm() {
147 let that = this; 176 let that = this;
148 that.$refs.ruleForm.validate(valid => { 177 that.$refs.ruleForm.validate(valid => {
149 if (valid) { 178 if (valid) {
150 addSysNotice(this.ruleForm).then(res => { 179 addSysNotice(this.ruleForm).then(res => {
151 if (res.code == 200) { 180 if (res.code == 200) {
152 this.$message.success('保存成功') 181 this.$message.success("保存成功");
153 this.$emit("input", false); 182 this.$emit("input", false);
154 this.resetRuleForm(); 183 this.resetRuleForm();
155 this.$parent.queryClick(); 184 this.$parent.queryClick();
156 } else { 185 } else {
157 this.$message.error(res.message) 186 this.$message.error(res.message);
158 } 187 }
159 }) 188 });
160 } else { 189 } else {
161 // console.log('error submit!!'); 190 // console.log('error submit!!');
162 return false; 191 return false;
...@@ -164,31 +193,38 @@ export default { ...@@ -164,31 +193,38 @@ export default {
164 }); 193 });
165 }, 194 },
166 //关闭窗口 195 //关闭窗口
167 closeDialog () { 196 closeDialog() {
168 this.$emit("input", false); 197 this.$emit("input", false);
169 this.resetRuleForm(); 198 this.resetRuleForm();
170 }, 199 },
171 // 200 //
172 resetRuleForm () { 201 resetRuleForm() {
173 this.$refs['ruleForm'].resetFields(); 202 this.$refs["ruleForm"].resetFields();
174 this.ruleForm.noticeType = '1' 203 this.ruleForm.noticeType = "1";
175 }, 204 },
176 beforeUpload (file) { 205 beforeUpload(file) {
177 return true 206 return true;
178 }, 207 },
179 //附件上传事件 208 //附件上传事件
180 async handleChange (file) { 209 async handleChange(file) {
181 var formdata = new FormData(); 210 var formdata = new FormData();
182 formdata.append("file", file.raw); 211 formdata.append("file", file.raw);
183 upload(formdata).then(res => { 212 upload(formdata).then(res => {
184 this.ruleForm.noticeFileUrl = res.message 213 this.ruleForm.noticeFileUrl = res.message;
185 }) 214 });
186 }, 215 },
187 //富文本图片上传事件 216 //富文本图片上传事件
188 async RichTexthandleChange (file) { 217 RichTexthandleChange(file) {
218 this.uploadPicture(file.raw)
219 },
220 //图片粘贴事件
221 clipboardPictureChange(file) {
222 this.uploadPicture(file)
223 },
224 uploadPicture(file){
189 let that = this; 225 let that = this;
190 var formdata = new FormData(); 226 var formdata = new FormData();
191 formdata.append("file", file.raw); 227 formdata.append("file", file);
192 upload(formdata).then(res => { 228 upload(formdata).then(res => {
193 //editor对象 229 //editor对象
194 const quill = that.$refs.myQuillEditor.quill; 230 const quill = that.$refs.myQuillEditor.quill;
...@@ -196,11 +232,11 @@ export default { ...@@ -196,11 +232,11 @@ export default {
196 // 获取光标所在位置 232 // 获取光标所在位置
197 const length = quill.selection.savedRange.index; 233 const length = quill.selection.savedRange.index;
198 // 插入图片 234 // 插入图片
199 quill.insertEmbed(length, 'image', res.message); 235 quill.insertEmbed(length, "image", res.message);
200 // 调整光标到最后 236 // 调整光标到最后
201 quill.setSelection(length + 1); 237 quill.setSelection(length + 1);
202 //that.quillUpdateImg = false; 238 //that.quillUpdateImg = false;
203 }) 239 });
204 }, 240 },
205 onSubmit() { 241 onSubmit() {
206 //console.log("submit!"); 242 //console.log("submit!");
...@@ -221,9 +257,9 @@ export default { ...@@ -221,9 +257,9 @@ export default {
221 onEditorChange({ quill, html, text }) { 257 onEditorChange({ quill, html, text }) {
222 //console.log("editor change!", quill, html, text); 258 //console.log("editor change!", quill, html, text);
223 //this.content = html; 259 //this.content = html;
224 }, 260 }
225 } 261 }
226 } 262 };
227 </script> 263 </script>
228 <style scoped lang="scss"> 264 <style scoped lang="scss">
229 // .ql-editor{ 265 // .ql-editor{
...@@ -232,5 +268,5 @@ export default { ...@@ -232,5 +268,5 @@ export default {
232 /deep/.ql-editor { 268 /deep/.ql-editor {
233 padding: 0px; 269 padding: 0px;
234 height: 500px; 270 height: 500px;
235 } 271 }
236 </style> 272 </style>
...\ No newline at end of file ...\ No newline at end of file
......