Skip to content

Commit

Permalink
Load media from cc-shared for root
Browse files Browse the repository at this point in the history
  • Loading branch information
aishwaryamathuria committed Mar 28, 2024
1 parent 6e09f6d commit 2247a58
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions creativecloud/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const [setLibs, getLibs] = (() => {

const miloLibs = setLibs('/libs');

const { createTag, localizeLink, loadStyle } = await import(`${miloLibs}/utils/utils.js`);
const { createTag, localizeLink, getConfig, loadStyle } = await import(`${miloLibs}/utils/utils.js`);
export { createTag, localizeLink };

function getDecorateAreaFn() {
Expand All @@ -58,13 +58,25 @@ function getDecorateAreaFn() {
if (lcpImg) lcpImgSet = true;
};

function replaceDotMedia(area = document) {
const currUrl = new URL(window.location);
const pathSeg = currUrl.pathname.split('/').length;
if (pathSeg >= 3) return;
const resetAttributeBase = (tag, attr) => {
area.querySelectorAll(`${tag}[${attr}^="./media_"]`).forEach((el) => {
el[attr] = `${new URL(`${getConfig().contentRoot}${el.getAttribute(attr).substring(1)}`, window.location).href}`;
});
};
resetAttributeBase('img', 'src');
resetAttributeBase('source', 'srcset');
}

async function loadLCPImage(area = document, { fragmentLink = null } = {}) {
const firstBlock = area.querySelector('body > main > div > div');
let fgDivs = null;
switch (true) {
case firstBlock?.classList.contains('changebg'): {
firstBlock.querySelector(':scope > div:nth-child(1)').querySelectorAll('img').forEach(eagerLoad);
const { getConfig } = await import(`${getLibs()}/utils/utils.js`);
import(`${getConfig().codeRoot}/deps/interactive-marquee-changebg/changeBgMarquee.js`);
break;
}
Expand All @@ -89,6 +101,7 @@ function getDecorateAreaFn() {
}

return (area, options) => {
replaceDotMedia();
if (!lcpImgSet) loadLCPImage(area, options);
};
}
Expand Down

0 comments on commit 2247a58

Please sign in to comment.