index.vue 4.79 KB
Newer Older
xd's avatar
xd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
<template>
  <div class="editor">
    <quill-editor
      v-model="content"
      ref="myQuillEditor"
      :options="options"
      :disabled="redact"
      @change="onEditorChange($event)"
    ></quill-editor>
    <van-uploader v-show="false" :afterRead="afterRead" :beforeRead="beforeRead">
      <van-button icon="photo" type="primary">上传图片</van-button>
    </van-uploader>
  </div>
  <!-- <div class="ql-editor" v-html="content"></div> -->
</template>

<script>
import "quill/dist/quill.core.css" //引入样式
import "quill/dist/quill.snow.css"//引入样式
import "quill/dist/quill.bubble.css"//引入样式
import { quillEditor } from "vue-quill-editor"//引入组件
export default {
  name: "quillEditors",
  props: {
    //是否可以编辑
    redact: {
      type: Boolean,
      default: false
    },
    //头部是否可显示
    title: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      content: ``,
      options: {
        placeholder: "请输入模板...",
        modules: {
          toolbar: {
            container: [
              ["bold", "italic", "underline", "strike"], //切换按钮  //bold 加粗 italic 斜 underline 下划线 strike删除线
              ["blockquote", "code-block"], //blockquote 引用 code-block 代码块
              ["link", "image"], //图片 link 链接  image图片
              [{ header: 1 }, { header: 2 }], // 标题,键值对的形式;1、2表示字体大小
              [{ list: "ordered" }, { list: "bullet" }], //排序 ordered 有序  bullet 无序
              // [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
              [{ script: "sub" }, { script: "super" }], // sub上标 / super下标
              [{ indent: "-1" }, { indent: "+1" }], // 减少缩进/缩进
              [{ direction: "rtl" }], // 文本方向
              [{ color: [] }, { background: [] }], // color 字体颜色 background 背景颜色  从主题默认下拉
              [{ align: [] }], //文本对齐方式
              [{ font: [] }], //字体格式
              [{ size: [] }] // 自定义下拉
            ],
            handlers: {
              image: value => {
                if (value) {
                  //禁止软键盘弹出
                  document.activeElement.blur();
                  // 触发input框选择图片文件
                  document.querySelector(".van-uploader input").click();
                } else {
                  this.quill.format("image", false);
                }
              }
            }
          }
        }
      }
    };
  },
  computed: {},
  watch: {
    //title变化时触发
    title(bool) {
      if (bool) {
        document.querySelector(".quill-editor .ql-toolbar").style.display =
          "none";
      }
    }
  },
  methods: {
    //图片上传成功
    afterRead(file) {
      console.log("后", file);
    },
    //图片上传前
    async beforeRead(file) {
      console.log("前", file, file.size / 1024);
      if (file.size == 0) {
        return false
      }
      if (!/^image\/(jpeg|png|jpg)$/.test(file.type)) {
        this.$toast("请上传 jpg,jpeg,png 格式图片");
        return false;
      }

      this.$toast.allowMultiple();
      let loading = this.$toast.loading({
        duration: 0,
        mask: false,
        forbidClick: true,
        message: "上传中..."
      });

      let fr = new FormData();
      fr.append("image", file);
      try {
        let quill = this.$refs.myQuillEditor.quill;
        let { msg, code, data } = await this.$post("img/V1/uploadImage", fr);
        if (code === 1) {
          console.log("上传成功", data[0]);
          // 获取光标所在位置
          let length = quill.getSelection().index;
          // 插入图片
          quill.insertEmbed(length, "image", data[0]);
          // 调整光标到最后
          quill.setSelection(length + 1);

          return true;
        } else {
          this.$toast(`${msg}`);
        }
      } catch (err) {
        console.log("错误", err);
        this.$toast("网络错误,请检查网络连接!");
      } finally {
        loading.clear();
      }
    },
    //内容改变触发
    onEditorChange() {
      this.$emit("ChangeText", this.content);
    }
  },
  mounted() {
    //初始化title
    this.title == true
      ? (document.querySelector(".quill-editor .ql-toolbar").style.display =
        "none")
      : "";
  },
  components: { quillEditor }
  // components: { editor }
};
</script>
<style lang='scss' >
.editor {
  height: 80%;
  position: relative;
  display: flex;
  flex-direction: column;
  .quill-editor {
    height: 100%;
  }
  .ql-toolbar {
    z-index: inherit;
    // position: absolute;
    // top: 0;
  }
  .ql-container {
    flex: 1;
    s,
    i,
    em {
      font-style: italic;
      text-decoration: line-through;
    }
  }
}
</style>