国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

富文本quill的封裝使用(超鏈接、圖片、視頻、音頻)

這篇具有很好參考價(jià)值的文章主要介紹了富文本quill的封裝使用(超鏈接、圖片、視頻、音頻)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、添加依賴(lài)

npm install quill@1.3.6

二、封裝組件

1、editor.vue

<template>
  <div>
    <el-upload
      :action="uploadUrl"
      :before-upload="handleBeforeUpload"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: none"
      ref="upload"
      v-if="this.type == 'url'"
    >
    </el-upload>
    <el-dialog
      class="linkBox"
      :visible="uploadTypeShow"
      :modal="false"
      width="600px"
    >
      <el-form ref="linkForm" :model="linkForm" label-width="150px">
        <el-form-item label="請(qǐng)輸入鏈接地址:">
          <el-input v-model="linkForm" clearable> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeLinkBox">取 消</el-button>
        <el-button type="primary" @click="linkChange(linkForm)"
          >確 定</el-button
        >
      </span>
    </el-dialog>
    <div class="editor" ref="editor" :style="styles"></div>
  </div>
</template>

<script>
import Quill from "quill";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { getToken } from "@/utils/auth";
import "quill/dist/quill.bubble.css";
import Audio from "./audio";
import Video from "./video";
Quill.register(Audio, true);
Quill.register(Video, true);

