@7sage/vidstack
Version:
UI component library for building high-quality, accessible video and audio experiences on the web.
130 lines (125 loc) • 4.36 kB
JavaScript
import '../define/vidstack-player.js';
import { isString, defineCustomElement, kebabToCamelCase } from '../chunks/vidstack-BGSTndAW.js';
import { isHTMLAudioElement, isHTMLVideoElement, isHTMLIFrameElement } from '../chunks/vidstack-BUfZMlAO.js';
import { isHTMLElement } from '../chunks/vidstack-C2US-gSO.js';
import '../chunks/vidstack-CejDJ6BP.js';
import '../chunks/vidstack-DJDnh4xT.js';
import '../chunks/vidstack-xMS8dnYq.js';
import '../chunks/vidstack-ChQTHmIQ.js';
import '../chunks/vidstack-Ci54COQW.js';
import '../chunks/vidstack-CTojmhKq.js';
import '../chunks/vidstack-DYbwIVLq.js';
import '../chunks/vidstack-D5EzK014.js';
import '../chunks/vidstack-B01xzxC4.js';
import '../chunks/vidstack-C9vIqaYT.js';
import '@floating-ui/dom';
import '../chunks/vidstack-Dihypf8P.js';
import '../chunks/vidstack-DsPOyKtl.js';
class VidstackPlayerLayout {
constructor(props) {
this.props = props;
}
name = "vidstack";
async load() {
await import('../define/vidstack-player-default-layout.js');
await import('../define/vidstack-player-ui.js');
}
create() {
const layouts = [
document.createElement("media-audio-layout"),
document.createElement("media-video-layout")
];
if (this.props) {
for (const [prop, value] of Object.entries(this.props)) {
for (const el of layouts) el[prop] = value;
}
}
return layouts;
}
}
class PlyrLayout {
constructor(props) {
this.props = props;
}
name = "plyr";
async load() {
await import('../define/plyr-layout.js');
}
create() {
const layout = document.createElement("media-plyr-layout");
if (this.props) {
for (const [prop, value] of Object.entries(this.props)) {
layout[prop] = value;
}
}
return [layout];
}
}
const LAYOUT_LOADED = Symbol();
class VidstackPlayer {
static async create({ target, layout, tracks, ...props }) {
if (isString(target)) {
target = document.querySelector(target);
}
if (!isHTMLElement(target)) {
throw Error(`[vidstack] target must be of type \`HTMLElement\`, found \`${typeof target}\``);
}
let player = document.createElement("media-player"), provider = document.createElement("media-provider"), layouts, isTargetContainer = !isHTMLAudioElement(target) && !isHTMLVideoElement(target) && !isHTMLIFrameElement(target);
player.setAttribute("keep-alive", "");
if (props.poster && layout?.name !== "plyr") {
if (!customElements.get("media-poster")) {
const { MediaPosterElement } = await import('../chunks/vidstack-D3ltXc3a.js');
defineCustomElement(MediaPosterElement);
}
const poster = document.createElement("media-poster");
if (layout?.name === "vidstack") poster.classList.add("vds-poster");
provider.append(poster);
}
if (layout) {
target.removeAttribute("controls");
if (!layout[LAYOUT_LOADED]) {
await layout.load();
layout[LAYOUT_LOADED] = true;
}
layouts = await layout.create();
}
const title = target.getAttribute("title");
if (title) player.setAttribute("title", title);
const width = target.getAttribute("width"), height = target.getAttribute("height");
if (width || height) {
if (width) player.style.width = width;
if (height) player.style.height = height;
player.style.aspectRatio = "unset";
}
for (const attr of target.attributes) {
const name = attr.name.replace("data-", ""), propName = kebabToCamelCase(name);
if (propName in player) {
player.setAttribute(name, attr.value);
} else if (layouts?.length) {
for (const layout2 of layouts) {
if (propName in layout2) {
layout2.setAttribute(name, attr.value);
}
}
}
}
for (const [prop, value] of Object.entries(props)) {
player[prop] = value;
}
if (tracks) {
for (const track of tracks) player.textTracks.add(track);
}
player.append(provider);
if (layouts) {
for (const layout2 of layouts) player.append(layout2);
}
if (isTargetContainer) {
target.append(player);
} else {
for (const child of [...target.children]) provider.append(child);
target.replaceWith(player);
}
return player;
}
}
export { PlyrLayout, VidstackPlayer, VidstackPlayerLayout };