fix: flowEngine

This commit is contained in:
chenos 2025-06-30 13:47:03 +08:00
parent 4aee822d94
commit 4d79e4cd02
4 changed files with 34 additions and 40 deletions

View File

@ -34,14 +34,15 @@ export class QuickEditForm extends DataBlockModel {
declare collection: Collection;
static async open(options: {
flowEngine: FlowEngine;
target: any;
dataSourceKey: string;
collectionName: string;
fieldPath: string;
filterByTk: string;
}) {
const { target, dataSourceKey, collectionName, fieldPath, filterByTk } = options;
const model = this.flowEngine.createModel({
const { flowEngine, target, dataSourceKey, collectionName, fieldPath, filterByTk } = options;
const model = flowEngine.createModel({
use: 'QuickEditForm',
stepParams: {
propsFlow: {
@ -54,7 +55,7 @@ export class QuickEditForm extends DataBlockModel {
},
}) as QuickEditForm;
await this.flowEngine.context.popover.open({
await flowEngine.context.popover.open({
target,
placement: 'rightTop',
content: (popover) => {
@ -87,33 +88,31 @@ export class QuickEditForm extends DataBlockModel {
this.ctx.shared.currentView.close();
}}
>
<FlowEngineProvider engine={this.flowEngine}>
<FormProvider form={this.form}>
<FormLayout layout={'vertical'}>
{this.mapSubModels('fields', (field) => {
return (
<FlowModelRenderer
model={field}
sharedContext={{ currentRecord: this.resource.getData() }}
fallback={<Skeleton paragraph={{ rows: 2 }} />}
/>
);
})}
</FormLayout>
<FormButtonGroup align="right">
<Button
onClick={() => {
this.ctx.shared.currentView.close();
}}
>
{this.translate('Cancel')}
</Button>
<Button type="primary" htmlType="submit">
{this.translate('Submit')}
</Button>
</FormButtonGroup>
</FormProvider>
</FlowEngineProvider>
<FormProvider form={this.form}>
<FormLayout layout={'vertical'}>
{this.mapSubModels('fields', (field) => {
return (
<FlowModelRenderer
model={field}
sharedContext={{ currentRecord: this.resource.getData() }}
fallback={<Skeleton paragraph={{ rows: 2 }} />}
/>
);
})}
</FormLayout>
<FormButtonGroup align="right">
<Button
onClick={() => {
this.ctx.shared.currentView.close();
}}
>
{this.translate('Cancel')}
</Button>
<Button type="primary" htmlType="submit">
{this.translate('Submit')}
</Button>
</FormButtonGroup>
</FormProvider>
</form>
);
}

View File

@ -163,6 +163,7 @@ export class TableModel extends DataBlockModel<TableModelStructure> {
// 阻止事件冒泡,避免触发行选中
try {
await QuickEditForm.open({
flowEngine: this.flowEngine,
target: ref.current,
dataSourceKey: this.collection.dataSourceKey,
collectionName: this.collection.name,

View File

@ -317,7 +317,7 @@ export class FlowEngine {
if (uid && this.modelInstances.has(uid)) {
return this.modelInstances.get(uid) as T;
}
const modelInstance = new (ModelClass as ModelConstructor<T>)({ ...options } as any);
const modelInstance = new (ModelClass as ModelConstructor<T>)({ ...options, flowEngine: this } as any);
modelInstance.onInit(options);

View File

@ -39,13 +39,11 @@ const modelMetas = new WeakMap<typeof FlowModel, FlowModelMeta>();
const modelFlows = new WeakMap<typeof FlowModel, Map<string, FlowDefinition>>();
export class FlowModel<Structure extends DefaultStructure = DefaultStructure> {
public static flowEngine: FlowEngine;
public readonly uid: string;
public sortIndex: number;
public props: IModelComponentProps = {};
public stepParams: StepParams = {};
// public flowEngine: FlowEngine;
public flowEngine: FlowEngine;
public parent: ParentFlowModel<Structure>;
public subModels: Structure['subModels'];
private _options: FlowModelOptions<Structure>;
@ -74,9 +72,10 @@ export class FlowModel<Structure extends DefaultStructure = DefaultStructure> {
private observerDispose: () => void;
constructor(options: FlowModelOptions<Structure>) {
if (!this.flowEngine) {
if (!options.flowEngine) {
throw new Error('FlowModel must be initialized with a FlowEngine instance.');
}
this.flowEngine = options.flowEngine;
if (this.flowEngine.getModel(options.uid)) {
// 此时 new FlowModel 并不创建新实例而是返回已存在的实例避免重复创建同一个model实例
return this.flowEngine.getModel(options.uid);
@ -136,11 +135,6 @@ export class FlowModel<Structure extends DefaultStructure = DefaultStructure> {
return modelMetas.get(this);
}
get flowEngine() {
// 取静态属性 flowEngine
return (this.constructor as typeof FlowModel).flowEngine;
}
private createSubModels(subModels: Record<string, CreateSubModelOptions | CreateSubModelOptions[]>) {
Object.entries(subModels || {}).forEach(([key, value]) => {
if (Array.isArray(value)) {