vue-quill-editor 부 텍스트 편집기 사용,사용자 정의 도구 모음,사용자 정의 그림 업로드

30177 단어 VUE
1.프로젝트 에 vue-quill-editor 설치
npm install vue-quill-editor --save

2.부 텍스트 편집기 설정 파일
quill-Config.js
//axios    ,       
import service from '../../utils/request';
/*           */
const uploadConfig = {
    action: 'oss/service/oss/upload', //            
    methods: 'POST', //            
    headers: {
        "Content-Type": "multipart/form-data"
    },
    token: '', //          token  ,    token    sessionStorage
    name: 'name', //            
    size: 10240, //            ,   Kb, 1M = 1024Kb
    accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon,image/jpg' //            
};

// toolbar        (      )
const toolOptions = [
    ['bold', 'italic', 'underline', 'strike'],
    ['blockquote', 'code-block'],
    [{
        'header': 1
    }, {
        'header': 2
    }],
    [{
        'list': 'ordered'
    }, {
        'list': 'bullet'
    }],
    [{
        'script': 'sub'
    }, {
        'script': 'super'
    }],
    [{
        'indent': '-1'
    }, {
        'indent': '+1'
    }],
    [{
        'direction': 'rtl'
    }],
    [{
        'size': ['small', false, 'large', 'huge']
    }],
    [{
        'header': [1, 2, 3, 4, 5, 6, false]
    }],
    [{
        'color': []
    }, {
        'background': []
    }],
    [{
        'font': []
    }],
    [{
        'align': []
    }],
    ['clean'],
    //['link', 'image', 'video']
    ['link', 'image']
];
const handlers = {
    image: function image() {
        var self = this;

        var fileInput = this.container.querySelector('input.ql-image[type=file]');
        if (fileInput === null) {
            fileInput = document.createElement('input');
            fileInput.setAttribute('type', 'file');

            //        
            if (uploadConfig.name) {
                fileInput.setAttribute('name', uploadConfig.name);
            }
            //           
            fileInput.setAttribute('accept', uploadConfig.accept);
            fileInput.classList.add('ql-image');
            //       
            fileInput.addEventListener('change', function () {

                //   formData
                let formData = new FormData();
                formData.append('file', fileInput.files[0]);
                fileInput.value = '';
                //     
                service.post('oss/service/oss/upload', formData, {
                    headers: {
                        "Content-Type": "multipart/form-data;"
                    }
                }).then(resp => {
                    var path = resp.data;
                    //var picPath = process.env.FTP_URL + path;
                    let length = self.quill.getSelection(true).index;
                    //     ,        ,img src       ,res.path             。  
                    //console.log(path)
                    self.quill.insertEmbed(length, 'image', path);
                    self.quill.setSelection(length + 1)
                    fileInput.value = ''
                });
            });
            this.container.appendChild(fileInput);
        }
        fileInput.click();
    }
};

export default {
    placeholder: '     ',
    theme: 'snow', //   
    modules: {
        toolbar: {
            container: toolOptions, //      
            handlers: handlers //     
        }
    }
};

utils/request.js
import axios from 'axios';

import {
    Message
} from 'element-ui'
//              service,           axios
const service = axios.create({
    // process.env.NODE_ENV === 'development'          
    // easy-mock    ,      
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    timeout: 15000
});


//    
service.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token_mark');
        config.headers.token_mark = token;
        return config;
    },
    error => {
        return Promise.reject();
    }
);

//    
service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            //   token       
            if (response.headers.token_mark) {
                sessionStorage.setItem('token_mark', response.headers.token_mark)
            }
            return response.data;
        } else {
            Message({
                showClose: true,
                duration: 0,
                message: '    ,      ',
                type: 'error'
            });
            Promise.reject();
        }
    },
    error => {
        Message({
            showClose: true,
            duration: 0,
            message: '    ,      ',
            type: 'error'
        });
        return Promise.reject();
    }
);
export default service;

3.부 텍스트 구성 요소 도입 및 프로젝트 에 설정
  • 설정 과 부 텍스트 구성 요소 도입
    //    
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import { quillEditor } from 'vue-quill-editor';
    import quillConfig from '../../../assets/js/quill-config';
    import Vue from 'vue';
    
  • 사용자 정의 설정 도입
    export default {
      data() {
        return {
          //     
          editorOption: quillConfig
      }
      }
    
  • 부 텍스트 구성 요소 도입
    //  , data()    
    components: {
        quillEditor
      }
    
  • 구성 요소 사용
    
    <quill-editor ref="myTextEditor" v-model="updateProductDetailModel.introduction" :options="editorOption">quill-editor>
    
  • 좋은 웹페이지 즐겨찾기