fix: sharedContext

This commit is contained in:
chenos 2025-06-19 12:14:18 +08:00
parent e96dc892b5
commit f8b6edf282
11 changed files with 55 additions and 32 deletions

View File

@ -30,7 +30,10 @@ AddNewActionModel.registerFlow({
function DrawerContent() {
return (
<div>
<FlowPageComponent parentId={ctx.model.uid} sharedContext={{ ...ctx.extra, currentDrawer }} />
<FlowPageComponent
parentId={ctx.model.uid}
sharedContext={{ parentBlockModel: ctx.shared.currentBlockModel, currentDrawer }}
/>
</div>
);
}

View File

@ -23,11 +23,11 @@ BulkDeleteActionModel.registerFlow({
steps: {
step1: {
async handler(ctx, params) {
if (!ctx.extra.currentResource) {
if (!ctx.shared?.currentBlockModel?.resource) {
ctx.globals.message.error('No resource selected for deletion.');
return;
}
const resource = ctx.extra.currentResource as MultiRecordResource;
const resource = ctx.shared.currentBlockModel.resource as MultiRecordResource;
if (resource.getSelectedRows().length === 0) {
ctx.globals.message.warning('No records selected for deletion.');
return;

View File

@ -7,6 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { MultiRecordResource } from '@nocobase/flow-engine';
import type { ButtonType } from 'antd/es/button';
import React from 'react';
import { ActionModel } from './ActionModel';
@ -54,11 +55,16 @@ DeleteActionModel.registerFlow({
},
step1: {
async handler(ctx, params) {
if (!ctx.extra.currentResource || !ctx.extra.currentRecord) {
if (!ctx.shared?.currentBlockModel?.resource) {
ctx.globals.message.error('No resource selected for deletion.');
return;
}
if (!ctx.shared.currentRecord) {
ctx.globals.message.error('No resource or record selected for deletion.');
return;
}
await ctx.extra.currentResource.destroy(ctx.extra.currentRecord);
const resource = ctx.shared.currentBlockModel.resource as MultiRecordResource;
await resource.destroy(ctx.shared.currentRecord);
ctx.globals.message.success('Record deleted successfully.');
},
},

View File

@ -33,7 +33,7 @@ export class FormModel extends BlockFlowModel {
<FormProvider form={this.form}>
<FormLayout layout={'vertical'}>
{this.mapSubModels('fields', (field) => (
<FlowModelRenderer model={field} showFlowSettings />
<FlowModelRenderer model={field} showFlowSettings sharedContext={{ currentBlockModel: this }} />
))}
</FormLayout>
<AddFieldButton
@ -65,7 +65,7 @@ export class FormModel extends BlockFlowModel {
/>
<FormButtonGroup>
{this.mapSubModels('actions', (action) => (
<FlowModelRenderer model={action} showFlowSettings extraContext={{ currentModel: this }} />
<FlowModelRenderer model={action} showFlowSettings sharedContext={{ currentBlockModel: this }} />
))}
<AddActionButton model={this} subModelBaseClass="ActionModel" />
</FormButtonGroup>
@ -118,9 +118,8 @@ FormModel.registerFlow({
resource.setAPIClient(ctx.globals.api);
ctx.model.resource = resource;
}
console.log('FormModel flow context', ctx.shared, ctx.model.getSharedContext());
if (ctx.shared.currentRecord) {
ctx.model.resource.setFilterByTk(ctx.shared.currentRecord.id);
if (ctx.shared.parentRecord) {
ctx.model.resource.setFilterByTk(ctx.shared.parentRecord.id);
await ctx.model.resource.refresh();
ctx.model.form.setInitialValues(ctx.model.resource.getData());
}

View File

@ -25,7 +25,7 @@ LinkActionModel.registerFlow({
step1: {
handler(ctx, params) {
ctx.globals.modal.confirm({
title: `${ctx.extra.currentRecord?.id}`,
title: `${ctx.shared.currentRecord?.id}`,
content: 'Are you sure you want to perform this action?',
});
},

View File

@ -25,17 +25,21 @@ SubmitActionModel.registerFlow({
steps: {
step1: {
async handler(ctx, params) {
if (ctx.extra.currentModel) {
await ctx.extra.currentModel.form.submit();
const values = ctx.extra.currentModel.form.values;
await ctx.extra.currentModel.resource.save(values);
if (!ctx.shared?.currentBlockModel?.resource) {
ctx.globals.message.error('No resource selected for submission.');
return;
}
const currentBlockModel = ctx.shared.currentBlockModel;
const currentResource = ctx.shared.currentBlockModel.resource;
await currentBlockModel.form.submit();
const values = currentBlockModel.form.values;
await currentBlockModel.resource.save(values);
console.log('Form submitted successfully:', ctx.shared.parentBlockModel);
// currentResource.refresh();
ctx.shared.parentBlockModel?.resource?.refresh();
if (ctx.shared.currentDrawer) {
ctx.shared.currentDrawer.destroy();
}
if (ctx.shared.currentResource) {
ctx.shared.currentResource.refresh();
}
},
},
},

View File

@ -100,12 +100,7 @@ const Columns = observer<any>(({ record, model, index }) => {
{model.mapSubModels('actions', (action: ActionModel) => {
const fork = action.createFork({}, `${index}`);
return (
<FlowModelRenderer
showFlowSettings
key={fork.uid}
model={fork}
extraContext={{ currentResource: model.parent.resource, currentRecord: record }}
/>
<FlowModelRenderer showFlowSettings key={fork.uid} model={fork} sharedContext={{ currentRecord: record }} />
);
})}
</Space>

View File

@ -77,11 +77,7 @@ export class TableModel extends BlockFlowModel<S> {
<Card>
<Space style={{ marginBottom: 16 }}>
{this.mapSubModels('actions', (action) => (
<FlowModelRenderer
model={action}
showFlowSettings
extraContext={{ currentModel: this, currentResource: this.resource }}
/>
<FlowModelRenderer model={action} showFlowSettings sharedContext={{ currentBlockModel: this }} />
))}
<AddActionModel
model={this}
@ -177,7 +173,7 @@ TableModel.registerFlow({
resource.setAPIClient(ctx.globals.api);
ctx.model.resource = resource;
await resource.refresh();
await ctx.model.applySubModelsAutoFlows('columns');
await ctx.model.applySubModelsAutoFlows('columns', null, { currentBlockModel: ctx.model });
},
},
},

View File

@ -31,7 +31,14 @@ ViewActionModel.registerFlow({
function DrawerContent() {
return (
<div>
<FlowPageComponent parentId={ctx.model.uid} sharedContext={{ ...ctx.extra, currentDrawer }} />
<FlowPageComponent
parentId={ctx.model.uid}
sharedContext={{
currentDrawer,
parentRecord: ctx.shared.currentRecord,
parentBlockModel: ctx.shared.currentBlockModel,
}}
/>
</div>
);
}

View File

@ -95,6 +95,10 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
onInit(options): void {}
get async() {
return this._options.async || false;
}
static get meta() {
return modelMetas.get(this);
}
@ -625,9 +629,14 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
return this.flowEngine.createModel(options);
}
async applySubModelsAutoFlows<K extends keyof Structure['subModels'], R>(subKey: K, extra?: Record<string, any>) {
async applySubModelsAutoFlows<K extends keyof Structure['subModels'], R>(
subKey: K,
extra?: Record<string, any>,
shared?: Record<string, any>,
) {
await Promise.all(
this.mapSubModels(subKey, async (column) => {
column.setSharedContext(shared);
await column.applyAutoFlows(extra);
}),
);
@ -731,6 +740,9 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
}
public getSharedContext() {
if (this.async || !this.parent) {
return this._sharedContext;
}
return {
...this.parent?.getSharedContext(),
...this._sharedContext, // 当前实例的 context 优先级最高

View File

@ -285,6 +285,7 @@ export interface DefaultStructure {
*/
export interface FlowModelOptions<Structure extends { parent?: any; subModels?: any } = DefaultStructure> {
uid: string;
async?: boolean; // 是否异步加载模型
props?: IModelComponentProps;
stepParams?: Record<string, any>;
subModels?: Structure['subModels'];