Allow post-window.onload editor registration

This allows grammar-checking support setup for contentEditable <div>s
added to the DOM tree later.
This commit is contained in:
Simon Rozman 2024-02-07 12:39:27 +01:00
parent 494107bafb
commit 81240397d7

View File

@ -1,5 +1,7 @@
const besUrl = 'http://localhost:225/api/v2/check' const besUrl = 'http://localhost:225/api/v2/check'
let besEditors = [] // Collection of all editors on page
class BesEditor { class BesEditor {
constructor(edit) { constructor(edit) {
this.el = edit this.el = edit
@ -11,6 +13,13 @@ class BesEditor {
edit.addEventListener('click', e => this.handleClick(e)) edit.addEventListener('click', e => this.handleClick(e))
} }
// Register editor
static register(edit) {
let editor = new BesEditor(edit)
besEditors.push(editor)
return editor
}
// Recursively grammar-proofs one node. // Recursively grammar-proofs one node.
async proof(el) { async proof(el) {
switch (el.nodeType) { switch (el.nodeType) {
@ -317,14 +326,11 @@ class BesEditor {
} }
} }
let besEditors = [] // Collection of all editors on page
window.onload = () => { window.onload = () => {
// Search and prepare all our editors found in the document. // Search and prepare all our editors found in the document.
document.querySelectorAll('.bes-online-editor').forEach(edit => { document
let editor = new BesEditor(edit) .querySelectorAll('.bes-online-editor')
besEditors.push(editor) .forEach(edit => BesEditor.register(edit))
})
} }
window.onresize = () => { window.onresize = () => {