vue-quill-editor 使用技巧

以下内容你得使用过 可能对你才会有所帮助   ^!^

1、引用

import { vueQuillEditor, Quill } from 'vue-quill-editor';

import {ImageResize} from 'quill-image-resize-module';

Quill.register('modules/imageResize',ImageResize); // 图片缩放

2、option 配置

defaultEditorOption: {

        placeholder: '请输入点评内容',

        // formats: getFormatList({ list: false }),

        modules: {

          toolbar: `#toolbar${id}`, //Math.random() .toString(36) .substr(2);// 随机数

          // 图片缩放

          imageResize: {

            modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]

          },

          // 粘贴图片配置

          ImageExtend: this.pasteImgConfig(),

          clipboard: {

            matchers: [[Node.ELEMENT_NODE, this.handlePaste]]

          },

          keyboard: {

            bindings: {

              'list autofill': {

                key: ' ',

                shiftKey: null,

                collapsed: true,

                format: {

                  list: false,

                  'code-block': false,

                  blockquote: false,

                  header: false,

                  table: false

                },

                prefix: /^\s*?(\d+\.|-|\*|\[ ?\]|\[x\])$/,

                handler() {

                  return true;

                }

              }

            }

          }

        }

      }

3、pasteImgConfig 上传配置图片

pasteImgConfig() {

      return {

        loading: false,

        name: 'file',

        action: URL,

        headers: (xhr) => {

          // let merchantInfo = JSON.parse(localStorage.getItem('merchantInfo'));

          xhr.setRequestHeader('Auth', JSON.parse(localStorage.getItem('userInfo')).auth || '');

        },

        start: () => {

          this.imgUploading = true;

        },

        response: (res) => {

          this.imgUploading = false;

          if (res.code === 0) {

            return `//cdn.****.com/${res.data.url}`;

          } else {

            $tool.message('error', '上传图片失败', 1500);

          }

        },

        onerror: () => {

          this.imgUploading = false;

          $tool.message('error', '上传图片失败', 1500);

        }

      };

    },

4、上传图片的方法

    // 上传图片

    async handleFileChange(e) {

      this.imgUploading = true;

      try {

        const file = e.target.files[0];

        e.target.value = '';

        if (file.size > 1024 * 1024 * 2) {

          $tool.message('error', '图片大小不能大于2M', 1500);

          this.imgUploading = false;

          return;

        }

        const res = await uploadImg(file);

        if (res && res.url) {

          const position = this.$refs.myTextEditor.quill.selection.savedRange.index;

          this.$refs.myTextEditor.quill.insertEmbed(position, 'image', `//cdn.***.com/${res.url}`);

        } else {

          $tool.message('error', '上传图片失败', 1500);

        }

      } catch (err) {}

      this.imgUploading = false;

    }


Quill 相关文档:https://www.kancloud.cn/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容