Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
- loadComponents func
- decorateImages
  • Loading branch information
ifahrentholz committed Feb 6, 2024
1 parent b399fdd commit c987bef
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
2 changes: 2 additions & 0 deletions dist/__chunks__/createOptimizedPicture.F94eikeI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/__chunks__/createOptimizedPicture.F94eikeI.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/main/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main/main.js.map

Large diffs are not rendered by default.

65 changes: 41 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class SectionService {
decorateImages() {
const picture = document.querySelectorAll('.default-content-wrapper picture');
picture.forEach((item) => {
item.parentElement?.classList.add('image', 'main');
const parentElement = item.parentElement;
if (parentElement) {
parentElement.classList.add('image', 'main');
}
});
}
}
Expand Down Expand Up @@ -230,33 +233,47 @@ class Main {

private loadComponents = async () => {
const sections = document.querySelectorAll<HTMLElement>('.section');
sections.forEach((section) => {
const components: ComponentMapping[] = [];
const blocks = section.querySelectorAll<HTMLDivElement>('[data-block-name]');
if (!blocks.length) {
section.style.removeProperty('display');

sections.forEach(async (section) => {
const components: ComponentMapping[] = this.collectComponents(section);
if (!components.length) {
this.showSection(section);
return;
}
blocks.forEach((block: HTMLDivElement) => {
block.style.display = 'none';
components.push({
name: block.dataset['blockName'] as string,
element: block,
});
});
if (components.length) {
components.forEach(async (component) => {
const componentModule = await import(
`${window.hlx.codeBasePath}/dist/${component.name}/${component.name}.js`
);
if (componentModule.default) {
await componentModule.default(component.element);
}
});
}
section.style.removeProperty('display');

await this.loadComponentModules(components);
this.showSection(section);
});
};

private collectComponents(section: HTMLElement): ComponentMapping[] {
const components: ComponentMapping[] = [];
const blocks = section.querySelectorAll<HTMLDivElement>('[data-block-name]');

blocks.forEach((block: HTMLDivElement) => {
block.style.display = 'none';
components.push({
name: block.dataset['blockName'] as string,
element: block,
});
});

return components;
}

private async loadComponentModules(components: ComponentMapping[]) {
for (const component of components) {
const componentModule = await import(`${window.hlx.codeBasePath}/dist/${component.name}/${component.name}.js`);

if (componentModule.default) {
await componentModule.default(component.element);
}
}
}

private showSection(section: HTMLElement) {
section.style.removeProperty('display');
}
}

(async function () {
Expand Down
11 changes: 11 additions & 0 deletions types/components/sidebar/sidebarPosts.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LitElement } from 'lit';
export declare class SidebarPosts extends LitElement {
private lastTreePosts;
connectedCallback(): Promise<void>;
render(): import("lit-html").TemplateResult<1> | undefined;
protected createRenderRoot(): HTMLElement | DocumentFragment;
private getLastThreePosts;
private fetchSitemap;
private renderPost;
private getPosts;
}

0 comments on commit c987bef

Please sign in to comment.