Merge pull request #237 from webzard-io/fix/windlike-patch

fix(editor): fix some errors and add test cases
This commit is contained in:
tanbowensg 2022-01-29 11:40:11 +08:00 committed by GitHub
commit c886327f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2086 additions and 5 deletions

View File

@ -42,4 +42,26 @@ describe('Field test', () => {
expect(field.getProperty('data')!.getProperty(1)!.isDynamic).toEqual(true);
expect(field.getProperty('data')!.getProperty(1)!.refs).toEqual({ fetch: ['data'] });
});
it('update array property', () => {
const field = new FieldModel({ data: ['A', 'B'] });
expect(field.rawValue).toEqual({ data: ['A', 'B'] });
field.update({data: ['B']});
expect(field.rawValue).toEqual({ data: ['B'] });
field.update({data: ['C']});
expect(field.rawValue).toEqual({ data: ['C'] });
})
it('update object property', () => {
const field = new FieldModel({ data: { a: 1, b: 2 }, value: '' });
expect(field.rawValue).toEqual({ data: { a: 1, b: 2 }, value: '' });
field.update({ data: { a: 1 } });
expect(field.rawValue).toEqual({ data: { a: 1 }, value: '' });
field.update({ data: { a: 2 } });
expect(field.rawValue).toEqual({ data: { a: 2 }, value: '' });
field.update({ value: 'text' });
expect(field.rawValue).toEqual({ data: { a: 2 }, value: 'text' });
})
});

View File

@ -0,0 +1,19 @@
import { AppModel } from '../../src/AppModel/AppModel';
import { ComponentId } from '../../src/AppModel/IAppModel';
import { registry } from '../sevices';
import { AppSchema } from './mock';
import { genOperation } from '../../src/operations';
describe('Component operations', ()=> {
it('Change component id', ()=> {
const appModel = new AppModel(AppSchema.spec.components, registry);
expect(appModel.getComponentById('text1' as ComponentId).id).toBe('text1');
const operation = genOperation(registry, 'modifyComponentId', {
componentId: 'text1',
newId: 'text2',
});
operation.do(appModel);
expect(appModel.getComponentById('text2' as ComponentId).id).toBe('text2');
})
})

View File

@ -0,0 +1,17 @@
import { Application } from '@sunmao-ui/core';
export const AppSchema: Application = {
kind: 'Application',
version: 'example/v1',
metadata: { name: 'dialog_component', description: 'dialog component example' },
spec: {
components: [
{
id: 'text1',
type: 'core/v1/text',
properties: { value: { raw: 'text', format: 'plain' } },
traits: [],
},
],
},
};

View File

@ -80,6 +80,11 @@ export class AppModel implements IAppModel {
}
}
changeComponentMapId(oldId: ComponentId, newId: ComponentId) {
this.componentMap[newId] = this.componentMap[oldId];
delete this.componentMap[oldId];
}
_bindComponentToModel(component: IComponentModel) {
this.componentMap[component.id] = component;
component.appModel = this;

View File

@ -170,7 +170,7 @@ export class ComponentModel implements IComponentModel {
removeChild(child: IComponentModel) {
const slotChildren = this.children[child.parentSlot!];
if (slotChildren) {
const index = slotChildren.indexOf(child)
const index = slotChildren.indexOf(child);
if (index > -1) {
slotChildren.splice(slotChildren.indexOf(child), 1);
child._isDirty = true;
@ -188,10 +188,10 @@ export class ComponentModel implements IComponentModel {
}
changeId(newId: ComponentId) {
const oldId = this.id;
const isIdExist = !!this.appModel.getComponentById(newId);
if (isIdExist) {
throw Error(`Id ${newId} already exist`);
return this;
}
this.id = newId;
for (const slot in this.children) {
@ -207,6 +207,7 @@ export class ComponentModel implements IComponentModel {
});
}
this._isDirty = true;
this.appModel.changeComponentMapId(oldId, newId);
return this;
}

View File

@ -50,6 +50,7 @@ export interface IAppModel {
getComponentById(id: ComponentId): IComponentModel | undefined;
removeComponent(componentId: ComponentId): void;
appendChild(component: IComponentModel): void;
changeComponentMapId(oldId: ComponentId, newId: ComponentId): void;
_bindComponentToModel(component: IComponentModel): void;
}

View File

@ -12,7 +12,7 @@ import 'codemirror/addon/comment/comment';
import 'codemirror/addon/hint/show-hint';
import 'tern/plugin/doc_comment';
import 'tern/plugin/complete_strings';
import ecma from 'tern/defs/ecmascript.json';
import ecma from '../../constants/ecmascript';
import tern, { Def } from 'tern';
// TODO: tern uses global variable, maybe there is some workaround

File diff suppressed because it is too large Load Diff

View File

@ -9,12 +9,14 @@ export type ModifyComponentIdLeafOperationContext = {
export class ModifyComponentIdLeafOperation extends BaseLeafOperation<ModifyComponentIdLeafOperationContext> {
do(prev: AppModel): AppModel {
const component = prev.getComponentById(this.context.componentId as ComponentId);
const oldId = this.context.componentId as ComponentId;
const newId = this.context.newId as ComponentId;
const component = prev.getComponentById(oldId);
if (!component) {
console.warn('component not found');
return prev;
}
component.changeId(this.context.newId as ComponentId);
component.changeId(newId);
return prev;
}