UNPKG

@vime/core

Version:

Customizable, extensible, accessible and framework agnostic media player.

1 lines 2.65 kB
import{r as t,c as s,h as i,g as e}from"./p-aa8acb66.js";import{c as a,p as n}from"./p-e3b0430a.js";import{c as h,h as r,e as o,x as c}from"./p-b018976f.js";import{L as d}from"./p-e855e573.js";import"./p-d84b1c8a.js";let m=0;const l=new Set,u=class{constructor(i){t(this,i),this.vmEmbedSrcChange=s(this,"vmEmbedSrcChange",3),this.vmEmbedMessage=s(this,"vmEmbedMessage",3),this.vmEmbedLoaded=s(this,"vmEmbedLoaded",3),this.srcWithParams="",this.hasEnteredViewport=!1,this.embedSrc="",this.mediaTitle="",this.params="",this.preconnections=[],h(this)}onEmbedSrcChange(){this.srcWithParams=r(this.embedSrc)&&this.embedSrc.length>0?a(this.embedSrc,this.params):void 0}srcWithParamsChange(){o(this.srcWithParams)||this.hasEnteredViewport||l.has(this.embedSrc)||n(this.srcWithParams)&&l.add(this.embedSrc),this.vmEmbedSrcChange.emit(this.srcWithParams)}preconnectionsChange(){this.hasEnteredViewport||this.preconnections.filter((t=>!l.has(t))).forEach((t=>{n(t)&&l.add(t)}))}connectedCallback(){this.lazyLoader=new d(this.host,["data-src"],(t=>{const s=t.getAttribute("data-src");t.removeAttribute("src"),c(s)||t.setAttribute("src",s)})),this.onEmbedSrcChange(),this.genIframeId()}disconnectedCallback(){this.lazyLoader.destroy()}onWindowMessage(t){var s,i,e;if(t.source!==(null===(s=this.iframe)||void 0===s?void 0:s.contentWindow)||r(this.origin)&&this.origin!==t.origin)return;const a=null!==(e=null===(i=this.decoder)||void 0===i?void 0:i.call(this,t.data))&&void 0!==e?e:t.data;a&&this.vmEmbedMessage.emit(a)}postMessage(t,s){var i,e,a,n,h,r;return a=this,n=void 0,r=function*(){null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage(JSON.stringify(t),null!=s?s:"*")},new((h=void 0)||(h=Promise))((function(t,s){function i(t){try{o(r.next(t))}catch(t){s(t)}}function e(t){try{o(r.throw(t))}catch(t){s(t)}}function o(s){var a;s.done?t(s.value):(a=s.value,a instanceof h?a:new h((function(t){t(a)}))).then(i,e)}o((r=r.apply(a,n||[])).next())}))}onLoad(){this.vmEmbedLoaded.emit()}genIframeId(){m+=1,this.id=`vm-iframe-${m}`}render(){return i("iframe",{id:this.id,class:"lazy",title:this.mediaTitle,"data-src":this.srcWithParams,allowFullScreen:!0,allow:"autoplay; encrypted-media; picture-in-picture;",onLoad:this.onLoad.bind(this),ref:t=>{this.iframe=t}})}get host(){return e(this)}static get watchers(){return{embedSrc:["onEmbedSrcChange"],params:["onEmbedSrcChange"],srcWithParams:["srcWithParamsChange"],preconnections:["preconnectionsChange"]}}};u.style=":host{z-index:var(--vm-media-z-index)}iframe{position:absolute;top:0;left:0;border:0;width:100%;height:100%;user-select:none}";export{u as vm_embed}