Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Add logic to load MetroJS for examples that do not do so themselves.
Browse files Browse the repository at this point in the history
  • Loading branch information
Potherca committed Aug 23, 2024
1 parent 62e1cd0 commit dfc21ff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/examples/example-helper.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main script::before {
content: '<script>';
}

main script[src]::before {
main script[src]:not([data-js="loaded"])::before {
content: '<script src="' attr(src) '">';
}

Expand Down
21 changes: 18 additions & 3 deletions docs/examples/example-helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
function addElements(container, elements) {
function addElements(container, elements, metroLoaded = false) {
if (metroLoaded === false && elements.length === 1 && elements[0].tagName === 'SCRIPT' && ! elements[0].src) {
metroLoaded = true
const script = document.createElement('script')
script.src = 'https://cdn.jsdelivr.net/npm/@muze-nl/metro/dist/everything.js'
script.setAttribute('data-js', 'loaded')
elements.unshift(script)

}

const currentElement = elements.shift()

if (currentElement !== undefined) {
container.appendChild(currentElement)

if (currentElement.tagName === 'SCRIPT' && currentElement.src) {
currentElement.addEventListener('load', () => addElements(container, elements))
currentElement.addEventListener('load', () => addElements(container, elements, metroLoaded))
} else {
addElements(container, elements)
addElements(container, elements, metroLoaded)
}
}
}
Expand All @@ -18,6 +27,12 @@ function createHTML(text) {

const fragment = range.createContextualFragment(text)

fragment.childNodes.forEach(element => {
if (element.nodeType === 3 && element.textContent.trim() === '') {
element.remove()
}
})

fragment.childNodes.forEach(element => {
console.info('element', element.nodeName, element)
elements.push(element)
Expand Down

0 comments on commit dfc21ff

Please sign in to comment.