vue項(xiàng)目中引用富文本編輯器vue-quill-editor

1.安裝依賴npm install vue-quill-editor --save
2.使用
(1)引入

import { quillEditor } from "vue-quill-editor";
import { Quill } from "vue-quill-editor";
import "quill/dist/quill.core.css"; // import styles
import "quill/dist/quill.snow.css"; // for snow theme
import "quill/dist/quill.bubble.css"; //
import Upload from "@/components/plugins/upload";

//一定要引入這三個(gè)css,不然文本編輯器會(huì)變得奇奇怪怪的杨拐。
//可以在main.js中引入,也可以在具體使用的.vue文件中引入
//注冊(cè) Vue.use(quillEditor)或者components: {quillEditor}

(2)引用


quill-editor

圖片或者視頻的上傳也可以使用自定義組件

工具欄toolbar的顯示可以自定義
toolbarOptions
自定義image和video的顯示

vue-quill-editor Api使用

如果需要改變文本域部分的樣式热康,如下:

.ql-toolbar {
  &.ql-snow {
    .ql-formats {
      margin-right: 0;
    }
    .ql-picker {
      height: 31px;
    }
  }
}
.link {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  text-align: right;
  padding-right: 10px;
  i {
    font-size: 20px;
  }
}
.quill-editor {
  line-height: normal;
  height: 500px;
  margin-bottom: 50px;
  .ql-video {
    video {
      width: 100%;
    }
  }
}

完整的代碼如下

<template>
  <div>
    <quill-editor
      ref="quillEditor"
      v-model="content"
      @change="change"
      @blur="blur"
      :options="options"
    />
    <div v-show="false">
      <upload
        ref="uploadRef"
        :itemData="config"
        :imgUrl="'legalPersonFront'"
        @uploadSuccess="upload"
      ></upload>
    </div>
  </div>
