mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-22 16:20:34 +03:00
9 lines
9.6 KiB
JavaScript
9 lines
9.6 KiB
JavaScript
/**
|
|
* vue-meta v3.0.0-alpha.8
|
|
* (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]},n=r(((t,e)=>{const{depth:r}=e;return!t||r>t?r:t}));var o=Object.freeze({__proto__:null,setup: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"),g=Symbol("kProxyTarget"),h=Symbol("kResolveContext");function b(t){if(c(t))return t.map(b);if(l(t)){const e={};for(const r in t)e[r]="context"===r?t[r]:b(t[r]);return e}return t}const y=(t,e,r)=>{const n=[];for(const o of t)o&&e in o&&(n.push(o[e]),r&&r(o));return n},v=(t,e=[],r,n)=>{if(!r&&!n&&(({active:r,sources:n}=t),e.length))for(let t=0;t<e.length;t++){const o=e[t];if(!r||!r[o])return void console.error(`recompute: segment ${o} not found on target`,e,r);r=r[o],n=n.map((t=>t[o])).filter(Boolean)}if(!r||!n)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})(...n),s=Object.keys(r);for(const t of s)o.includes(t)||delete r[t];for(const s of o){let o=!1;for(let t=0;t<n.length;t++){const e=n[t];if(e&&s in e&&void 0!==e[s]){o=d(e[s]);break}}if(o){r[s]||(r[s]={});const o=[];for(const t of n)s in t&&o.push(t[s]);v(t,[...e,s],r[s],o);continue}!r[s]&&c(n[0][s])&&(r[s]=[]);const i=[],a=y(n,s,(t=>i.push(t[h])));let u=t.resolve(a,i,r[s],s,e);d(u)&&(u=b(u)),r[s]=u}},S=(t,r,n,o=[])=>{const s=A(t,n,o),i=e.markRaw(new Proxy(r,s));return!o.length&&t.sources&&t.sources.push(i),i},A=(t,e,r=[])=>({get:(n,o,s)=>{if(o===p)return!0;if(o===m)return t.sources;if(o===g)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=S(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 i=c(e);let a,u=!1,{sources:l,active:f}=t,p=0;for(const t of r){if(l=y(l,t),i&&p===r.length-1){a=t;break}c(f)&&(u=!0),f=f[t],p++}if(u)return v(t),s;if(d(o))return v(t,r),s;let m,g=[];i?(m=l,g=l.map((t=>t[h]))):m=y(l,n,(t=>g.push(t[h])));let S=t.resolve(m,g,f,n,r);d(S)&&(S=b(S)),i&&a?f[a]=S:f[n]=S}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&&e[t])),o&&u===r.length-1){s=t;break}a=a[t],u++}if(i.some((t=>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=b(u)),o&&s?a[s]=u:a[n]=u}else delete a[n]}return o}}),k={};function j(t,r,n,o){return"attributesFor"in o?function(t,r,n,o){const{attributesFor:s}=o;if(!s||!n)return;if(t.isSSR)return{to:"",vnode:e.h(`ssr-${s}`,n)};if(!k[s]){const[t,e]=Array.from(document.querySelectorAll(s));if(!t)return void console.error("Could not find element for selector",s,", won't render attributes");e&&console.warn("Found multiple elements for selector",s),k[s]={el:t,attrs:[]}}const{el:i,attrs:a}=k[s];for(const e in n){let o=w(t,`${r}(${e})`,n[e],n);c(o)&&(o=o.join(",")),i.setAttribute(e,o||""),a.includes(e)||a.push(e)}const u=a.filter((t=>!n[t]));for(const t of u)i.removeAttribute(t)}(t,r,n,o):"group"in o?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)})).filter(Boolean).flat()}(t,r,n,o):C(t,r,n,o)}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))).filter(Boolean).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&&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 g=s&&s.fullName||r,h=s&&s.slotName||r;let{attrs:b}=n;if(b||"object"!=typeof n)b||(b={});else{b={...n},delete b.tag,delete b.children,delete b.to;for(const t of a)delete b[t]}if(p)d=w(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}=o;if(!e){const t=o.keyAttribute||l("keyAttribute");t&&(b[t]=g)}b[r]=w(t,h,b[r]||d,s),d=""}else d=w(t,h,d,n)}const y=s&&s.tagNamespace?`${s.tagNamespace}:${f}`:f;if("title"===y&&!t.isSSR)return void(document.title=d);m&&d&&(b.innerHTML=d);const v=e.h(y,b,d||void 0);return{to:n.to,vnode:v}}function w({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 M="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,N=(t=>M?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("meta_active");function R(t,e,r){for(const n in e)n in r?l(t[n])?R(t[n],e[n],r[n]):e[n]!==r[n]&&(t[n]=e[n]):t[n]=e[n];for(const n in r)e&&n in e||delete t[n]}function O(t){if(t||(t=e.getCurrentInstance()||void 0),t)return t.appContext.config.globalProperties.$metaManager}function x(t,r){const n=e.getCurrentInstance()||void 0;if(!r&&n&&(r=O(n)),!r)throw new Error("No manager or current instance");e.isProxy(t)&&(e.watch(t,((t,e)=>{R(o.meta,t,e)})),t=t.value);const o=r.addMeta(t,n);return o}const $=e.defineComponent({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=O();if(t)return t.render({slots:e})}}),P="data-vm-ssr";function F(t,r,n,o){const s=c(o)?o:[o];t?n.endsWith("Attrs")||s.forEach((t=>{t.props||(t.props={}),t.props[P]=!0})):s.forEach(((t,r)=>{t.type===e.Comment&&s.splice(r,1)})),r[n]||(r[n]=[]),r[n].push(...s)}class I{isSSR=!1;config;target;resolver;ssrCleanedUp=!1;constructor(t,e,r,n){this.isSSR=t,this.config=e,this.target=r,n&&"setup"in n&&a(n.setup)&&(this.resolver=n)}static create=(t,r,n)=>{const o=((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=S(n,t,e||{});return r&&o(),s},delSource:(t,e=!0)=>{const n=r.findIndex((e=>e===t||e[g]===t));return n>-1&&(r.splice(n,1),e&&o(),!0)}}})(((t,e,r,o,s)=>a(n)?n(t,e,r,o,s):n.resolve(t,e,r,o,s)),e.reactive({}));return new I(t,r,o,n)};install(t){t.component("Metainfo",$),t.config.globalProperties.$metaManager=this,t.provide(N,this.target.context.active)}addMeta(t,r){r||(r=e.getCurrentInstance()||void 0);const n={removed:[]},o={vm:r},{resolver:s}=this;s&&s.setup&&s.setup(o);const i=this.target.addSource(t,o,!0),c=t=>this.unmount(!!t,i,n,r);return r&&e.onUnmounted(c),{meta:i,onRemoved:t=>n.removed.push(t),unmount:c}}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}={}){const r=this.target.context.active,{isSSR:n}=this;if(!n&&!this.ssrCleanedUp){this.ssrCleanedUp=!0;const t=()=>{const t=document.querySelectorAll("[data-vm-ssr]");t&&t.length&&t.forEach((t=>t.parentNode&&t.parentNode.removeChild(t)))};"loading"===document.readyState?window.addEventListener("DOMContentLoaded",t,{once:!0}):t()}const o={};for(const e in r){const s=this.config[e]||{};let i=j({isSSR:n,metainfo:r,slots:t},e,r[e],s);if(!i)continue;c(i)||(i=[i]);let a="base"!==e&&r[e].to;!a&&"to"in s&&(a=s.to),!a&&"attributesFor"in s&&(a=e);for(const{to:t,vnode:e}of i)F(this.isSSR,o,t||a||"head",e)}if(t)for(const e in t){const n="default"===e?"head":e;if("head"!==n&&"body"!==n)continue;const s=t[e];a(s)&&F(this.isSSR,o,n,s({metainfo:r}))}return Object.keys(o).map((t=>e.h(e.Teleport,{to:t},o[t])))}}const U={keyName:"metaInfo"};return t.createMetaManager=(t=!1,e,r)=>I.create(t,e||s,r||o),t.deepestResolver=o,t.defaultConfig=s,t.getCurrentManager=O,t.plugin=(t,r={})=>{const n=Object.assign({},U,r);t.mixin((t=>({created(){const r=e.getCurrentInstance();if(!r?.type||!(t.keyName in r.type))return;const n=r.type[t.keyName];a(n)?x(e.computed(n)):x(n)}}))(n))},t.resolveOption=r,t.useActiveMeta=function(){return e.inject(N)},t.useMeta=x,Object.defineProperty(t,"__esModule",{value:!0}),t}({},Vue);
|