多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

49 lines
1.3 KiB

import type { MaybeRef } from '@vueuse/core'
export interface CellUrlOptions {
behavior?: string
overlay?: string
}
type ParsedRules = [RegExp, CellUrlOptions]
const parseUrlRules = (serialized?: string): ParsedRules[] | undefined => {
if (!serialized) return undefined
try {
return Object.entries(JSON.parse(serialized)).map(([key, value]) => [new RegExp(key), value] as ParsedRules)
} catch (err) {
console.error(err)
return undefined
}
}
export function useCellUrlConfig(url?: MaybeRef<string>) {
const router = useRouter()
const route = router.currentRoute
const config = computed(() => ({
behavior: route.value.query.url_behavior as string | undefined,
overlay: route.value.query.url_overlay as string | undefined,
rules: parseUrlRules(route.value.query.url_rules as string),
}))
const options = computed(() => {
const options = { behavior: config.value.behavior, overlay: config.value.overlay }
if (config.value.rules && (!config.value.behavior || !config.value.overlay)) {
for (const [regex, value] of config.value.rules) {
if (unref(url)?.match(regex)) return Object.assign(options, value)
}
}
return options
})
return {
cellUrlConfig: config.value,
cellUrlOptions: options,
}
}