</template>
<script>
import _ from "lodash";
import { quillEditor } from "vue-quill-editor";
import { Quill } from "vue-quill-editor";
import "quill/dist/quill.core.css"; // import styles
import "quill/dist/quill.snow.css"; // for snow theme
import "quill/dist/quill.bubble.css"; //
import Upload from "@/components/plugins/upload";
//引入修改過的video模塊并注冊(cè)
import Video from '../../../utils/video'
Quill.register(Video,true)
export default {
  name: "forms",
  components: {
    quillEditor,
    Upload
  },
  props: {
    value: {
      type: String,
      default() {
        return "";
      }
    },
    defaultValue: {
      type: String,
      default() {
        return "";
      }
    },
    props: {
      type: Object,
      default() {
        return {};
      }
    },
    toolbarOptions: {
      type: Array,
      default() {
        return [
          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
          ['blockquote', 'code-block'],
          [{ 'header': 1 }, { 'header': 2 }],               // custom button values
          [{ 'list': 'ordered'}, { 'list': 'bullet' }],
          [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
          [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
          [{ 'direction': 'rtl' }],                         // text direction
          [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
          [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
          [{ 'color': [] }, { 'background': [] }],          // dropdown with defaultfromtheme
          [{ 'font': [] }],
          [{ 'align': [] }],
          ['clean'],
          ['image'],
                //工具欄
        ];
      }
    }
  },
  data() {
    return {
      config: {
        label: "",
        type: "source",
        key: "corporateBankingCert",
        colspan: 0,
        width: 0,
        height: 0
      },
      content: "",
      isInit: false
      // toolbarOpt: [["image", "video"], [{ align: [] }]]
    };
  },
  watch: {
    content() {
      this.$emit("input", this.content);
    },
    value() {
      if (!this.isInit) {
        this.isInit = true;
        this.content = this.value;
      }
    }
  },
  computed: {
    options() {
      return {
        modules: {
          toolbar: {
            // container: ,  // 工具欄
            container: this.toolbarOptions,
            handlers: {
              image: value => {
                if (value) {
                  this.$refs.uploadRef.handleClick();
                } else {
                  this.quill.format("image", false);
                }
              },
              video: value => {
                if (value) {
                  this.$refs.uploadRef.handleClick();
                } else {
                  this.quill.format("video", false);
                }
              }
            }
          }
        },
        placeholder: "請(qǐng)輸入",
        theme: "snow", // or 'bubble',
        ...this.props
      };
    },
    quill() {
      const { quill } = this.$refs.quillEditor || {};
      return quill;
    }
  },
  created() {
    this.isInit = false;
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.setSelection(this.content.length);
      // this.blur();
      if (!_.get(this.props, "autoHeight")) {
        this.quill.root.style.height = `${_.get(this.props, "height", 300)}px`;
      } else if (_.get(this.props, "minHeight")) {
        this.quill.root.style.minHeight = `${_.get(
          this.props,
          "minHeight",
          300
        )}px`;
      }
    },
    //失去焦點(diǎn)
    blur() {
      this.quill.enable(false);
      this.$nextTick(() => {
        if (this.quill) {
          this.quill.enable(true);
          this.quill.blur();
        }
      });
    },
    //獲取焦點(diǎn)
    focus() {
      this.$nextTick(() => {
        if (this.quill) {
          this.quill.focus();
        }
      });
    },
    //光標(biāo)位置
    setSelection(index) {
      this.$nextTick(() => {
        let _index = index;
        if (this.quill) {
          if (!_.isNumber(index)) {
            _index = this.getSelectionIndex() + 1;
          }
          this.quill.setSelection(_index);
        }
      });
    },
    getSelectionIndex() {
      return _.get(this.quill.getSelection(), "index", 0);
    },
    change(option) {
      this.$emit("change", option);
    },
    upload($event,type) {
      let quill = this.quill;
      // 如果上傳成功
      if ($event) {
        // 獲取光標(biāo)所在位置
        let index = this.getSelectionIndex();
        // 根據(jù)返回類型,插入圖片/視頻,res為服務(wù)器返回的圖片鏈接地址
        if(type.includes("image")){
          quill.insertEmbed(index, "image", $event);
        }else if(type.includes("video")){
          quill.insertEmbed(index, "video", $event);
        }
        // 調(diào)整光標(biāo)到最后
        this.setSelection();
      } else {
        // 提示信息,需引入Message
        // Message.error('圖片插入失敗')
      }
    },
    addLink() {
      console.log("add");
    }
  }
};
</script>
<style lang="scss" scoped>
/deep/ .ql-toolbar {
  &.ql-snow {
    .ql-formats {
      margin-right: 0;
    }
    .ql-picker {
      height: 31px;
    }
  }
}
.link {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  text-align: right;
  padding-right: 10px;
  i {
    font-size: 20px;
  }
}
.quill-editor {
  line-height: normal;
  height: 500px;
  margin-bottom: 50px;
  .ql-video {
    video {
      width: 100%;
    }
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末酪耳,一起剝皮案震驚了整個(gè)濱河市抢呆,隨后出現(xiàn)的幾起案子煮嫌,更是在濱河造成了極大的恐慌,老刑警劉巖抱虐,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件昌阿,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡恳邀,警方通過查閱死者的電腦和手機(jī)懦冰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谣沸,“玉大人刷钢,你說我怎么就攤上這事∪楦剑” “怎么了内地?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)赋除。 經(jīng)常有香客問我阱缓,道長(zhǎng),這世上最難降的妖魔是什么举农? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任荆针,我火速辦了婚禮,結(jié)果婚禮上颁糟,老公的妹妹穿的比我還像新娘航背。我一直安慰自己,他們只是感情好棱貌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布沃粗。 她就那樣靜靜地躺著,像睡著了一般键畴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上突雪,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天起惕,我揣著相機(jī)與錄音,去河邊找鬼咏删。 笑死惹想,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的督函。 我是一名探鬼主播嘀粱,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼激挪,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了锋叨?” 一聲冷哼從身側(cè)響起垄分,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎娃磺,沒想到半個(gè)月后薄湿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡偷卧,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年豺瘤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片听诸。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡坐求,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晌梨,到底是詐尸還是另有隱情桥嗤,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布派任,位于F島的核電站砸逊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏掌逛。R本人自食惡果不足惜师逸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望豆混。 院中可真熱鬧篓像,春花似錦、人聲如沸皿伺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鸵鸥。三九已至奠滑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間妒穴,已是汗流浹背宋税。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留讼油,地道東北人杰赛。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像矮台,于是被迫代替她去往敵國(guó)和親乏屯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子根时,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容