2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-21 14:20:34 +03:00
Files
vue-meta/dist/vue-meta.global.min.js
T
2021-05-23 22:36:11 +00:00

9 lines
9.3 KiB
JavaScript

/**
* vue-meta v3.0.0-alpha.7
* (c) 2021
* - Pim (@pimlie)
* - All the amazing contributors
* @license MIT
*/
var VueMeta=function(t,e){"use strict";const r=(t,e)=>(r,o)=>{let n=-1;if(o.reduce(((e,r,o)=>{const s=t(e,r);return s!==e?(n=o,s):e}),e),n>-1)return r[n]},o=r(((t,e)=>{const{depth:r}=e;return!t||r>t?r:t}));var n=Object.freeze({__proto__:null,setup:t=>{let e=0;if(t.vm){let{vm:r}=t;do{r.parent&&(e++,r=r.parent)}while(r&&r.parent&&r!==r.root)}t.depth=e},resolve:o});const s={body:{tag:"script",to:"body"},base:{valueAttribute:"href"},charset:{tag:"meta",nameless:!0,valueAttribute:"charset"},description:{tag:"meta"},og:{group:!0,namespacedAttribute:!0,tag:"meta",keyAttribute:"property"},twitter:{group:!0,namespacedAttribute:!0,tag:"meta"},htmlAttrs:{attributesFor:"html"},headAttrs:{attributesFor:"head"},bodyAttrs:{attributesFor:"body"}},i={title:{attributes:!1},base:{contentAsAttribute:!0,attributes:["href","target"]},meta:{contentAsAttribute:!0,keyAttribute:"name",attributes:["content","name","http-equiv","charset"]},link:{contentAsAttribute:!0,attributes:["href","crossorigin","rel","media","integrity","hreflang","type","referrerpolicy","sizes","imagesrcset","imagesizes","as","color"]},style:{attributes:["media"]},script:{attributes:["src","type","nomodule","async","defer","crossorigin","integrity","referrerpolicy"]},noscript:{attributes:!1}};Object.freeze({}),Object.freeze([]);const c=Array.isArray,a=t=>"function"==typeof t,u=t=>"string"==typeof t,l=t=>null!==t&&"object"==typeof t,f=Object.prototype.toString,d=t=>"[object Object]"===f.call(t),p=Symbol("kIsProxy"),m=Symbol("kProxySources"),h=Symbol("kProxyTarget"),b=Symbol("kResolveContext");function g(t){if(c(t))return t.map(g);if(l(t)){const e={};for(const r in t)e[r]="context"===r?t[r]:g(t[r]);return e}return t}const v=(t,e,r)=>{const o=[];for(const n of t)n&&e in n&&(o.push(n[e]),r&&r(n));return o},y=(t,e=[],r,o)=>{if(!r&&!o&&(({active:r,sources:o}=t),e.length))for(let t=0;t<e.length;t++){const n=e[t];if(!r||!r[n])return void console.error(`recompute: segment ${n} not found on target`,e,r);r=r[n],o=o.map((t=>t[n])).filter(Boolean)}if(!r||!o)return;const n=((t,...e)=>{const r=t?Object.keys(t):[];if(e)for(const t of e)if(t&&l(t))for(const e in t)r.includes(e)||r.push(e);return r})(...o),s=Object.keys(r);for(const t of s)n.includes(t)||delete r[t];for(const s of n){let n=!1;for(let t=0;t<o.length;t++){const e=o[t];if(e&&s in e&&void 0!==e[s]){n=d(e[s]);break}}if(n){r[s]||(r[s]={});const n=[];for(const t of o)s in t&&n.push(t[s]);y(t,[...e,s],r[s],n);continue}!r[s]&&c(o[0][s])&&(r[s]=[]);const i=[],a=v(o,s,(t=>i.push(t[b])));let u=t.resolve(a,i,r[s],s,e);d(u)&&(u=g(u)),r[s]=u}},S=(t,r,o,n=[])=>{const s=A(t,o,n),i=e.markRaw(new Proxy(r,s));return!n.length&&t.sources&&t.sources.push(i),i},A=(t,e,r=[])=>({get:(o,n,s)=>{if(n===p)return!0;if(n===m)return t.sources;if(n===h)return o;if(n===b)return e;let i=Reflect.get(o,n,s);if(!l(i))return i;if(!i[p]){const s=[...r,n];i=S(t,i,e,s),Reflect.set(o,n,i)}return i},set:(e,o,n)=>{const s=Reflect.set(e,o,n);if(s){const i=c(e);let a,u=!1,{sources:l,active:f}=t,p=0;for(const t of r){if(l=v(l,t),i&&p===r.length-1){a=t;break}c(f)&&(u=!0),f=f[t],p++}if(u)return y(t),s;if(d(n))return y(t,r),s;let m,h=[];i?(m=l,h=l.map((t=>t[b]))):m=v(l,o,(t=>h.push(t[b])));let S=t.resolve(m,h,f,o,r);d(S)&&(S=g(S)),i&&a?f[a]=S:f[o]=S}return s},deleteProperty:(e,o)=>{const n=Reflect.deleteProperty(e,o);if(n){const n=c(e);let s,i=t.sources,a=t.active,u=0;for(const t of r){if(i=i.map((e=>e&&e[t])),n&&u===r.length-1){s=t;break}a=a[t],u++}if(i.some((t=>t&&o in t))){let e,c=[];n?(e=i,c=i.map((t=>t[b]))):e=v(i,o,(t=>c.push(t[b])));let u=t.resolve(e,c,a,o,r);d(u)&&(u=g(u)),n&&s?a[s]=u:a[o]=u}else delete a[o]}return n}}),k={};function j(t,r,o,n){return"attributesFor"in n?function(t,r,o,n){const{attributesFor:s}=n;if(!s||!o)return;if(t.isSSR)return{to:"",vnode:e.h(`ssr-${s}`,o)};if(!k[s]){const[t,e]=Array.from(document.querySelectorAll(s));if(!t)return void console.error("Could not find element for selector",s,", won't render attributes");e&&console.warn("Found multiple elements for selector",s),k[s]={el:t,attrs:[]}}const{el:i,attrs:a}=k[s];for(const e in o){let n=C(t,`${r}(${e})`,o[e],o);c(n)&&(n=n.join(",")),i.setAttribute(e,n||""),a.includes(e)||a.push(e)}const u=a.filter((t=>!o[t]));for(const t of u)i.removeAttribute(t)}(t,r,o,n):"group"in n?function(t,e,r,o){if(c(r))return console.warn("Specifying an array for group properties isnt supported"),[];return Object.keys(r).map((n=>{const s={group:e,data:r};if(o.namespaced)s.tagNamespace=!0===o.namespaced?e:o.namespaced;else if(o.namespacedAttribute){const t=!0===o.namespacedAttribute?e:o.namespacedAttribute;s.fullName=`${t}:${n}`,s.slotName=`${t}(${n})`}return w(t,e,r[n],o,s)})).filter(Boolean).flat()}(t,r,o,n):w(t,r,o,n)}function w(t,r,o,n={},s){const a=["content","json","rawContent"],l=t=>function(t,e){for(const r of t){const t=i[r];if(r&&t)return t[e]}}([f,n.tag],t);if(c(o))return o.map((e=>w(t,r,e,n,s))).filter(Boolean).flat();const{tag:f=n.tag||r}=o;let d="",p=!1,m=!1;if(u(o))d=o;else if(o.children&&c(o.children))p=!0,d=o.children.map((e=>{const o=w(t,r,e,n,s);return c(o)?o.map((({vnode:t})=>t)):o&&o.vnode}));else{let t=0;for(const e of a){if(!d&&o[e]){d=1===t?JSON.stringify(o[e]):o[e],m=t>1;break}t++}}const h=s&&s.fullName||r,b=s&&s.slotName||r;let{attrs:g}=o;if(g||"object"!=typeof o)g||(g={});else{g={...o},delete g.tag,delete g.children,delete g.to;for(const t of a)delete g[t]}if(p)d=C(t,b,d,o);else{const e=!!l("contentAsAttribute");let{valueAttribute:r}=n;if(!r&&e){const[t]=l("attributes");r=u(e)?e:t}if(r){const{nameless:e}=n;if(!e){const t=n.keyAttribute||l("keyAttribute");t&&(g[t]=h)}g[r]=C(t,b,g[r]||d,s),d=""}else d=C(t,b,d,o)}const v=s&&s.tagNamespace?`${s.tagNamespace}:${f}`:f;if("title"===v&&!t.isSSR)return void(document.title=d);m&&d&&(g.innerHTML=d);const y=e.h(v,g,d||void 0);return{to:o.to,vnode:y}}function C({metainfo:t,slots:e},r,o,n){const s=e&&e[r];if(!s||!a(s))return o;const i={content:o,metainfo:t};if(n&&n.group){const{group:t,data:e}=n;i[t]=e}const c=s(i);if(c&&c.length){const{children:t}=c[0];return t?t.toString():""}return o}const M="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,R=(t=>M?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("meta_active");function O(t,e,r){for(const o in e)o in r?l(t[o])?O(t[o],e[o],r[o]):e[o]!==r[o]&&(t[o]=e[o]):t[o]=e[o];for(const o in r)e&&o in e||delete t[o]}function x(t){if(t||(t=e.getCurrentInstance()||void 0),t)return t.appContext.config.globalProperties.$metaManager}const N=e.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=x();if(t)return t.render({slots:e})}}),$="data-vm-ssr";function P(t,r,o,n){const s=c(n)?n:[n];t?o.endsWith("Attrs")||s.forEach((t=>{t.props||(t.props={}),t.props[$]=!0})):s.forEach(((t,r)=>{t.type===e.Comment&&s.splice(r,1)})),r[o]||(r[o]=[]),r[o].push(...s)}class F{constructor(t,e,r,o){this.isSSR=!1,this.ssrCleanedUp=!1,this.isSSR=t,this.config=e,this.target=r,o&&"setup"in o&&a(o.setup)&&(this.resolver=o)}install(t){t.component("Metainfo",N),t.config.globalProperties.$metaManager=this,t.provide(R,this.target.context.active)}addMeta(t,r){r||(r=e.getCurrentInstance()||void 0);const o={removed:[]},n={vm:r},{resolver:s}=this;s&&s.setup&&s.setup(n);const i=this.target.addSource(t,n,!0),c=t=>this.unmount(!!t,i,o,r);return r&&e.onUnmounted(c),{meta:i,onRemoved:t=>o.removed.push(t),unmount:c}}unmount(t,e,r,o){if(o){const{$el:n}=o.proxy;if(n&&n.offsetParent){let o=new MutationObserver((s=>{for(const{removedNodes:i}of s)i&&i.forEach((s=>{s===n&&o&&(o.disconnect(),o=void 0,this.reallyUnmount(t,e,r))}))}));return void o.observe(n.parentNode,{childList:!0})}}this.reallyUnmount(t,e,r)}async reallyUnmount(t,e,r){this.target.delSource(e),!t&&r&&await Promise.all(r.removed.map((t=>t())))}render({slots:t}={}){const r=this.target.context.active,{isSSR:o}=this;if(!o&&!this.ssrCleanedUp){this.ssrCleanedUp=!0;const t=()=>{const t=document.querySelectorAll("[data-vm-ssr]");t&&t.length&&t.forEach((t=>t.parentNode&&t.parentNode.removeChild(t)))};"loading"===document.readyState?window.addEventListener("DOMContentLoaded",t,{once:!0}):t()}const n={};for(const e in r){const s=this.config[e]||{};let i=j({isSSR:o,metainfo:r,slots:t},e,r[e],s);if(!i)continue;c(i)||(i=[i]);let a="base"!==e&&r[e].to;!a&&"to"in s&&(a=s.to),!a&&"attributesFor"in s&&(a=e);for(const{to:t,vnode:e}of i)P(this.isSSR,n,t||a||"head",e)}if(t)for(const e in t){const o="default"===e?"head":e;if("head"!==o&&"body"!==o)continue;const s=t[e];a(s)&&P(this.isSSR,n,o,s({metainfo:r}))}return Object.keys(n).map((t=>e.h(e.Teleport,{to:t},n[t])))}}return F.create=(t,r,o)=>{const n=((t,e)=>{const r=[],o={active:e,resolve:t,sources:r},n=()=>y(o);return{context:o,compute:n,addSource:(t,e,r=!1)=>{const s=S(o,t,e||{});return r&&n(),s},delSource:(t,e=!0)=>{const o=r.findIndex((e=>e===t||e[h]===t));return o>-1&&(r.splice(o,1),e&&n(),!0)}}})(((t,e,r,n,s)=>a(o)?o(t,e,r,n,s):o.resolve(t,e,r,n,s)),e.reactive({}));return new F(t,r,n,o)},t.createMetaManager=(t=!1,e,r)=>F.create(t,e||s,r||n),t.deepestResolver=n,t.defaultConfig=s,t.getCurrentManager=x,t.resolveOption=r,t.useActiveMeta=function(){return e.inject(R)},t.useMeta=function(t,r){const o=e.getCurrentInstance()||void 0;if(!r&&o&&(r=x(o)),!r)throw new Error("No manager or current instance");e.isProxy(t)&&(e.watch(t,((t,e)=>{O(n.meta,t,e)})),t=t.value);const n=r.addMeta(t,o);return n},Object.defineProperty(t,"__esModule",{value:!0}),t}({},Vue);