2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-18 15:00:33 +03:00
Files
vue-meta/dist/vue-meta.esm-browser.min.js
T
2021-02-01 00:08:35 +01: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"}},d={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 m=Array.isArray,b=t=>"function"==typeof t,g=t=>"string"==typeof t,y=t=>null!==t&&"object"==typeof t,h=Object.prototype.toString,v=t=>"[object Object]"===h.call(t),A=Symbol("kIsProxy"),S=Symbol("kProxySources"),k=Symbol("kProxyTarget"),N=Symbol("kResolveContext");function j(t){if(m(t))return t.map(j);if(y(t)){const e={};for(const o in t)e[o]="context"===o?t[o]:j(t[o]);return e}return t}const O=(t,e,o)=>{const r=[];for(const n of t)e in n&&(r.push(n[e]),o&&o(n));return r},w=(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(v(e[0][s])){o[s]||(o[s]={});const n=[];for(const t of e)s in t&&n.push(t[s]);w(t,n,o[s],[...r,s]);continue}!o[s]&&m(e[0][s])&&(o[s]=[]);const n=[],c=O(e,s,(t=>n.push(t[N])));let i=t.resolve(c,n,o[s],s,r);v(i)&&(i=j(i)),o[s]=i}},x=(e,o,r,n=[])=>{const s=$(e,r,n),c=t(new Proxy(o,s));return!n.length&&e.sources&&e.sources.push(c),c},$=(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=x(t,c,e,s),r[n]=c}return c},set:(e,r,n)=>{const s=Reflect.set(e,r,n);if(s){const n=m(e);let c,i=!1,{sources:a,active:u}=t,f=0;for(const t of o){if(a=O(a,t),n&&f===o.length-1){c=t;break}m(u)&&(i=!0),u=u[t],f++}if(i)return w(t),s;let l,p=[];n?(l=a,p=a.map((t=>t[N]))):l=O(a,r,(t=>p.push(t[N])));let d=t.resolve(l,p,u,r,o);v(d)&&(d=j(d)),n&&c?u[c]=d:u[r]=d}return s},deleteProperty:(e,r)=>{const n=Reflect.deleteProperty(e,r);if(n){const n=m(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=O(c,r,(t=>a.push(t[N])));let u=t.resolve(e,a,i,r,o);v(u)&&(u=j(u)),n&&s?i[s]=u:i[r]=u}else delete i[r]}return n}}),E={};function M(t,e,o,r){return"attributesFor"in r?function(t,e,o,r){const{attributesFor:n}=r;if(!n)return;if(!E[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),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):"group"in r?function(t,e,o,r){if(m(o))return console.warn("Specifying an array for group properties isnt supported"),[];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){for(const o of t){const t=d[o];if(o&&t)return t[e]}}([a,n.tag],t);if(m(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(g(r))u=r;else if(r.children&&m(r.children))f=!0,u=r.children.map((e=>{const r=P(t,o,e,n,s);return m(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,b=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,b,u,r);else{const e=!!i("contentAsAttribute");let{valueAttribute:o}=n;if(!o&&e){const[t]=i("attributes");o=g(e)?e:t}if(o){const{nameless:e,keyAttribute:r}=n;e||r&&(y[r]=p),y[o]=F(t,b,y[o]||u,s),u=""}else u=F(t,b,u,r)}const h=s&&s.tagNamespace?`${s.tagNamespace}:${a}`:a;l&&u&&(y.innerHTML=u);const v=e(h,y,u||void 0);return{to:r.to,vnode:v}}function F({metainfo:t,slots:e},o,r,n){const s=e&&e[o];if(!s)return r;const c={content:r,metainfo:t};if(n&&n.group){const{group:t,data:e}=n;c[t]=e}const i=s(c);if(i&&i.length){const{children:t}=i[0];return t?t.toString():""}return r}const _="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,C=(t=>_?Symbol("[vue-meta]: "+t):"[vue-meta]: "+t)("active_meta");function z(t){if(t||(t=o()||void 0),t)return t.appContext.config.globalProperties.$metaManager}function R(t,e){const r=o();if(!e&&r&&(e=z(r)),!e)throw new Error("No manager or current instance");return e.addMeta(t,r||void 0)}function q(){return r(C)}const D=n({name:"Metainfo",inheritAttrs:!1,setup:(t,{slots:e})=>()=>{const t=z();if(t)return t.render({slots:e})}}),L=s({});function T(t,e,o){const r=m(o)?o:[o];r.forEach(((t,e)=>{t.type===a&&r.splice(e,1)})),t[e]||(t[e]=[]),t[e].push(...r)}function I(t,r){let n=!1;const{addSource:s,delSource:a}=((t,e={})=>{const o=[];e||(e={});const r={active:e,resolve:t,sources:o},n=()=>w(r);return{context:r,compute:n,addSource:(t,e,o=!1)=>{const s=x(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)}}})(((t,e,o,n,s)=>b(r)?r(t,e,o,n,s):r.resolve(t,e,o,n,s)),L),u={config:t,install(t){t.component("Metainfo",D),t.config.globalProperties.$metaManager=u,t.provide(C,L)},addMeta(t,e){e||(e=o()||void 0);const n={vm:e};r&&"setup"in r&&b(r.setup)&&r.setup(n);const i=s(t,n,!0),u=()=>a(i);return e&&c(u),{meta:i,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 o={};for(const e in L){const r=this.config[e]||{};let n=M({metainfo:L,slots:t},e,L[e],r);if(!n)continue;m(n)||(n=[n]);let s="base"!==e&&L[e].to;!s&&"to"in r&&(s=r.to),!s&&"attributesFor"in r&&(s=e);for(const{to:t,vnode:e}of n)T(o,t||s||"head",e)}if(t)for(const e in t){const r="default"===e?"head":e;if("head"!==r&&"body"!==r)continue;const n=t[e];b(n)&&T(o,r,n({metainfo:L}))}return Object.keys(o).map((t=>e(i,{to:t},o[t])))}};return u}export{I as createMetaManager,l as deepestResolver,p as defaultConfig,z as getCurrentManager,u as resolveOption,q as useActiveMeta,R as useMeta};