Add number validator API

This commit is contained in:
the1812 2022-03-12 17:24:26 +08:00
parent 084776ccd3
commit ba312452f7
5 changed files with 26 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { ComponentMetadata } from '@/components/types'
import { getUID, none } from '@/core/utils'
import { getNumberValidator, getUID, none } from '@/core/utils'
import { autoMatchMedal } from './auto-match'
export const component: ComponentMetadata = {
@ -26,7 +26,7 @@ export const component: ComponentMetadata = {
maxBadgeCount: {
defaultValue: 256,
displayName: '最大加载数量',
validator: (value: number) => lodash.clamp(value, 1, 256),
validator: getNumberValidator(1, 256),
},
defaultMedalID: {
displayName: '默认勋章ID',

View File

@ -1,5 +1,6 @@
import { ComponentMetadata } from '@/components/types'
import { addComponentListener } from '@/core/settings'
import { getNumberValidator } from '@/core/utils'
export const component: ComponentMetadata = {
name: 'autoHideSidebar',
@ -21,7 +22,7 @@ export const component: ComponentMetadata = {
triggerWidth: {
defaultValue: 8,
displayName: '触发区域宽度 (px)',
validator: (value: number) => lodash.clamp(value, 1, 1000),
validator: getNumberValidator(1, 1000),
},
},
description: {

View File

@ -1,6 +1,7 @@
import { ComponentMetadata } from '@/components/types'
import { contentLoaded } from '@/core/life-cycle'
import { mountVueComponent } from '@/core/utils'
import { addComponentListener } from '@/core/settings'
import { getNumberValidator, mountVueComponent } from '@/core/utils'
import { homeUrls } from '../urls'
export const component: ComponentMetadata = {
@ -18,6 +19,9 @@ export const component: ComponentMetadata = {
componentsTags.style,
],
entry: () => {
addComponentListener('freshHome.maxWidth', (width: number) => {
document.documentElement.style.setProperty('--home-max-width-override', `${width}px`)
}, true)
contentLoaded(async () => {
const FreshHome = await import('./FreshHome.vue')
const freshHome = mountVueComponent(FreshHome)
@ -45,6 +49,11 @@ export const component: ComponentMetadata = {
displayName: '启用横向滚动',
defaultValue: false,
},
maxWidth: {
displayName: '最大宽度 (px)',
defaultValue: 1440,
validator: getNumberValidator(1000, 3000),
},
},
unload: () => document.body.classList.add('home-redesign-off'),
reload: () => document.body.classList.remove('home-redesign-off'),

View File

@ -1,5 +1,6 @@
import { ComponentMetadata } from '@/components/types'
import { addComponentListener } from '@/core/settings'
import { getNumberValidator } from '@/core/utils'
export const component: ComponentMetadata = {
name: 'sidebarOffset',
@ -23,7 +24,7 @@ export const component: ComponentMetadata = {
offset: {
displayName: '偏移量 (%)',
defaultValue: 0,
validator: (value: number) => lodash.clamp(value, -40, 40),
validator: getNumberValidator(-40, 40),
},
},
}

View File

@ -459,3 +459,13 @@ export const disableWindowScroll = async (action?: () => unknown | Promise<unkno
}
return restore
}
/**
* ,
* @param clampLower
* @param clampUpper
*/
export const getNumberValidator = (clampLower = -Infinity, clampUpper = Infinity) => (
(value: number, oldValue: number) => (
lodash.isNumber(value) ? lodash.clamp(value, clampLower, clampUpper) : oldValue
)
)