Skip to content

Commit

Permalink
feat(action): export getActionClass, extend action
Browse files Browse the repository at this point in the history
  • Loading branch information
dxq613 authored and simaQ committed Mar 19, 2020
1 parent 3559e52 commit db635b7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export {
} from './geometry/label';

// 注册 interaction
export { getInteraction, registerInteraction, registerAction } from './interaction';
export { getInteraction, registerInteraction, registerAction, getActionClass } from './interaction';

// 注册 facet
export { getFacet, registerFacet } from './facet';
Expand Down
2 changes: 1 addition & 1 deletion src/interaction/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as Action } from './base'; // 导出 Action 基类
export { createAction, registerAction } from './register';
export { createAction, registerAction, getActionClass } from './register';
9 changes: 9 additions & 0 deletions src/interaction/action/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export function createAction(actionName: string, context: IInteractionContext):
return action;
}

/**
* 根据 action 的 name 获取定义的类
* @param actionName action 的 name
*/
export function getActionClass(actionName: string) {
const actionOption = ActionCache[actionName];
return actionOption && actionOption.ActionClass;
}

/**
* 注册 Action
* @param actionName - action 的名称
Expand Down
2 changes: 1 addition & 1 deletion src/interaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ export function createInteraction(name: string, view: View, cfg?: LooseObject) {
}

export { default as Interaction } from './interaction';
export { Action, registerAction } from './action';
export { Action, registerAction, getActionClass } from './action';
5 changes: 5 additions & 0 deletions tests/unit/interaction/interaction-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Action from '../../../src/interaction/action/base';
import {
createAction,
createCallbackAction,
getActionClass,
registerAction,
unregisterAction,
} from '../../../src/interaction/action/register';
Expand All @@ -28,6 +29,10 @@ class CustomAction extends Action {
}
registerAction('custom', CustomAction); // 注册
describe('create action test', () => {
it('getActionClass', () => {
expect(getActionClass('custom')).toBe(CustomAction);
expect(getActionClass('test11')).toBe(undefined);
});
it('create action', () => {
const context = new Context(null);
const action = createAction('custom', context);
Expand Down

0 comments on commit db635b7

Please sign in to comment.