UNPKG

vue3-disqus

Version:

Vue component to integrate Disqus comments in your application, with support for SPA

2 lines (1 loc) 3.84 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("vue"),S="Disqus shortname is required",w=["api_key","author_s3","category_id","identifier","integration","language","remote_auth_s3","slug","title","url"],D=["name","button","icon","url","logout","profile_url","width","height"],E=["preData","preInit","afterRender","onInit","onReady","onIdentify","onPaginate","onNewComment","beforeComment","preReset"],h=Symbol("disqus"),O=e=>e.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase().replace("on-",""),y=e=>requestAnimationFrame(()=>requestAnimationFrame(e)),I=t.defineComponent({name:"DisqusComments",props:{url:String,shortname:String,class:String,identifier:{type:String,required:!0},pageConfig:{type:Object,validator:e=>Object.keys(e).every(o=>w.includes(o))},ssoConfig:{type:Object,validator:e=>Object.keys(e).every(o=>D.includes(o))},language:{type:String,default:"en"},lazy:{type:Boolean,default:!0},lazyConfig:{type:Object,default:()=>({root:null,rootMargin:"300px",threshold:.5})}},setup(e,{emit:o}){const s=t.inject(h,{}),u=t.getCurrentInstance(),a=t.ref(null),f=t.computed(()=>{const n=e.shortname||s.shortname;if(!n)throw new Error(S);return n});t.watch(()=>e.language,d),t.onMounted(()=>{if(e.lazy)return m();g()});function g(){if(s&&(s.reset=d),window.DISQUS)return d();window.disqus_config=function(){_(this)},l(),t.watch(()=>e.url,d)}function d(){window.DISQUS.reset({reload:!0,config:function(){_(this)}})}function m(){var n;if("IntersectionObserver"in window)return a.value=new IntersectionObserver(c,e.lazyConfig),a.value.observe((n=u==null?void 0:u.proxy)==null?void 0:n.$el);g()}function c(n){n.forEach(r=>{var i;r.isIntersecting&&((i=a.value)==null||i.disconnect(),g())})}function l(){const n=+new Date,r=document,i=r.createElement("script");i.setAttribute("id","embed-disqus-script"),i.setAttribute("data-timestamp",n.toString()),i.type="text/javascript",i.async=!0,i.src=`//${f.value}.disqus.com/embed.js`,(r.body||r.head).appendChild(i)}function _(n){v(n),p(n),e.ssoConfig&&Object.keys(e.ssoConfig).length&&Object.assign(n.sso,e.ssoConfig),n.language=e.language}function v(n){const r={url:e.url,identifier:e.identifier};Object.assign(n.page,r),e.pageConfig&&Object.keys(e.pageConfig).length&&Object.assign(n.page,e.pageConfig)}function p(n){E.forEach(r=>{n.callbacks[r]=[i=>{o(O(r),i)}]})}}});const C=(e,o)=>{const s=e.__vccOpts||e;for(const[u,a]of o)s[u]=a;return s},$={id:"disqus_thread"};function j(e,o,s,u,a,f){return t.openBlock(),t.createElementBlock("div",$)}const q=C(I,[["render",j]]),k=t.defineComponent({name:"DisqusCount",props:{url:String,shortname:String,identifier:{type:String,required:!0},tag:{type:String,default:"span"}},setup(e){const o=t.inject(h,{}),s=t.computed(()=>e.tag==="a"?`${e.url}#disqus_thread`:"#"),u=t.computed(()=>e.tag==="span"?e.url:null),a=t.computed(()=>{const c=e.shortname||o.shortname;if(!c)throw new Error(S);return c}),f={href:s.value,"data-disqus-url":u.value,"data-disqus-identifier":e.identifier};t.onMounted(g);function g(){if("DISQUSWIDGETS"in window)return y(()=>d());m(),t.watch(()=>e.url,()=>y(()=>d()))}function d(){window.DISQUSWIDGETS.getCount({reset:!0})}function m(){if(document.getElementById("dsq-count-scr"))return;const c=document,l=c.createElement("script");l.async=!0,l.id="dsq-count-scr",l.src=`//${a.value}.disqus.com/count.js`,(c.head||c.body).appendChild(l)}return{disqusAttrs:f}}});function A(e,o,s,u,a,f){return t.openBlock(),t.createBlock(t.resolveDynamicComponent(e.tag),t.mergeProps({class:"disqus-comment-count"},e.disqusAttrs),null,16)}const b=C(k,[["render",A]]);function R(e,o={}){e.component("DisqusComments",q),e.component("DisqusCount",b);const s={shortname:o.shortname,reset:null};e.provide(h,s)}exports.DisqusComments=q;exports.DisqusCount=b;exports.default=R;