mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-19 21:06:06 +08:00
feat(core): 新增事件处理前后的钩子函数配置
This commit is contained in:
parent
dc37d4e8bd
commit
b78d2fda1f
@ -20,14 +20,8 @@ import { EventEmitter } from 'events';
|
|||||||
|
|
||||||
import { isEmpty } from 'lodash-es';
|
import { isEmpty } from 'lodash-es';
|
||||||
|
|
||||||
import {
|
import { createDataSourceManager, DataSourceManager, type DataSourceManagerData } from '@tmagic/data-source';
|
||||||
createDataSourceManager,
|
import type { CodeBlockDSL, Id, JsEngine, MApp, RequestFunction } from '@tmagic/schema';
|
||||||
type DataSource,
|
|
||||||
DataSourceManager,
|
|
||||||
type DataSourceManagerData,
|
|
||||||
ObservedDataClass,
|
|
||||||
} from '@tmagic/data-source';
|
|
||||||
import type { CodeBlockDSL, DataSourceSchema, Id, JsEngine, MApp, RequestFunction } from '@tmagic/schema';
|
|
||||||
|
|
||||||
import Env from './Env';
|
import Env from './Env';
|
||||||
import EventHelper from './EventHelper';
|
import EventHelper from './EventHelper';
|
||||||
@ -35,34 +29,9 @@ import Flexible from './Flexible';
|
|||||||
import FlowState from './FlowState';
|
import FlowState from './FlowState';
|
||||||
import Node from './Node';
|
import Node from './Node';
|
||||||
import Page from './Page';
|
import Page from './Page';
|
||||||
|
import { AppOptionsConfig, ErrorHandler } from './type';
|
||||||
import { transformStyle as defaultTransformStyle } from './utils';
|
import { transformStyle as defaultTransformStyle } from './utils';
|
||||||
|
|
||||||
export type ErrorHandler = (
|
|
||||||
err: Error,
|
|
||||||
node: DataSource<DataSourceSchema> | Node | undefined,
|
|
||||||
info: Record<string, any>,
|
|
||||||
) => void;
|
|
||||||
|
|
||||||
export interface AppOptionsConfig {
|
|
||||||
ua?: string;
|
|
||||||
env?: Env;
|
|
||||||
config?: MApp;
|
|
||||||
platform?: 'editor' | 'mobile' | 'tv' | 'pc';
|
|
||||||
jsEngine?: JsEngine;
|
|
||||||
designWidth?: number;
|
|
||||||
curPage?: Id;
|
|
||||||
useMock?: boolean;
|
|
||||||
disabledFlexible?: boolean;
|
|
||||||
pageFragmentContainerType?: string | string[];
|
|
||||||
iteratorContainerType?: string | string[];
|
|
||||||
transformStyle?: (style: Record<string, any>) => Record<string, any>;
|
|
||||||
request?: RequestFunction;
|
|
||||||
DataSourceObservedData?: ObservedDataClass;
|
|
||||||
dataSourceManagerInitialData?: DataSourceManagerData;
|
|
||||||
nodeStoreInitialData?: () => any;
|
|
||||||
errorHandler?: ErrorHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
class App extends EventEmitter {
|
class App extends EventEmitter {
|
||||||
[x: string]: any;
|
[x: string]: any;
|
||||||
static nodeClassMap = new Map<string, typeof Node>();
|
static nodeClassMap = new Map<string, typeof Node>();
|
||||||
@ -134,7 +103,11 @@ class App extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.platform !== 'editor') {
|
if (this.platform !== 'editor') {
|
||||||
this.eventHelper = new EventHelper({ app: this });
|
this.eventHelper = new EventHelper({
|
||||||
|
app: this,
|
||||||
|
beforeEventHandler: options.beforeEventHandler,
|
||||||
|
afterEventHandler: options.afterEventHandler,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.transformStyle =
|
this.transformStyle =
|
||||||
|
@ -37,16 +37,29 @@ import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX } from '@tmagic/utils';
|
|||||||
import type { default as TMagicApp } from './App';
|
import type { default as TMagicApp } from './App';
|
||||||
import FlowState from './FlowState';
|
import FlowState from './FlowState';
|
||||||
import type { default as TMagicNode } from './Node';
|
import type { default as TMagicNode } from './Node';
|
||||||
|
import { AfterEventHandler, BeforeEventHandler } from './type';
|
||||||
|
|
||||||
export default class EventHelper extends EventEmitter {
|
export default class EventHelper extends EventEmitter {
|
||||||
public app: TMagicApp;
|
public app: TMagicApp;
|
||||||
|
|
||||||
private nodeEventList = new Map<(fromCpt: TMagicNode, ...args: any[]) => void, symbol>();
|
private nodeEventList = new Map<(fromCpt: TMagicNode, ...args: any[]) => void, symbol>();
|
||||||
private dataSourceEventList = new Map<string, Map<string, (...args: any[]) => void>>();
|
private dataSourceEventList = new Map<string, Map<string, (...args: any[]) => void>>();
|
||||||
|
private beforeEventHandler?: BeforeEventHandler;
|
||||||
|
private afterEventHandler?: AfterEventHandler;
|
||||||
|
|
||||||
constructor({ app }: { app: TMagicApp }) {
|
constructor({
|
||||||
|
app,
|
||||||
|
beforeEventHandler,
|
||||||
|
afterEventHandler,
|
||||||
|
}: {
|
||||||
|
app: TMagicApp;
|
||||||
|
beforeEventHandler?: BeforeEventHandler;
|
||||||
|
afterEventHandler?: AfterEventHandler;
|
||||||
|
}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.beforeEventHandler = beforeEventHandler;
|
||||||
|
this.afterEventHandler = afterEventHandler;
|
||||||
this.app = app;
|
this.app = app;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +72,10 @@ export default class EventHelper extends EventEmitter {
|
|||||||
|
|
||||||
public bindNodeEvents(node: TMagicNode) {
|
public bindNodeEvents(node: TMagicNode) {
|
||||||
node.events?.forEach((event, index) => {
|
node.events?.forEach((event, index) => {
|
||||||
|
if (!event.name) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let eventNameKey = `${event.name}_${node.data.id}`;
|
let eventNameKey = `${event.name}_${node.data.id}`;
|
||||||
|
|
||||||
// 页面片容器可以配置页面片内组件的事件,形式为“${nodeId}.${eventName}”
|
// 页面片容器可以配置页面片内组件的事件,形式为“${nodeId}.${eventName}”
|
||||||
@ -150,6 +167,10 @@ export default class EventHelper extends EventEmitter {
|
|||||||
private async eventHandler(config: EventConfig | number, fromCpt: TMagicNode | DataSource | undefined, args: any[]) {
|
private async eventHandler(config: EventConfig | number, fromCpt: TMagicNode | DataSource | undefined, args: any[]) {
|
||||||
const eventConfig = typeof config === 'number' ? (fromCpt as TMagicNode).events[config] : config;
|
const eventConfig = typeof config === 'number' ? (fromCpt as TMagicNode).events[config] : config;
|
||||||
|
|
||||||
|
if (typeof this.beforeEventHandler === 'function') {
|
||||||
|
this.beforeEventHandler({ eventConfig, source: fromCpt, args });
|
||||||
|
}
|
||||||
|
|
||||||
if (has(eventConfig, 'actions')) {
|
if (has(eventConfig, 'actions')) {
|
||||||
// EventConfig类型
|
// EventConfig类型
|
||||||
const flowState = new FlowState();
|
const flowState = new FlowState();
|
||||||
@ -177,6 +198,10 @@ export default class EventHelper extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof this.afterEventHandler === 'function') {
|
||||||
|
this.afterEventHandler({ eventConfig, source: fromCpt, args });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async actionHandler(
|
private async actionHandler(
|
||||||
|
@ -28,7 +28,7 @@ export * from '@tmagic/utils';
|
|||||||
export { default as EventHelper } from './EventHelper';
|
export { default as EventHelper } from './EventHelper';
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
|
|
||||||
export { type AppOptionsConfig } from './App';
|
export * from './type';
|
||||||
export { default as Env } from './Env';
|
export { default as Env } from './Env';
|
||||||
export { default as Page } from './Page';
|
export { default as Page } from './Page';
|
||||||
export { default as Node } from './Node';
|
export { default as Node } from './Node';
|
||||||
|
45
packages/core/src/type.ts
Normal file
45
packages/core/src/type.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import type { DataSource, DataSourceManagerData, ObservedDataClass } from '@tmagic/data-source';
|
||||||
|
import type { DataSourceSchema, EventConfig, Id, JsEngine, MApp, RequestFunction } from '@tmagic/schema';
|
||||||
|
|
||||||
|
import type Env from './Env';
|
||||||
|
import type TMagicNode from './Node';
|
||||||
|
|
||||||
|
export type ErrorHandler = (
|
||||||
|
err: Error,
|
||||||
|
node: DataSource<DataSourceSchema> | TMagicNode | undefined,
|
||||||
|
info: Record<string, any>,
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
export interface AppOptionsConfig {
|
||||||
|
ua?: string;
|
||||||
|
env?: Env;
|
||||||
|
config?: MApp;
|
||||||
|
platform?: 'editor' | 'mobile' | 'tv' | 'pc';
|
||||||
|
jsEngine?: JsEngine;
|
||||||
|
designWidth?: number;
|
||||||
|
curPage?: Id;
|
||||||
|
useMock?: boolean;
|
||||||
|
disabledFlexible?: boolean;
|
||||||
|
pageFragmentContainerType?: string | string[];
|
||||||
|
iteratorContainerType?: string | string[];
|
||||||
|
transformStyle?: (style: Record<string, any>) => Record<string, any>;
|
||||||
|
request?: RequestFunction;
|
||||||
|
DataSourceObservedData?: ObservedDataClass;
|
||||||
|
dataSourceManagerInitialData?: DataSourceManagerData;
|
||||||
|
nodeStoreInitialData?: () => any;
|
||||||
|
errorHandler?: ErrorHandler;
|
||||||
|
beforeEventHandler?: BeforeEventHandler;
|
||||||
|
afterEventHandler?: AfterEventHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type BeforeEventHandler = (args: {
|
||||||
|
eventConfig: EventConfig;
|
||||||
|
source: TMagicNode | DataSource | undefined;
|
||||||
|
args: any[];
|
||||||
|
}) => void;
|
||||||
|
|
||||||
|
export type AfterEventHandler = (args: {
|
||||||
|
eventConfig: EventConfig;
|
||||||
|
source: TMagicNode | DataSource | undefined;
|
||||||
|
args: any[];
|
||||||
|
}) => void;
|
Loading…
x
Reference in New Issue
Block a user