export default {
  name: "Editor",
  props: {
    /* 編輯器的內(nèi)容 */
    value: {
      type: String,
      default: "",
    },
    /* 高度 */
    height: {
      type: Number,
      default: null,
    },
    /* 最小高度 */
    minHeight: {
      type: Number,
      default: null,
    },
    /* 只讀 */
    readOnly: {
      type: Boolean,
      default: false,
    },
    // 上傳文件大小限制(MB)
    fileSize: {
      type: Number,
      default: 5,
    },
    /* 類(lèi)型(base64格式、url格式) */
    type: {
      type: String,
      default: "url",
    },
  },
  data() {
    return {
      linkForm: "",
      uploadTypeShow: false,
      uploadType: "",
      uploadUrl: process.env.VUE_APP_BASE_API + "/system/oss/upload", // 上傳的圖片服務(wù)器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      Quill: null,
      currentValue: "",
      options: {
        theme: "snow",
        bounds: document.body,
        debug: "warn",
        modules: {
          // 工具欄配置
          toolbar: [
            ["bold", "italic", "underline", "strike"], // 加粗 斜體 下劃線 刪除線
            ["blockquote", "code-block"], // 引用  代碼塊
            [{ list: "ordered" }, { list: "bullet" }], // 有序、無(wú)序列表
            [{ indent: "-1" }, { indent: "+1" }], // 縮進(jìn)
            [{ size: ["small", false, "large", "huge"] }], // 字體大小
            [{ header: [1, 2, 3, 4, 5, 6, false] }], // 標(biāo)題
            [{ color: [] }, { background: [] }], // 字體顏色、字體背景顏色
            [{ align: [] }], // 對(duì)齊方式
            ["clean"], // 清除文本格式
            ["link", "image", "video", "audio"], // 鏈接、圖片、視頻、音頻
          ],
        },
        placeholder: "請(qǐng)輸入內(nèi)容",
        readOnly: this.readOnly,
      },
    };
  },
  computed: {
    styles() {
      let style = {};
      if (this.minHeight) {
        style.minHeight = `${this.minHeight}px`;
      }
      if (this.height) {
        style.height = `${this.height}px`;
      }
      return style;
    },
  },
  watch: {
    value: {
      handler(val) {
        if (val !== this.currentValue) {
          this.currentValue = val === null ? "" : val;
          if (this.Quill) {
            this.Quill.pasteHTML(this.currentValue);
          }
        }
      },
      immediate: true,
    },
  },
  mounted() {
    this.init();
  },
  beforeDestroy() {
    this.Quill = null;
  },
  methods: {
    init() {
      const editor = this.$refs.editor;
      this.Quill = new Quill(editor, this.options);
      // 如果設(shè)置了上傳地址則自定義圖片上傳事件
      if (this.type == "url") {
        let toolbar = this.Quill.getModule("toolbar");
        toolbar.addHandler("image", (value) => {
          this.uploadType = "image";
          if (value) {
            this.$refs.upload.$children[0].$refs.input.click();
          } else {
            this.quill.format("image", false);
          }
        });
        toolbar.addHandler("video", (value) => {
          this.uploadType = "video";
          if (value) {
            this.$refs.upload.$children[0].$refs.input.click();
          } else {
            this.quill.format("video", false);
          }
        });
        toolbar.addHandler("link", (value) => {
          this.uploadType = "link";
          if (value) {
            this.uploadTypeShow = true;
          } else {
            this.Quill.format("link", false);
          }
        });
        toolbar.addHandler("audio", (value) => {
          this.uploadType = "audio";
          if (value) {
            this.$refs.upload.$children[0].$refs.input.click();
          } else {
            this.Quill.format("audio", false);
          }
        });
      }
      this.Quill.pasteHTML(this.currentValue);
      this.Quill.on("text-change", (delta, oldDelta, source) => {
        const html = this.$refs.editor.children[0].innerHTML;
        const text = this.Quill.getText();
        const quill = this.Quill;
        this.currentValue = html;
        this.$emit("input", html);
        this.$emit("on-change", { html, text, quill });
      });
      this.Quill.on("text-change", (delta, oldDelta, source) => {
        this.$emit("on-text-change", delta, oldDelta, source);
      });
      this.Quill.on("selection-change", (range, oldRange, source) => {
        this.$emit("on-selection-change", range, oldRange, source);
      });
      this.Quill.on("editor-change", (eventName, ...args) => {
        this.$emit("on-editor-change", eventName, ...args);
      });
    },
    // 上傳前校檢格式和大小
    handleBeforeUpload(file) {
      console.log(file.type);
      // 校檢文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上傳文件大小不能超過(guò) ${this.fileSize} MB!`);
          return false;
        }
      }
      if (this.uploadType == "image" && file.type.includes("image") == false) {
        this.$message.error("請(qǐng)上傳圖片類(lèi)型文件");
        return false;
      }
      if (this.uploadType == "video" && file.type.includes("video") == false) {
        this.$message.error("請(qǐng)上傳視頻類(lèi)型文件");
        return false;
      }
      if (this.uploadType == "audio" && file.type.includes("audio") == false) {
        this.$message.error("請(qǐng)上傳音頻類(lèi)型文件");
        return false;
      }
      return true;
    },
    handleUploadSuccess(res, file) {
      console.log(res, file);
      // 獲取富文本組件實(shí)例
      let quill = this.Quill;
      // 如果上傳成功
      if (res.code == 200) {
        // 獲取光標(biāo)所在位置
        let length = quill.getSelection().index;
        if (this.uploadType != "image") {
          quill.insertEmbed(
            length + 1,
            this.uploadType,
            { url: res.data.url, name: file.name },
            "api"
          );
          quill.insertText(length + 2, "");
          quill.setSelection(length + 2);
        } else {
          // 插入圖片  res.url為服務(wù)器返回的圖片地址
          quill.insertEmbed(length, this.uploadType, res.data.url);
          // 調(diào)整光標(biāo)到最后
          quill.setSelection(length + 1);
        }
      } else {
        this.$message.error(res.msg);
      }
    },
    handleUploadError() {
      this.$message.error("插入失敗");
    },
    linkChange(href) {
      this.Quill.format("link", href);
      this.closeLinkBox();
    },
    closeLinkBox() {
      this.uploadTypeShow = false;
      this.linkForm = "";
    },
  },
};
</script>

<style>
.editor,
.ql-toolbar {
  white-space: pre-wrap !important;
  line-height: normal !important;
}
.quill-img {
  display: none;
}
.ql-snow .ql-tooltip {
  z-index: 10;
}

.ql-snow button.ql-audio {
  background-image: url("./audio.svg");
  background-repeat: no-repeat;
  background-position: center;
}

.ql-snow .ql-tooltip[data-mode="link"]::before {
  content: "請(qǐng)輸入鏈接地址:";
}
.ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  border-right: 0px;
  content: "保存";
  padding-right: 0px;
}

.ql-snow .ql-tooltip[data-mode="video"]::before {
  content: "請(qǐng)輸入視頻地址:";
}

.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
  content: "14px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  content: "10px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  content: "18px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  content: "32px";
}

.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
  content: "文本";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  content: "標(biāo)題1";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  content: "標(biāo)題2";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  content: "標(biāo)題3";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  content: "標(biāo)題4";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  content: "標(biāo)題5";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  content: "標(biāo)題6";
}

.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: "標(biāo)準(zhǔn)字體";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  content: "襯線字體";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  content: "等寬字體";
}
.ql-clipboard {
  position: fixed;
  display: none;
  left: 50%;
  top: 50%;
}
.linkBox .el-dialog__header {
  display: none;
}
.linkBox .el-dialog__body {
  padding-bottom: 0 !important;
}
.linkBox .el-dialog {
  margin-top: 40vh !important;
}
</style>

