feat(runtime): add updateBehavior/ updatePlugin api (#5585)

This commit is contained in:
Aaron 2024-03-22 19:42:22 +08:00 committed by GitHub
parent b9d81ec02d
commit 19405ae0bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 97 additions and 9 deletions

View File

@ -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([]);
});

View File

@ -1,9 +1,11 @@
type ExtensionMetaOptions = {
interface ExtensionMetaOptions {
type: string;
key: string;
};
}
type CustomExtensionOptions = Record<string, any>;
interface CustomExtensionOptions {
[key: string]: any;
}
/**
* <zh/>

View File

@ -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 || [];
}

View File

@ -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;
}

View File

@ -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;
}