天天看點

Vue版富文本編輯器-vue-quill-editor-配置案例

vue-quill-editor-文檔

Vue版富文本編輯器-vue-quill-editor-配置案例

簡單使用

<!--
 * @Author: Jackie
 * @Date: 2021-10-27 11:26:42
 * @LastEditTime: 2021-12-06 17:32:51
 * @LastEditors: Jackie
 * @Description: 錄入系統公告
 * @version: 
-->
<template>
    <div class="lrxtgg">
      <quill-editor
            class="ql-editor"
            v-model="description"
            ref="myQuillEditor"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
          >
          </quill-editor>
    </div>
</template>
 
<script>
import { quillEditor } from 'vue-quill-editor' //調用編輯器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
 
export default {
  name: 'Lrxtgg',
  components: {
    quillEditor,
  },
  data() {
    return {
      description:'',
      editorOption: {
        // 編輯器配置
        placeholder: '請在這裡輸入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], //加粗,斜體,下劃線,删除線
            ['blockquote', 'code-block'], //引用,代碼塊
            [{ header: 1 }, { header: 2 }], // 标題,鍵值對的形式;1、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'], //清除字型樣式
            ['image'], //上傳圖檔、上傳視訊 ['image', 'video']
          ],
        },
      },
    }
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    /**=====================富文本編輯器======================*/
    // 準備編輯器
    onEditorReady(editor) {},
    onEditorBlur() {}, // 失去焦點事件
    onEditorFocus() {}, // 獲得焦點事件
    // 内容改變事件
    onEditorChange() {},
    // 轉碼
    escapeStringHTML(content) {
      content = content.replace(/</g, '<')
      content = content.replace(/>/g, '>')
      return content
    },
  },
  destroyed() {},
  activated() {},
  deactivated() {},
}
</script>
 
<style lang="less" scoped>
.lrxtgg {
  .quill-editor /deep/ .ql-container {
    min-height: 460px;
  }
}
</style>           

完整使用

配置富文本編輯器,字号、字型等顯示内容配置
<template>
  <div class="HomeDetails">
	<quill-editor
	  v-model="editContent"
	  ref="myQuillEditor"
	  :options="editorOption"
	  @blur="onEditorBlur($event)"
	  @focus="onEditorFocus($event)"
	  @change="onEditorChange($event)"
	>
	</quill-editor>
	<hr />
	<!-- 讀取展示 -->
	<div class="ql-container ql-snow">
	  <div class="ql-editor">
		{{ conversion }}
	  </div>
	</div>
  </div>
</template>
 
<script>
import { quillEditor } from "vue-quill-editor"; //調用編輯器
import * as Quill from "quill";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
// 設定字型大小
const fontSizeStyle = Quill.import("attributors/style/size"); // 引入這個後會把樣式寫在style上
fontSizeStyle.whitelist = [
  "12px",
  "14px",
  "16px",
  "18px",
  "20px",
  "24px",
  "28px",
  "32px",
  "36px",
];
Quill.register(fontSizeStyle, true);
// 設定字型樣式
const Font = Quill.import("attributors/style/font"); // 引入這個後會把樣式寫在style上
const fonts = ["SimSun", "SimHei", "Microsoft-YaHei", "KaiTi", "FangSong"];
Font.whitelist = fonts; // 将字型加入到白名單
Quill.register(Font, true);
// 工具欄
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜體 下劃線 删除線 -----['bold', 'italic', 'underline', 'strike']
  [{ color: [] }, { background: [] }], // 字型顔色、字型背景顔色-----[{ color: [] }, { background: [] }]
  [{ align: [] }], // 對齊方式-----[{ align: [] }]
  [{ size: fontSizeStyle.whitelist }], // 字型大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{ font: fonts }], // 字型種類-----[{ font: [] }]
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标題
  [{ direction: "ltl" }], // 文本方向-----[{'direction': 'rtl'}]
  [{ direction: "rtl" }], // 文本方向-----[{'direction': 'rtl'}]
  [{ indent: "-1" }, { indent: "+1" }], // 縮進-----[{ indent: '-1' }, { indent: '+1' }]
  [{ list: "ordered" }, { list: "bullet" }], // 有序、無序清單-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{ script: "sub" }, { script: "super" }], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  ["blockquote", "code-block"], // 引用  代碼塊-----['blockquote', 'code-block']
  ["clean"], // 清除文本格式-----['clean']
  ["link", "image", "video"], // 連結、圖檔、視訊-----['link', 'image', 'video']
];
 
