mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2024-11-27 08:39:59 +08:00
Merge pull request #237 from webzard-io/fix/windlike-patch
fix(editor): fix some errors and add test cases
This commit is contained in:
commit
c886327f06
@ -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' });
|
||||
})
|
||||
});
|
||||
|
19
packages/editor/__tests__/operations/component.spec.ts
Normal file
19
packages/editor/__tests__/operations/component.spec.ts
Normal 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');
|
||||
})
|
||||
})
|
17
packages/editor/__tests__/operations/mock.ts
Normal file
17
packages/editor/__tests__/operations/mock.ts
Normal 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: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
2014
packages/editor/src/constants/ecmascript.ts
Normal file
2014
packages/editor/src/constants/ecmascript.ts
Normal file
File diff suppressed because it is too large
Load Diff
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user