Skip to content

Commit

Permalink
feat: fragment block for crosswalk (#36)
Browse files Browse the repository at this point in the history
* feat: add fragment def and model

* feat: add fragment def and model

* feat: preserve the fragment block, replace contents with fragment
  • Loading branch information
buuhuu authored Jul 31, 2024
1 parent 6ab5c1a commit 4643b51
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
*/
export async function loadFragment(path) {
if (path && path.startsWith('/')) {
// eslint-disable-next-line no-param-reassign
path = path.replace(/(\.plain)?\.html/, '');
const resp = await fetch(`${path}.plain.html`);
if (resp.ok) {
const main = document.createElement('main');
Expand Down Expand Up @@ -48,8 +50,9 @@ export default async function decorate(block) {
if (fragment) {
const fragmentSection = fragment.querySelector(':scope .section');
if (fragmentSection) {
block.closest('.section').classList.add(...fragmentSection.classList);
block.closest('.fragment').replaceWith(...fragment.childNodes);
block.classList.add(...fragmentSection.classList);
block.classList.remove('section');
block.replaceChildren(...fragmentSection.childNodes);
}
}
}
15 changes: 15 additions & 0 deletions component-definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@
}
}
}
},
{
"title": "Fragment",
"id": "fragment",
"plugins": {
"xwalk": {
"page": {
"resourceType": "core/franklin/components/block/v1/block",
"template": {
"name": "Fragment",
"model": "fragment"
}
}
}
}
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion component-filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"title",
"hero",
"cards",
"columns"
"columns",
"fragment"
]
},
{
Expand Down
10 changes: 10 additions & 0 deletions component-models.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,15 @@
"valueType": "string"
}
]
},
{
"id": "fragment",
"fields": [
{
"component": "aem-content",
"name": "reference",
"label": "Reference"
}
]
}
]

0 comments on commit 4643b51

Please sign in to comment.