diff --git a/online-editor.js b/online-editor.js index 364dd7a..8baf049 100644 --- a/online-editor.js +++ b/online-editor.js @@ -242,30 +242,41 @@ function besGetBlockParent(el, edit) { function besHandleClick(e, editor) { const targetEl = e.target + const popup = document.querySelector('bes-popup-el') if (targetEl.tagName === 'DIV') { const editorData = besEditors[editor.id] const divIndex = editorData.children.findIndex( child => child.elements === targetEl ) const matches = editorData.children[divIndex]?.matches - if (!matches) return - for (let m of matches) { - if (m.rects) { - for (let r of m.rects) { - const clientX = e.clientX - const clientY = e.clientY - if (isPointInRect(clientX, clientY, r)) { - const popup = document.querySelector('my-component') - popup.changeText(m.message) - popup.show(clientX, clientY) - } - } - } + if (!matches) { + popup.hide() + return } + if (besRenderPopup(matches, popup, e.clientX, e.clientY)) return + } else { + popup.hide() } } -function isPointInRect(x, y, rect) { +function besRenderPopup(matches, popup, clientX, clientY) { + for (let m of matches) { + if (m.rects) { + for (let r of m.rects) { + if (besIsPointInRect(clientX, clientY, r)) { + popup.changeText(m.message) + popup.show(clientX, clientY) + return true + } + } + } else { + popup.hide() + } + } + return false +} + +function besIsPointInRect(x, y, rect) { return ( x >= rect.x && x <= rect.x + rect.width && diff --git a/popup.js b/popup.js index 4ae3fa7..7af4734 100644 --- a/popup.js +++ b/popup.js @@ -1,4 +1,4 @@ -class MyComponent extends HTMLElement { +class BesPopupEl extends HTMLElement { constructor() { super() this.attachShadow({ mode: 'open' }) @@ -14,6 +14,9 @@ class MyComponent extends HTMLElement { :host { position: relative; display: inline-block; + z-index: -1 + } + :host(.show){ z-index: 10; } .popup-text { @@ -25,12 +28,25 @@ class MyComponent extends HTMLElement { } .bes-popup-container { visibility: hidden; - max-width: 200px; - background-color: white; + max-width: 300px; + bacground-color: rgb(47, 50, 55); box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 8px; } + .bes-toolbar { + display: flex; + justify-content: end; + padding: 5px 2px; + } + .bes-toolbar button { + margin-right: 2px; + } + .bes-text-div{ + background-color: white; + padding: 10px; + border-radius: 5px; + } :host(.show) .bes-popup-container { visibility: visible; animation: fadeIn 1s; @@ -41,15 +57,19 @@ class MyComponent extends HTMLElement { }