export default {
  name: "HomeDetails",
  components: { quillEditor },
  data() {
    return {
      // 配置方式二
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions,
            // handlers: {
            //   image: this.handleImgUpload,
            // },
          },
        },
      },
      // 配置方式一
      // editorOption: {
      //   // 編輯器配置
      //   placeholder: "請在這裡輸入",
      //   modules: {
      //     toolbar: [
      //       ["bold", "italic", "underline", "strike"], //加粗,斜體,下劃線,删除線
      //       ["blockquote", "code-block"], //引用,代碼塊
      //       [{ header: 1 }, { header: 2 }], // 标題,鍵值對的形式;1、2表示字型大小
      //       [{ list: "ordered" }, { list: "bullet" }], //清單
      //       [{ script: "sub" }, { script: "super" }], // 上下标
      //       [{ indent: "-1" }, { indent: "+1" }], // 縮進
      //       // [{ direction: "ltl" }], // 文本方向
      //       [{ direction: "rtl" }], // 文本方向
      //       [{ size: ["small", false, "large", "huge"] }], // 字型大小
      //       [{ header: [1, 2, 3, 4, 5, 6, false] }], //幾級标題
      //       [{ color: [] }, { background: [] }], // 字型顔色,字型背景顔色
      //       [{ font: [] }], //字型
      //       [{ align: [] }], //對齊方式
      //       ["link"], // ["link", "image", "video"], //上傳圖檔、上傳視訊
      //       ["clean"], //清除字型樣式
      //     ],
      //   },
      // },
      editContent: "<h1><strong>Vue JackieDYH</strong></h1>", // 編輯器内容
      conversion: "", // 示範内容
    };
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.conversion = this.escapeStringHTML(this.editContent);
    });
  },
  methods: {
    // ========================富文本========================
    onEditorReady(editor) {
      // 準備編輯器
    },
    onEditorBlur() {}, // 失去焦點事件
    onEditorFocus() {}, // 獲得焦點事件
    // 内容改變事件
    onEditorChange() {
      this.conversion = this.escapeStringHTML(this.editContent);
    },
    // 轉碼
    escapeStringHTML(content) {
      content = content.replace(/</g, "<");
      content = content.replace(/>/g, ">");
      return content;
    },
    // ========================富文本END========================
  },
};
</script>
<style>
.ql-editor {
  min-height: 150px;
}
.ql-snow .ql-tooltip[data-mode="link"]::before {
  content: "請輸傳入連結接位址:";
}
.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: "請輸入視訊位址:";
}
.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: "标題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: "标題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: "标題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: "标題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: "标題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: "标題6";
}
 
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: "預設";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimSun"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimSun"]::before {
  content: "宋體";
}
.ql-snow
  .ql-picker.ql-font
  .ql-picker-label[data-value="Microsoft-YaHei"]::before,
.ql-snow
  .ql-picker.ql-font
  .ql-picker-item[data-value="Microsoft-YaHei"]::before {
  content: "微軟雅黑";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="KaiTi"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="KaiTi"]::before {
  content: "楷體";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="FangSong"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="FangSong"]::before {
  content: "仿宋";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimHei"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimHei"]::before {
  content: "黑體";
}
.ql-toolbar.ql-snow + .ql-container.ql-snow {
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}
.ql-toolbar.ql-snow {
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}
.ql-snow .ql-stroke,
.ql-snow .ql-picker {
  color: #999;
  stroke: #999;
}
.ql-snow .ql-fill,
.ql-snow .ql-stroke.ql-fill {
  fill: #999;
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="12px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="12px"]::before {
  content: "12px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="14px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="14px"]::before {
  content: "14px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="16px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="16px"]::before {
  content: "16px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="20px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="20px"]::before {
  content: "20px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="24px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="24px"]::before {
  content: "24px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="36px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="36px"]::before {
  content: "36px";
}
.ql-container {
  font-size: 14px;
}
</style>
 
<style lang="scss" scoped>
.HomeDetails {
  padding: 40px 0 0 80px;
  .quill-editor /deep/ .ql-container {
    min-height: 490px;
  }
  .ql-container {
    min-height: 500px;
  }
}
</style>           

繼續閱讀