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

Template boiler plate code #26

Merged
merged 2 commits into from
Oct 25, 2023
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
31 changes: 31 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
waitForLCP,
loadBlocks,
loadCSS,
toClassName,
getMetadata,
} from './aem.js';

const LCP_BLOCKS = []; // add your LCP blocks to the list
Expand Down Expand Up @@ -42,6 +44,34 @@ async function loadFonts() {
}
}

const TEMPLATE_LIST = [
'Default',
'Plasmids',
'Proteins',
'mRNA',
];

/**
* Run template specific decoration code.
* @param {Element} main The container element
*/
async function decorateTemplates(main) {
try {
const template = toClassName(getMetadata('template'));
const templates = TEMPLATE_LIST;
if (templates.includes(template)) {
const mod = await import(`../templates/${template}/${template}.js`);
loadCSS(`${window.hlx.codeBasePath}/templates/${template}/${template}.css`);
if (mod.default) {
await mod.default(main);
}
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Auto Blocking failed', error);
}
}

/**
* Builds all synthetic blocks in a container element.
* @param {Element} main The container element
Expand Down Expand Up @@ -79,6 +109,7 @@ async function loadEager(doc) {
const main = doc.querySelector('main');
if (main) {
decorateMain(main);
await decorateTemplates(main);
document.body.classList.add('appear');
await waitForLCP(LCP_BLOCKS);
}
Expand Down
1 change: 1 addition & 0 deletions templates/Default/Default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Default template css */
1 change: 1 addition & 0 deletions templates/Default/Default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Default template
1 change: 1 addition & 0 deletions templates/Plasmids/Plasmids.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Plasmids */
1 change: 1 addition & 0 deletions templates/Plasmids/Plasmids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Plasmids
1 change: 1 addition & 0 deletions templates/Proteins/Proteins.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Proteins */
1 change: 1 addition & 0 deletions templates/Proteins/Proteins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Proteins
1 change: 1 addition & 0 deletions templates/mRNA/mRNA.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* mRNA */
1 change: 1 addition & 0 deletions templates/mRNA/mRNA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// mRNA