From 04a637a7a474815be4c57f3555b51e3f3da9e725 Mon Sep 17 00:00:00 2001 From: Markus Haack Date: Sun, 3 Mar 2024 22:50:33 +0100 Subject: [PATCH] feat: allow loading scripts and styles in UE directly (#6) --- scripts/aem.js | 7 +++++- scripts/editor-support-rte.js | 47 +++++++++++++++++++++++++++++++++++ scripts/editor-support.js | 1 - 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 scripts/editor-support-rte.js diff --git a/scripts/aem.js b/scripts/aem.js index b238a632d2..b1e048f95b 100644 --- a/scripts/aem.js +++ b/scripts/aem.js @@ -140,7 +140,12 @@ function setup() { const scriptEl = document.querySelector('script[src$="/scripts/scripts.js"]'); if (scriptEl) { try { - [window.hlx.codeBasePath] = new URL(scriptEl.src).pathname.split('/scripts/scripts.js'); + const scriptURL = new URL(scriptEl.src, window.location); + if (scriptURL.host === window.location.host) { + [window.hlx.codeBasePath] = scriptURL.pathname.split('/scripts/scripts.js'); + } else { + [window.hlx.codeBasePath] = scriptURL.href.split('/scripts/scripts.js'); + } } catch (error) { // eslint-disable-next-line no-console console.log(error); diff --git a/scripts/editor-support-rte.js b/scripts/editor-support-rte.js new file mode 100644 index 0000000000..0136b1203f --- /dev/null +++ b/scripts/editor-support-rte.js @@ -0,0 +1,47 @@ +// group editable texts in single wrappers if applicable +// +// this script should execute after script.js by editor-support.js + +// eslint-disable-next-line import/prefer-default-export +export function decorateRichtext(container = document) { + function deleteInstrumentation(element) { + delete element.dataset.richtextResource; + delete element.dataset.richtextProp; + delete element.dataset.richtextFilter; + } + + let element; + // eslint-disable-next-line no-cond-assign + while (element = container.querySelector('[data-richtext-resource]')) { + const { richtextResource, richtextProp, richtextFilter } = element.dataset; + deleteInstrumentation(element); + const siblings = []; + let sibling = element; + // eslint-disable-next-line no-cond-assign + while (sibling = sibling.nextElementSibling) { + if (sibling.dataset.richtextResource === richtextResource + && sibling.dataset.richtextProp === richtextProp) { + deleteInstrumentation(sibling); + siblings.push(sibling); + } else break; + } + const orphanElements = document.querySelectorAll(`[data-richtext-id="${richtextResource}"][data-richtext-prop="${richtextProp}"]`); + if (orphanElements.length) { + // eslint-disable-next-line no-console + console.warn('Found orphan elements of a richtext, that were not consecutive siblings of ' + + 'the first paragraph.', orphanElements); + orphanElements.forEach((el) => deleteInstrumentation(el)); + } else { + const group = document.createElement('div'); + group.dataset.aueResource = richtextResource; + group.dataset.aueProp = richtextProp; + if (richtextFilter) group.dataset.aueFilter = richtextFilter; + group.dataset.aueBehavior = 'component'; + group.dataset.aueType = 'richtext'; + element.replaceWith(group); + group.append(element, ...siblings); + } + } +} + +decorateRichtext(); diff --git a/scripts/editor-support.js b/scripts/editor-support.js index bbfbae6515..6b6856649b 100644 --- a/scripts/editor-support.js +++ b/scripts/editor-support.js @@ -7,7 +7,6 @@ import { loadBlock, loadBlocks, } from './aem.js'; -// eslint-disable-next-line import/no-unresolved import { decorateRichtext } from './editor-support-rte.js'; import { decorateMain } from './scripts.js';