mirror of
https://gitee.com/antv/g6.git
synced 2024-11-30 02:38:20 +08:00
feat(runtime): add updateBehavior/ updatePlugin api (#5585)
This commit is contained in:
parent
b9d81ec02d
commit
19405ae0bc
@ -65,16 +65,24 @@ describe('Graph', () => {
|
||||
expect(graph.getLayout()).toEqual({ type: 'd3force' });
|
||||
});
|
||||
|
||||
it('getBehaviors/setBehaviors', () => {
|
||||
it('getBehaviors/setBehaviors/updateBehavior', () => {
|
||||
expect(graph.getBehaviors()).toEqual(['zoom-canvas', 'drag-canvas']);
|
||||
graph.setBehaviors(['drag-canvas']);
|
||||
expect(graph.getBehaviors()).toEqual(['drag-canvas']);
|
||||
graph.setBehaviors([{ key: 'behavior-1', type: 'zoom-canvas', enable: false }]);
|
||||
expect(graph.getBehaviors()).toEqual([{ key: 'behavior-1', type: 'zoom-canvas', enable: false }]);
|
||||
graph.updateBehavior({ key: 'behavior-1', enable: true });
|
||||
expect(graph.getBehaviors()).toEqual([{ key: 'behavior-1', type: 'zoom-canvas', enable: true }]);
|
||||
});
|
||||
|
||||
it('getPlugins/setPlugins', () => {
|
||||
it('getPlugins/setPlugins/updatePlugin', () => {
|
||||
expect(graph.getPlugins()).toEqual([]);
|
||||
graph.setPlugins([{ type: 'test' }]);
|
||||
expect(graph.getPlugins()).toEqual([{ type: 'test' }]);
|
||||
graph.setPlugins([{ key: 'plugin-1', type: 'test' }]);
|
||||
expect(graph.getPlugins()).toEqual([{ key: 'plugin-1', type: 'test' }]);
|
||||
graph.updatePlugin({ key: 'plugin-1', enable: false });
|
||||
expect(graph.getPlugins()).toEqual([{ key: 'plugin-1', type: 'test', enable: false }]);
|
||||
graph.setPlugins([]);
|
||||
});
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
type ExtensionMetaOptions = {
|
||||
interface ExtensionMetaOptions {
|
||||
type: string;
|
||||
key: string;
|
||||
};
|
||||
}
|
||||
|
||||
type CustomExtensionOptions = Record<string, any>;
|
||||
interface CustomExtensionOptions {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 扩展配置项
|
||||
|
@ -18,6 +18,8 @@ import type {
|
||||
PluginOptions,
|
||||
ThemeOptions,
|
||||
} from '../spec';
|
||||
import { UpdateBehaviorOption } from '../spec/behavior';
|
||||
import { UpdatePluginOption } from '../spec/plugin';
|
||||
import type {
|
||||
CallableValue,
|
||||
DataID,
|
||||
@ -198,20 +200,78 @@ export class Graph extends EventEmitter {
|
||||
return this.options.layout!;
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 设置交互
|
||||
*
|
||||
* <en/> Set behaviors
|
||||
* @param behaviors - <zh/> 交互配置 | <en/> behavior configuration
|
||||
*/
|
||||
public setBehaviors(behaviors: CallableValue<BehaviorOptions>): void {
|
||||
this.options.behaviors = isFunction(behaviors) ? behaviors(this.getBehaviors()) : behaviors;
|
||||
this.context.behavior?.setBehaviors(this.options.behaviors);
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 更新交互
|
||||
*
|
||||
* <en/> Update behavior
|
||||
* @param behavior - <zh/> 交互配置 | <en/> behavior configuration
|
||||
*/
|
||||
public updateBehavior(behavior: UpdateBehaviorOption): void {
|
||||
this.setBehaviors((behaviors) =>
|
||||
behaviors.map((_behavior) => {
|
||||
if (isObject(_behavior) && _behavior.key === behavior.key) {
|
||||
return { ..._behavior, ...behavior };
|
||||
}
|
||||
return _behavior;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 获取交互
|
||||
*
|
||||
* <en/> Get behaviors
|
||||
* @returns <zh/> 交互配置 | <en/> behavior configuration
|
||||
*/
|
||||
public getBehaviors(): BehaviorOptions {
|
||||
return this.options.behaviors || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 设置插件
|
||||
*
|
||||
* <en/> Set plugins
|
||||
* @param plugins - <zh/> 插件配置 | <en/> plugin configuration
|
||||
*/
|
||||
public setPlugins(plugins: CallableValue<PluginOptions>): void {
|
||||
this.options.plugins = isFunction(plugins) ? plugins(this.getPlugins()) : plugins;
|
||||
this.context.plugin?.setPlugins(this.options.plugins);
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 更新插件
|
||||
*
|
||||
* <en/> Update plugin
|
||||
* @param plugin - <zh/> 插件配置 | <en/> plugin configuration
|
||||
*/
|
||||
public updatePlugin(plugin: UpdatePluginOption): void {
|
||||
this.setPlugins((plugins) =>
|
||||
plugins.map((_plugin) => {
|
||||
if (isObject(_plugin) && _plugin.key === plugin.key) {
|
||||
return { ..._plugin, ...plugin };
|
||||
}
|
||||
return _plugin;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* <zh/> 获取插件
|
||||
*
|
||||
* <en/> Get plugins
|
||||
* @returns <zh/> 插件配置 | <en/> plugin configuration
|
||||
*/
|
||||
public getPlugins(): PluginOptions {
|
||||
return this.options.plugins || [];
|
||||
}
|
||||
|
@ -1,6 +1,15 @@
|
||||
import type { BuiltInBehaviorOptions } from '../behaviors/types';
|
||||
import type { ExtensionOptions, LooselyExtensionOption } from '../registry/extension/types';
|
||||
import type { ExtensionOptions } from '../registry/extension/types';
|
||||
|
||||
export type BehaviorOptions = ExtensionOptions<BuiltInBehaviorOptions>;
|
||||
|
||||
export type CustomBehaviorOption = LooselyExtensionOption;
|
||||
export interface UpdateBehaviorOption {
|
||||
key: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface CustomBehaviorOption {
|
||||
type?: string;
|
||||
key?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
@ -1,6 +1,15 @@
|
||||
import type { BuiltInPluginOptions } from '../plugins/types';
|
||||
import type { ExtensionOptions, LooselyExtensionOption } from '../registry/extension/types';
|
||||
import type { ExtensionOptions } from '../registry/extension/types';
|
||||
|
||||
export type PluginOptions = ExtensionOptions<BuiltInPluginOptions>;
|
||||
|
||||
export type CustomPluginOption = LooselyExtensionOption;
|
||||
export interface UpdatePluginOption {
|
||||
key: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface CustomPluginOption {
|
||||
type?: string;
|
||||
key?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user