2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-20 14:00:33 +03:00
Files
vue-meta/dist/vue-meta.esm-browser.min.js
T
2021-01-25 00:42:01 +00:00

9 lines
7.8 KiB
JavaScript

/**
* vue-meta v3.0.0-alpha.0
* (c) 2021
* - Pim (@pimlie)
* - All the amazing contributors
* @license MIT
*/
import{markRaw as t,h as e,getCurrentInstance as o,inject as r,defineComponent as n,reactive as s,onUnmounted as c,Teleport as i,Comment as a}from"vue";const u=t=>(e,o)=>{let r=-1;if(o.reduce(((e,o,n)=>{const s=t(e,o);return s!==e?(r=n,s):e}),void 0),r>-1)return e[r]};const f=u(((t,e)=>{const{depth:o}=e;if(!t||o>t)return t}));var l=Object.freeze({__proto__:null,setup:function(t){let e=0;if(t.vm){let{vm:o}=t;do{o.parent&&(e++,o=o.parent)}while(o&&o.parent&&o!==o.root)}t.depth=e},resolve:f});const p={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"}};"production"===process.env.NODE_ENV||Object.freeze({}),"production"===process.env.NODE_ENV||Object.freeze([]);const d=Array.isArray,m=t=>"function"==typeof t,b=t=>"string"==typeof t,y=t=>null!==t&&"object"==typeof t,g=Object.prototype.toString,h=t=>"[object Object]"===g.call(t),v={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}};const A=Symbol("kIsProxy"),S=Symbol("kProxySources"),k=Symbol("kProxyTarget"),N=Symbol("kResolveContext");function w(t){if(d(t))return t.map(w);if(y(t)){const e={};for(const o in t)e[o]="context"===o?t[o]:w(t[o]);return e}return t}const j=(t,e,o)=>{const r=[];for(const n of t)e in n&&(r.push(n[e]),o&&o(n));return r},O=(t,e,o,r=[])=>{if(r.length||(o||(o=t.active),e||(e=t.sources)),!o||!e)return;const n=((t,...e)=>{const o=t?Object.keys(t):[];if(e)for(const t of e)if(t&&y(t))for(const e in t)o.includes(e)||o.push(e);return o})(...e),s=Object.keys(o);for(const t of s)n.includes(t)||delete o[t];for(const s of n){if(h(e[0][s])){o[s]||(o[s]={});const n=[];for(const t of e)s in t&&n.push(t[s]);O(t,n,o[s],[...r,s]);continue}!o[s]&&d(e[0][s])&&(o[s]=[]);const n=[],c=j(e,s,(t=>n.push(t[N])));let i=t.resolve(c,n,o[s],s,r);h(i)&&(i=w(i)),o[s]=i}},$=(e,o,r,n=[])=>{const s=x(e,r,n),c=t(new Proxy(o,s));return!n.length&&e.sources&&e.sources.push(c),c},x=(t,e,o=[])=>({get:(r,n,s)=>{if(n===A)return!0;if(n===S)return t.sources;if(n===k)return r;if(n===N)return e;let c=Reflect.get(r,n,s);if(!y(c))return c;if(!c[A]){const s=[...o,n];c=$(t,c,e,s),r[n]=c}return c},set:(e,r,n)=>{const s=Reflect.set(e,r,n);if(s){const n=d(e);let c,i=!1,{sources:a,active:u}=t,f=0;for(const t of o){if(a=j(a,t),n&&f===o.length-1){c=t;break}d(u)&&(i=!0),u=u[t],f++}if(i)return O(t),s;let l,p=[];n?(l=a,p=a.map((t=>t[N]))):l=j(a,r,(t=>p.push(t[N])));let m=t.resolve(l,p,u,r,o);h(m)&&(m=w(m)),n&&c?u[c]=m:u[r]=m}return s},deleteProperty:(e,r)=>{const n=Reflect.deleteProperty(e,r);if(n){const n=d(e);let s,c=t.sources,i=t.active,a=0;for(const t of o){if(c=c.map((e=>e[t])),n&&a===o.length-1){s=t;break}i=i[t],a++}if(c.some((t=>r in t))){let e,a=[];n?(e=c,a=c.map((t=>t[N]))):e=j(c,r,(t=>a.push(t[N])));let u=t.resolve(e,a,i,r,o);h(u)&&(u=w(u)),n&&s?i[s]=u:i[r]=u}else delete i[r]}return n}}),E={};function M(t,e,o,r){return r.attributesFor?function(t,e,o,r={}){const{attributesFor:n}=r;if(!E[n]){const[t,e]=Array.from(document.querySelectorAll(n));if(!t)return void console.error("Could not find element with selector",n,", won't render attributes");e&&console.warn("Found multiple elements with selector",n),E[n]={el:t,attrs:[]}}const{el:s,attrs:c}=E[n];for(const r in o){const n=F(t,`${e}(${r})`,o[r],o);s.setAttribute(r,`${n||""}`),c.includes(r)||c.push(r)}const i=c.filter((t=>!o[t]));for(const t of i)s.removeAttribute(t)}(t,e,o,r):r.group?function(t,e,o,r){if(d(o))return console.warn("Specifying an array for group properties isnt supported mostly as we didnt found a use-case for this yet. If you have one, please create an issue on the vue-meta repo"),[];return Object.keys(o).map((n=>{const s={group:e,data:o};if(r.namespaced)s.tagNamespace=!0===r.namespaced?e:r.namespaced;else if(r.namespacedAttribute){const t=!0===r.namespacedAttribute?e:r.namespacedAttribute;s.fullName=`${t}:${n}`,s.slotName=`${t}(${n})`}return P(t,e,o[n],r,s)})).flat()}(t,e,o,r):P(t,e,o,r)}function P(t,o,r,n={},s){const c=["content","json","rawContent"],i=t=>function(t,e,o){if(o&&e in o)return o[e];if(d(t)){for(const o of t)if(o&&o in v)return v[o][e]}else if(t in v)return v[t][e]}([a,n.tag],t,n);if(d(r))return r.map((e=>P(t,o,e,n,s))).flat();const{tag:a=n.tag||o}=r;let u,f=!1,l=!1;if(b(r))u=r;else if(r.children&&d(r.children))f=!0,u=r.children.map((e=>{const r=P(t,o,e,n,s);return d(r)?r.map((({vnode:t})=>t)):r.vnode}));else{let t=0;for(const e of c){if(!u&&r[e]){u=1===t?JSON.stringify(r[e]):r[e],l=t>1;break}t++}}const p=s&&s.fullName||o,m=s&&s.slotName||o;let{attrs:y}=r;if(y||"object"!=typeof r)y||(y={});else{y={...r},delete y.tag,delete y.children,delete y.to;for(const t of c)delete y[t]}if(f)u=F(t,m,u,r);else{const e=i("contentAsAttribute");let o=n.valueAttribute;if(!o&&e){const t=i("attributes");o=b(e)?e:t[0]}if(o){if(!n.nameless){const t=i("keyAttribute");t&&(y[t]=p)}y[o]=F(t,m,y[o]||u,s),u=void 0}else u=F(t,m,u,r)}const g=s&&s.tagNamespace?`${s.tagNamespace}:${a}`:a;let h;return l?(y.innerHTML=u,h=e(g,y)):h=e(g,y,u),{to:r.to,vnode:h}}function F({metainfo:t,slots:e},o,r,n){if(!e||!e[o])return r;const s={content:r,metainfo:t};n&&n.group&&(s[n.group]=n.data);const c=e[o](s);return c&&c.length?c[0].children:r}const C="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,_=(z="metainfo",C?Symbol("[vue-meta]: "+z):"[vue-meta]: "+z);var z;function R(t){return t||(t=o()),t.appContext.config.globalProperties.$metaManager}function q(t,e){const r=o();if(!e&&r&&(e=R(r)),!e)throw new Error("No manager or current instance");return e.addMeta(t,r||void 0)}function D(){return r(_)}const I=n({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=R();if(t)return t.render({slots:e})}}),L=s({});function T(t,e,o){const r=d(o)?o:[o];r.forEach(((t,e)=>{t.type===a&&r.splice(e,1)})),t[e]||(t[e]=[]),t[e].push(...r)}function V(t,o){const{addSource:r,delSource:n}=((t,e={})=>{const o=[];e||(e={});const r={active:e,resolve:t,sources:o},n=()=>O(r);return{context:r,active:e,resolve:t,sources:o,addSource:(t,e,o=!1)=>{const s=$(r,t,e||{});return o&&n(),s},delSource:(t,e=!0)=>{const r=o.findIndex((e=>e===t||e[k]===t));return r>-1&&(o.splice(r,1),e&&n(),!0)},compute:n}})(((t,e,r,n,s)=>m(o)?o(t,e,r,n,s):o.resolve(t,e,r,n,s)),L);let s=!1;const a={config:t,install(t){t.component("Metainfo",I),t.config.globalProperties.$metaManager=a,t.provide(_,L)},addMeta(t,e){const s={vm:e};o&&"setup"in o&&m(o.setup)&&o.setup(s);const i=r(t,s,!0),a=()=>n(i);return e&&c(a),{meta:i,unmount:a}},render({slots:t}={}){s||(s=!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 o={};for(const e in L){const r=this.config[e]||{},n=M({metainfo:L,slots:t},e,L[e],r);if(!n)continue;const s=d(n)?n:[n],c="base"!==e&&L[e].to||r.to||(r.attributesFor?e:"head");for(const{to:t,vnode:e}of s)T(o,t||c,e)}if(t)for(const e in t){const r=t[e];m(r)&&T(o,"default"===e?"head":e,r({metainfo:L}))}return Object.keys(o).map((t=>e(i,{to:t},o[t])))}};return a}export{V as createMetaManager,l as deepestResolver,p as defaultConfig,R as getCurrentManager,u as resolveOption,q as useMeta,D as useMetainfo};