-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(youtube-player): Add width and height as inputs #16734
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './youtube-module'; | ||
export * from './youtube-player'; | ||
export {YouTubePlayer} from './youtube-player'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,9 @@ declare global { | |
interface Window { YT: typeof YT | undefined; } | ||
} | ||
|
||
export const DEFAULT_PLAYER_WIDTH = 640; | ||
export const DEFAULT_PLAYER_HEIGHT = 390; | ||
|
||
// The native YT.Player doesn't expose the set videoId, but we need it for | ||
// convenience. | ||
interface Player extends YT.Player { | ||
|
@@ -62,17 +65,33 @@ type UninitializedPlayer = Pick<Player, 'videoId' | 'destroy' | 'addEventListene | |
}) | ||
export class YouTubePlayer implements AfterViewInit, OnDestroy { | ||
/** YouTube Video ID to view */ | ||
get videoId(): string | undefined { | ||
return this._player && this._player.videoId; | ||
} | ||
|
||
@Input() | ||
get videoId(): string | undefined { return this._player && this._player.videoId; } | ||
set videoId(videoId: string | undefined) { | ||
this._videoId.emit(videoId); | ||
} | ||
|
||
private _videoId = new EventEmitter<string | undefined>(); | ||
|
||
/** Height of video player */ | ||
@Input() | ||
get height(): number | undefined { return this._height; } | ||
set height(height: number | undefined) { | ||
this._height = height || DEFAULT_PLAYER_HEIGHT; | ||
this._heightObs.emit(this._height); | ||
} | ||
private _height = DEFAULT_PLAYER_HEIGHT; | ||
private _heightObs = new EventEmitter<number>(); | ||
|
||
/** Width of video player */ | ||
@Input() | ||
get width(): number | undefined { return this._width; } | ||
set width(width: number | undefined) { | ||
this._width = width || DEFAULT_PLAYER_WIDTH; | ||
this._widthObs.emit(this._width); | ||
} | ||
private _width = DEFAULT_PLAYER_WIDTH; | ||
private _widthObs = new EventEmitter<number>(); | ||
|
||
/** Outputs are direct proxies from the player itself. */ | ||
@Output() ready = new EventEmitter<YT.PlayerEvent>(); | ||
@Output() stateChange = new EventEmitter<YT.OnStateChangeEvent>(); | ||
|
@@ -94,17 +113,25 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy { | |
'Please install the YouTube Player API Reference for iframe Embeds: ' + | ||
'https://developers.google.com/youtube/iframe_api_reference'); | ||
} | ||
// Add initial values to all of the inputs. | ||
const widthObs = this._widthObs.pipe(startWith(this._width)); | ||
const heightObs = this._heightObs.pipe(startWith(this._height)); | ||
|
||
/** An observable of the currently loaded player. */ | ||
const playerObs = | ||
createPlayerObservable( | ||
this._youtubeContainer, | ||
this._videoId, | ||
widthObs, | ||
heightObs, | ||
this.createEventsBoundInZone(), | ||
).pipe(waitUntilReady(), takeUntil(this._destroyed), publish()); | ||
|
||
/** Set up side effects to bind inputs to the player. */ | ||
playerObs.subscribe(player => this._player = player); | ||
|
||
bindSizeToPlayer(playerObs, widthObs, heightObs); | ||
|
||
bindCueVideoCall(playerObs, this._videoId, this._destroyed); | ||
|
||
// After all of the subscriptions are set up, connect the observable. | ||
|
@@ -146,6 +173,16 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy { | |
} | ||
} | ||
|
||
/** Listens to changes to the given width and height and sets it on the player. */ | ||
function bindSizeToPlayer( | ||
playerObs: Observable<YT.Player | undefined>, | ||
widthObs: Observable<number>, | ||
heightObs: Observable<number> | ||
) { | ||
return combineLatest(playerObs, widthObs, heightObs) | ||
.subscribe(([player, width, height]) => player && player.setSize(width, height)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Width and height are needed for the constructor of the player as well, and I think a stream fits better when managing the player initialization. I could use ngOnChanges for the setSize call, but that won't eliminate the streams entirely, so I think it'll just add complexity if we have two different means of monitoring changes on the same value. |
||
} | ||
|
||
/** | ||
* Returns an observable that emits the loaded player once it's ready. Certain properties/methods | ||
* won't be available until the iframe finishes loading. | ||
|
@@ -190,13 +227,16 @@ function fromPlayerOnReady(player: UninitializedPlayer): Observable<Player> { | |
function createPlayerObservable( | ||
youtubeContainer: Observable<HTMLElement>, | ||
videoIdObs: Observable<string | undefined>, | ||
widthObs: Observable<number>, | ||
heightObs: Observable<number>, | ||
events: YT.Events, | ||
): Observable<UninitializedPlayer | undefined> { | ||
|
||
const playerOptions = | ||
videoIdObs | ||
.pipe( | ||
map((videoId) => videoId ? ({videoId, events}) : undefined), | ||
withLatestFrom(combineLatest(widthObs, heightObs)), | ||
map(([videoId, [width, height]]) => videoId ? ({videoId, width, height, events}) : undefined), | ||
); | ||
|
||
return combineLatest(youtubeContainer, playerOptions) | ||
|
Uh oh!
There was an error while loading. Please reload this page.