remove parseType in runtime

This commit is contained in:
Bowen Tan 2022-01-05 11:22:36 +08:00
parent fae2591088
commit fdff89c1d5
7 changed files with 53 additions and 66 deletions

View File

@ -1,5 +1,4 @@
import { ApplicationComponent } from '@sunmao-ui/core';
import { parseType } from '@sunmao-ui/runtime';
import { ApplicationComponent, parseType } from '@sunmao-ui/core';
import { ComponentModel } from './ComponentModel';
import {
ComponentId,

View File

@ -2,7 +2,8 @@ import React from 'react';
import { flatten } from 'lodash-es';
import { FormControl, FormLabel, Input, Textarea, VStack } from '@chakra-ui/react';
import { TSchema } from '@sinclair/typebox';
import { parseType, parseTypeBox } from '@sunmao-ui/runtime';
import { parseType } from '@sunmao-ui/core';
import { parseTypeBox } from '@sunmao-ui/runtime';
import { eventBus } from '../../eventBus';
import { EventTraitForm } from './EventTraitForm';
import { GeneralTraitFormList } from './GeneralTraitFormList';

View File

@ -1,5 +1,4 @@
import { ApplicationComponent } from '@sunmao-ui/core';
import { parseType } from '@sunmao-ui/runtime';
import { ApplicationComponent, parseType } from '@sunmao-ui/core';
import { isDraft, original } from 'immer';
import { get } from 'lodash-es';
import { registry } from '../setup';
@ -36,12 +35,15 @@ export function getComponentAndChildrens(
if (!target) {
return [];
}
return allComponents.reduce<ApplicationComponent[]>((result, component) => {
const slotTrait = component.traits.find(trait => trait.type === 'core/v1/slot');
const slotId = get(slotTrait, 'properties.container.id');
if (slotId === componentId) {
return result.concat(getComponentAndChildrens(component.id, allComponents));
}
return result;
}, [target]);
return allComponents.reduce<ApplicationComponent[]>(
(result, component) => {
const slotTrait = component.traits.find(trait => trait.type === 'core/v1/slot');
const slotId = get(slotTrait, 'properties.container.id');
if (slotId === componentId) {
return result.concat(getComponentAndChildrens(component.id, allComponents));
}
return result;
},
[target]
);
}

View File

@ -2,13 +2,16 @@ import { Static } from '@sinclair/typebox';
import React, { useCallback, useEffect, useMemo } from 'react';
import { get } from 'lodash-es';
import { useDeepCompareMemo } from 'use-deep-compare';
import { RuntimeApplication } from '@sunmao-ui/core';
import { UIServices, RuntimeModuleSchema } from '../../types/RuntimeSchema';
import { Application, parseType, RuntimeApplication } from '@sunmao-ui/core';
import {
UIServices,
RuntimeModuleSchema,
RuntimeApplicationComponent,
} from '../../types/RuntimeSchema';
import { EventHandlerSchema } from '../../types/TraitPropertiesSchema';
import { resolveAppComponents } from '../../services/resolveAppComponents';
import { ImplWrapper } from './ImplWrapper';
import { watch } from '../../utils/watchReactivity';
import { parseTypeComponents } from '../../utils/parseType';
import { ImplementedRuntimeModule } from '../../services/registry';
import { getSlotWithMap } from './Slot';
@ -46,20 +49,20 @@ const ModuleRendererContent: React.FC<Props & { moduleSpec: ImplementedRuntimeMo
}).obj;
}
const evalWithScope = useCallback(<T extends Record<string, any>>(
obj: T,
scope: Record<string, any>
): T =>{
const hasScopeKey = (exp: string) => {
return Object.keys(scope).some(key => exp.includes('{{') && exp.includes(key));
};
return services.stateManager.mapValuesDeep({ obj }, ({ value }) => {
if (typeof value === 'string' && hasScopeKey(value)) {
return services.stateManager.maskedEval(value, true, scope);
}
return value;
}).obj;
}, [services.stateManager]);
const evalWithScope = useCallback(
<T extends Record<string, any>>(obj: T, scope: Record<string, any>): T => {
const hasScopeKey = (exp: string) => {
return Object.keys(scope).some(key => exp.includes('{{') && exp.includes(key));
};
return services.stateManager.mapValuesDeep({ obj }, ({ value }) => {
if (typeof value === 'string' && hasScopeKey(value)) {
return services.stateManager.maskedEval(value, true, scope);
}
return value;
}).obj;
},
[services.stateManager]
);
// first eval the property, handlers, id of module
const evaledProperties = evalObject(properties);
@ -172,3 +175,19 @@ const ModuleRendererContent: React.FC<Props & { moduleSpec: ImplementedRuntimeMo
return <>{result}</>;
};
function parseTypeComponents(
c: Application['spec']['components'][0]
): RuntimeApplicationComponent {
return {
...c,
parsedType: parseType(c.type),
traits: c.traits.map(t => {
return {
...t,
parsedType: parseType(t.type),
};
}),
};
}

View File

@ -6,7 +6,7 @@ import {
parseType,
parseVersion,
} from '@sunmao-ui/core';
import { RuntimeApplicationComponent } from '..';
import { RuntimeApplicationComponent } from '../types/RuntimeSchema';
export class RuntimeAppSchemaManager {
private runtimeComponentsCache: Record<string, RuntimeApplicationComponent> = {};
private componentsCache: Record<string, ApplicationComponent> = {};

View File

@ -3,6 +3,7 @@ import {
RuntimeModuleSpec,
ApplicationComponent,
RuntimeComponentSpec,
parseType,
} from '@sunmao-ui/core';
// components
/* --- plain --- */
@ -26,7 +27,6 @@ import {
ComponentImplementationProps,
TraitImplementation,
} from '../types/RuntimeSchema';
import { parseType } from '../utils/parseType';
import { parseModuleSchema } from '../utils/parseModuleSchema';
import { cloneDeep } from 'lodash-es';

View File

@ -1,34 +0,0 @@
import { Application } from '@sunmao-ui/core';
import { RuntimeApplicationComponent } from '../types/RuntimeSchema';
// parse component Type
export function parseType(v: string) {
const TYPE_REG = /^([a-zA-Z0-9_\d]+\/[a-zA-Z0-9_\d]+)\/([a-zA-Z0-9_\d]+)$/;
function isValidType(v: string): boolean {
return TYPE_REG.test(v);
}
if (!isValidType(v)) {
throw new Error(`Invalid type string: "${v}"`);
}
const [, version, name] = v.match(TYPE_REG) ?? [];
return {
version,
name,
};
}
export function parseTypeComponents(
c: Application['spec']['components'][0]
): RuntimeApplicationComponent {
return {
...c,
parsedType: parseType(c.type),
traits: c.traits.map(t => {
return {
...t,
parsedType: parseType(t.type),
};
}),
};
}