diff --git a/packages/amis-editor-core/src/plugin.ts b/packages/amis-editor-core/src/plugin.ts index 78e5a41fb..882252f8c 100644 --- a/packages/amis-editor-core/src/plugin.ts +++ b/packages/amis-editor-core/src/plugin.ts @@ -506,6 +506,7 @@ export interface RendererJSONSchemaResolveEventContext } export interface IGlobalEvent { + label: string; name: string; // 事件名称,唯一 description: string; // 事件描述 mapping: Array<{ diff --git a/packages/amis-editor/examples/Editor.tsx b/packages/amis-editor/examples/Editor.tsx index 55023276d..e0bf4cb51 100644 --- a/packages/amis-editor/examples/Editor.tsx +++ b/packages/amis-editor/examples/Editor.tsx @@ -318,6 +318,7 @@ const editorLanguages = [ const globalEvents = [ { name: 'globalEventA', + label: '全局事件A', description: '全局事件动作A', mapping: [ { @@ -332,6 +333,7 @@ const globalEvents = [ }, { name: 'globalEventB', + label: '全局事件B', description: '全局事件动作A', mapping: [ { diff --git a/packages/amis-editor/src/renderer/event-control/helper.tsx b/packages/amis-editor/src/renderer/event-control/helper.tsx index 19e4e779b..57df3e9af 100644 --- a/packages/amis-editor/src/renderer/event-control/helper.tsx +++ b/packages/amis-editor/src/renderer/event-control/helper.tsx @@ -3218,7 +3218,9 @@ export const getEventControlConfig = ( } } - delete config.data; + if (config.actionType !== 'broadcast') { + delete config.data; + } // 处理下 addItem 的初始化 if (action.actionType === 'addItem') { diff --git a/packages/amis-editor/src/renderer/event-control/index.tsx b/packages/amis-editor/src/renderer/event-control/index.tsx index 1ad6bdb3f..3022d4e45 100644 --- a/packages/amis-editor/src/renderer/event-control/index.tsx +++ b/packages/amis-editor/src/renderer/event-control/index.tsx @@ -152,7 +152,7 @@ export class EventControl extends React.Component< constructor(props: EventControlProps) { super(props); - const {events, value, data, rawType} = props; + const {events, value, data, rawType, globalEvents} = props; const eventPanelActive: { [prop: string]: boolean; @@ -169,6 +169,10 @@ export class EventControl extends React.Component< eventPanelActive[event.eventName] = true; }); + globalEvents?.forEach(event => { + eventPanelActive[event.name] = true; + }); + this.state = { onEvent: value ?? this.generateEmptyDefault(pluginEvents), events: pluginEvents, @@ -619,14 +623,16 @@ export class EventControl extends React.Component< actionTree, actions: pluginActions, commonActions, - allComponents + allComponents, + globalEvents } = this.props; const {events, onEvent} = this.state; - const eventConfig = events.find( item => item.eventName === data.actionData!.eventKey ); - + const globalEventConfig = globalEvents?.find( + item => item.name === data.actionData!.eventKey + ); // 收集当前事件动作出参 let actions = onEvent[data.actionData!.eventKey].actions; @@ -642,11 +648,33 @@ export class EventControl extends React.Component< let jsonSchema: any = {}; - // 动态构建事件参数 - if (typeof eventConfig?.dataSchema === 'function') { - jsonSchema = eventConfig.dataSchema(manager)?.[0]; + if (globalEventConfig) { + jsonSchema = { + 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 { - jsonSchema = {...(eventConfig?.dataSchema?.[0] ?? {})}; + // 动态构建事件参数 + if (typeof eventConfig?.dataSchema === 'function') { + jsonSchema = eventConfig.dataSchema(manager)?.[0]; + } else { + jsonSchema = {...(eventConfig?.dataSchema?.[0] ?? {})}; + } } actions @@ -1174,7 +1202,7 @@ export class EventControl extends React.Component< tooltipPlacement: 'left', disabled: Object.keys(onEvent).includes(item.name), actionType: '', - label: item.name, + label: item.label, onClick: this.addGlobalEvent.bind( this, item,