Skip to content

Commit

Permalink
feat: allow loading scripts and styles in UE directly (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhaack authored Mar 3, 2024
1 parent 18e872f commit 04a637a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 6 additions & 1 deletion scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions scripts/editor-support-rte.js
Original file line number Diff line number Diff line change
@@ -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();
1 change: 0 additions & 1 deletion scripts/editor-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 04a637a

Please sign in to comment.