1
1
<template >
2
2
<div class =" quillWrapper" >
3
3
<div ref =" quillContainer" :id =" id" ></div >
4
- <input v-if =" useImageUploader " @change =" customImageHanlder ? sendFileToParent($event) : handleUpload ($event)" ref =" fileInput" id =" file-upload" type =" file" style =" display :none ;" >
4
+ <input v-if =" useCustomImageHandler " @change =" emitImageInfo ($event)" ref =" fileInput" id =" file-upload" type =" file" style =" display :none ;" >
5
5
</div >
6
6
</template >
7
+
7
8
<script >
8
9
import Quill from ' quill'
9
- import axios from ' axios'
10
10
import ' quill/dist/quill.core.css'
11
11
import ' quill/dist/quill.snow.css'
12
- const CLOUDINARY_URL = ' https://api.cloudinary.com/v1_1/dkrcloudinary/upload'
13
- const UPLOAD_PRESET = ' ptvbj5nu'
14
12
15
13
var defaultToolbar = [
16
14
[' bold' , ' italic' , ' underline' , ' strike' ],
@@ -39,27 +37,25 @@ export default {
39
37
placeholder: String ,
40
38
disabled: Boolean ,
41
39
editorToolbar: Array ,
42
- functionProp: Function
40
+ useCustomImageHandler: {
41
+ type: Boolean ,
42
+ default: false
43
+ }
43
44
},
44
45
45
46
data () {
46
47
return {
47
48
quill: null ,
48
49
editor: null ,
49
50
toolbar: this .editorToolbar ? this .editorToolbar : defaultToolbar,
50
- useImageUploader: true ,
51
- customImageHanlder: true
52
51
}
53
52
},
54
53
55
54
mounted () {
56
55
this .initializeVue2Editor ()
57
56
this .handleUpdatedEditor ()
58
-
59
- },
60
- created () {
61
- this .functionProp
62
57
},
58
+
63
59
watch: {
64
60
value (val ) {
65
61
if (val != this .editor .innerHTML && ! this .quill .hasFocus ()) {
@@ -81,17 +77,13 @@ export default {
81
77
setQuillElement () {
82
78
this .quill = new Quill (this .$refs .quillContainer , {
83
79
modules: {
84
- toolbar: {
85
- container: this .toolbar , // Selector for toolbar container
86
- handlers: {
87
- ' image' : this .imageHandler
88
- }
89
- }
80
+ toolbar: this .toolbar
90
81
},
91
82
placeholder: this .placeholder ? this .placeholder : ' ' ,
92
83
theme: ' snow' ,
93
84
readOnly: this .disabled ? this .disabled : false ,
94
85
})
86
+ this .checkForCustomImageHandler ()
95
87
},
96
88
97
89
setEditorElement () {
@@ -102,85 +94,32 @@ export default {
102
94
this .editor .innerHTML = this .value || ' '
103
95
},
104
96
97
+ checkForCustomImageHandler () {
98
+ this .useCustomImageHandler === true ? this .setupCustomImageHandler () : ' '
99
+ },
100
+
101
+ setupCustomImageHandler () {
102
+ let toolbar = this .quill .getModule (' toolbar' );
103
+ toolbar .addHandler (' image' , this .customImageHandler );
104
+ },
105
+
105
106
handleUpdatedEditor () {
106
107
this .quill .on (' text-change' , () => {
107
108
this .$emit (' input' , this .editor .innerHTML )
108
109
})
109
110
},
110
- imageHandler (image , callback ) {
111
- this .$refs .fileInput .click ();
112
- // var range = this.quill.getSelection();
113
111
114
- // this.quill.insertEmbed(range.index, 'image', value, Quill.sources.USER);
115
-
116
- // var IMGUR_CLIENT_ID = 'bcab3ce060640ba';
117
- // var IMGUR_API_URL = 'https://api.imgur.com/3/image';
118
- console .log (image);
119
- // var range = this.quill.getSelection();
120
- // alert('fired!!!')
121
- // var value = prompt('What is the image URL');
122
- // this.quill.insertEmbed(range.index, 'image', value, Quill.sources.USER);
123
- },
124
- handleUpload ($event ) {
125
- console .log (' Using Method in VueEditor' );
126
- let vm = this
127
- var file = $event .target .files [0 ]
128
- var formData = new FormData ();
129
-
130
- formData .append (' file' , file)
131
- formData .append (' upload_preset' , UPLOAD_PRESET )
132
-
133
- var xhrObj = new XMLHttpRequest ();
134
- // xhrObj.upload.addEventListener("loadstart", loadStartFunction, false);
135
- // xhrObj.upload.addEventListener("progress", progressFunction, false);
136
- // xhrObj.upload.addEventListener("load", transferCompleteFunction, false);
137
- xhrObj .open (" post" , CLOUDINARY_URL );
138
- xhrObj .send (formData);
139
-
140
- xhrObj .onload = function () {
141
- if (this .status == 200 ) {
142
- let uploadResponse = JSON .parse (this .response );
143
- let url = uploadResponse .url
144
-
145
- let range = vm .quill .getSelection ();
146
- let cursorLocation = range .index
147
- vm .quill .insertEmbed (range .index , ' image' , url, Quill .sources .API );
148
- // var image = document.createElement('img');
149
- // image.src = resp.dataUrl;
150
- // document.body.appendChild(image);
151
- };
152
- };
112
+ customImageHandler (image , callback ) {
113
+ this .$refs .fileInput .click ();
153
114
},
154
- sendFileToParent ($event ) {
115
+
116
+ emitImageInfo ($event ) {
155
117
let file = $event .target .files [0 ]
156
118
let Editor = this .quill
157
119
let range = Editor .getSelection ();
158
120
let cursorLocation = range .index
159
121
this .$emit (' imageAdded' , file, Editor, cursorLocation)
160
- },
161
- // handleUpload($event) {
162
- // var file = $event.target.files[0]
163
- // var formData = new FormData();
164
- // console.log(file);
165
- // formData.append('file', file)
166
- // formData.append('upload_preset', UPLOAD_PRESET)
167
- // axios({
168
- // url: CLOUDINARY_URL,
169
- // method: 'POST',
170
- // headers:{
171
- // 'Content-Type': 'application/x-www-form-urlencoded'
172
- // },
173
- // data: formData
174
- // })
175
- // .then((result) => {
176
- // let url = result.data.url
177
- // var range = this.quill.getSelection();
178
- // this.quill.insertEmbed(range.index, 'image', url, Quill.sources.API);
179
- // })
180
- // .catch((err) => {
181
- // console.log(err);
182
- // })
183
- // }
122
+ }
184
123
}
185
124
}
186
125
</script >
0 commit comments