Enhance handleClick function to support invocation from rich HTML content

This commit is contained in:
Aljaž Grilc 2024-02-20 17:19:37 +01:00
parent aa905921f9
commit 88685b05c7

View File

@ -352,32 +352,25 @@ class BesEditor {
return nodes
}
// TODO: Improve this function to support copied rich html content from news sites, etc.
handleClick(e) {
const targetEl = e.target
const popup = document.querySelector('bes-popup-el')
if (targetEl.tagName === 'DIV') {
const divIndex = this.children.findIndex(
child => child.elements === targetEl
)
const matches = this.children[divIndex]?.matches
if (!matches) {
popup.hide()
return
}
if (
BesEditor.renderPopup(
targetEl,
matches,
popup,
e.clientX,
e.clientY,
this
)
)
return
else popup.hide()
} else popup.hide()
// If target has not parent with class 'bes-online-editor', find target's parent whose parent is 'bes-online-editor'
const target = BesEditor.findParent(targetEl)
? BesEditor.findParent(targetEl)
: targetEl
const divIndex = this.children.findIndex(child => child.elements === target)
const matches = this.children[divIndex]?.matches
if (!matches) {
popup.hide()
return
}
if (
BesEditor.renderPopup(target, matches, popup, e.clientX, e.clientY, this)
)
return
else popup.hide()
}
handleScrollEvent(editor, scrollPanel) {
@ -385,6 +378,17 @@ class BesEditor {
this.offsetTop = editor.scrollTop
}
static findParent(target) {
let element = target
while (element && element.parentNode) {
if (element.parentNode.classList?.contains('bes-online-editor')) {
return element
}
element = element.parentNode
}
return null
}
static renderPopup(el, matches, popup, clientX, clientY, editor) {
for (let m of matches) {
if (m.rects) {