From cead44c238ba240e8c17fa4f5f209e57bb4bcc76 Mon Sep 17 00:00:00 2001 From: Dirk Date: Tue, 13 Feb 2024 13:48:41 +0100 Subject: [PATCH] fix: reload for sections --- scripts/editor-support.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/scripts/editor-support.js b/scripts/editor-support.js index 634b10f227..b899fb4a3a 100644 --- a/scripts/editor-support.js +++ b/scripts/editor-support.js @@ -16,14 +16,15 @@ async function handleEditorUpdate(event) { if (!updates.length) return; const { content } = updates[0]; + const parsedUpdate = new DOMParser().parseFromString(content, 'text/html'); const element = document.querySelector(`[data-aue-resource="${resource}"]`); - if (element) { + // proceed only if the updated element exists and is not a section + if (element && !element.matches('.section')) { const block = element.parentElement?.closest('.block') || element?.closest('.block'); if (block) { const blockResource = block.getAttribute('data-aue-resource'); if (!blockResource || !blockResource.startsWith(connectionPrefix)) return; - const newBlockDocument = new DOMParser().parseFromString(content, 'text/html'); - const newBlock = newBlockDocument?.querySelector(`[data-aue-resource="${blockResource}"]`); + const newBlock = parsedUpdate.querySelector(`[data-aue-resource="${blockResource}"]`); if (newBlock) { newBlock.style.display = 'none'; block.insertAdjacentElement('afterend', newBlock); @@ -36,24 +37,22 @@ async function handleEditorUpdate(event) { // remove the old block and show the new one block.remove(); newBlock.style.display = null; + return; + } + } else { + const newElements = parsedUpdate.querySelectorAll(`[data-aue-resource="${resource}"],[data-richtext-resource="${resource}"]`); + if (newElements.length) { + const { parentElement } = element; + element.replaceWith(...newElements); + // decorate buttons and icons + decorateButtons(parentElement); + decorateIcons(parentElement); + return; } - - return; - } - - const updatedSection = new DOMParser().parseFromString(content, 'text/html'); - const newElement = updatedSection?.querySelector(`[data-aue-resource="${resource}"]`); - if (newElement) { - newElement.style.display = 'none'; - element.insertAdjacentElement('afterend', newElement); - // decorate buttons and icons - decorateButtons(newElement); - decorateIcons(newElement); - // remove the old element and show the new one - element.remove(); - newElement.style.display = null; } } + + window.location.reload(); } document.querySelector('main')?.addEventListener('aue:content-patch', handleEditorUpdate);