mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-19 20:50:34 +03:00
9 lines
7.8 KiB
JavaScript
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(t,e){"use strict";const r=t=>(e,r)=>{let n=-1;if(r.reduce(((e,r,o)=>{const s=t(e,r);return s!==e?(n=o,s):e}),void 0),n>-1)return e[n]};const n=r(((t,e)=>{const{depth:r}=e;if(!t||r>t)return 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={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}};"production"===process.env.NODE_ENV||Object.freeze({}),"production"===process.env.NODE_ENV||Object.freeze([]);const c=Array.isArray,i=t=>"function"==typeof t,a=t=>"string"==typeof t,u=t=>null!==t&&"object"==typeof t,f=Object.prototype.toString,l=t=>"[object Object]"===f.call(t),d=Symbol("kIsProxy"),p=Symbol("kProxySources"),m=Symbol("kProxyTarget"),b=Symbol("kResolveContext");function g(t){if(c(t))return t.map(g);if(u(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},h=(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&&u(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(l(e[0][s])){r[s]||(r[s]={});const o=[];for(const t of e)s in t&&o.push(t[s]);h(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[b])));let a=t.resolve(i,o,r[s],s,n);l(a)&&(a=g(a)),r[s]=a}},v=(t,r,n,o=[])=>{const s=A(t,n,o),c=e.markRaw(new Proxy(r,s));return!o.length&&t.sources&&t.sources.push(c),c},A=(t,e,r=[])=>({get:(n,o,s)=>{if(o===d)return!0;if(o===p)return t.sources;if(o===m)return n;if(o===b)return e;let c=Reflect.get(n,o,s);if(!u(c))return c;if(!c[d]){const s=[...r,o];c=v(t,c,e,s),n[o]=c}return c},set:(e,n,o)=>{const s=Reflect.set(e,n,o);if(s){const o=c(e);let i,a=!1,{sources:u,active:f}=t,d=0;for(const t of r){if(u=y(u,t),o&&d===r.length-1){i=t;break}c(f)&&(a=!0),f=f[t],d++}if(a)return h(t),s;let p,m=[];o?(p=u,m=u.map((t=>t[b]))):p=y(u,n,(t=>m.push(t[b])));let v=t.resolve(p,m,f,n,r);l(v)&&(v=g(v)),o&&i?f[i]=v:f[n]=v}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[b]))):e=y(i,n,(t=>c.push(t[b])));let u=t.resolve(e,c,a,n,r);l(u)&&(u=g(u)),o&&s?a[s]=u:a[n]=u}else delete a[n]}return o}}),S={};function k(t,e,r,n){return"attributesFor"in n?function(t,e,r,n){const{attributesFor:o}=n;if(!o)return;if(!S[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),S[o]={el:t,attrs:[]}}const{el:s,attrs:c}=S[o];for(const n in r){const o=M(t,`${e}(${n})`,r[n],r);s.setAttribute(n,o||""),c.includes(n)||c.push(n)}const i=c.filter((t=>!r[t]));for(const t of i)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 j(t,e,r[o],n,s)})).flat()}(t,e,r,n):j(t,e,r,n)}function j(t,r,n,o={},i){const u=["content","json","rawContent"],f=t=>function(t,e){for(const r of t){const t=s[r];if(r&&t)return t[e]}}([l,o.tag],t);if(c(n))return n.map((e=>j(t,r,e,o,i))).flat();const{tag:l=o.tag||r}=n;let d="",p=!1,m=!1;if(a(n))d=n;else if(n.children&&c(n.children))p=!0,d=n.children.map((e=>{const n=j(t,r,e,o,i);return c(n)?n.map((({vnode:t})=>t)):n.vnode}));else{let t=0;for(const e of u){if(!d&&n[e]){d=1===t?JSON.stringify(n[e]):n[e],m=t>1;break}t++}}const b=i&&i.fullName||r,g=i&&i.slotName||r;let{attrs:y}=n;if(y||"object"!=typeof n)y||(y={});else{y={...n},delete y.tag,delete y.children,delete y.to;for(const t of u)delete y[t]}if(p)d=M(t,g,d,n);else{const e=!!f("contentAsAttribute");let{valueAttribute:r}=o;if(!r&&e){const[t]=f("attributes");r=a(e)?e:t}if(r){const{nameless:e,keyAttribute:n}=o;e||n&&(y[n]=b),y[r]=M(t,g,y[r]||d,i),d=""}else d=M(t,g,d,n)}const h=i&&i.tagNamespace?`${i.tagNamespace}:${l}`:l;m&&d&&(y.innerHTML=d);const v=e.h(h,y,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)return n;const c={content:n,metainfo:t};if(o&&o.group){const{group:t,data:e}=o;c[t]=e}const i=s(c);if(i&&i.length){const{children:t}=i[0];return t?t.toString():""}return n}const N="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,O=(t=>N?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("active_meta");function C(t){if(t||(t=e.getCurrentInstance()||void 0),t)return t.appContext.config.globalProperties.$metaManager}const w=e.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=C();if(t)return t.render({slots:e})}}),$=e.reactive({});function x(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)}return t.createMetaManager=function(t,r){let n=!1;const{addSource:o,delSource:s}=((t,e={})=>{const r=[];e||(e={});const n={active:e,resolve:t,sources:r},o=()=>h(n);return{context:n,compute:o,addSource:(t,e,r=!1)=>{const s=v(n,t,e||{});return r&&o(),s},delSource:(t,e=!0)=>{const n=r.findIndex((e=>e===t||e[m]===t));return n>-1&&(r.splice(n,1),e&&o(),!0)}}})(((t,e,n,o,s)=>i(r)?r(t,e,n,o,s):r.resolve(t,e,n,o,s)),$),a={config:t,install(t){t.component("Metainfo",w),t.config.globalProperties.$metaManager=a,t.provide(O,$)},addMeta(t,n){n||(n=e.getCurrentInstance()||void 0);const c={vm:n};r&&"setup"in r&&i(r.setup)&&r.setup(c);const a=o(t,c,!0),u=()=>s(a);return n&&e.onUnmounted(u),{meta:a,unmount:u}},render({slots:t}={}){n||(n=!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 $){const n=this.config[e]||{};let o=k({metainfo:$,slots:t},e,$[e],n);if(!o)continue;c(o)||(o=[o]);let s="base"!==e&&$[e].to;!s&&"to"in n&&(s=n.to),!s&&"attributesFor"in n&&(s=e);for(const{to:t,vnode:e}of o)x(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];i(o)&&x(r,n,o({metainfo:$}))}return Object.keys(r).map((t=>e.h(e.Teleport,{to:t},r[t])))}};return a},t.deepestResolver=o,t.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"}},t.getCurrentManager=C,t.resolveOption=r,t.useActiveMeta=function(){return e.inject(O)},t.useMeta=function(t,r){const n=e.getCurrentInstance();if(!r&&n&&(r=C(n)),!r)throw new Error("No manager or current instance");return r.addMeta(t,n||void 0)},Object.defineProperty(t,"__esModule",{value:!0}),t}({},Vue);
|