编辑模式代码
⾃定义编辑器组件代码
<template>
<div class="baseDiv">
<div class="editor" ref="editor" :></div>
<div class="rightMenu" v-if="rightMenuShow">
<div class="lake-sidebar-title">
<span>⼤纲</span>
<button
@click="sidebarClick"
type="button"
aria-label="Close"
class="el-dialog__headerbtn"
>
<i class="el-dialog__close el-icon el-icon-close"></i>
</button>
</div>
<div
class="lake-sidebar-content lake-scrollable scroll-y"
>
<div class="lake-synopsis">
<div class="larkui-synopsis-wrap">
<ul class="larkui-synopsis">
<li
class="larkui-synopsis-item larkui-synopsis-item-active"
v-for="(item, i) in outlineList"
>
<span
class="larkui-synopsis-item-link larkui-synopsis-item-link-1" :class="
item.tagName == 'H3'
? 'h3Padding'
:
item.tagName == 'H4'
? 'h4Padding'
: ''
"
>
<a :href="item.id" :title="item.title">
<span>{{ item.title }}</span>
</a>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
// 富⽂本组件
import Quill from "quill";
import"quill/ss";
import"quill/dist/quill.snow.css";
import"quill/dist/quill.bubble.css";
export default {
name: "Editors",
props: {
/* 编辑器的内容 */
value: {
type: String,
default: "",
},
/* ⾼度 */
height: {
type: Number,
default: null,
},
outline: {
type: String,
default: ""
}
},
data(){
const that = this;
return{
readOnly: false,
rightMenuShow: true,
Quill: null,
currentValue: "",
options: {
theme: "snow",
bounds: document.body,
debug: "warn",
modules: {
// ⼯具栏配置
toolbar: {
container: [
["bold", "italic", "underline", "strike"], // 加粗斜体下划线删除线
["blockquote", "code-block"], // 引⽤代码块
[{ list: "ordered"}, { list: "bullet"}], // 有序、⽆序列表
[{ indent: "-1"}, { indent: "+1"}], // 缩进
[{ size: [false, "small", "large", "huge"]}], // 字体⼤⼩
[{ header: [false, 1, 2, 3, 4]}], // 标题
[{ color: []}, { background: []}], // 字体颜⾊、字体背景颜⾊
[{ align: []}], // 对齐⽅式
["clean"], // 清除⽂本格式
["link", "image", "video"], // 链接、图⽚、视频
["sidebar"],
],
handlers: {
shadeBox: null,
sidebar: function(){
that.rightMenuShow =!that.rightMenuShow;
},
},
},
},
placeholder: " 请输⼊标题",
},
outlineList: [],
};
},
computed: {
styles(){
let style ={};
if(this.height){
style.height =`${this.height}px`;
}
style.width ="50%";
style.margin ="0 0 0 25%";
style.height ="calc(100% - 42px)";
return style;
},
},
value: {
handler(val){
if(val !== this.currentValue){asp富文本编辑器
this.currentValue = val === null ? "": val;
if(this.Quill){
this.Quill.pasteHTML(this.currentValue);
console.log(this.currentValue);
}
}
},
immediate: true,
},
},
mounted(){
this.init();
},
beforeDestroy(){
this.Quill = null;
},
methods: {
init(){
this.outlineList = JSON.parse(this.outline);
const editor = this.$refs.editor;
this.Quill = new Quill(editor, this.options);
this.Quill.pasteHTML(this.currentValue);
this.parsingHtml(this.$refs.editor.children[0].innerHTML);
("text-change", (delta, oldDelta, source)=>{
const html = this.$refs.editor.children[0].innerHTML;
const text = Text();
const quill = this.Quill;
this.currentValue = html;
this.$emit("input", html);
this.$emit("on-change", { html, text, quill});
this.parsingHtml(html);
});
("text-change", (delta, oldDelta, source)=>{
this.$emit("on-text-change", delta, oldDelta, source);
});
("selection-change", (range, oldRange, source)=>{
this.$emit("on-selection-change", range, oldRange, source);
});
("editor-change", (eventName, ...args)=>{
this.$emit("on-editor-change", eventName, ...args);
});
const sourceEditorButton = document.querySelector(".ql-sidebar"); sourceEditorButton.icon ='el-icon-s-unfold';
sourceEditorButton.style.cssText =
"width:40px; border:1px solid #ccc; border-radius:5px;";
sourceEditorButton.innerText ="⼤纲";
},
sidebarClick(){
this.rightMenuShow =!this.rightMenuShow;
},
parsingHtml(html){
const el = ateElement("div");
const that = this;
el.innerHTML = html;
let hList =[];
that.outlineList =[];
let index = 0;
let bol =true;
// 遍历所有html标签,寻h标签作为⼤纲
el.querySelectorAll("*").forEach((item)=>{
item.tagName =="H2"||
item.tagName =="H3"||
item.tagName =="H4"
){
index ++;
that.outlineList.push({
title: item.innerText,
tagName: item.tagName,
id: '#' + item.tagName + index
});
}
});
that.$emit("getOutlineList", that.outlineList);
// 为富⽂本中的内容增加id属性,锚点使⽤
index = 0;
this.$emit("title", item.innerText);
bol =false;
}
if(
item.tagName =="H2"||
item.tagName =="H3"||
item.tagName =="H4"
){
index++;
item.id = item.tagName + index;
}
})
},
},
};
</script>
<style type="scss">
html,body{height:100%;overflow: hidden;}
#app,.baseDiv{height: 100%;}
</style>
<style>
.editor,
.ql-toolbar {
white-space: pre-wrap !important;
line-height: normal !important;
}
.quill-img {
display: none;
}
.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,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论