From 81240397d7582334d1fff7755cc4eaedfa6152c7 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 7 Feb 2024 12:39:27 +0100 Subject: [PATCH] Allow post-window.onload editor registration This allows grammar-checking support setup for contentEditable
s added to the DOM tree later. --- online-editor.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/online-editor.js b/online-editor.js index 60fde9c..a9bd9cb 100644 --- a/online-editor.js +++ b/online-editor.js @@ -1,5 +1,7 @@ const besUrl = 'http://localhost:225/api/v2/check' +let besEditors = [] // Collection of all editors on page + class BesEditor { constructor(edit) { this.el = edit @@ -11,6 +13,13 @@ class BesEditor { 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. async proof(el) { switch (el.nodeType) { @@ -317,14 +326,11 @@ class BesEditor { } } -let besEditors = [] // Collection of all editors on page - window.onload = () => { // Search and prepare all our editors found in the document. - document.querySelectorAll('.bes-online-editor').forEach(edit => { - let editor = new BesEditor(edit) - besEditors.push(editor) - }) + document + .querySelectorAll('.bes-online-editor') + .forEach(edit => BesEditor.register(edit)) } window.onresize = () => {