diff --git a/online-editor.js b/online-editor.js
index 8acecb0..0c0e4df 100644
--- a/online-editor.js
+++ b/online-editor.js
@@ -6,7 +6,19 @@ window.onload = () => {
// Search and prepare all our editors found in the document.
document.querySelectorAll('.bes-online-editor').forEach(edit => {
let editor = {
- timer: null
+ timer: null,
+ children: [
+ {
+ elements: null,
+ isProofed: false,
+ matches: [
+ {
+ range: null,
+ rects: null
+ }
+ ]
+ }
+ ]
}
besEditors[edit.id] = editor
besProof(edit)
@@ -15,7 +27,7 @@ window.onload = () => {
e => besHandleBeforeInput(edit.id, e),
false
)
- edit.addEventListener('click', e => besHandleClick(e))
+ edit.addEventListener('click', e => besHandleClick(e, edit))
// TODO: Handle editor resizes.
})
}
@@ -62,6 +74,7 @@ async function besProof(el) {
return response.json()
})
.then(responseData => {
+ const matches = [{ range: null, rects: null, message: null }]
responseData.matches.forEach(match => {
let range = document.createRange()
@@ -98,10 +111,15 @@ async function besProof(el) {
}
}
- besAddMistake(range, match)
+ const clientRect = besAddMistake(range, match)
+ matches.push({
+ range: range,
+ rects: clientRect,
+ message: match.message
+ })
})
- besMarkProofed(el)
+ besMarkProofed(el, matches)
})
.catch(error => {
// TODO: Make parsing issues non-fatal. But show an error sign somewhere in the UI.
@@ -153,8 +171,15 @@ function besIsProofed(el) {
}
// Mark given block element as grammar-proofed.
-function besMarkProofed(el) {
- el.setAttribute('besProofed', 'true')
+function besMarkProofed(el, matches) {
+ const editorId = el.parentElement.id
+ const editor = besEditors[editorId]
+ editor.children.push({
+ isProofed: true,
+ elements: el,
+ matches: matches
+ })
+ // editor.children['matches'] = matches
}
// Mark given block element as not grammar-proofed.
@@ -196,6 +221,7 @@ function besAddMistake(range, match) {
// return true;
// });
}
+ return clientRects
}
// Tests if given element is block element.
@@ -223,14 +249,52 @@ function besGetBlockParent(el, edit) {
return el
}
-function besHandleClick(e) {
- switch (e.target) {
- case e.target.closest('span'):
- const clicked = e.target.closest('span')
- const infoText = clicked?.dataset.info
- const myComponent = document.querySelector('my-component')
- myComponent.setAttribute('my-attribute', infoText)
- console.log(clicked)
- break
+function besHandleClick(e, editor) {
+ console.log(e)
+ const targetEl = e.target
+ if (targetEl.tagName === 'DIV') {
+ // Find div inside besEditors[editor.id].children
+ console.log(besEditors[editor.id])
+ const editorData = besEditors[editor.id]
+ const divIndex = editorData.children.findIndex(
+ child => child.elements === targetEl
+ )
+ const matches = editorData.children[divIndex].matches
+
+ for (let m of matches) {
+ console.log(m)
+ if (m.rects) {
+ for (let r of m.rects) {
+ console.log(r)
+ const x = e.clientX
+ const y = e.clientY
+ if (isPointInRect(x, y, r)) {
+ console.log(m.message)
+ const popup = document.querySelector('my-component')
+ popup.changeText(m.message)
+ popup.toggle('show')
+ }
+ }
+ }
+ }
+ console.log('The point is outside the rectangle')
}
+ // switch (e.target) {
+ // case e.target.closest('span'):
+ // const clicked = e.target.closest('span')
+ // const infoText = clicked?.dataset.info
+ // const myComponent = document.querySelector('my-component')
+ // myComponent.setAttribute('my-attribute', infoText)
+ // console.log(clicked)
+ // break
+ // }
+}
+
+function isPointInRect(x, y, rect) {
+ return (
+ x >= rect.x &&
+ x <= rect.x + rect.width &&
+ y >= rect.y &&
+ y <= rect.y + rect.height
+ )
}
diff --git a/popup.js b/popup.js
index b2d5dbc..6cd7962 100644
--- a/popup.js
+++ b/popup.js
@@ -1,15 +1,52 @@
class MyComponent extends HTMLElement {
- static get observedAttributes() {
- return ['my-attribute']
+ constructor() {
+ super()
+ this.attachShadow({ mode: 'open' })
}
- attributeChangedCallback(name, oldValue, newValue) {
- if (name === 'my-attribute') {
- console.log(
- `Value of my-attribute changed from ${oldValue} to ${newValue}`
- )
- // You can perform some action based on the new value here
- }
+ connectedCallback() {
+ this.render()
+ }
+
+ render() {
+ this.shadowRoot.innerHTML = `
+
+
+ `
+ }
+
+ toggle() {
+ this.classList.toggle('show')
+ }
+
+ changeText(text) {
+ this.shadowRoot.querySelector('.popup-text').textContent = text
}
}