2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-22 04:00:33 +03:00
Files
vue-meta/dist/vue-meta.global.min.js
T
2021-04-04 23:06:23 +00:00

9 lines
8.5 KiB
JavaScript

/**
* vue-meta v3.0.0-alpha.3
* (c) 2021
* - Pim (@pimlie)
* - All the amazing contributors
* @license MIT
*/
var VueMeta=function(t,e){"use strict";const r=(t,e)=>(r,n)=>{let o=-1;if(n.reduce(((e,r,n)=>{const s=t(e,r);return s!==e?(o=n,s):e}),e),o>-1)return r[o]};const n=r(((t,e)=>{const{depth:r}=e;return!t||r>t?r:t}));var o=Object.freeze({__proto__:null,setup:function(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:n});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"),b=Symbol("kProxyTarget"),h=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 y=(t,e,r)=>{const n=[];for(const o of t)e in o&&(n.push(o[e]),r&&r(o));return n},v=(t,e,r,n=[])=>{if(n.length||(r||(r=t.active),e||(e=t.sources)),!r||!e)return;const o=((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})(...e),s=Object.keys(r);for(const t of s)o.includes(t)||delete r[t];for(const s of o){if(d(e[0][s])){r[s]||(r[s]={});const o=[];for(const t of e)s in t&&o.push(t[s]);v(t,o,r[s],[...n,s]);continue}!r[s]&&c(e[0][s])&&(r[s]=[]);const o=[],i=y(e,s,(t=>o.push(t[h])));let a=t.resolve(i,o,r[s],s,n);d(a)&&(a=g(a)),r[s]=a}},A=(t,r,n,o=[])=>{const s=S(t,n,o),i=e.markRaw(new Proxy(r,s));return!o.length&&t.sources&&t.sources.push(i),i},S=(t,e,r=[])=>({get:(n,o,s)=>{if(o===p)return!0;if(o===m)return t.sources;if(o===b)return n;if(o===h)return e;let i=Reflect.get(n,o,s);if(!l(i))return i;if(!i[p]){const s=[...r,o];i=A(t,i,e,s),Reflect.set(n,o,i)}return i},set:(e,n,o)=>{const s=Reflect.set(e,n,o);if(s){const o=c(e);let i,a=!1,{sources:u,active:l}=t,f=0;for(const t of r){if(u=y(u,t),o&&f===r.length-1){i=t;break}c(l)&&(a=!0),l=l[t],f++}if(a)return v(t),s;let p,m=[];o?(p=u,m=u.map((t=>t[h]))):p=y(u,n,(t=>m.push(t[h])));let b=t.resolve(p,m,l,n,r);d(b)&&(b=g(b)),o&&i?l[i]=b:l[n]=b}return s},deleteProperty:(e,n)=>{const o=Reflect.deleteProperty(e,n);if(o){const o=c(e);let s,i=t.sources,a=t.active,u=0;for(const t of r){if(i=i.map((e=>e[t])),o&&u===r.length-1){s=t;break}a=a[t],u++}if(i.some((t=>n in t))){let e,c=[];o?(e=i,c=i.map((t=>t[h]))):e=y(i,n,(t=>c.push(t[h])));let u=t.resolve(e,c,a,n,r);d(u)&&(u=g(u)),o&&s?a[s]=u:a[n]=u}else delete a[n]}return o}}),k={};function w(t,e,r,n){return"attributesFor"in n?function(t,e,r,n){const{attributesFor:o}=n;if(!o)return;if(!k[o]){const[t,e]=Array.from(document.querySelectorAll(o));if(!t)return void console.error("Could not find element for selector",o,", won't render attributes");e&&console.warn("Found multiple elements for selector",o),k[o]={el:t,attrs:[]}}const{el:s,attrs:i}=k[o];for(const n in r){const o=M(t,`${e}(${n})`,r[n],r);s.setAttribute(n,o||""),i.includes(n)||i.push(n)}const c=i.filter((t=>!r[t]));for(const t of c)s.removeAttribute(t)}(t,e,r,n):"group"in n?function(t,e,r,n){if(c(r))return console.warn("Specifying an array for group properties isnt supported"),[];return Object.keys(r).map((o=>{const s={group:e,data:r};if(n.namespaced)s.tagNamespace=!0===n.namespaced?e:n.namespaced;else if(n.namespacedAttribute){const t=!0===n.namespacedAttribute?e:n.namespacedAttribute;s.fullName=`${t}:${o}`,s.slotName=`${t}(${o})`}return C(t,e,r[o],n,s)})).flat()}(t,e,r,n):C(t,e,r,n)}function C(t,r,n,o={},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,o.tag],t);if(c(n))return n.map((e=>C(t,r,e,o,s))).flat();const{tag:f=o.tag||r}=n;let d="",p=!1,m=!1;if(u(n))d=n;else if(n.children&&c(n.children))p=!0,d=n.children.map((e=>{const n=C(t,r,e,o,s);return c(n)?n.map((({vnode:t})=>t)):n.vnode}));else{let t=0;for(const e of a){if(!d&&n[e]){d=1===t?JSON.stringify(n[e]):n[e],m=t>1;break}t++}}const b=s&&s.fullName||r,h=s&&s.slotName||r;let{attrs:g}=n;if(g||"object"!=typeof n)g||(g={});else{g={...n},delete g.tag,delete g.children,delete g.to;for(const t of a)delete g[t]}if(p)d=M(t,h,d,n);else{const e=!!l("contentAsAttribute");let{valueAttribute:r}=o;if(!r&&e){const[t]=l("attributes");r=u(e)?e:t}if(r){const{nameless:e,keyAttribute:n}=o;e||n&&(g[n]=b),g[r]=M(t,h,g[r]||d,s),d=""}else d=M(t,h,d,n)}const y=s&&s.tagNamespace?`${s.tagNamespace}:${f}`:f;m&&d&&(g.innerHTML=d);const v=e.h(y,g,d||void 0);return{to:n.to,vnode:v}}function M({metainfo:t,slots:e},r,n,o){const s=e&&e[r];if(!s||!a(s))return n;const i={content:n,metainfo:t};if(o&&o.group){const{group:t,data:e}=o;i[t]=e}const c=s(i);if(c&&c.length){const{children:t}=c[0];return t?t.toString():""}return n}const j="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,O=(t=>j?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("meta_active");function N(t){if(t||(t=e.getCurrentInstance()||void 0),t)return t.appContext.config.globalProperties.$metaManager}const P=e.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=N();if(t)return t.render({slots:e})}}),x=e.reactive({});function $(t,r,n){const o=c(n)?n:[n];o.forEach(((t,r)=>{t.type===e.Comment&&o.splice(r,1)})),t[r]||(t[r]=[]),t[r].push(...o)}class R{constructor(t,e,r){this.ssrCleanedUp=!1,this.config=t,this.target=e,r&&"setup"in r&&a(r.setup)&&(this.resolver=r)}install(t){t.component("Metainfo",P),t.config.globalProperties.$metaManager=this,t.provide(O,x)}addMeta(t,r){r||(r=e.getCurrentInstance()||void 0);const n={removed:[]},o={vm:r};this.resolver&&this.resolver.setup(o);const s=this.target.addSource(t,o,!0),i=t=>this.unmount(!!t,s,n,r);return r&&e.onUnmounted(i),{meta:s,onRemoved:t=>n.removed.push(t),unmount:i}}unmount(t,e,r,n){if(n){const{$el:o}=n.proxy;if(o&&o.offsetParent){let n=new MutationObserver((s=>{for(const{removedNodes:i}of s)i&&i.forEach((s=>{s===o&&n&&(n.disconnect(),n=void 0,this.reallyUnmount(t,e,r))}))}));return void n.observe(o.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}={}){this.ssrCleanedUp||(this.ssrCleanedUp=!0,window.addEventListener("DOMContentLoaded",(()=>{const t=document.querySelectorAll("[data-vm-ssr]");t&&t.length&&Array.from(t).forEach((t=>t.parentNode&&t.parentNode.removeChild(t)))})));const r={};for(const e in x){const n=this.config[e]||{};let o=w({metainfo:x,slots:t},e,x[e],n);if(!o)continue;c(o)||(o=[o]);let s="base"!==e&&x[e].to;!s&&"to"in n&&(s=n.to),!s&&"attributesFor"in n&&(s=e);for(const{to:t,vnode:e}of o)$(r,t||s||"head",e)}if(t)for(const e in t){const n="default"===e?"head":e;if("head"!==n&&"body"!==n)continue;const o=t[e];a(o)&&$(r,n,o({metainfo:x}))}return Object.keys(r).map((t=>e.h(e.Teleport,{to:t},r[t])))}}return R.create=(t,e)=>{const r=((t,e)=>{const r=[],n={active:e,resolve:t,sources:r},o=()=>v(n);return{context:n,compute:o,addSource:(t,e,r=!1)=>{const s=A(n,t,e||{});return r&&o(),s},delSource:(t,e=!0)=>{const n=r.findIndex((e=>e===t||e[b]===t));return n>-1&&(r.splice(n,1),e&&o(),!0)}}})(((t,r,n,o,s)=>a(e)?e(t,r,n,o,s):e.resolve(t,r,n,o,s)),x);return new R(t,r,e)},t.createMetaManager=(t,e)=>R.create(t||s,e||o),t.deepestResolver=o,t.defaultConfig=s,t.getCurrentManager=N,t.resolveOption=r,t.useActiveMeta=function(){return e.inject(O)},t.useMeta=function(t,r){const n=e.getCurrentInstance()||void 0;if(!r&&n&&(r=N(n)),!r)throw new Error("No manager or current instance");e.isProxy(t)&&(e.watch(t,((t,e)=>{!function(t,e,r){for(const n in e)n in r?l(t[n])||e[n]!==r[n]&&(t[n]=e[n]):t[n]=e[n];for(const n in r)n in e||delete t[n]}(o.meta,t,e)})),t=t.value);const o=r.addMeta(t,n);return o},Object.defineProperty(t,"__esModule",{value:!0}),t}({},Vue);