2、video.js

import Quill from 'quill'
 
const BlockEmbed = Quill.import('blots/block/embed')
// const Link = Quill.import('formats/link')
 
class Video extends BlockEmbed {
  static create(value) {
    console.log(value,'value')
    const node = super.create(value);
    node.setAttribute('src', value.url);
    node.setAttribute('controls', true);
    node.setAttribute('name', value.name);
    node.setAttribute('controlsList', 'nodownload');
    // nofullscreen 不要全屏按鈕
    // nodownload 不要下載按鈕
    // noremoteplayback 不要遠(yuǎn)程回放
    node.setAttribute('controlsList', 'nodownload noremoteplayback') // 控制刪除
    node.setAttribute('type', 'video')
    node.setAttribute('style', 'object-fit:fill;width: 100%;')
    node.setAttribute('preload', 'auto')    // auto - 當(dāng)頁(yè)面加載后載入整個(gè)視頻  meta - 當(dāng)頁(yè)面加載后只載入元數(shù)據(jù)  none - 當(dāng)頁(yè)面加載后不載入視頻
    node.setAttribute('playsinline', 'true')
    node.setAttribute('x-webkit-airplay', 'allow')
    // node.setAttribute('x5-video-player-type', 'h5') // 啟用H5播放器,是wechat安卓版特性
    node.setAttribute('x5-video-orientation', 'portraint') // 豎屏播放 聲明了h5才能使用  播放器支付的方向,landscape橫屏,portraint豎屏,默認(rèn)值為豎屏
    node.setAttribute('x5-playsinline', 'true') // 兼容安卓 不全屏播放
    node.setAttribute('x5-video-player-fullscreen', 'true')
    return node;
  }
  // 添加value獲取當(dāng)前的audio元素。拿到audio元素的屬性。
  static value(domNode) {
    console.log(domNode,'domNode')
    const value = {
      url: '',
      name: '',
    };
    // 這里要加判斷。不然會(huì)顯示undefined
    if (domNode.getAttribute('src')) {
      value.url = domNode.getAttribute('src');
      value.name = domNode.getAttribute('name');
    }
    return value;
  }
 
}
Video.blotName = 'video'
Video.className = 'ql-video'
Video.tagName = 'video'
 
export default Video

3、audio.js

import Quill from 'quill'
?
const BlockEmbed = Quill.import('blots/block/embed')
// const Link = Quill.import('formats/link')
?
class Audio extends BlockEmbed {
? static create(value) {
? ? console.log(value,'value')
? ? const node = super.create(value);
? ? node.setAttribute('src', value.url);
? ? node.setAttribute('controls', true);
? ? node.setAttribute('name', value.name);
? ? node.setAttribute('controlsList', 'nodownload');
? ? return node;
? }
? // 添加value獲取當(dāng)前的audio元素。拿到audio元素的屬性。
? static value(domNode) {
? ? console.log(domNode,'domNode')
? ? const value = {
? ? ? url: '',
? ? ? name: '',
? ? };
? ? // 這里要加判斷。不然會(huì)顯示undefined
? ? if (domNode.getAttribute('src')) {
? ? ? value.url = domNode.getAttribute('src');
? ? ? value.name = domNode.getAttribute('name');
? ? }
? ? return value;
? }
?
}
Audio.blotName = 'audio'
Audio.className = 'ql-audio'
Audio.tagName = 'audio'
?
export default Audio

4、audio.svg

<svg t="1664559512511" class="icon" viewBox="0 0 1024 1024" version="1.1"
? ? xmlns="http://www.w3.org/2000/svg" p-id="3622" width="18" height="18">
? ? <path d="M512 128c-212.1 0-384 171.9-384 384v360c0 13.3 10.7 24 24 24h184c35.3 0 64-28.7 64-64V624c0-35.3-28.7-64-64-64H200v-48c0-172.3 139.7-312 312-312s312 139.7 312 312v48H688c-35.3 0-64 28.7-64 64v208c0 35.3 28.7 64 64 64h184c13.3 0 24-10.7 24-24V512c0-212.1-171.9-384-384-384z" p-id="3623"></path>
</svg>

三、在需要的文件中引用組件

<editor  v-model="form.remark" :min-height="192" />

文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-775396.html

到了這里,關(guān)于富文本quill的封裝使用(超鏈接、圖片、視頻、音頻)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包