mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
Add number validator API
This commit is contained in:
parent
084776ccd3
commit
ba312452f7
@ -1,5 +1,5 @@
|
|||||||
import { ComponentMetadata } from '@/components/types'
|
import { ComponentMetadata } from '@/components/types'
|
||||||
import { getUID, none } from '@/core/utils'
|
import { getNumberValidator, getUID, none } from '@/core/utils'
|
||||||
import { autoMatchMedal } from './auto-match'
|
import { autoMatchMedal } from './auto-match'
|
||||||
|
|
||||||
export const component: ComponentMetadata = {
|
export const component: ComponentMetadata = {
|
||||||
@ -26,7 +26,7 @@ export const component: ComponentMetadata = {
|
|||||||
maxBadgeCount: {
|
maxBadgeCount: {
|
||||||
defaultValue: 256,
|
defaultValue: 256,
|
||||||
displayName: '最大加载数量',
|
displayName: '最大加载数量',
|
||||||
validator: (value: number) => lodash.clamp(value, 1, 256),
|
validator: getNumberValidator(1, 256),
|
||||||
},
|
},
|
||||||
defaultMedalID: {
|
defaultMedalID: {
|
||||||
displayName: '默认勋章ID',
|
displayName: '默认勋章ID',
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { ComponentMetadata } from '@/components/types'
|
import { ComponentMetadata } from '@/components/types'
|
||||||
import { addComponentListener } from '@/core/settings'
|
import { addComponentListener } from '@/core/settings'
|
||||||
|
import { getNumberValidator } from '@/core/utils'
|
||||||
|
|
||||||
export const component: ComponentMetadata = {
|
export const component: ComponentMetadata = {
|
||||||
name: 'autoHideSidebar',
|
name: 'autoHideSidebar',
|
||||||
@ -21,7 +22,7 @@ export const component: ComponentMetadata = {
|
|||||||
triggerWidth: {
|
triggerWidth: {
|
||||||
defaultValue: 8,
|
defaultValue: 8,
|
||||||
displayName: '触发区域宽度 (px)',
|
displayName: '触发区域宽度 (px)',
|
||||||
validator: (value: number) => lodash.clamp(value, 1, 1000),
|
validator: getNumberValidator(1, 1000),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
description: {
|
description: {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { ComponentMetadata } from '@/components/types'
|
import { ComponentMetadata } from '@/components/types'
|
||||||
import { contentLoaded } from '@/core/life-cycle'
|
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'
|
import { homeUrls } from '../urls'
|
||||||
|
|
||||||
export const component: ComponentMetadata = {
|
export const component: ComponentMetadata = {
|
||||||
@ -18,6 +19,9 @@ export const component: ComponentMetadata = {
|
|||||||
componentsTags.style,
|
componentsTags.style,
|
||||||
],
|
],
|
||||||
entry: () => {
|
entry: () => {
|
||||||
|
addComponentListener('freshHome.maxWidth', (width: number) => {
|
||||||
|
document.documentElement.style.setProperty('--home-max-width-override', `${width}px`)
|
||||||
|
}, true)
|
||||||
contentLoaded(async () => {
|
contentLoaded(async () => {
|
||||||
const FreshHome = await import('./FreshHome.vue')
|
const FreshHome = await import('./FreshHome.vue')
|
||||||
const freshHome = mountVueComponent(FreshHome)
|
const freshHome = mountVueComponent(FreshHome)
|
||||||
@ -45,6 +49,11 @@ export const component: ComponentMetadata = {
|
|||||||
displayName: '启用横向滚动',
|
displayName: '启用横向滚动',
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
|
maxWidth: {
|
||||||
|
displayName: '最大宽度 (px)',
|
||||||
|
defaultValue: 1440,
|
||||||
|
validator: getNumberValidator(1000, 3000),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
unload: () => document.body.classList.add('home-redesign-off'),
|
unload: () => document.body.classList.add('home-redesign-off'),
|
||||||
reload: () => document.body.classList.remove('home-redesign-off'),
|
reload: () => document.body.classList.remove('home-redesign-off'),
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { ComponentMetadata } from '@/components/types'
|
import { ComponentMetadata } from '@/components/types'
|
||||||
import { addComponentListener } from '@/core/settings'
|
import { addComponentListener } from '@/core/settings'
|
||||||
|
import { getNumberValidator } from '@/core/utils'
|
||||||
|
|
||||||
export const component: ComponentMetadata = {
|
export const component: ComponentMetadata = {
|
||||||
name: 'sidebarOffset',
|
name: 'sidebarOffset',
|
||||||
@ -23,7 +24,7 @@ export const component: ComponentMetadata = {
|
|||||||
offset: {
|
offset: {
|
||||||
displayName: '偏移量 (%)',
|
displayName: '偏移量 (%)',
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
validator: (value: number) => lodash.clamp(value, -40, 40),
|
validator: getNumberValidator(-40, 40),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -459,3 +459,13 @@ export const disableWindowScroll = async (action?: () => unknown | Promise<unkno
|
|||||||
}
|
}
|
||||||
return restore
|
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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user