From e38bce2a6dc01d18475cd02a3fcf4bec779d382c Mon Sep 17 00:00:00 2001 From: the1812 Date: Sat, 31 Dec 2022 14:23:20 +0800 Subject: [PATCH] Add compatibility API --- src/components/switch-options-old.ts | 74 ++++++++++++++++++++++++++++ src/components/switch-options.ts | 2 + 2 files changed, 76 insertions(+) create mode 100644 src/components/switch-options-old.ts diff --git a/src/components/switch-options-old.ts b/src/components/switch-options-old.ts new file mode 100644 index 000000000..d844d248b --- /dev/null +++ b/src/components/switch-options-old.ts @@ -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 + } +} diff --git a/src/components/switch-options.ts b/src/components/switch-options.ts index 7bcc3fe75..6a7b63e48 100644 --- a/src/components/switch-options.ts +++ b/src/components/switch-options.ts @@ -352,3 +352,5 @@ export const newSwitchComponentWrapper = ( return component as SwitchComponentMetadata } } + +export { createSwitchOptions } from './switch-options-old'