Skip to content

Commit

Permalink
feat: support plugin service
Browse files Browse the repository at this point in the history
  • Loading branch information
neuqzxy committed Jan 17, 2025
1 parent 9b030ea commit 70dd33b
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 26 deletions.
19 changes: 14 additions & 5 deletions packages/vstory-core/src/character/character-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import type { ICharacterRuntimeConfig, ILayoutLine } from './../interface/charac
import type { IGraphic } from '@visactor/vrender-core';
import { Generator } from '@visactor/vrender-core';
import type { ICharacter } from '../interface/character';
import type { ICharacterConfig, ICharacterInitOption } from '../interface/dsl/dsl';
import type { ICharacterConfig, ICharacterInitOption, IUpdateConfigParams } from '../interface/dsl/dsl';
import { cloneDeep, isArray } from '@visactor/vutils';
import type { ICharacterPickInfo, IStoryEvent } from '../interface/event';
import type { IStory } from '../interface/story';
import type { IStoryCanvas } from '../interface/canvas';
import type { IConfigProcess } from './config-transform/interface';
import type { IUpdateConfigParams } from './chart/interface/runtime';
import { getLayoutLine } from '../utils/layout';
import { foreachAllConstructor } from '../utils/type';
import { ThemeManager } from '../theme/theme-manager';
import { RuntimeStore } from '../store';
import { Events } from '../constants/events';

