forked from hlxsites/bitdefender
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from bitdefender/DEX-21187
add fragment block and style adjustments for medium image size
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.fragment { | ||
z-index: 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Fragment Block | ||
* Include content from one Helix page in another. | ||
* https://www.hlx.live/developer/block-collection/fragment | ||
*/ | ||
|
||
import { | ||
decorateMain, getLanguageCountryFromPath, | ||
} from '../../scripts/scripts.js'; | ||
|
||
import { | ||
loadBlocks, | ||
} from '../../scripts/lib-franklin.js'; | ||
|
||
/** | ||
* Loads a fragment. | ||
* @param {string} path The path to the fragment | ||
* @returns {HTMLElement} The root element of the fragment | ||
*/ | ||
async function loadFragment(path) { | ||
const { country, language } = getLanguageCountryFromPath(); | ||
if (path && path.startsWith('/')) { | ||
try { | ||
// eslint-disable-next-line no-param-reassign | ||
path = path.replace(/lang/g, `${language}-${country}`); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log(error); | ||
} | ||
const resp = await fetch(`${path}.plain.html`); | ||
if (resp.ok) { | ||
const main = document.createElement('main'); | ||
main.innerHTML = await resp.text(); | ||
decorateMain(main); | ||
await loadBlocks(main); | ||
return main; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export default async function decorate(block) { | ||
const link = block.querySelector('a'); | ||
const path = link ? link.getAttribute('href') : block.textContent.trim(); | ||
const fragment = await loadFragment(path); | ||
if (fragment) { | ||
const fragmentSection = fragment.querySelector(':scope .section'); | ||
if (fragmentSection) { | ||
block.closest('.section').classList.add(...fragmentSection.classList); | ||
block.closest('.fragment-wrapper').replaceWith(...fragmentSection.childNodes); | ||
} | ||
} | ||
} |