react-native-navigation的迁移库

OptionsProcessor.ts 2.5KB

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