網(wǎng)站首頁 編程語言 正文
?參考快速擴(kuò)展一個(gè)菜單 · wangEditor 用戶文檔
繼承 按鈕 菜單樣式自定義拓展 格式刷功能菜單按鈕
const _this = this
const { $, BtnMenu } = E
// 自定義 格式刷 菜單繼F承 BtnMenu class
class FormatPainter extends BtnMenu {
constructor(editor) {
// data-title屬性表示當(dāng)鼠標(biāo)懸停在該按鈕上時(shí)提示該按鈕的功能簡(jiǎn)述
const $elem = E.$(
`<div class="w-e-menu" data-title="格式刷">
<i class="el-icon-s-open"></i>
</div>`
)
super($elem, editor)
}
// 菜單點(diǎn)擊事件,獲取點(diǎn)擊格式刷后的下一次點(diǎn)擊文本
clickHandler() {
let nodeArray = []
//獲取當(dāng)前選中元素所有父樣式
function getAllStyle(dom) {
if (!dom) return
const tagName = dom.tagName.toLowerCase()
if (tagName === 'p') {
nodeArray.push({
tagName: 'span',
attributes: Array.from(dom.attributes).map((i) => {
return {
name: i.name,
value: i.value,
}
}),
})
return
} else {
nodeArray.push({
tagName: tagName,
attributes: Array.from(dom.attributes).map((i) => {
return {
name: i.name,
value: i.value,
}
}),
})
getAllStyle(dom.parentNode)
}
return nodeArray
}
//獲取鼠標(biāo)位置的節(jié)點(diǎn)
let containerEle = this.editor.selection.getSelectionStartElem()
.elems[0]
let brushStyle = getAllStyle(containerEle)
if (!!brushStyle) {
//有復(fù)制到的格式就開啟格式刷
_this.ifBrushed = true //格式刷開啟
_this.brushStyle = brushStyle //格式刷樣式存儲(chǔ)
}
}
// 菜單是否被激活(如果不需要,這個(gè)函數(shù)可以空著)
tryChangeActive() {}
}
E.registerMenu('FormatPainter', FormatPainter)
//監(jiān)聽鼠標(biāo)抬起事件
this.$refs['editor'].addEventListener('mouseup', () => {
//延時(shí)調(diào)用確保富文本編輯器中的選中文本已經(jīng)該改變
setTimeout(() => {
this.containerEleChange()
})
})
/**
* @description: 監(jiān)聽文本點(diǎn)擊事件
* @return void
*/
containerEleChange() {
let containerEle = this.editor.selection.getSelectionContainerElem()
.elems[0] //選區(qū)所在的 DOM 節(jié)點(diǎn)
let containerEleStart = this.editor.selection.getSelectionStartElem()
.elems[0] //選區(qū)開始的 DOM 節(jié)點(diǎn)
let containerEleEnd = this.editor.selection.getSelectionEndElem().elems[0] //選區(qū)結(jié)束的 DOM 節(jié)點(diǎn)
let ifEmpty = this.editor.selection.isSelectionEmpty() //判斷選區(qū)是否為“空”(即沒有選中任何文字)
let containerText = this.editor.selection.getSelectionText() //選中的文字
//復(fù)制style到選中的文本
function addStyle(text, nodeArray) {
let currentNode = null
nodeArray.forEach((ele, index) => {
let node = document.createElement(ele.tagName)
for (const attr of ele.attributes) {
node.setAttribute(attr.name, attr.value)
}
if (index === 0) {
node.innerText = text
currentNode = node
} else {
node.appendChild(currentNode)
currentNode = node
}
})
return currentNode
}
if (this.ifBrushed) {
// 格式刷開啟則開始復(fù)制style
let containerEleNew
let containerEleText
if (ifEmpty) {
//判斷選區(qū)是否為“空”(即沒有選中任何文字)
containerEleText = containerEle.innerText
containerEleNew = addStyle(containerEle.innerText, this.brushStyle) //新的樣式
} else {
//選中一段文字
containerEleText = containerText
containerEleNew = addStyle(containerText, this.brushStyle) //新的樣式
}
if (containerEleStart === containerEleEnd) {
//選區(qū)前后相等,選中的區(qū)域中間不夾雜其他標(biāo)簽
let innerText = containerEle.innerText
if (ifEmpty) {
// 沒有選中的文本直接全部替換掉
containerEle.innerHTML = containerEleNew.innerHTML
} else {
//有選中的文本則替換選中的文本
containerEle.innerHTML = innerText.replace(
containerEleText,
containerEleNew.innerHTML
)
}
} else {
//選區(qū)前后不相等,選中的區(qū)域中間夾雜其他標(biāo)簽,操作和選區(qū)為“空”一樣
containerEleNew = addStyle(containerEle.innerText, this.brushStyle) //新的樣式
containerEle.innerHTML = containerEleNew.innerHTML
}
}
this.ifBrushed = false
},
//清除監(jiān)聽
beforeDestroy() {
this.$refs['editor'].removeEventListener('click', this.containerEleChange)
},
原文鏈接:https://blog.csdn.net/sugerinaflat/article/details/122331334
相關(guān)推薦
- 2022-11-01 ElasticSearch寫入流程實(shí)例解析_相關(guān)技巧
- 2023-03-04 C++容器適配器的概念與示例_C 語言
- 2022-05-19 yolov5調(diào)用usb攝像頭及本地?cái)z像頭的方法實(shí)例_python
- 2023-03-16 淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法流程講解_Android
- 2022-01-30 VSCode標(biāo)簽內(nèi)的代碼塊無法折疊問題解決
- 2022-05-10 FactoryBean配置文件定義的 類型 調(diào)用時(shí)返回 不同的類型
- 2022-03-08 android?studio項(xiàng)目:綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時(shí)器_Android
- 2023-01-05 find?命令全集_linux shell
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支