lowcode-engine/packages/designer/src/plugin/plugin-utils.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-03-27 10:39:41 +08:00
import { isPlainObject } from 'lodash-es';
import {
IPublicTypePluginRegisterOptions,
IPublicTypePluginDeclaration,
} from '@alilc/lowcode-types';
2022-03-30 15:08:26 +08:00
export function isValidPreferenceKey(
key: string,
2022-12-22 12:10:00 +08:00
preferenceDeclaration: IPublicTypePluginDeclaration,
2022-03-30 15:08:26 +08:00
): boolean {
if (!preferenceDeclaration || !Array.isArray(preferenceDeclaration.properties)) {
return false;
}
return preferenceDeclaration.properties.some((prop) => {
return prop.key === key;
});
}
2022-12-25 13:49:05 +08:00
export function isLowCodeRegisterOptions(opts: any): opts is IPublicTypePluginRegisterOptions {
2022-12-22 12:10:00 +08:00
return opts && ('autoInit' in opts || 'override' in opts);
}
2024-03-27 10:39:41 +08:00
export function filterValidOptions(opts: any, preferenceDeclaration: IPublicTypePluginDeclaration) {
2022-03-30 15:08:26 +08:00
if (!opts || !isPlainObject(opts)) return opts;
const filteredOpts = {} as any;
2022-12-22 12:10:00 +08:00
Object.keys(opts).forEach((key) => {
2022-03-30 15:08:26 +08:00
if (isValidPreferenceKey(key, preferenceDeclaration)) {
const v = opts[key];
if (v !== undefined && v !== null) {
filteredOpts[key] = v;
}
}
});
return filteredOpts;
2024-03-27 10:39:41 +08:00
}