export abstract class CharacterBase<T> implements ICharacter {
readonly id: string;
Expand Down Expand Up @@ -56,8 +56,16 @@ export abstract class CharacterBase<T> implements ICharacter {
this._canvas = option.canvas;
}

setConfig(config: Partial<IUpdateConfigParams>, forceMergeOption: boolean = true) {
if (forceMergeOption === false) {
setConfig(
config: Partial<IUpdateConfigParams>,
params: {
forceMergeOption?: boolean;
mode?: number;
} = {}
) {
const { forceMergeOption = true } = params;
this.story.emit(Events.BEFORE_SET_CONFIG, { config, character: this, params });
if (!forceMergeOption) {
const { options, ...rest } = config;
this.configProcess.updateConfig(rest, config, this._config);
this._config.options = options;
Expand All @@ -68,6 +76,7 @@ export abstract class CharacterBase<T> implements ICharacter {
this.applyConfigToAttribute(diffConfig, this._config);
}
this._setAttributes(this._attribute);
this.story.emit(Events.AFTER_SET_CONFIG, { config, character: this, params });
}

init(): void {
Expand Down Expand Up @@ -102,7 +111,7 @@ export abstract class CharacterBase<T> implements ICharacter {
this.init();
}

protected diffConfig(config: IUpdateConfigParams): IUpdateConfigParams {
diffConfig(config: IUpdateConfigParams): IUpdateConfigParams {
return config;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vstory-core/src/character/chart/character-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { CharacterBase } from '../character-base';
import type { IChartGraphicAttribute } from './graphic/vchart-graphic';
import { VChartGraphic } from './graphic/vchart-graphic';
import { getChartModelWithEvent } from './utils/vchart-pick';
import type { ICharacterConfig, ICharacterInitOption } from '../../interface/dsl/dsl';
import type { ICharacterConfig, ICharacterInitOption, IUpdateConfigParams } from '../../interface/dsl/dsl';
import type { IChartCharacterConfig } from '../../interface/dsl/chart';
import { getLayoutFromWidget } from '../../utils/layout';
import type { IChartCharacterRuntime, IUpdateConfigParams } from './interface/runtime';
import type { IChartCharacterRuntime } from './interface/runtime';
import { ChartConfigProcess } from './chart-config-process';
import type { ICharacterChart } from './interface/character-chart';
import { mergeChartOption } from '../../utils/chart';
Expand Down
2 changes: 0 additions & 2 deletions packages/vstory-core/src/character/chart/interface/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ export interface IChartCharacterRuntime {
export interface IChartCharacterRuntimeConstructor {
new (): IChartCharacterRuntime;
}

export type IUpdateConfigParams = Omit<Partial<ICharacterConfig>, 'id' | 'type'>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IGraphic } from '@visactor/vrender-core';
import { CharacterBase } from '../character-base';
import type { IComponentCharacterConfig } from '../../interface/dsl/component';
import type { IComponentCharacterRuntime, IUpdateConfigParams } from './interface/runtime';
import type { ICharacterInitOption } from '../../interface/dsl/dsl';
import type { IComponentCharacterRuntime } from './interface/runtime';
import type { ICharacterInitOption, IUpdateConfigParams } from '../../interface/dsl/dsl';
import { ComponentConfigProcess } from './component-config-process';
import type { ICharacterComponent } from './interface/character-component';
import type { IStoryEvent, ICharacterPickInfo } from '../../interface/event';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ export interface IComponentCharacterRuntime {
export interface IComponentCharacterRuntimeConstructor {
new (): IComponentCharacterRuntime;
}

export type IUpdateConfigParams = Omit<Partial<ICharacterConfig>, 'id' | 'type'>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cloneDeep, isValid, merge } from '@visactor/vutils';
import type { ICharacter } from '../../interface/character';
import { deepMergeWithDeletedAttr } from '../../utils/merge';
import type { IConfigProcess } from './interface';
import type { IUpdateConfigParams } from '../chart/interface/runtime';
import type { IUpdateConfigParams } from '../../interface/dsl/dsl';

export class ConfigProcessBase implements IConfigProcess {
protected _character: ICharacter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ICharacter } from '../../interface/character';
import type { IUpdateConfigParams } from '../chart/interface/runtime';
import type { IUpdateConfigParams } from '../../interface/dsl/dsl';

export interface IConfigProcess {
checkEnable: (diffConfig: IUpdateConfigParams, config: IUpdateConfigParams) => boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/vstory-core/src/character/table/character-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { ICharacterPickInfo, IStoryEvent } from '../../interface/event';
import { CharacterBase } from '../character-base';
import type { ITableGraphicAttribute } from './graphic/vtable-graphic';
import { VTableGraphic } from './graphic/vtable-graphic';
import type { ICharacterConfig, ICharacterInitOption } from '../../interface/dsl/dsl';
import type { ICharacterConfig, ICharacterInitOption, IUpdateConfigParams } from '../../interface/dsl/dsl';
import type { ITableCharacterConfig } from '../../interface/dsl/table';
import { getLayoutFromWidget } from '../../utils/layout';
import type { ITableCharacterRuntime, IUpdateConfigParams } from './interface/runtime';
import type { ITableCharacterRuntime } from './interface/runtime';
import { TableConfigProcess } from './table-config-process';
import type { ICharacterTable, IVTable } from './interface/character-table';
import { isArray } from '@visactor/vutils';
Expand Down
2 changes: 0 additions & 2 deletions packages/vstory-core/src/character/table/interface/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ export interface ITableCharacterRuntime {
export interface ITableCharacterRuntimeConstructor {
new (): ITableCharacterRuntime;
}

export type IUpdateConfigParams = Omit<Partial<ICharacterConfig>, 'id' | 'type'>;
4 changes: 4 additions & 0 deletions packages/vstory-core/src/constants/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const Events = {
BEFORE_SET_CONFIG: 'beforeSetConfig',
AFTER_SET_CONFIG: 'afterSetConfig'
};
70 changes: 70 additions & 0 deletions packages/vstory-core/src/core/plugin-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { IPlugin, IPluginService } from '../interface/plugin-service';
import type { IStory } from '../interface/story';

export class DefaultPluginService implements IPluginService {
declare onStartupFinishedPlugin: IPlugin[];
declare onRegisterPlugin: IPlugin[];
declare story: IStory;
declare actived: boolean;

constructor() {
this.onStartupFinishedPlugin = [];
this.onRegisterPlugin = [];
this.actived = false;
}

active(story: IStory, params: { pluginList?: IPlugin[] }) {
this.story = story;
this.actived = true;

// 启动插件
const { pluginList } = params;
pluginList &&
pluginList.forEach(p => {
this.register(p);
});
}

findPluginsByName(name: string): IPlugin[] {
const arr: IPlugin[] = [];
this.onStartupFinishedPlugin.forEach(plugin => {
if (plugin.name === name) {
arr.push(plugin);
}
});
this.onRegisterPlugin.forEach(plugin => {
if (plugin.name === name) {
arr.push(plugin);
}
});
return arr;
}

register(plugin: IPlugin) {
if (plugin.activeEvent === 'onStartupFinished') {
this.onStartupFinishedPlugin.push(plugin);
} else if (plugin.activeEvent === 'onRegister') {
this.onRegisterPlugin.push(plugin);
plugin.activate(this);
}
}
unRegister(plugin: IPlugin) {
if (plugin.activeEvent === 'onStartupFinished') {
this.onStartupFinishedPlugin.splice(this.onStartupFinishedPlugin.indexOf(plugin), 1);
} else if (plugin.activeEvent === 'onRegister') {
this.onRegisterPlugin.splice(this.onStartupFinishedPlugin.indexOf(plugin), 1);
}
plugin.deactivate(this);
}

release(...params: any): void {
this.onStartupFinishedPlugin.forEach(plugin => {
plugin.deactivate(this);
});
this.onStartupFinishedPlugin = [];
this.onRegisterPlugin.forEach(plugin => {
plugin.deactivate(this);
});
this.onRegisterPlugin = [];
}
}
12 changes: 10 additions & 2 deletions packages/vstory-core/src/core/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import type { ICharacterConfig, IStoryDSL } from '../interface/dsl/dsl';
import { StoryCanvas } from './canvas';
import type { IStoryCanvas } from '../interface/canvas';
import type { IAABBBoundsLike } from '@visactor/vutils';
import { isString } from '@visactor/vutils';
import { EventEmitter, isString } from '@visactor/vutils';
import type { ICharacter } from '../interface/character';
import type { IPlayer } from '../interface/player';
import type { ICharacterTree } from '../interface/character-tree';
import { CharacterTree } from './character-tree';
import type { IPluginService } from '../interface/plugin-service';
import { DefaultPluginService } from './plugin-service';

type NodeCanvas = any;

Expand All @@ -27,13 +29,14 @@ export interface IStoryInitOption {
theme?: string;
}

export class Story implements IStory {
export class Story extends EventEmitter implements IStory {
readonly id: string;
protected _canvas: IStoryCanvas;
protected _dsl: IStoryDSL | null;
protected _player: IPlayer;
protected _characterTree: ICharacterTree;
protected _theme: string;
pluginService: IPluginService;

get canvas(): IStoryCanvas {
return this._canvas;
Expand All @@ -48,6 +51,7 @@ export class Story implements IStory {
}

constructor(dsl: IStoryDSL | null, option: IStoryInitOption) {
super();
this.id = `test-mvp_${Generator.GenAutoIncrementId()}`;
const {
dom,
Expand Down Expand Up @@ -80,6 +84,10 @@ export class Story implements IStory {
this._characterTree = new CharacterTree(this);
this._dsl = dsl;
this._theme = theme;
this.pluginService = new DefaultPluginService();
this.pluginService.active(this, {
pluginList: []
});
}

init(player: IPlayer) {
Expand Down
3 changes: 3 additions & 0 deletions packages/vstory-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ export * from './module/graphic';
export * from './module/character';

export * from './constants/character';
export * from './constants/events';

export * from './interface/action-processor';
export * from './interface/dsl/dsl';
export * from './interface/character';
export * from './interface/story';
export * from './interface/event';
export * from './interface/plugin-service';
export * from './interface/releaseable';
export * from './core/processorRegistry';
export * from './tools/global-ticker';
export * from './character/component/character-component';
Expand Down
9 changes: 7 additions & 2 deletions packages/vstory-core/src/interface/character.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IGraphic } from '@visactor/vrender-core';
import { IGroup } from '@visactor/vrender-core';
import type { ICharacterConfig } from './dsl/dsl';
import type { ICharacterConfig, IUpdateConfigParams } from './dsl/dsl';
import type { ICharacterPickInfo, IStoryEvent } from './event';
import type { IReleaseable } from './releaseable';
import type { IStory } from './story';
Expand All @@ -17,6 +17,10 @@ export interface ILayoutLine {
bounds: IAABBBounds;
}

export enum SetConfigMode {
default = 0,
animate = 1
}
export interface ICharacter extends IReleaseable {
id: string;
type: string;
Expand Down Expand Up @@ -47,7 +51,8 @@ export interface ICharacter extends IReleaseable {
checkEvent: (event: IStoryEvent) => false | (ICharacterPickInfo & any);

toDSL: () => ICharacterConfig;
setConfig: (config: Partial<ICharacterConfig>) => void;
setConfig: (config: Partial<ICharacterConfig>, params?: { forceMergeOption?: boolean; mode?: SetConfigMode }) => void;
diffConfig: (config: IUpdateConfigParams) => IUpdateConfigParams;

getAttribute: () => any;

Expand Down
2 changes: 2 additions & 0 deletions packages/vstory-core/src/interface/dsl/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export type ICharacterConfig =
| ITableCharacterConfig
| IPivotChartCharacterConfig;

export type IUpdateConfigParams = Omit<Partial<ICharacterConfig>, 'id' | 'type'>;

export interface ICharacterInitOption {
story: IStory;
canvas: IStoryCanvas;
Expand Down
18 changes: 18 additions & 0 deletions packages/vstory-core/src/interface/plugin-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Releaseable } from '@visactor/vrender-core';
import type { IStory } from './story';

export interface IPluginService extends Releaseable {
register: (plugin: IPlugin) => void;
unRegister: (plugin: IPlugin) => void;
active: (story: IStory, params: { pluginList?: IPlugin[] }) => void;
actived: boolean;
story: IStory;
findPluginsByName: (name: string) => IPlugin[];
}

export interface IPlugin {
name: string;
activeEvent: 'onStartupFinished' | 'onRegister';
activate: (context: IPluginService) => void;
deactivate: (context: IPluginService) => void;
}
3 changes: 2 additions & 1 deletion packages/vstory-core/src/interface/story.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EventEmitter } from '@visactor/vutils';
import type { IStoryCanvas } from './canvas';
import type { ICharacter } from './character';
import type { IActionSpec, ICharacterConfig, IStoryDSL } from './dsl/dsl';
Expand All @@ -9,7 +10,7 @@ export type IActionParams = {
sceneId: string;
};

export interface IStory extends IReleaseable {
export interface IStory extends IReleaseable, EventEmitter {
readonly id: string;
readonly canvas: IStoryCanvas;
readonly player: IPlayer;
Expand Down
1 change: 1 addition & 0 deletions packages/vstory-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './edit';
// export * from './edit-component';
export * from './interface';
export * from './register';
export * from './plugins';
1 change: 1 addition & 0 deletions packages/vstory-editor/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './snapshot/snapshot';
Loading

0 comments on commit 70dd33b

Please sign in to comment.