From 255bad312b8dd706711a8473f9a84694b20384c2 Mon Sep 17 00:00:00 2001 From: msagolj Date: Wed, 9 Oct 2024 16:55:02 +0200 Subject: [PATCH] extend editor-support.js --- scripts/editor-support.js | 100 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/scripts/editor-support.js b/scripts/editor-support.js index 0fcfda4..a822a63 100644 --- a/scripts/editor-support.js +++ b/scripts/editor-support.js @@ -10,6 +10,55 @@ import { import { decorateRichtext } from './editor-support-rte.js'; import { decorateMain } from './scripts.js'; +function getState(block) { + const state = {}; + if (block.matches('.tabs')) state.activeTabId = block.querySelector('[aria-selected="true"]').dataset.tabId; + if (block.matches('.carousel')) { + const container = block.querySelector('.panel-container'); + state.scrollLeft = container.scrollLeft; + } + return state; +} + +function restoreState(newBlock, state) { + if (state.activeTabId) { + newBlock.querySelector(`[data-tab-id="${state.activeTabId}"]`).click(); + } + if (state.scrollLeft) { + newBlock.querySelector('.panel-container').scrollTo({ left: state.scrollLeft, behavior: 'instant' }); + } +} + +function setIdsforRTETitles(articleContentSection) { + // find all titles with no id in the article content section + articleContentSection + .querySelectorAll('h1:not([id]),h2:not([id]),h3:not([id]),h4:not([id]),h5:not([id]),h6:not([id])') + .forEach((title) => { + title.id = title.textContent + .toLowerCase() + .trim() + .replaceAll('[^a-z0-9-]', '-') + .replaceAll('-{2,}', '-') + .replaceAll('^-+', '') + .replaceAll('-+$', ''); + }); +} + +// set the filter for an UE editable +function setUEFilter(element, filter) { + element.dataset.aueFilter = filter; +} + +function updateUEInstrumentation() { + // ----- if browse page, identified by theme + if (document.querySelector('body[class^=browse-]').hasAttribute('data-aem-template')) { + document.createRange().createContextualFragment(` +
+ ${document.body.dataset.aemTemplate} + main?.addEventListener(eventType, async (event) => { event.stopPropagation(); const applied = await applyChanges(event); - if (!applied) window.location.reload(); + if (applied) { + updateUEInstrumentation(); + } else { + window.location.reload(); + } })); + + main.addEventListener('aue:ui-select', handleEditorSelect); } -attachEventListners(document.querySelector('main')); +attachEventListeners(document.querySelector('main')); + +// update UE component filters on page load +updateUEInstrumentation();