Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update scripts for improved UE loading #6

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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