Merge pull request #11228 from allenve/feat-globalevent-panel

feat: 全局事件动作配置面板
This commit is contained in:
Allen 2024-11-20 14:49:53 +08:00 committed by GitHub
commit 66fc73b18c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 10 deletions

View File

@ -506,6 +506,7 @@ export interface RendererJSONSchemaResolveEventContext
} }
export interface IGlobalEvent { export interface IGlobalEvent {
label: string;
name: string; // 事件名称,唯一 name: string; // 事件名称,唯一
description: string; // 事件描述 description: string; // 事件描述
mapping: Array<{ mapping: Array<{

View File

@ -318,6 +318,7 @@ const editorLanguages = [
const globalEvents = [ const globalEvents = [
{ {
name: 'globalEventA', name: 'globalEventA',
label: '全局事件A',
description: '全局事件动作A', description: '全局事件动作A',
mapping: [ mapping: [
{ {
@ -332,6 +333,7 @@ const globalEvents = [
}, },
{ {
name: 'globalEventB', name: 'globalEventB',
label: '全局事件B',
description: '全局事件动作A', description: '全局事件动作A',
mapping: [ mapping: [
{ {

View File

@ -3218,7 +3218,9 @@ export const getEventControlConfig = (
} }
} }
delete config.data; if (config.actionType !== 'broadcast') {
delete config.data;
}
// 处理下 addItem 的初始化 // 处理下 addItem 的初始化
if (action.actionType === 'addItem') { if (action.actionType === 'addItem') {

View File

@ -152,7 +152,7 @@ export class EventControl extends React.Component<
constructor(props: EventControlProps) { constructor(props: EventControlProps) {
super(props); super(props);
const {events, value, data, rawType} = props; const {events, value, data, rawType, globalEvents} = props;
const eventPanelActive: { const eventPanelActive: {
[prop: string]: boolean; [prop: string]: boolean;
@ -169,6 +169,10 @@ export class EventControl extends React.Component<
eventPanelActive[event.eventName] = true; eventPanelActive[event.eventName] = true;
}); });
globalEvents?.forEach(event => {
eventPanelActive[event.name] = true;
});
this.state = { this.state = {
onEvent: value ?? this.generateEmptyDefault(pluginEvents), onEvent: value ?? this.generateEmptyDefault(pluginEvents),
events: pluginEvents, events: pluginEvents,
@ -619,14 +623,16 @@ export class EventControl extends React.Component<
actionTree, actionTree,
actions: pluginActions, actions: pluginActions,
commonActions, commonActions,
allComponents allComponents,
globalEvents
} = this.props; } = this.props;
const {events, onEvent} = this.state; const {events, onEvent} = this.state;
const eventConfig = events.find( const eventConfig = events.find(
item => item.eventName === data.actionData!.eventKey item => item.eventName === data.actionData!.eventKey
); );
const globalEventConfig = globalEvents?.find(
item => item.name === data.actionData!.eventKey
);
// 收集当前事件动作出参 // 收集当前事件动作出参
let actions = onEvent[data.actionData!.eventKey].actions; let actions = onEvent[data.actionData!.eventKey].actions;
@ -642,11 +648,33 @@ export class EventControl extends React.Component<
let jsonSchema: any = {}; let jsonSchema: any = {};
// 动态构建事件参数 if (globalEventConfig) {
if (typeof eventConfig?.dataSchema === 'function') { jsonSchema = {
jsonSchema = eventConfig.dataSchema(manager)?.[0]; type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: (globalEventConfig.mapping || []).reduce(
(acc: any, item) => {
acc[item.key] = {
type: item.type,
title: `${item.key}(全局事件参数)`
};
return acc;
},
{}
)
}
}
};
} else { } else {
jsonSchema = {...(eventConfig?.dataSchema?.[0] ?? {})}; // 动态构建事件参数
if (typeof eventConfig?.dataSchema === 'function') {
jsonSchema = eventConfig.dataSchema(manager)?.[0];
} else {
jsonSchema = {...(eventConfig?.dataSchema?.[0] ?? {})};
}
} }
actions actions
@ -1174,7 +1202,7 @@ export class EventControl extends React.Component<
tooltipPlacement: 'left', tooltipPlacement: 'left',
disabled: Object.keys(onEvent).includes(item.name), disabled: Object.keys(onEvent).includes(item.name),
actionType: '', actionType: '',
label: item.name, label: item.label,
onClick: this.addGlobalEvent.bind( onClick: this.addGlobalEvent.bind(
this, this,
item, item,