Add compatibility API

This commit is contained in:
the1812 2022-12-31 14:23:20 +08:00
parent 79a411b815
commit e38bce2a6d
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/*
* Switch Options API, ,
*/
import { getComponentSettings, addComponentListener } from '@/core/settings'
import { ComponentMetadata, OptionsMetadata } from './component'
type Switches = {
[key: string]: {
displayName: string
defaultValue: boolean
}
}
export interface SwitchOptions {
name: string
switches: Switches
radio?: boolean
dimAt?: 'checked' | 'notChecked'
switchProps?: {
checkedIcon?: string
notCheckedIcon?: string
iconPosition?: 'left' | 'right'
}
}
export const createSwitchOptions = (options: SwitchOptions) => {
if (options.radio === undefined) {
options.radio = false
}
const { name: optionName, switches } = options
const extendComponentOptions: OptionsMetadata = {}
Object.entries(switches).forEach(([key, { displayName, defaultValue }]) => {
extendComponentOptions[`switch-${key}`] = {
defaultValue,
displayName,
hidden: true,
}
})
return (component: ComponentMetadata) => {
const optionDisplayName = `${component.displayName}选项`
const selfOption = {
componentName: component.name,
optionDisplayName,
}
Object.assign(options, selfOption)
extendComponentOptions[optionName] = {
defaultValue: options,
displayName: optionDisplayName,
}
component.options = { ...component.options, ...extendComponentOptions }
if (!component.widget) {
component.widget = {
component: () => import('./SwitchOptions.vue').then(m => m.default),
options,
}
}
const originalEntry = component.entry
component.entry = async (...args) => {
originalEntry?.(...args)
const { name } = component
const componentOptions = getComponentSettings(name).options
Object.keys(componentOptions).forEach(key => {
if (key.startsWith('switch-')) {
addComponentListener(
`${name}.${key}`,
(value: boolean) => {
document.body.classList.toggle(`${name}-${key}`, value)
},
true,
)
}
})
}
return component
}
}

View File

@ -352,3 +352,5 @@ export const newSwitchComponentWrapper = <N extends string, S extends string>(
return component as SwitchComponentMetadata<O, N, S>
}
}
export { createSwitchOptions } from './switch-options-old'