-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add model-viewer block for the Web Components docs page #459
Closed
+1,277
−0
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a4db2f5
(feat) add visible-for-demo block for Web Components guide page
bdelacretaz c1f6a2c
Improved web component example with delayed loading
bdelacretaz 3f5439e
fix:model-viewer block tests
bdelacretaz e51d956
fix: lazy-loader tests
bdelacretaz 5b70658
fix: improve test
bdelacretaz 92a10cb
fix: disable eslint on minified 3rd party code
bdelacretaz 42eb6e0
fix: load lazy script async
bdelacretaz 7079f93
fix:update test for async script
bdelacretaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
blocks/model-viewer/assets/smithsonian-neil-armstrong/README.md
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,2 @@ | ||
The assets in this folder come from the | ||
model-viewer demo page at https://modelviewer.dev/ |
Binary file added
BIN
+446 KB
blocks/model-viewer/assets/smithsonian-neil-armstrong/environment-image.hdr
Binary file not shown.
Binary file not shown.
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 @@ | ||
.model-viewer model-viewer { | ||
height: 20em; | ||
} |
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,36 @@ | ||
import '../../web-components/lazy-loader.js'; | ||
|
||
// Replace the block's content with model-viewer custom element | ||
// inside a lazy-loader so that it's loaded delayed | ||
export default function decorate($block) { | ||
const assetPrefix = '/blocks/model-viewer/assets'; | ||
|
||
// First row of the block defines the model, second one the alt text | ||
const model = $block.querySelector(':scope > div > div ')?.textContent?.trim(); | ||
const alt = $block.querySelector(':scope > div:nth-child(2) > div')?.textContent?.trim(); | ||
|
||
// Need a template element for lazy-loader to work | ||
const loader = document.createElement('lazy-loader'); | ||
loader.innerHTML = ` | ||
<p class="remove"> | ||
<em>If all goes well, this should be replaced by an interactive 3D model of Neil Armstrong's Spacesuit...</em> | ||
</p> | ||
<template> | ||
<script | ||
src='/web-components/google-model-viewer.min.js' | ||
type='module' | ||
></script> | ||
<model-viewer | ||
alt="${alt}" | ||
ar='true' | ||
src='${assetPrefix}/${model}/3d-model.glb' | ||
environment-image='${assetPrefix}/${model}/environment-image.hdr' | ||
poster='${assetPrefix}/${model}/poster.webp' | ||
shadow-intensity=1 | ||
camera-controls='true' | ||
touch-action='pan-y' | ||
></model-viewer> | ||
</template> | ||
`; | ||
$block.replaceChildren(loader); | ||
} |
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,13 @@ | ||
<div class="section content model-viewer-container"> | ||
<div class="model-viewer-wrapper"> | ||
<div class="model-viewer block"> | ||
<div> | ||
<div>the-model-name</div> | ||
</div> | ||
<div> | ||
<div>The model's description</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
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,42 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* global describe it */ | ||
|
||
import { readFile } from '@web/test-runner-commands'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import decorate from '../../../blocks/model-viewer/model-viewer.js'; | ||
|
||
const mock = await readFile({ path: './model-viewer.mock.html' }); | ||
document.body.innerHTML = mock; | ||
|
||
describe('Model viewer block', async () => { | ||
it('has a model-viewer element', async () => { | ||
const block = document.querySelector('.block'); | ||
decorate(block); | ||
const loader = document.querySelector('lazy-loader'); | ||
expect(loader).to.exist; | ||
const template = loader.querySelector('template'); | ||
const cloned = template.content.cloneNode(true); | ||
document.body.append(cloned); | ||
|
||
const mv = document.body.querySelector('model-viewer'); | ||
expect(mv).to.exist; | ||
const expected = { | ||
src: '/blocks/model-viewer/assets/the-model-name/3d-model.glb', | ||
alt: 'The model\'s description', | ||
ar: 'true', | ||
'environment-image': '/blocks/model-viewer/assets/the-model-name/environment-image.hdr', | ||
poster: '/blocks/model-viewer/assets/the-model-name/poster.webp', | ||
'shadow-intensity': '1', | ||
'camera-controls': 'true', | ||
'touch-action': 'pan-y', | ||
}; | ||
Object.keys(expected).forEach((k) => { | ||
expect(mv.getAttribute(k)).to.equal(expected[k], `Expected attribute value '${k}' to match`); | ||
}); | ||
|
||
const script = document.body.querySelector('script'); | ||
expect(script).to.exist; | ||
expect(script.getAttribute('src')).to.equal('/web-components/google-model-viewer.min.js'); | ||
expect(script.getAttribute('type')).to.equal('module'); | ||
}); | ||
}); |
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,7 @@ | ||
<script type="module" src="../../web-components/lazy-loader.js"></script> | ||
<lazy-loader> | ||
<template> | ||
<script src="42" type="test-type"></script> | ||
<some-custom-element id="12"></some-custom-element> | ||
</template> | ||
</lazy-loader> |
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,38 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* global describe it */ | ||
|
||
import { readFile } from '@web/test-runner-commands'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import '../../web-components/lazy-loader.js'; | ||
|
||
document.body.innerHTML = await readFile({ path: './lazy-loader.mock.html' }); | ||
|
||
describe('Lazy-loader Web Component test', async () => { | ||
const sceSelector = 'some-custom-element'; | ||
const scriptSelector = "script[src='42']"; | ||
|
||
it('Registers a lazy-loader component', async () => { | ||
const loader = customElements.get('lazy-loader'); | ||
expect(loader).to.exist; | ||
}); | ||
|
||
it('Does not activate elements before hlx:delayed ', async () => { | ||
expect(document.querySelector(sceSelector)).to.be.null; | ||
expect(document.querySelector(scriptSelector)).to.be.null; | ||
}); | ||
|
||
it('Activates elements once hlx:delayed is received', async () => { | ||
document.dispatchEvent(new CustomEvent('hlx:delayed')); | ||
const sce = document.body.querySelector(sceSelector); | ||
expect(sce).to.exist; | ||
expect(sce.getAttribute('id')).to.equal('12'); | ||
}); | ||
|
||
it('Moves scripts under document.head with async loading', async () => { | ||
document.dispatchEvent(new CustomEvent('hlx:delayed')); | ||
const script = document.head.querySelector(scriptSelector); | ||
expect(script).to.exist; | ||
expect(script.getAttribute('type')).to.equal('test-type'); | ||
expect(script.getAttribute('async')).to.equal('true'); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
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,35 @@ | ||
// Web Component that activates any <template> | ||
// elements that it contains only once the | ||
// 'hlx:delayed' event is received. | ||
class LazyLoader extends HTMLElement { | ||
static register() { | ||
customElements.define('lazy-loader', LazyLoader); | ||
} | ||
|
||
constructor() { | ||
super(); | ||
document.addEventListener('hlx:delayed', this.activateTemplates.bind(this)); | ||
} | ||
|
||
activateTemplates() { | ||
this.querySelectorAll('template').forEach((t) => { | ||
this.append(t.content.cloneNode(true)); | ||
this.querySelectorAll('script').forEach((s) => { | ||
// move any script tags found in the template | ||
// to document.head to activate them | ||
const script = document.createElement('script'); | ||
script.setAttribute('src', s.getAttribute('src')); | ||
script.setAttribute('type', s.getAttribute('type')); | ||
script.setAttribute('async', 'true'); | ||
document.head.appendChild(script); | ||
s.parentNode.removeChild(s); | ||
}); | ||
// Remove any "please wait" elements | ||
this.querySelectorAll('.remove').forEach((r) => { | ||
r.parentNode.removeChild(r); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
LazyLoader.register(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra
</div>