mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-22 21:50:34 +03:00
9 lines
8.6 KiB
JavaScript
9 lines
8.6 KiB
JavaScript
/**
|
|
* vue-meta v3.0.0-alpha.2
|
|
* (c) 2021
|
|
* - Pim (@pimlie)
|
|
* - All the amazing contributors
|
|
* @license MIT
|
|
*/
|
|
var VueMeta=function(t,e){"use strict";const r=t=>(e,r)=>{let o=-1;if(r.reduce(((e,r,n)=>{const s=t(e,r);return s!==e?(o=n,s):e}),void 0),o>-1)return e[o]};const o=r(((t,e)=>{const{depth:r}=e;if(!t||r>t)return t}));var n=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:o});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 i=Array.isArray,c=t=>"function"==typeof t,a=t=>"string"==typeof t,u=t=>null!==t&&"object"==typeof t,l=Object.prototype.toString,f=t=>"[object Object]"===l.call(t),d=Symbol("kIsProxy"),p=Symbol("kProxySources"),m=Symbol("kProxyTarget"),b=Symbol("kResolveContext");function h(t){if(i(t))return t.map(h);if(u(t)){const e={};for(const r in t)e[r]="context"===r?t[r]:h(t[r]);return e}return t}const g=(t,e,r)=>{const o=[];for(const n of t)e in n&&(o.push(n[e]),r&&r(n));return o},y=(t,e,r,o=[])=>{if(o.length||(r||(r=t.active),e||(e=t.sources)),!r||!e)return;const n=((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)n.includes(t)||delete r[t];for(const s of n){if(f(e[0][s])){r[s]||(r[s]={});const n=[];for(const t of e)s in t&&n.push(t[s]);y(t,n,r[s],[...o,s]);continue}!r[s]&&i(e[0][s])&&(r[s]=[]);const n=[],c=g(e,s,(t=>n.push(t[b])));let a=t.resolve(c,n,r[s],s,o);f(a)&&(a=h(a)),r[s]=a}},v=(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===d)return!0;if(n===p)return t.sources;if(n===m)return o;if(n===b)return e;let i=Reflect.get(o,n,s);if(!u(i))return i;if(!i[d]){const s=[...r,n];i=v(t,i,e,s),o[n]=i}return i},set:(e,o,n)=>{const s=Reflect.set(e,o,n);if(s){const n=i(e);let c,a=!1,{sources:u,active:l}=t,d=0;for(const t of r){if(u=g(u,t),n&&d===r.length-1){c=t;break}i(l)&&(a=!0),l=l[t],d++}if(a)return y(t),s;let p,m=[];n?(p=u,m=u.map((t=>t[b]))):p=g(u,o,(t=>m.push(t[b])));let v=t.resolve(p,m,l,o,r);f(v)&&(v=h(v)),n&&c?l[c]=v:l[o]=v}return s},deleteProperty:(e,o)=>{const n=Reflect.deleteProperty(e,o);if(n){const n=i(e);let s,c=t.sources,a=t.active,u=0;for(const t of r){if(c=c.map((e=>e[t])),n&&u===r.length-1){s=t;break}a=a[t],u++}if(c.some((t=>o in t))){let e,i=[];n?(e=c,i=c.map((t=>t[b]))):e=g(c,o,(t=>i.push(t[b])));let u=t.resolve(e,i,a,o,r);f(u)&&(u=h(u)),n&&s?a[s]=u:a[o]=u}else delete a[o]}return n}}),S={};function N(t,e,r,o){return"attributesFor"in o?function(t,e,r,o){const{attributesFor:n}=o;if(!n)return;if(!S[n]){const[t,e]=Array.from(document.querySelectorAll(n));if(!t)return void console.error("Could not find element for selector",n,", won't render attributes");e&&console.warn("Found multiple elements for selector",n),S[n]={el:t,attrs:[]}}const{el:s,attrs:i}=S[n];for(const o in r){const n=w(t,`${e}(${o})`,r[o],r);s.setAttribute(o,n||""),i.includes(o)||i.push(o)}const c=i.filter((t=>!r[t]));for(const t of c)s.removeAttribute(t)}(t,e,r,o):"group"in o?function(t,e,r,o){if(i(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 k(t,e,r[n],o,s)})).flat()}(t,e,r,o):k(t,e,r,o)}function k(t,r,o,n={},c){const u=["content","json","rawContent"],l=t=>function(t,e){for(const r of t){const t=s[r];if(r&&t)return t[e]}}([f,n.tag],t);if(i(o))return o.map((e=>k(t,r,e,n,c))).flat();const{tag:f=n.tag||r}=o;let d="",p=!1,m=!1;if(a(o))d=o;else if(o.children&&i(o.children))p=!0,d=o.children.map((e=>{const o=k(t,r,e,n,c);return i(o)?o.map((({vnode:t})=>t)):o.vnode}));else{let t=0;for(const e of u){if(!d&&o[e]){d=1===t?JSON.stringify(o[e]):o[e],m=t>1;break}t++}}const b=c&&c.fullName||r,h=c&&c.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 u)delete g[t]}if(p)d=w(t,h,d,o);else{const e=!!l("contentAsAttribute");let{valueAttribute:r}=n;if(!r&&e){const[t]=l("attributes");r=a(e)?e:t}if(r){const{nameless:e,keyAttribute:o}=n;e||o&&(g[o]=b),g[r]=w(t,h,g[r]||d,c),d=""}else d=w(t,h,d,o)}const y=c&&c.tagNamespace?`${c.tagNamespace}:${f}`:f;m&&d&&(g.innerHTML=d);const v=e.h(y,g,d||void 0);return{to:o.to,vnode:v}}function w({metainfo:t,slots:e},r,o,n){const s=e&&e[r];if(!s||!c(s))return o;const i={content:o,metainfo:t};if(n&&n.group){const{group:t,data:e}=n;i[t]=e}const a=s(i);if(a&&a.length){const{children:t}=a[0];return t?t.toString():""}return o}const C="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,M=(t=>C?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("meta_active");function O(t){if(t||(t=e.getCurrentInstance()||void 0),t)return t.appContext.config.globalProperties.$metaManager}const j=e.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=O();if(t)return t.render({slots:e})}}),P=e.reactive({});function x(t,r,o){const n=i(o)?o:[o];n.forEach(((t,r)=>{t.type===e.Comment&&n.splice(r,1)})),t[r]||(t[r]=[]),t[r].push(...n)}class ${constructor(t,e,r){this.ssrCleanedUp=!1,this.config=t,this.target=e,r&&"setup"in r&&c(r.setup)&&(this.resolver=r)}install(t){t.component("Metainfo",j),t.config.globalProperties.$metaManager=this,t.provide(M,P)}addMeta(t,r){r||(r=e.getCurrentInstance()||void 0);const o={removed:[]},n={vm:r};this.resolver&&this.resolver.setup(n);const s=this.target.addSource(t,n,!0),i=t=>this.unmount(!!t,s,o,r);return r&&e.onUnmounted(i),{meta:s,onRemoved:t=>o.removed.push(t),unmount:i}}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}={}){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 P){const o=this.config[e]||{};let n=N({metainfo:P,slots:t},e,P[e],o);if(!n)continue;i(n)||(n=[n]);let s="base"!==e&&P[e].to;!s&&"to"in o&&(s=o.to),!s&&"attributesFor"in o&&(s=e);for(const{to:t,vnode:e}of n)x(r,t||s||"head",e)}if(t)for(const e in t){const o="default"===e?"head":e;if("head"!==o&&"body"!==o)continue;const n=t[e];c(n)&&x(r,o,n({metainfo:P}))}return Object.keys(r).map((t=>e.h(e.Teleport,{to:t},r[t])))}}return $.create=(t,e)=>{const r=((t,e={})=>{const r=[];e||(e={});const o={active:e,resolve:t,sources:r},n=()=>y(o);return{context:o,compute:n,addSource:(t,e,r=!1)=>{const s=v(o,t,e||{});return r&&n(),s},delSource:(t,e=!0)=>{const o=r.findIndex((e=>e===t||e[m]===t));return o>-1&&(r.splice(o,1),e&&n(),!0)}}})(((t,r,o,n,s)=>c(e)?e(t,r,o,n,s):e.resolve(t,r,o,n,s)),P);return new $(t,r,e)},t.createMetaManager=(t,e)=>$.create(t,e),t.deepestResolver=n,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=O,t.resolveOption=r,t.useActiveMeta=function(){return e.inject(M)},t.useMeta=function(t,r){const o=e.getCurrentInstance()||void 0;if(!r&&o&&(r=O(o)),!r)throw new Error("No manager or current instance");e.isProxy(t)&&(e.watch(t,((t,e)=>{!function(t,e,r){for(const o in e)o in r?u(t[o])||e[o]!==r[o]&&(t[o]=e[o]):t[o]=e[o];for(const o in r)o in e||delete t[o]}(n.meta,t,e)})),t=t.value);const n=r.addMeta(t,o);return n},Object.defineProperty(t,"__esModule",{value:!0}),t}({},Vue);
|