react-native-navigation的迁移库

OptionsProcessor.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import isEqual from 'lodash/isEqual'
  2. import isObject from 'lodash/isObject'
  3. import isArray from 'lodash/isArray'
  4. import endsWith from 'lodash/endsWith'
  5. import forEach from 'lodash/forEach'
  6. import { Store } from '../components/Store';
  7. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  8. import { ColorService } from '../adapters/ColorService';
  9. import { AssetService } from '../adapters/AssetResolver';
  10. import { Options } from '../interfaces/Options';
  11. export class OptionsProcessor {
  12. constructor(
  13. private store: Store,
  14. private uniqueIdProvider: UniqueIdProvider,
  15. private colorService: ColorService,
  16. private assetService: AssetService,
  17. ) {}
  18. public processOptions(options: Options) {
  19. this.processObject(options);
  20. }
  21. private processObject(objectToProcess: object) {
  22. forEach(objectToProcess, (value, key) => {
  23. this.processColor(key, value, objectToProcess);
  24. if (!value) {
  25. return;
  26. }
  27. this.processComponent(key, value, objectToProcess);
  28. this.processImage(key, value, objectToProcess);
  29. this.processButtonsPassProps(key, value);
  30. if (!isEqual(key, 'passProps') && (isObject(value) || isArray(value))) {
  31. this.processObject(value);
  32. }
  33. });
  34. }
  35. private processColor(key: string, value: any, options: Record<string, any>) {
  36. if (isEqual(key, 'color') || endsWith(key, 'Color')) {
  37. options[key] = value === null ? 'NoColor' : this.colorService.toNativeColor(value);
  38. }
  39. }
  40. private processImage(key: string, value: any, options: Record<string, any>) {
  41. if (
  42. isEqual(key, 'icon') ||
  43. isEqual(key, 'image') ||
  44. endsWith(key, 'Icon') ||
  45. endsWith(key, 'Image')
  46. ) {
  47. options[key] = this.assetService.resolveFromRequire(value);
  48. }
  49. }
  50. private processButtonsPassProps(key: string, value: any) {
  51. if (endsWith(key, 'Buttons')) {
  52. forEach(value, (button) => {
  53. if (button.passProps && button.id) {
  54. this.store.updateProps(button.id, button.passProps);
  55. button.passProps = undefined;
  56. }
  57. });
  58. }
  59. }
  60. private processComponent(key: string, value: any, options: Record<string, any>) {
  61. if (isEqual(key, 'component')) {
  62. value.componentId = value.id ? value.id : this.uniqueIdProvider.generate('CustomComponent');
  63. if (value.passProps) {
  64. this.store.updateProps(value.componentId, value.passProps);
  65. }
  66. options[key].passProps = undefined;
  67. }
  68. }
  69. }