2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-21 08:40:34 +03:00
Files
vue-meta/dist/vue-meta.global.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
*/
var VueMeta=function(e,t){"use strict";const r=e=>(t,r)=>{let o=-1;if(r.reduce(((t,r,n)=>{const s=e(t,r);return s!==t?(o=n,s):t}),void 0),o>-1)return t[o]};const o=r(((e,t)=>{const{depth:r}=t;if(!e||r>e)return e}));var n=Object.freeze({__proto__:null,setup:function(e){let t=0;if(e.vm){let{vm:r}=e;do{r.parent&&(t++,r=r.parent)}while(r&&r.parent&&r!==r.root)}e.depth=t},resolve:o});"production"===process.env.NODE_ENV||Object.freeze({}),"production"===process.env.NODE_ENV||Object.freeze([]);const s=Array.isArray,i=e=>"function"==typeof e,c=e=>"string"==typeof e,a=e=>null!==e&&"object"==typeof e,u=Object.prototype.toString,f=e=>"[object Object]"===u.call(e),l={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 p=Symbol("kIsProxy"),d=Symbol("kProxySources"),m=Symbol("kProxyTarget"),b=Symbol("kResolveContext");function g(e){if(s(e))return e.map(g);if(a(e)){const t={};for(const r in e)t[r]="context"===r?e[r]:g(e[r]);return t}return e}const y=(e,t,r)=>{const o=[];for(const n of e)t in n&&(o.push(n[t]),r&&r(n));return o},h=(e,t,r,o=[])=>{if(o.length||(r||(r=e.active),t||(t=e.sources)),!r||!t)return;const n=((e,...t)=>{const r=e?Object.keys(e):[];if(t)for(const e of t)if(e&&a(e))for(const t in e)r.includes(t)||r.push(t);return r})(...t),i=Object.keys(r);for(const e of i)n.includes(e)||delete r[e];for(const i of n){if(f(t[0][i])){r[i]||(r[i]={});const n=[];for(const e of t)i in e&&n.push(e[i]);h(e,n,r[i],[...o,i]);continue}!r[i]&&s(t[0][i])&&(r[i]=[]);const n=[],c=y(t,i,(e=>n.push(e[b])));let a=e.resolve(c,n,r[i],i,o);f(a)&&(a=g(a)),r[i]=a}},v=(e,r,o,n=[])=>{const s=A(e,o,n),i=t.markRaw(new Proxy(r,s));return!n.length&&e.sources&&e.sources.push(i),i},A=(e,t,r=[])=>({get:(o,n,s)=>{if(n===p)return!0;if(n===d)return e.sources;if(n===m)return o;if(n===b)return t;let i=Reflect.get(o,n,s);if(!a(i))return i;if(!i[p]){const s=[...r,n];i=v(e,i,t,s),o[n]=i}return i},set:(t,o,n)=>{const i=Reflect.set(t,o,n);if(i){const n=s(t);let c,a=!1,{sources:u,active:l}=e,p=0;for(const e of r){if(u=y(u,e),n&&p===r.length-1){c=e;break}s(l)&&(a=!0),l=l[e],p++}if(a)return h(e),i;let d,m=[];n?(d=u,m=u.map((e=>e[b]))):d=y(u,o,(e=>m.push(e[b])));let v=e.resolve(d,m,l,o,r);f(v)&&(v=g(v)),n&&c?l[c]=v:l[o]=v}return i},deleteProperty:(t,o)=>{const n=Reflect.deleteProperty(t,o);if(n){const n=s(t);let i,c=e.sources,a=e.active,u=0;for(const e of r){if(c=c.map((t=>t[e])),n&&u===r.length-1){i=e;break}a=a[e],u++}if(c.some((e=>o in e))){let t,s=[];n?(t=c,s=c.map((e=>e[b]))):t=y(c,o,(e=>s.push(e[b])));let u=e.resolve(t,s,a,o,r);f(u)&&(u=g(u)),n&&i?a[i]=u:a[o]=u}else delete a[o]}return n}}),S={};function k(e,t,r,o){return o.attributesFor?function(e,t,r,o={}){const{attributesFor:n}=o;if(!S[n]){const[e,t]=Array.from(document.querySelectorAll(n));if(!e)return void console.error("Could not find element with selector",n,", won't render attributes");t&&console.warn("Found multiple elements with selector",n),S[n]={el:e,attrs:[]}}const{el:s,attrs:i}=S[n];for(const o in r){const n=w(e,`${t}(${o})`,r[o],r);s.setAttribute(o,`${n||""}`),i.includes(o)||i.push(o)}const c=i.filter((e=>!r[e]));for(const e of c)s.removeAttribute(e)}(e,t,r,o):o.group?function(e,t,r,o){if(s(r))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(r).map((n=>{const s={group:t,data:r};if(o.namespaced)s.tagNamespace=!0===o.namespaced?t:o.namespaced;else if(o.namespacedAttribute){const e=!0===o.namespacedAttribute?t:o.namespacedAttribute;s.fullName=`${e}:${n}`,s.slotName=`${e}(${n})`}return j(e,t,r[n],o,s)})).flat()}(e,t,r,o):j(e,t,r,o)}function j(e,r,o,n={},i){const a=["content","json","rawContent"],u=e=>function(e,t,r){if(r&&t in r)return r[t];if(s(e)){for(const r of e)if(r&&r in l)return l[r][t]}else if(e in l)return l[e][t]}([f,n.tag],e,n);if(s(o))return o.map((t=>j(e,r,t,n,i))).flat();const{tag:f=n.tag||r}=o;let p,d=!1,m=!1;if(c(o))p=o;else if(o.children&&s(o.children))d=!0,p=o.children.map((t=>{const o=j(e,r,t,n,i);return s(o)?o.map((({vnode:e})=>e)):o.vnode}));else{let e=0;for(const t of a){if(!p&&o[t]){p=1===e?JSON.stringify(o[t]):o[t],m=e>1;break}e++}}const b=i&&i.fullName||r,g=i&&i.slotName||r;let{attrs:y}=o;if(y||"object"!=typeof o)y||(y={});else{y={...o},delete y.tag,delete y.children,delete y.to;for(const e of a)delete y[e]}if(d)p=w(e,g,p,o);else{const t=u("contentAsAttribute");let r=n.valueAttribute;if(!r&&t){const e=u("attributes");r=c(t)?t:e[0]}if(r){if(!n.nameless){const e=u("keyAttribute");e&&(y[e]=b)}y[r]=w(e,g,y[r]||p,i),p=void 0}else p=w(e,g,p,o)}const h=i&&i.tagNamespace?`${i.tagNamespace}:${f}`:f;let v;return m?(y.innerHTML=p,v=t.h(h,y)):v=t.h(h,y,p),{to:o.to,vnode:v}}function w({metainfo:e,slots:t},r,o,n){if(!t||!t[r])return o;const s={content:o,metainfo:e};n&&n.group&&(s[n.group]=n.data);const i=t[r](s);return i&&i.length?i[0].children:o}const M="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,N=(O="metainfo",M?Symbol("[vue-meta]: "+O):"[vue-meta]: "+O);var O;function C(e){return e||(e=t.getCurrentInstance()),e.appContext.config.globalProperties.$metaManager}const $=t.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(e,{slots:t})=>()=>{const e=C();if(e)return e.render({slots:t})}}),x=t.reactive({});function P(e,r,o){const n=s(o)?o:[o];n.forEach(((e,r)=>{e.type===t.Comment&&n.splice(r,1)})),e[r]||(e[r]=[]),e[r].push(...n)}return e.createMetaManager=function(e,r){const{addSource:o,delSource:n}=((e,t={})=>{const r=[];t||(t={});const o={active:t,resolve:e,sources:r},n=()=>h(o);return{context:o,active:t,resolve:e,sources:r,addSource:(e,t,r=!1)=>{const s=v(o,e,t||{});return r&&n(),s},delSource:(e,t=!0)=>{const o=r.findIndex((t=>t===e||t[m]===e));return o>-1&&(r.splice(o,1),t&&n(),!0)},compute:n}})(((e,t,o,n,s)=>i(r)?r(e,t,o,n,s):r.resolve(e,t,o,n,s)),x);let c=!1;const a={config:e,install(e){e.component("Metainfo",$),e.config.globalProperties.$metaManager=a,e.provide(N,x)},addMeta(e,s){const c={vm:s};r&&"setup"in r&&i(r.setup)&&r.setup(c);const a=o(e,c,!0),u=()=>n(a);return s&&t.onUnmounted(u),{meta:a,unmount:u}},render({slots:e}={}){c||(c=!0,window.addEventListener("DOMContentLoaded",(()=>{const e=document.querySelectorAll("[data-vm-ssr]");e&&e.length&&Array.from(e).forEach((e=>e.parentNode&&e.parentNode.removeChild(e)))})));const r={};for(const t in x){const o=this.config[t]||{},n=k({metainfo:x,slots:e},t,x[t],o);if(!n)continue;const i=s(n)?n:[n],c="base"!==t&&x[t].to||o.to||(o.attributesFor?t:"head");for(const{to:e,vnode:t}of i)P(r,e||c,t)}if(e)for(const t in e){const o=e[t];i(o)&&P(r,"default"===t?"head":t,o({metainfo:x}))}return Object.keys(r).map((e=>t.h(t.Teleport,{to:e},r[e])))}};return a},e.deepestResolver=n,e.defaultConfig={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"}},e.getCurrentManager=C,e.resolveOption=r,e.useMeta=function(e,r){const o=t.getCurrentInstance();if(!r&&o&&(r=C(o)),!r)throw new Error("No manager or current instance");return r.addMeta(e,o||void 0)},e.useMetainfo=function(){return t.inject(N)},Object.defineProperty(e,"__esModule",{value:!0}),e}({},Vue);