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

View File

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

View File

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