Skip to content

Commit

Permalink
chore: Add asset plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Deep Maheshwari committed Nov 12, 2024
1 parent 1db4372 commit 925bfea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions scripts/aem-assets-plugin-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// The based path of the aem-assets-plugin code.
const codeBasePath = '/plugins/aem-assets-plugin';

// The blocks that are to be used from the aem-assets-plugin.
const blocks = ['video'];

// Initialize the aem-assets-plugin.
export default async function assetsInit() {
const { loadBlock, createOptimizedPicture } = await import(`${codeBasePath}/scripts/aem-assets.js`);
window.hlx = window.hlx || {};
window.hlx.aemassets = {
codeBasePath,
blocks,
loadBlock,
createOptimizedPicture,
};
}
6 changes: 6 additions & 0 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ function createOptimizedPicture(
eager = false,
breakpoints = [{ media: '(min-width: 600px)', width: '2000' }, { width: '750' }],
) {
if (window.hlx?.aemassets?.createOptimizedPicture) {
return window.hlx.aemassets.createOptimizedPicture(src, alt, eager, breakpoints);
}
const url = new URL(src, window.location.href);
const picture = document.createElement('picture');
const { pathname } = url;
Expand Down Expand Up @@ -580,6 +583,9 @@ function buildBlock(blockName, content) {
* @param {Element} block The block element
*/
async function loadBlock(block) {
if (window.hlx?.aemassets?.loadBlock) {
return window.hlx.aemassets.loadBlock(block);
}
const status = block.dataset.blockStatus;
if (status !== 'loading' && status !== 'loaded') {
block.dataset.blockStatus = 'loading';
Expand Down
3 changes: 3 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
loadCSS,
} from './aem.js';

import assetsInit from './aem-assets-plugin-support.js';

/**
* Moves all the attributes from a given elmenet to another given element.
* @param {Element} from the element to copy attributes from
Expand Down Expand Up @@ -145,4 +147,5 @@ async function loadPage() {
loadDelayed();
}

await assetsInit(); // This to be done before loadPage() function invocation
loadPage();

0 comments on commit 925bfea

Please sign in to comment.