mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-20 02:03:35 +08:00
Add configurable tab controls, breadcrumb icons, and interactive useMenu examples. Batch pointer updates per frame and scope writes to the tab cache.
115 lines
3.2 KiB
TypeScript
115 lines
3.2 KiB
TypeScript
import { CONTENT_TRANSITION_OPTIONS } from '@/app-config'
|
|
import {
|
|
DEFAULT_THEME_STYLE,
|
|
themeStyles,
|
|
} from '@ray-template/ui/theme-presets'
|
|
import { pick } from 'lodash-es'
|
|
import * as v from 'valibot'
|
|
import { LAYOUT_MODES } from './constant'
|
|
import type { SettingState } from './types'
|
|
|
|
export const PREFERENCE_FILE_LIMIT = 256 * 1024
|
|
|
|
const flag = v.boolean()
|
|
const finite = v.pipe(v.number(), v.finite())
|
|
const size = v.pipe(finite, v.minValue(0))
|
|
const watermark = v.pipe(
|
|
v.strictObject({
|
|
content: v.string(),
|
|
cross: flag,
|
|
fontSize: finite,
|
|
lineHeight: finite,
|
|
width: finite,
|
|
height: finite,
|
|
xOffset: finite,
|
|
yOffset: finite,
|
|
xGap: finite,
|
|
yGap: finite,
|
|
rotate: finite,
|
|
}),
|
|
v.check(({ width, height, xGap, yGap }) =>
|
|
[width + xGap, height + yGap].every((edge) => edge > 0 && edge <= 4096),
|
|
),
|
|
)
|
|
const preferences = v.strictObject({
|
|
layoutConfig: v.strictObject({
|
|
mode: v.picklist(LAYOUT_MODES),
|
|
contentWidth: v.picklist(['fluid', 'fixed']),
|
|
contentMaxWidth: v.pipe(finite, v.minValue(640), v.maxValue(2560)),
|
|
contentPadding: v.pipe(finite, v.minValue(0), v.maxValue(48)),
|
|
fixedHeader: flag,
|
|
fixedFooter: flag,
|
|
}),
|
|
appTheme: v.picklist(['light', 'dark', 'auto']),
|
|
themeStyle: v.optional(v.picklist(themeStyles), DEFAULT_THEME_STYLE),
|
|
primaryColor: v.optional(
|
|
v.pipe(
|
|
v.string(),
|
|
v.maxLength(64),
|
|
v.regex(/^(?:#[\da-f]+|(?:rgba?|hsla?)\([\d.,%+\s/-]+\))$/i),
|
|
v.check((value) => {
|
|
const { style } = document.createElement('span')
|
|
|
|
style.color = value
|
|
|
|
return style.color !== ''
|
|
}),
|
|
),
|
|
),
|
|
contentTransition: v.picklist(
|
|
CONTENT_TRANSITION_OPTIONS.map(({ value }) => value),
|
|
),
|
|
drawerPlacement: v.picklist(['left', 'right']),
|
|
breadcrumbSwitch: flag,
|
|
breadcrumbIcon: v.optional(flag, true),
|
|
breadcrumbIconFallback: v.optional(flag, true),
|
|
menuTagSwitch: flag,
|
|
menuTagRound: v.optional(flag, false),
|
|
menuTagTop: v.optional(flag, true),
|
|
menuTagFixed: v.optional(flag, true),
|
|
menuTagDrag: v.optional(flag, true),
|
|
copyrightSwitch: flag,
|
|
colorWeakness: flag,
|
|
menuConfig: v.strictObject({
|
|
menuWidth: size,
|
|
collapsedWidth: size,
|
|
collapsedMode: v.picklist(['width', 'transform']),
|
|
collapsedIndent: size,
|
|
iconSize: size,
|
|
collapsedIconSize: size,
|
|
accordion: flag,
|
|
menuSiderBarLogo: flag,
|
|
inverted: flag,
|
|
nativeScrollbar: flag,
|
|
}),
|
|
watermarkSwitch: flag,
|
|
watermarkConfig: watermark,
|
|
dynamicDocumentTitle: flag,
|
|
keepAliveConfig: v.strictObject({
|
|
setupKeepAlive: flag,
|
|
maxKeepAliveLength: v.pipe(size, v.integer()),
|
|
keepAliveExclude: v.array(v.string()),
|
|
}),
|
|
})
|
|
|
|
const preferenceFile = v.strictObject({
|
|
format: v.literal('ray-template-preferences'),
|
|
version: v.literal(1),
|
|
preferences,
|
|
})
|
|
|
|
export function createPreferenceFile(state: SettingState) {
|
|
return v.parse(preferenceFile, {
|
|
format: 'ray-template-preferences',
|
|
version: 1,
|
|
preferences: {
|
|
...pick(state, Object.keys(preferences.entries)),
|
|
primaryColor: state.primaryColorOverride.common?.primaryColor,
|
|
},
|
|
})
|
|
}
|
|
|
|
export function parsePreferenceFile(text: string) {
|
|
return v.parse(preferenceFile, JSON.parse(text)).preferences
|
|
}
|