chore(editor): change code style

This commit is contained in:
MrWindlike 2022-01-28 09:14:03 +08:00
parent 3eabd7ae4a
commit d83ff4169b
4 changed files with 57 additions and 46 deletions

View File

@ -12,11 +12,11 @@ export class FieldModel implements IFieldModel {
refs: Record<ComponentId | ModuleId, string[]> = {};
private value: unknown | Array<IFieldModel> | Record<string, IFieldModel>;
constructor (value: unknown) {
constructor(value: unknown) {
this.update(value);
}
get rawValue () {
get rawValue() {
if (isObject(this.value)) {
if (isArray(this.value)) {
return this.value.map(field => field.rawValue);
@ -32,30 +32,39 @@ export class FieldModel implements IFieldModel {
return this.value;
}
update (value: unknown, shouldExtendValues = true) {
private updateValue(value: unknown, shouldExtendValues = true) {
if (isObject(value)) {
const isArrayValue = isArray(value);
const isOldValueObject = isObject(this.value);
this.value = (Object.keys(value) as Array<keyof typeof value>).reduce((result, key) => {
const oldValue: IFieldModel | null = isObject(this.value) ? this.value[key] : null;
let newValue: FieldModel;
this.value = (Object.keys(value) as Array<keyof typeof value>).reduce(
(result, key) => {
const oldValue: IFieldModel | null = isObject(this.value)
? this.value[key]
: null;
let newValue: FieldModel;
if (oldValue) {
(oldValue as IFieldModel).update(value[key], false);
newValue = oldValue;
} else {
newValue = new FieldModel(value[key]);
}
if (oldValue) {
(oldValue as IFieldModel).update(value[key], false);
newValue = oldValue;
} else {
newValue = new FieldModel(value[key]);
}
if (isArray(result)) {
result.push(newValue);
} else {
result[key] = newValue;
}
if (isArray(result)) {
result.push(newValue);
} else {
result[key] = newValue;
}
return result;
}, (isArrayValue ? [] : (shouldExtendValues && isOldValueObject ? this.value : {})) as Record<string, IFieldModel>);
return result;
},
(isArrayValue
? []
: shouldExtendValues && isOldValueObject
? this.value
: {}) as Record<string, IFieldModel>
);
} else {
this.value = value;
}
@ -63,19 +72,23 @@ export class FieldModel implements IFieldModel {
this.parseReferences();
}
getProperty (key: string | number): FieldModel | undefined {
update(value: unknown) {
this.updateValue(value);
}
getProperty(key: string | number): FieldModel | undefined {
if (typeof this.value === 'object') {
return (this.value as any)[key];
}
return undefined;
}
getValue () {
getValue() {
return this.value;
}
traverse (cb: (f: IFieldModel, key: string) => void) {
function _traverse (field: FieldModel, key: string) {
traverse(cb: (f: IFieldModel, key: string) => void) {
function _traverse(field: FieldModel, key: string) {
if (isObject(field.value)) {
for (const _key in field.value) {
const val = field.getProperty(_key);
@ -90,7 +103,7 @@ export class FieldModel implements IFieldModel {
_traverse(this, '');
}
private parseReferences () {
private parseReferences() {
if (!this.isDynamic || typeof this.value !== 'string') return;
const exps = flattenDeep(
@ -117,7 +130,7 @@ export class FieldModel implements IFieldModel {
break;
default:
}
}
},
});
});
}

View File

@ -2,7 +2,7 @@ import {
ComponentSchema,
TraitSchema,
MethodSchema,
RuntimeTrait
RuntimeTrait,
} from '@sunmao-ui/core';
export type ComponentId = string & {
@ -118,7 +118,7 @@ export interface ITraitModel {
export interface IFieldModel {
// value: any;
isDynamic: boolean;
update: (value: unknown, shouldExtendValues?: boolean) => void;
update: (value: unknown) => void;
getProperty: (key: string) => IFieldModel | void;
getValue: () => unknown | void | IFieldModel;
traverse: (cb: (f: IFieldModel, key: string) => void) => void;

View File

@ -43,20 +43,26 @@ export const renderField = (properties: {
if (typeof value !== 'object') {
const ref = React.createRef<HTMLTextAreaElement>();
const onBlur = () => {
const operation = type
? genOperation(registry, 'modifyTraitProperty', {
const operation = type ? genOperation(
registry,
'modifyTraitProperty',
{
componentId: selectedComponentId,
traitIndex: index,
properties: {
[fullKey]: ref.current?.value,
},
})
: genOperation(registry, 'modifyComponentProperty', {
}
) : genOperation(
registry,
'modifyComponentProperty',
{
componentId: selectedComponentId,
properties: {
[fullKey]: ref.current?.value,
},
});
}
);
eventBus.send('operation', operation);
};
const onChange = (event: any) => {
@ -66,12 +72,7 @@ export const renderField = (properties: {
return (
<FormControl key={`${selectedComponentId}-${fullKey}`}>
<FormLabel>{fullKey}</FormLabel>
<Textarea
ref={ref}
onChange={onChange}
onBlur={onBlur}
value={textareaValue}
/>
<Textarea ref={ref} onChange={onChange} onBlur={onBlur} value={textareaValue} />
</FormControl>
);
} else {

View File

@ -7,7 +7,7 @@ import {
Input,
Select,
Switch,
VStack
VStack,
} from '@chakra-ui/react';
import { Static } from '@sinclair/typebox';
import { CloseIcon } from '@chakra-ui/icons';
@ -40,7 +40,7 @@ export const EventHandlerForm: React.FC<Props> = observer(props => {
initialValues: handler,
onSubmit: values => {
onChange(values);
}
},
});
const updateMethods = useCallback(
@ -89,8 +89,7 @@ export const EventHandlerForm: React.FC<Props> = observer(props => {
value={formik.values.type}
>
{eventTypes.map(e => (
<option key={e}
value={e}>
<option key={e} value={e}>
{e}
</option>
))}
@ -129,8 +128,7 @@ export const EventHandlerForm: React.FC<Props> = observer(props => {
value={formik.values.method.name}
>
{methods.map(m => (
<option key={m}
value={m}>
<option key={m} value={m}>
{m}
</option>
))}
@ -192,8 +190,7 @@ export const EventHandlerForm: React.FC<Props> = observer(props => {
);
return (
<Box position="relative"
width="100%">
<Box position="relative" width="100%">
<VStack className={formWrapperCSS}>
{hideEventType ? null : typeField}
{targetField}