vue uses the vue quill editor rich text editor and uploads images to the server
- 2021-10-24 18:47:50
- OfStack
Directory 1. Preparation 2. Defining global component quill-editor1, defining template template 2, defining rich text option configuration 3. Related method 1, changing the original rich text editor upload picture binding method 2, uploading events
1. Preparations
Download vue-quill-editor
npm install vue-quill-editor --save Or yarn add vue-quill-editor
2. Define global components quill-editor
After downloading vue-quill-editor, we need to define a global component named quill-editor
1. Define the template template
<div>
<quill-editor
v-model="value"
ref="myQuillEditor"
:options="editorOption"
@change="onEditorChange"
>
</quill-editor>
<input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleChange" />
</div>
2. Define rich text option configuration
editorOption: {
toolbar: [
['bold', 'italic', 'underline'], // Bold, italic, underline, strikeout , 'strike'
['blockquote', 'code-block'], // Reference, code block
[{ 'header': 1 }, { 'header': 2 }], //H1 H2
[{ 'list': 'ordered' }, { 'list': 'bullet' }], // List
[{ 'script': 'sub' }, { 'script': 'super' }], // Superscript, subscript
[{ 'indent': '-1' }, { 'indent': '+1' }], // Indent
[{ 'direction': 'rtl' }], // Text editing direction, left to right or right to left
[{ 'size': ['small', false, 'large', 'huge'] }], // Text size
[{ 'header': [1, 2, 3, 4, 5, 6, false] }], // Selected text container height
[{ 'font': [] }], // Font style
[{ 'color': [] }, { 'background': [] }], // Color, background color
[{ 'align': [] }], // Alignment
['clean'], // Clear all styles of selected text
['link', 'image', 'video'] // Hyperlinks, pictures, video links
],
}
3. Relevant methods
1. Change the binding method of uploading pictures in the original rich text editor
mounted() {
if (this.$refs.myQuillEditor) {
//myQuillEditor Change to your own
this.$refs.myQuillEditor.quill.getModule("toolbar").addHandler("image", this.imgHandler);
}
},
methods:{
imgHandler(state) {
if (state) {
// Trigger input Click of , fileBtn Change to your own
this.$refs.fileBtn.click()
}
}
}
2. Upload events
handleChange(e) {
const files = Array.prototype.slice.call(e.target.files);
if (!files) {
return;
}
let formdata = new FormData();
formdata.append("file_name", files[0].name);
formdata.append("imgs", files[0]);
// Used axios Request
this.axios({
url: this.$store.state.baseUrl + 'upload/ueditorFile',
method: 'post',
data: formdata,
headers: {'client-identity': localStorage.getItem('session_id')}
}).then((res) => {
// This is set to null in order to contact uploading the same map to trigger change Events
this.$refs.fileBtn.value = "";
if (res.data.code == 200) {
let selection = this.$refs.myQuillEditor.quill.getSelection();
// Here is the returned picture address. If the interface is not an accessible address, you should splice it yourself
let imgUrl = this.$store.state.baseUrl + res.data.data;
imgUrl = imgUrl.replace(/\\/g,"/")
// Get quill Insert a picture with the cursor of
this.$refs.myQuillEditor.quill.insertEmbed(selection != null ? selection.index : 0, 'image', imgUrl)
// When the insertion is complete, move the cursor back 1 Bit
this.$refs.myQuillEditor.quill.setSelection(selection.index + 1);
}
})
}
Finally, use this global quill component in the parent component and pass the relevant parameters you need, and you are finished ~