123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import isEqual from 'lodash/isEqual'
- import isObject from 'lodash/isObject'
- import isArray from 'lodash/isArray'
- import endsWith from 'lodash/endsWith'
- import forEach from 'lodash/forEach'
-
- import { Store } from '../components/Store';
- import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
- import { ColorService } from '../adapters/ColorService';
- import { AssetService } from '../adapters/AssetResolver';
- import { Options } from '../interfaces/Options';
-
- export class OptionsProcessor {
- constructor(
- private store: Store,
- private uniqueIdProvider: UniqueIdProvider,
- private colorService: ColorService,
- private assetService: AssetService,
- ) {}
-
- public processOptions(options: Options) {
- this.processObject(options);
- }
-
- private processObject(objectToProcess: object) {
- forEach(objectToProcess, (value, key) => {
- this.processColor(key, value, objectToProcess);
-
- if (!value) {
- return;
- }
-
- this.processComponent(key, value, objectToProcess);
- this.processImage(key, value, objectToProcess);
- this.processButtonsPassProps(key, value);
-
- if (!isEqual(key, 'passProps') && (isObject(value) || isArray(value))) {
- this.processObject(value);
- }
- });
- }
-
- private processColor(key: string, value: any, options: Record<string, any>) {
- if (isEqual(key, 'color') || endsWith(key, 'Color')) {
- options[key] = value === null ? 'NoColor' : this.colorService.toNativeColor(value);
- }
- }
-
- private processImage(key: string, value: any, options: Record<string, any>) {
- if (
- isEqual(key, 'icon') ||
- isEqual(key, 'image') ||
- endsWith(key, 'Icon') ||
- endsWith(key, 'Image')
- ) {
- options[key] = this.assetService.resolveFromRequire(value);
- }
- }
-
- private processButtonsPassProps(key: string, value: any) {
- if (endsWith(key, 'Buttons')) {
- forEach(value, (button) => {
- if (button.passProps && button.id) {
- this.store.updateProps(button.id, button.passProps);
- button.passProps = undefined;
- }
- });
- }
- }
-
- private processComponent(key: string, value: any, options: Record<string, any>) {
- if (isEqual(key, 'component')) {
- value.componentId = value.id ? value.id : this.uniqueIdProvider.generate('CustomComponent');
- if (value.passProps) {
- this.store.updateProps(value.componentId, value.passProps);
- }
- options[key].passProps = undefined;
- }
- }
- }
|