Skip to content
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

✨ [amp story captions] style-preset attribute with styles #37967

Merged
merged 36 commits into from Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
65cc1a1
Remove css doc header.
processprocess Mar 28, 2022
c4d9e32
default style
processprocess Mar 28, 2022
ac91de3
Update template
processprocess Mar 28, 2022
66e9422
update naming convention
processprocess Mar 28, 2022
4ea2014
remove nested captions.
processprocess Mar 28, 2022
0283bdb
css targeting
processprocess Mar 28, 2022
c67bae3
vtt
processprocess Mar 28, 2022
1805aba
vtt timing
processprocess Mar 28, 2022
6608e0e
naming
processprocess Mar 28, 2022
6659c0e
voice tag in vtt file
processprocess Mar 28, 2022
1b88c93
responsive typography
processprocess Apr 5, 2022
08d6642
responsive default margins
processprocess Apr 5, 2022
2fcc30d
revert
processprocess Apr 12, 2022
7e98b36
revert
processprocess Apr 12, 2022
9f2e923
newline
processprocess Apr 12, 2022
094e5ce
revert
processprocess Apr 12, 2022
31f2bc3
preset example page and style scope
processprocess Apr 12, 2022
bb65d92
preset css in shadow dom
processprocess Apr 12, 2022
c9892a8
lint
processprocess Apr 12, 2022
2968f39
newline, prect
processprocess Apr 13, 2022
1951d5a
revert
processprocess Apr 13, 2022
81d6239
preset values and tests
processprocess Apr 13, 2022
aaf391e
separate function and comment.
processprocess Apr 13, 2022
a4049d0
appear preset css
processprocess Apr 13, 2022
b2fed7e
variables
processprocess Apr 13, 2022
eff3cf1
styles
processprocess Apr 13, 2022
48629cc
capitalize
processprocess Apr 13, 2022
3c0d40c
capitalize
processprocess Apr 13, 2022
94f7ce6
Update validation.
processprocess Apr 13, 2022
5e23882
Merge branch 'main' into captions-style
processprocess Apr 14, 2022
4c7502e
Shadow reset. Move preset check to buildCB. Revert amp-video.
processprocess Apr 14, 2022
0dec2a0
change attribute name to style-preset
processprocess Apr 14, 2022
454855b
update css variable name
processprocess Apr 14, 2022
457d281
font stack with va
processprocess Apr 14, 2022
091d195
space
processprocess Apr 15, 2022
4cb7db7
deps
processprocess Apr 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions build-system/test-configs/dep-check-config.js
Expand Up @@ -227,6 +227,10 @@ exports.rules = [
// TODO(ccordry): remove this after createShadowRootWithStyle is moved to src
'extensions/amp-story-auto-ads/0.1/amp-story-auto-ads.js->extensions/amp-story/1.0/utils.js',
'extensions/amp-story-auto-ads/0.1/story-ad-ui.js->extensions/amp-story/1.0/utils.js',

// Story captions
'extensions/amp-story-captions/0.1/amp-story-captions.js->extensions/amp-story/1.0/utils.js',

// Story education
'extensions/amp-story-education/0.1/amp-story-education.js->extensions/amp-story/1.0/amp-story-store-service.js',
'extensions/amp-story-education/0.1/amp-story-education.js->extensions/amp-story/1.0/utils.js',
Expand Down
41 changes: 41 additions & 0 deletions examples/amp-story/amp-story-captions.html
Expand Up @@ -11,6 +11,7 @@
<script async custom-element="amp-audio" src="https://cdn.ampproject.org/v0/amp-audio-0.1.js"></script>
<script async custom-element="amp-story-captions" src="https://cdn.ampproject.org/v0/amp-story-captions-0.1.js"></script>
<script async custom-element="amp-story" src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Shadows+Into+Light&display=swap" rel="stylesheet">
<style amp-custom>
.bottom {
align-content: end;
Expand All @@ -31,6 +32,10 @@
.page2 {
background-color: #fff;
}

amp-story-captions {
/* font-size: 2px !important; */
}
</style>
</head>
<body>
Expand Down Expand Up @@ -74,6 +79,42 @@
</amp-story-grid-layer>
</amp-story-page>

<amp-story-page id="page-captions-preset-default" auto-advance-after="video-container">
<amp-story-grid-layer template="fill">
<amp-video id="video-container"
src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"
width="1280" height="720" layout="responsive" autoplay captions-id="captions-preset-default">
<track kind="subtitles" src="captions-for-container-layout.vtt" srclang="en" default>
</amp-video>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical" class="bottom">
<div class="scrim">
<div class="static-text">Default preset example.</div>
<div class="static-text">This element is always above captions and never overlaps.</div>
<amp-story-captions style-preset="default" id="captions-preset-default" layout="container"></amp-story-captions>
<div class="static-text">This element is always below captions and never overlaps.</div>
</div>
</amp-story-grid-layer>
</amp-story-page>

<amp-story-page id="page-captions-preset-appear" auto-advance-after="video-container">
<amp-story-grid-layer template="fill">
<amp-video id="video-container"
src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"
width="1280" height="720" layout="responsive" autoplay captions-id="captions-preset-appear">
<track kind="subtitles" src="captions-for-container-layout.vtt" srclang="en" default>
</amp-video>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical" class="bottom">
<div class="scrim">
<div class="static-text">Appear preset example.</div>
<div class="static-text">This element is always above captions and never overlaps.</div>
<amp-story-captions style-preset="appear" style="--story-captions-font-size: .8em; --story-captions-font-family: 'Shadows Into Light'" id="captions-preset-appear" layout="container"></amp-story-captions>
<div class="static-text">This element is always below captions and never overlaps.</div>
</div>
</amp-story-grid-layer>
</amp-story-page>

<amp-story-page id="page-fill" auto-advance-after="video-fill">
<amp-story-grid-layer template="fill">
<amp-video id="video-fill"
Expand Down
48 changes: 48 additions & 0 deletions extensions/amp-story-captions/0.1/amp-story-captions-presets.css
@@ -0,0 +1,48 @@
@import '../../amp-story/1.0/amp-story-shadow-reset.css';

/* default */
processprocess marked this conversation as resolved.
Show resolved Hide resolved

.amp-story-captions-default .amp-story-captions-cue-wrapper {
font-size: calc(2.5 * var(--story-page-vh, 8px)) !important;
font-family: 'Roboto', sans-serif !important;
processprocess marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this use Helvetica on iOS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On MacOs Yes

Screen Shot 2022-04-18 at 11 42 39 AM

It may use San Fransisco on iOS, which is the system font. Double checking.

margin: 1em 0 !important;
display: flex !important;
justify-content: center !important;
flex-direction: column !important;
align-items: center !important;
width: 100% !important;
}

.amp-story-captions-default .amp-story-captions-cue {
text-align: center !important;
color: #fff !important;
border-radius: .94em !important;
padding: 0.35em 0.94em !important;
background-color: hsla(0, 0%, 10%, 0.6) !important;
max-width: calc(var(--story-page-vw) * 92) !important;
}

.amp-story-captions-default .amp-story-captions-future {
color: #fff !important;
}

/* fade in */

.amp-story-captions-appear .amp-story-captions-cue-wrapper {
font-size: var(--story-captions-font-size, calc(2.5 * var(--story-page-vh, 8px))) !important;
font-family: var(--story-captions-font-family, 'Roboto'), 'Roboto', sans-serif !important;
}

.amp-story-captions-appear .amp-story-captions-cue {
color: #fff !important;
text-shadow: .06em .06em black !important;
}

.amp-story-captions-appear .amp-story-captions-cue span {
transition: opacity .3s cubic-bezier(0.3, 0, 0, 1) !important;
opacity: 1 !important;
}

.amp-story-captions-appear .amp-story-captions-cue span.amp-story-captions-future {
opacity: .1 !important;
}
16 changes: 0 additions & 16 deletions extensions/amp-story-captions/0.1/amp-story-captions.css
@@ -1,19 +1,3 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

amp-story-captions {
white-space: pre-wrap;
}
Expand Down
26 changes: 23 additions & 3 deletions extensions/amp-story-captions/0.1/amp-story-captions.js
@@ -1,3 +1,4 @@
import * as Preact from '#core/dom/jsx';
import {Layout_Enum, isLayoutSizeDefined} from '#core/dom/layout';
import {toArray} from '#core/types/array';

Expand All @@ -6,6 +7,14 @@ import {listen} from '#utils/event-helper';
import {TrackRenderer} from './track-renderer';

import {CSS} from '../../../build/amp-story-captions-0.1.css';
import {CSS as PRESETS_CSS} from '../../../build/amp-story-captions-presets-0.1.css';
import {createShadowRootWithStyle} from '../../amp-story/1.0/utils';

/**
* List of valid preset values.
* @const {Array<string>}
*/
const presetValues = ['default', 'appear'];

export class AmpStoryCaptions extends AMP.BaseElement {
/** @param {!AmpElement} element */
Expand All @@ -27,8 +36,16 @@ export class AmpStoryCaptions extends AMP.BaseElement {

/** @override */
buildCallback() {
this.container_ = this.element.ownerDocument.createElement('div');
this.element.appendChild(this.container_);
this.container_ = <div />;
// Check if style-preset is defined and valid.
// If valid it renders in shadow dom with preset syles.
const preset = this.element.getAttribute('style-preset');
if (presetValues.includes(preset)) {
this.container_.classList.add(`amp-story-captions-${preset}`);
createShadowRootWithStyle(this.element, this.container_, PRESETS_CSS);
processprocess marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.element.appendChild(this.container_);
}
}

/** @override */
Expand Down Expand Up @@ -57,7 +74,10 @@ export class AmpStoryCaptions extends AMP.BaseElement {
);
}

/** Creates new track renderers for current textTracks. */
/**
* Creates new track renderers for current textTracks.
* @private
*/
updateTracks_() {
while (this.trackRenderers_.length) {
this.trackRenderers_.pop().dispose();
Expand Down
29 changes: 23 additions & 6 deletions extensions/amp-story-captions/0.1/test/test-amp-story-captions.js
Expand Up @@ -45,12 +45,7 @@ describes.realWin(
win.document.body.appendChild(element);
});

it('should contain be empty when built', async () => {
await element.whenBuilt();
expect(element.querySelector('div').textContent).to.equal('');
});

it('update on cuechange', async () => {
async function setUpVideoWithCaptions() {
await element.whenBuilt();
const impl = await element.getImpl();

Expand Down Expand Up @@ -80,6 +75,16 @@ describes.realWin(
expect(element.querySelector('div').textContent).to.equal(
'first caption'
);
return video;
}

it('should be empty when built', async () => {
await element.whenBuilt();
expect(element.querySelector('div').textContent).to.equal('');
});

it('update on cuechange', async () => {
const video = await setUpVideoWithCaptions();

video.currentTime = 0.5;
await new Promise((resolve) => {
Expand All @@ -89,5 +94,17 @@ describes.realWin(
'second caption'
);
});

it('should apply preset when attribute is present and value is valid', async () => {
element.setAttribute('style-preset', 'default');
await setUpVideoWithCaptions();
expect(element.querySelector('.amp-story-captions-default')).to.exist;
});

it('should not apply preset when attribute value is invalid', async () => {
element.setAttribute('style-preset', 'dflt');
await setUpVideoWithCaptions();
expect(element.querySelector('.amp-story-captions-default')).to.be.null;
});
}
);
Expand Up @@ -23,7 +23,7 @@
<amp-video src="https://example.com/video.mp4" poster="https://example.com/poster.png" width="1080" height="1920" layout="responsive" crossorigin autoplay captions-id="captions">
<track kind="subtitles" src="https://example.com/captions.vtt" srclang="en" default>
</amp-video>
<amp-story-captions id="captions" layout="fixed-height" height="300"></amp-story-captions>
<amp-story-captions id="captions" layout="fixed-height" height="300" style-preset="default"></amp-story-captions>
</amp-story-grid-layer>
</amp-story-page>
</amp-story>
Expand Down
Expand Up @@ -24,7 +24,7 @@ PASS
| <amp-video src="https://example.com/video.mp4" poster="https://example.com/poster.png" width="1080" height="1920" layout="responsive" crossorigin autoplay captions-id="captions">
| <track kind="subtitles" src="https://example.com/captions.vtt" srclang="en" default>
| </amp-video>
| <amp-story-captions id="captions" layout="fixed-height" height="300"></amp-story-captions>
| <amp-story-captions id="captions" layout="fixed-height" height="300" style-preset="default"></amp-story-captions>
| </amp-story-grid-layer>
| </amp-story-page>
| </amp-story>
Expand Down
2 changes: 2 additions & 0 deletions extensions/amp-story-captions/0.1/track-renderer.js
Expand Up @@ -41,6 +41,7 @@ export class TrackRenderer {
/** @private {!Element} */
this.element_ = container.ownerDocument.createElement('div');
container.appendChild(this.element_);
this.element_.classList.add('amp-story-captions-cue-wrapper');

/** @private {!Array<number>} */
this.cueTimestamps_ = [];
Expand Down Expand Up @@ -74,6 +75,7 @@ export class TrackRenderer {
this.cueTimestamps_.length = 0;
toArray(this.track_.activeCues).forEach((cue) => {
const cueElement = this.element_.ownerDocument.createElement('div');
cueElement.classList.add('amp-story-captions-cue');
const html = cue.getCueAsHTML();
let section = this.element_.ownerDocument.createElement('span');
cueElement.appendChild(section);
Expand Down
Expand Up @@ -44,4 +44,9 @@ tags: { # <amp-story-captions>
child_tags: {
mandatory_num_child_tags: 0
}
attrs: {
name: "style-preset"
value: "default"
value: "appear"
}
}