1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
import{Ht as e,_n as t,fn as n,gn as r}from"./runtime-core.esm-bundler-DkysjgTd.js";import{n as i,t as a}from"./objectSpread2-BmonrgTA.js";import{n as o}from"./objectWithoutProperties-BjoYlK0I.js";import{o as s,t as c}from"./merge-C-fCyOMx.js";import{n as l}from"./defu-1oANYd5X.js";import{E as u}from"./dist-DUuDCbWu.js";import{a as d,t as f}from"./dist-D5mNzO5q.js";import{t as p}from"./module-CyhaP5hi.js";var m=class{constructor({storageType:e=`localStorage`}={}){if(i(this,`storage`,void 0),typeof window>`u`)throw TypeError(`LocalStorageDriver is not available in non-browser environments. Use MemoryStorageDriver instead.`);this.storage=e===`localStorage`?window.localStorage:window.sessionStorage}clear(){var e=this;return o(function*(){e.storage.clear()})()}getItem(e){var t=this;return o(function*(){let n=t.storage.getItem(e);if(n===null)return null;try{return JSON.parse(n)}catch(n){return t.storage.removeItem(e),null}})()}keys(){var e=this;return o(function*(){let t=[];for(let n=0;n<e.storage.length;n++){let r=e.storage.key(n);r!==null&&t.push(r)}return t})()}removeItem(e){var t=this;return o(function*(){t.storage.removeItem(e)})()}setItem(e,t){var n=this;return o(function*(){n.storage.setItem(e,JSON.stringify(t))})()}},h=class{constructor(){i(this,`store`,new Map)}clear(){var e=this;return o(function*(){e.store.clear()})()}getItem(e){var t=this;return o(function*(){let n=t.store.get(e);return n==null?null:n})()}keys(){var e=this;return o(function*(){return[...e.store.keys()]})()}removeItem(e){var t=this;return o(function*(){t.store.delete(e)})()}setItem(e,t){var n=this;return o(function*(){n.store.set(e,t)})()}},g=class{constructor({driver:e,prefix:t=``}={}){i(this,`driver`,void 0),i(this,`prefix`,void 0),this.driver=e||this.createDefaultDriver(),this.prefix=t,!this.prefix&&this.driver instanceof m&&console.warn(`[StorageManager] empty prefix combined with LocalStorageDriver — clear()/keys() will affect every localStorage entry.`)}clear(){var e=this;return o(function*(){let t=yield e.driver.keys(),n=e.prefix?`${e.prefix}-`:``,r=t.filter(e=>e.startsWith(n));yield Promise.all(r.map(t=>e.driver.removeItem(t)))})()}clearExpiredItems(){var e=this;return o(function*(){let t=yield e.driver.keys(),n=e.prefix?`${e.prefix}-`:``,r=t.filter(e=>e.startsWith(n));for(let t of r){let n=yield e.driver.getItem(t);n&&n.expiry&&Date.now()>n.expiry&&(yield e.driver.removeItem(t))}})()}getItem(e,t=null){var n=this;return o(function*(){let r=n.getFullKey(e),i=yield n.driver.getItem(r);return i?i.expiry&&Date.now()>i.expiry?(yield n.driver.removeItem(r),t):i.value:t})()}keys(){var e=this;return o(function*(){let t=yield e.driver.keys(),n=e.prefix?`${e.prefix}-`:``;return n?t.filter(e=>e.startsWith(n)).map(e=>e.slice(n.length)):t})()}removeItem(e){var t=this;return o(function*(){let n=t.getFullKey(e);yield t.driver.removeItem(n)})()}setItem(e,t,n){var r=this;return o(function*(){let i=r.getFullKey(e),a={expiry:n?Date.now()+n:void 0,value:t};yield r.driver.setItem(i,a)})()}createDefaultDriver(){try{if(typeof window<`u`&&window.localStorage)return new m}catch(e){console.warn(`localStorage is not accessible, falling back to MemoryStorageDriver:`,e)}return new h}getFullKey(e){return this.prefix?`${this.prefix}-${e}`:e}};function _(e,t=`__vben-styles__`,n=`:root`){let r=document.querySelector(`#${t}`)||document.createElement(`style`);r.id=t;let i=`${n} {`;for(let t in e)Object.prototype.hasOwnProperty.call(e,t)&&(i+=`${t}: ${e[t]};`);i+=`}`,r.textContent=i,document.querySelector(`#${t}`)||setTimeout(()=>{document.head.append(r)})}var v={app:{accessMode:`frontend`,authPageLayout:`panel-right`,checkUpdatesInterval:1,colorGrayMode:!1,colorWeakMode:!1,compact:!1,contentCompact:`wide`,contentCompactWidth:1200,contentPadding:0,contentPaddingBottom:0,contentPaddingLeft:0,contentPaddingRight:0,contentPaddingTop:0,defaultAvatar:`https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp`,defaultHomePath:`/dashboard`,dynamicTitle:!0,enableCheckUpdates:!0,enableCopyPreferences:!0,enablePreferences:!0,enableRefreshToken:!1,enableStickyPreferencesNavigationBar:!0,isMobile:!1,layout:`sidebar-nav`,locale:`zh-CN`,loginExpiredMode:`page`,name:`Vben Admin`,preferencesButtonPosition:`auto`,timezone:`Asia/Shanghai`,watermark:!1,watermarkContent:``,zIndex:200},breadcrumb:{enable:!0,hideOnlyOne:!1,showHome:!1,showIcon:!0,styleType:`normal`},copyright:{companyName:`Vben`,companySiteLink:`https://www.vben.pro`,date:`2024`,enable:!0,icp:`闽ICP备19024351号`,icpLink:`https://beian.miit.gov.cn/`,settingShow:!0},footer:{enable:!1,fixed:!1,height:32},header:{enable:!0,height:50,hidden:!1,menuAlign:`start`,mode:`fixed`},logo:{enable:!0,fit:`contain`,source:`https://unpkg.com/@vbenjs/static-source@0.1.7/source/logo-v1.webp`,showText:!0,logoMode:`icon`},navigation:{accordion:!0,split:!0,styleType:`rounded`},shortcutKeys:{enable:!0,globalEscape:!1,globalLockScreen:!0,globalLogout:!0,globalPreferences:!0,globalSearch:!0},sidebar:{autoActivateChild:!1,collapsed:!1,collapsedButton:!0,collapsedShowTitle:!1,collapseWidth:60,draggable:!0,enable:!0,expandOnHover:!0,extraCollapse:!1,extraCollapsedWidth:60,fixedButton:!0,hidden:!1,mixedWidth:80,width:224},tabbar:{draggable:!0,enable:!0,height:38,keepAlive:!0,maxCount:0,middleClickToClose:!1,persist:!0,showIcon:!0,showMaximize:!0,showMore:!0,showRefresh:!0,styleType:`chrome`,visitHistory:!0,wheelable:!0},theme:{builtinType:`default`,colorDestructive:`hsl(348 100% 61%)`,colorPrimary:`hsl(212 100% 45%)`,colorSuccess:`hsl(144 57% 58%)`,colorWarning:`hsl(42 84% 61%)`,mode:`dark`,radius:`0.5`,fontSize:16,semiDarkHeader:!1,semiDarkSidebar:!1,semiDarkSidebarSub:!1},transition:{enable:!0,loading:!0,name:`fade-slide`,progress:!0},widget:{fullscreen:!0,fullscreenButtonPosition:`header`,globalSearch:!0,globalSearchButtonPosition:`header`,languageToggle:!0,languageToggleButtonPosition:`header`,lockScreen:!0,lockScreenButtonPosition:`header`,logoutButtonPosition:`header`,notification:!0,notificationButtonPosition:`header`,refresh:!0,refreshButtonPosition:`header`,sidebarToggle:!0,themeToggle:!0,themeToggleButtonPosition:`header`,timezone:!0,timezoneButtonPosition:`header`,order:[`globalSearch`,`preferences`,`themeToggle`,`languageToggle`,`timezone`,`fullscreen`,`refresh`,`notification`,`lockScreenBtn`,`logoutBtn`]}};function y(e){let{a:t,h:n,l:r,s:i}=new p(e).toHsl(),a=`hsl(${Math.round(n)} ${Math.round(i*100)}% ${Math.round(r*100)}%)`;return t<1?`${a} ${t}`:a}function b(e){let{a:t,h:n,l:r,s:i}=new p(e).toHsl(),a=`${Math.round(n)} ${Math.round(i*100)}% ${Math.round(r*100)}%`;return t<1?`${a} / ${t}`:a}function x(e){return e?new p(e).isValid:!1}function S(e=``){if(typeof e!=`string`)throw TypeError(`Color should be string!`);let t=/^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(e);if(t)return t.splice(1).map(e=>Number.parseInt(e,16));let n=/^#?([\da-f])([\da-f])([\da-f])$/i.exec(e);if(n)return n.splice(1).map(e=>Number.parseInt(e+e,16));if(e.includes(`,`))return e.split(`,`).map(e=>Number.parseInt(e));throw Error(`Invalid color format! Use #ABC or #AABBCC or r,g,b`)}function C(e){return`#`+e.map(e=>`0${e.toString(16).toUpperCase()}`.slice(-2)).join(``)}function w(e,t){return e.map(e=>Math.round(e+(255-e)*t))}function T(e,t){return e.map(e=>Math.round(e*t))}var E=e=>t=>w(t,e),D=e=>t=>T(t,e),O={50:E(.95),100:E(.9),200:E(.75),300:E(.6),400:E(.3),500:e=>e,600:D(.9),700:D(.6),800:D(.45),900:D(.3),950:D(.2)};function k(e,t=O){let n={},r=S(e);for(let[e,i]of Object.entries(t))n[e]=C(i(r));return n}function A(e){let t={};return e.forEach(({alias:e,color:n,name:r})=>{if(n){let i=k(new p(n).toHexString()),a=i[500];Object.keys(i).forEach(n=>{let o=i[n];if(o){let i=b(o);t[`--${r}-${n}`]=i,e&&(t[`--${e}-${n}`]=i),n===`500`&&(a=i)}}),e&&a&&(t[`--${e}`]=a)}}),t}var j=[{color:`hsl(212 100% 45%)`,type:`default`},{color:`hsl(245 82% 67%)`,type:`violet`},{color:`hsl(347 77% 60%)`,type:`pink`},{color:`hsl(42 84% 61%)`,type:`yellow`},{color:`hsl(231 98% 65%)`,type:`sky-blue`},{color:`hsl(161 90% 43%)`,type:`green`},{color:`hsl(240 5% 26%)`,darkPrimaryColor:`hsl(0 0% 98%)`,primaryColor:`hsl(240 5.9% 10%)`,type:`zinc`},{color:`hsl(181 84% 32%)`,type:`deep-green`},{color:`hsl(211 91% 39%)`,type:`deep-blue`},{color:`hsl(18 89% 40%)`,type:`orange`},{color:`hsl(0 75% 42%)`,type:`rose`},{color:`hsl(0 0% 25%)`,darkPrimaryColor:`hsl(0 0% 98%)`,primaryColor:`hsl(240 5.9% 10%)`,type:`neutral`},{color:`hsl(215 25% 27%)`,darkPrimaryColor:`hsl(0 0% 98%)`,primaryColor:`hsl(240 5.9% 10%)`,type:`slate`},{color:`hsl(217 19% 27%)`,darkPrimaryColor:`hsl(0 0% 98%)`,primaryColor:`hsl(240 5.9% 10%)`,type:`gray`},{color:``,type:`custom`}],M=[{offset:-5,timezone:`America/New_York`,label:`America/New_York(GMT-5)`},{offset:0,timezone:`Europe/London`,label:`Europe/London(GMT0)`},{offset:8,timezone:`Asia/Shanghai`,label:`Asia/Shanghai(GMT+8)`},{offset:9,timezone:`Asia/Tokyo`,label:`Asia/Tokyo(GMT+9)`},{offset:9,timezone:`Asia/Seoul`,label:`Asia/Seoul(GMT+9)`}],N=[...j].slice(0,7);function P(e){var t;let n=document.documentElement;if(!n)return;let r=(t=e==null?void 0:e.theme)==null?{}:t,{builtinType:i,mode:a,radius:o}=r;if(Reflect.has(r,`mode`)){let e=I(a);n.classList.toggle(`dark`,e)}Reflect.has(r,`builtinType`)&&n.dataset.theme!==i&&(n.dataset.theme=i);let s=[...j].find(e=>e.type===i),c=``;if(s&&(c=I(e.theme.mode)&&s.darkPrimaryColor||s.primaryColor||s.color),(c||Reflect.has(r,`colorPrimary`)||Reflect.has(r,`colorDestructive`)||Reflect.has(r,`colorSuccess`)||Reflect.has(r,`colorWarning`))&&F(e),Reflect.has(r,`radius`)&&document.documentElement.style.setProperty(`--radius`,`${o}rem`),Reflect.has(r,`fontSize`)){let e=r.fontSize;document.documentElement.style.setProperty(`--font-size-base`,`${e}px`),document.documentElement.style.setProperty(`--menu-font-size`,`calc(${e}px * 0.875)`)}}function F(e){if(!e.theme)return;let{colorDestructive:t,colorPrimary:n,colorSuccess:r,colorWarning:i}=e.theme,a=A([{color:n,name:`primary`},{alias:`warning`,color:i,name:`yellow`},{alias:`success`,color:r,name:`green`},{alias:`destructive`,color:t,name:`red`}]);Object.entries({"--green-500":`--success`,"--primary-500":`--primary`,"--red-500":`--destructive`,"--yellow-500":`--warning`}).forEach(([e,t])=>{let n=a[e];n&&document.documentElement.style.setProperty(t,n)}),_(a)}function I(e){let t=e===`dark`;return e===`auto`&&(t=window.matchMedia(`(prefers-color-scheme: dark)`).matches),t}var L={CUSTOM:`preferences-custom`,MAIN:`preferences`,LOCALE:`preferences-locale`,THEME:`preferences-theme`},R=new class{constructor(){var e=this;i(this,`cache`,void 0),i(this,`customPreferencesExtension`,null),i(this,`customState`,r({})),i(this,`debouncedSave`,void 0),i(this,`initialCustomPreferences`,{}),i(this,`initialPreferences`,v),i(this,`isInitialized`,!1),i(this,`state`,void 0),i(this,`clearCache`,o(function*(){yield Promise.all(Object.values(L).map(t=>e.cache.removeItem(t)))})),i(this,`getCustomPreferences`,()=>t(this.customState)),i(this,`getInitialCustomPreferences`,()=>this.cloneValue(this.initialCustomPreferences)),i(this,`getInitialPreferences`,()=>this.initialPreferences),i(this,`getPreferences`,()=>t(this.state)),i(this,`getPreferencesExtension`,()=>this.customPreferencesExtension?this.cloneValue(this.customPreferencesExtension):null),i(this,`initPreferences`,function(){var t=o(function*({namespace:t,overrides:n,extension:r}){if(e.isInitialized)return;e.cache=new g({prefix:t}),e.initialPreferences=l({},n,v),e.customPreferencesExtension=r==null?null:r,e.initialCustomPreferences=e.resolveCustomPreferencesDefaults(e.customPreferencesExtension);let i=(yield e.loadFromCache())||{};e.sanitizeCachedArray(i,`widget`,`order`);let a=c({},i,e.initialPreferences);e.updatePreferences(a);let o=(yield e.loadCustomFromCache())||{};e.replaceCustomPreferences(c({},e.sanitizeCustomPreferences(o),e.initialCustomPreferences)),yield e.saveToCache(),e.setupWatcher(),e.initPlatform(),e.isInitialized=!0});return function(e){return t.apply(this,arguments)}}()),i(this,`resetPreferences`,o(function*(){Object.assign(e.state,e.initialPreferences),e.replaceCustomPreferences(e.initialCustomPreferences),yield e.saveToCache(),e.handleUpdates(e.state)})),i(this,`updateCustomPreferences`,e=>{if(!this.customPreferencesExtension)return;let t=this.sanitizeCustomPreferences(e);Object.keys(t).length!==0&&(this.replaceCustomPreferences(c({},t,n(this.customState))),this.debouncedSave())}),i(this,`updatePreferences`,e=>{let t=c({},e,n(this.state));Object.assign(this.state,t),this.handleUpdates(e),this.debouncedSave()}),this.cache=new g,this.state=r(a({},v)),this.debouncedSave=u(()=>this.saveToCache(),150)}cloneValue(e){return Array.isArray(e)?e.map(e=>this.cloneValue(e)):e&&typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,t])=>[e,this.cloneValue(t)])):e}handleUpdates(e){let{theme:t,app:n}=e;t&&(Object.keys(t).length>0||Reflect.has(t,`fontSize`))&&P(this.state),n&&(Reflect.has(n,`colorGrayMode`)||Reflect.has(n,`colorWeakMode`))&&this.updateColorMode(this.state)}initPlatform(){document.documentElement.dataset.platform=s()?`macOs`:`window`}isAlmostInteger(e,t=Number.EPSILON*10){return Math.abs(e-Math.round(e))<t}isValidCustomPreferenceValue(e,t){switch(e.component){case`number`:{var n,r,i;if(typeof t!=`number`||!Number.isFinite(t))return!1;let a=this.resolveNumericConstraint((n=e.componentProps)==null?void 0:n.max),o=this.resolveNumericConstraint((r=e.componentProps)==null?void 0:r.min),s=this.resolveNumericConstraint((i=e.componentProps)==null?void 0:i.step);if(o!==void 0&&t<o||a!==void 0&&t>a)return!1;if(s!==void 0){if(s<=0)return!1;let e=(t-(o==null?0:o))/s;if(!this.isAlmostInteger(e))return!1}return!0}case`select`:return typeof t==`string`&&e.options.some(e=>e.value===t);case`switch`:return typeof t==`boolean`;default:return typeof t==`string`}}loadCustomFromCache(){var e=this;return o(function*(){return e.cache.getItem(L.CUSTOM)})()}loadFromCache(){var e=this;return o(function*(){return e.cache.getItem(L.MAIN)})()}replaceCustomPreferences(e){Object.keys(this.customState).forEach(e=>{Reflect.deleteProperty(this.customState,e)}),Object.assign(this.customState,e)}resolveCustomPreferencesDefaults(e){if(!e)return{};let t={};for(let n of e.fields)t[n.key]=n.defaultValue;return t}resolveNumericConstraint(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}sanitizeCachedArray(e,t,n){var r;let i=e==null||(r=e[t])==null?void 0:r[n];if(!Array.isArray(i)||i.length<=1)return;let a=new Set,o=i.filter(e=>!a.has(e)&&(a.add(e),!0));o.length!==i.length&&(e[t][n]=o)}sanitizeCustomPreferences(e){if(!this.customPreferencesExtension)return{};let t={};for(let n of this.customPreferencesExtension.fields){let r=e[n.key];r!==void 0&&this.isValidCustomPreferenceValue(n,r)&&(t[n.key]=r)}return t}saveToCache(){var e=this;return o(function*(){try{if(yield e.cache.setItem(L.MAIN,e.state),yield e.cache.setItem(L.LOCALE,e.state.app.locale),yield e.cache.setItem(L.THEME,e.state.theme.mode),e.customPreferencesExtension){yield e.cache.setItem(L.CUSTOM,a({},e.customState));return}yield e.cache.removeItem(L.CUSTOM)}catch(e){console.error(`Failed to save preferences to cache:`,e)}})()}setupWatcher(){if(this.isInitialized)return;let t=d(f).smaller(`md`);e(()=>t.value,e=>{this.updatePreferences({app:{isMobile:e}})},{immediate:!0}),window.matchMedia(`(prefers-color-scheme: dark)`).addEventListener(`change`,({matches:e})=>{this.state.theme.mode===`auto`&&(this.updatePreferences({theme:{mode:e?`dark`:`light`}}),this.updatePreferences({theme:{mode:`auto`}}))})}updateColorMode(e){let{colorGrayMode:t,colorWeakMode:n}=e.app,r=document.documentElement;r.classList.toggle(`invert-mode`,n),r.classList.toggle(`grayscale-mode`,t)}};export{M as a,N as i,I as n,y as o,j as r,x as s,R as t}; |