@nocobase/client-v2 2.2.50 → 2.2.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/flow/FlowModelRepository.d.ts +4 -1
- package/es/flow/FlowPage.d.ts +1 -1
- package/es/flow/useRouteContentRequest.d.ts +19 -0
- package/es/index.mjs +117 -85
- package/lib/index.js +102 -70
- package/package.json +7 -7
- package/src/flow/FlowModelRepository.ts +47 -1
- package/src/flow/FlowPage.tsx +19 -10
- package/src/flow/__tests__/FlowModelRepository.test.ts +38 -1
- package/src/flow/__tests__/FlowPage.test.tsx +78 -46
- package/src/flow/models/base/PageModel/PageTabModel.tsx +27 -12
- package/src/flow/models/blocks/details/DetailsItemModel.tsx +62 -1
- package/src/flow/models/blocks/details/__tests__/DetailsItemModel.spacing.test.tsx +77 -0
- package/src/flow/useRouteContentRequest.tsx +42 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.52",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"@formily/antd-v5": "1.2.3",
|
|
28
28
|
"@formily/react": "^2.2.27",
|
|
29
29
|
"@formily/shared": "^2.2.27",
|
|
30
|
-
"@nocobase/evaluators": "2.2.
|
|
31
|
-
"@nocobase/flow-engine": "2.2.
|
|
32
|
-
"@nocobase/sdk": "2.2.
|
|
33
|
-
"@nocobase/shared": "2.2.
|
|
34
|
-
"@nocobase/utils": "2.2.
|
|
30
|
+
"@nocobase/evaluators": "2.2.52",
|
|
31
|
+
"@nocobase/flow-engine": "2.2.52",
|
|
32
|
+
"@nocobase/sdk": "2.2.52",
|
|
33
|
+
"@nocobase/shared": "2.2.52",
|
|
34
|
+
"@nocobase/utils": "2.2.52",
|
|
35
35
|
"ahooks": "^3.7.2",
|
|
36
36
|
"antd": "5.24.2",
|
|
37
37
|
"antd-style": "3.7.1",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"react-router-dom": "^6.30.1",
|
|
50
50
|
"zxing-wasm": "^3.1.3"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "27ab36740f5cb8235d3b35f2b41246c4771532b0"
|
|
53
53
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { FlowModel, IFlowModelRepository } from '@nocobase/flow-engine';
|
|
10
|
+
import { type CreateModelOptions, type FlowEngine, FlowModel, IFlowModelRepository } from '@nocobase/flow-engine';
|
|
11
11
|
import type { BaseApplication } from '../BaseApplication';
|
|
12
12
|
import _ from 'lodash';
|
|
13
13
|
|
|
@@ -180,6 +180,21 @@ export class FlowModelRepository implements IFlowModelRepository<FlowModel> {
|
|
|
180
180
|
return data ? _.cloneDeep(data) : data;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
async ensureRouteContent(options: CreateModelOptions) {
|
|
184
|
+
const existing = await this.findOne({ parentId: options.parentId, subKey: options.subKey });
|
|
185
|
+
if (existing) return existing;
|
|
186
|
+
|
|
187
|
+
const response = await this.app.apiClient.request({
|
|
188
|
+
method: 'POST',
|
|
189
|
+
url: 'flowModels:save',
|
|
190
|
+
params: { ensureRouteContent: true },
|
|
191
|
+
data: options,
|
|
192
|
+
});
|
|
193
|
+
const persisted = await this.findOne({ uid: response.data.data });
|
|
194
|
+
if (!persisted) throw new Error('Could not load the initialized route content');
|
|
195
|
+
return persisted;
|
|
196
|
+
}
|
|
197
|
+
|
|
183
198
|
async save(model: FlowModel, options?: { onlyStepParams?: boolean }) {
|
|
184
199
|
const data = model.serialize();
|
|
185
200
|
if (options?.onlyStepParams) {
|
|
@@ -220,3 +235,34 @@ export class FlowModelRepository implements IFlowModelRepository<FlowModel> {
|
|
|
220
235
|
return response.data?.data;
|
|
221
236
|
}
|
|
222
237
|
}
|
|
238
|
+
|
|
239
|
+
// A tab renderer can be mounted again when the UI Editor adds or removes its controls.
|
|
240
|
+
const confirmedRouteModels = new WeakSet<FlowModel>();
|
|
241
|
+
|
|
242
|
+
export function isRouteContentConfirmed(model: FlowModel) {
|
|
243
|
+
return confirmedRouteModels.has(model);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export async function loadRouteContentForEditing(engine: FlowEngine, options: CreateModelOptions) {
|
|
247
|
+
const repository = engine.modelRepository;
|
|
248
|
+
if (!(repository instanceof FlowModelRepository)) {
|
|
249
|
+
return engine.loadOrCreateModel(options);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// A route model can live in the outer engine while its page lives in this view's engine.
|
|
253
|
+
let cached = engine.findModelByParentId(options.parentId, options.subKey);
|
|
254
|
+
if (!cached) {
|
|
255
|
+
engine.forEachModel((model) => {
|
|
256
|
+
if (model.parentId === options.parentId && model.subKey === options.subKey) cached = model;
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (cached && isRouteContentConfirmed(cached)) return cached;
|
|
260
|
+
const data = await repository.ensureRouteContent({ ...options, uid: cached?.uid });
|
|
261
|
+
if (cached && cached.uid !== data.uid) engine.removeModelWithSubModels(cached.uid);
|
|
262
|
+
const model = await engine.createModelAsync(data);
|
|
263
|
+
if (model.parent && model.parent.subModels[options.subKey] !== model) {
|
|
264
|
+
model.parent.setSubModel(options.subKey, model);
|
|
265
|
+
}
|
|
266
|
+
confirmedRouteModels.add(model);
|
|
267
|
+
return model;
|
|
268
|
+
}
|
package/src/flow/FlowPage.tsx
CHANGED
|
@@ -15,6 +15,10 @@ import {
|
|
|
15
15
|
useFlowViewContext,
|
|
16
16
|
} from '@nocobase/flow-engine';
|
|
17
17
|
import type { FlowEngineContext, FlowModel, FlowModelRendererProps, ModelConstructor } from '@nocobase/flow-engine';
|
|
18
|
+
import { observer } from '@formily/reactive-react';
|
|
19
|
+
import { loadRouteContentForEditing } from './FlowModelRepository';
|
|
20
|
+
import { RouteContent, useRouteContentRequest } from './useRouteContentRequest';
|
|
21
|
+
import { RouteModel } from './models/base/RouteModel';
|
|
18
22
|
import { useRequest } from 'ahooks';
|
|
19
23
|
import React from 'react';
|
|
20
24
|
import FlowRoute from './components/FlowRoute';
|
|
@@ -85,12 +89,13 @@ const bindViewLayoutState = (model: FlowModel, ctx?: FlowPageViewContext | null)
|
|
|
85
89
|
});
|
|
86
90
|
};
|
|
87
91
|
|
|
88
|
-
export const FlowPage =
|
|
92
|
+
export const FlowPage = observer((props: FlowPageProps & Record<string, unknown>) => {
|
|
89
93
|
const { pageModelClass = 'ChildPageModel', parentId, onModelLoaded, defaultTabTitle, ...rest } = props;
|
|
90
94
|
const flowEngine = useFlowEngine();
|
|
95
|
+
const isRoute = !!parentId && flowEngine.getModel(parentId, true) instanceof RouteModel;
|
|
91
96
|
const ctx = useFlowViewContext<FlowPageViewContext>();
|
|
92
|
-
const {
|
|
93
|
-
async () => {
|
|
97
|
+
const { model, loading, error, needsConfirmation } = useRouteContentRequest(
|
|
98
|
+
async (forEditing) => {
|
|
94
99
|
const ModelClass = await flowEngine.getModelClassAsync(pageModelClass);
|
|
95
100
|
if (!ModelClass) {
|
|
96
101
|
throw new Error(`[NocoBase] Page model class '${pageModelClass}' is not registered.`);
|
|
@@ -101,7 +106,7 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
|
|
|
101
106
|
async: true,
|
|
102
107
|
parentId,
|
|
103
108
|
subKey: 'page',
|
|
104
|
-
subType: 'object',
|
|
109
|
+
subType: 'object' as const,
|
|
105
110
|
use: pageModelClass,
|
|
106
111
|
};
|
|
107
112
|
const isRuntimeRecordScopedPage =
|
|
@@ -136,7 +141,11 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
|
|
|
136
141
|
}
|
|
137
142
|
const data =
|
|
138
143
|
(isRuntimeRecordScopedPage && (await flowEngine.loadModel({ ...options, refresh: true }))) ||
|
|
139
|
-
(
|
|
144
|
+
(forEditing
|
|
145
|
+
? await loadRouteContentForEditing(flowEngine, options)
|
|
146
|
+
: await flowEngine.loadOrCreateModel(options, {
|
|
147
|
+
skipSave: isRoute || !flowEngine.context.flowSettingsEnabled,
|
|
148
|
+
}));
|
|
140
149
|
if (data?.uid && onModelLoaded) {
|
|
141
150
|
data.context.addDelegate(ctx);
|
|
142
151
|
bindViewLayoutState(data, ctx);
|
|
@@ -145,17 +154,17 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
|
|
|
145
154
|
}
|
|
146
155
|
return data;
|
|
147
156
|
},
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
},
|
|
157
|
+
isRoute && flowEngine.context.flowSettingsEnabled,
|
|
158
|
+
[parentId, pageModelClass, defaultTabTitle],
|
|
151
159
|
);
|
|
152
160
|
if (error) {
|
|
153
161
|
throw error;
|
|
154
162
|
}
|
|
155
|
-
if (
|
|
163
|
+
if (!model?.uid || (loading && !isRoute)) {
|
|
156
164
|
return <SkeletonFallback style={{ margin: ctx?.isMobileLayout ? 8 : ctx?.themeToken.marginBlock }} />;
|
|
157
165
|
}
|
|
158
|
-
|
|
166
|
+
const page = <InternalFlowPage uid={model.uid} {...rest} />;
|
|
167
|
+
return isRoute ? <RouteContent pending={needsConfirmation}>{page}</RouteContent> : page;
|
|
159
168
|
});
|
|
160
169
|
|
|
161
170
|
FlowPage.displayName = 'FlowPage';
|
|
@@ -8,9 +8,46 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { describe, it, expect, vi } from 'vitest';
|
|
11
|
-
import {
|
|
11
|
+
import { FlowEngine, FlowModel } from '@nocobase/flow-engine';
|
|
12
|
+
import type { BaseApplication } from '../../BaseApplication';
|
|
13
|
+
import { FlowModelRepository, loadRouteContentForEditing } from '../FlowModelRepository';
|
|
12
14
|
|
|
13
15
|
describe('FlowModelRepository', () => {
|
|
16
|
+
it('keeps the browse UID when the route parent is outside the current engine', async () => {
|
|
17
|
+
const engine = new FlowEngine();
|
|
18
|
+
engine.registerModels({ FlowModel });
|
|
19
|
+
const request = vi
|
|
20
|
+
.fn()
|
|
21
|
+
.mockResolvedValueOnce({ data: {} })
|
|
22
|
+
.mockResolvedValueOnce({ data: { data: 'browse-uid' } })
|
|
23
|
+
.mockResolvedValueOnce({
|
|
24
|
+
data: { data: { uid: 'browse-uid', use: 'FlowModel', parentId: 'route', subKey: 'page' } },
|
|
25
|
+
});
|
|
26
|
+
engine.setModelRepository(new FlowModelRepository({ apiClient: { request } } as unknown as BaseApplication));
|
|
27
|
+
const options = { use: 'FlowModel', parentId: 'route', subKey: 'page', subType: 'object' as const };
|
|
28
|
+
const browsing = engine.createModel({ ...options, uid: 'browse-uid' });
|
|
29
|
+
expect(await loadRouteContentForEditing(engine, options)).toBe(browsing);
|
|
30
|
+
expect(await loadRouteContentForEditing(engine, options)).toBe(browsing);
|
|
31
|
+
expect(request).toHaveBeenCalledTimes(3);
|
|
32
|
+
expect(request.mock.calls[1][0].data).toMatchObject({ ...options, uid: browsing.uid });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('retries the first edit when the initialization request fails', async () => {
|
|
36
|
+
const engine = new FlowEngine();
|
|
37
|
+
engine.registerModels({ FlowModel });
|
|
38
|
+
const options = { use: 'FlowModel', parentId: 'route', subKey: 'page', subType: 'object' as const };
|
|
39
|
+
const model = engine.createModel({ ...options, uid: 'browse-uid' });
|
|
40
|
+
const request = vi
|
|
41
|
+
.fn()
|
|
42
|
+
.mockResolvedValueOnce({ data: {} })
|
|
43
|
+
.mockRejectedValueOnce(new Error('Save failed'))
|
|
44
|
+
.mockResolvedValueOnce({ data: { data: { ...options, uid: model.uid } } });
|
|
45
|
+
engine.setModelRepository(new FlowModelRepository({ apiClient: { request } } as unknown as BaseApplication));
|
|
46
|
+
await expect(loadRouteContentForEditing(engine, options)).rejects.toThrow('Save failed');
|
|
47
|
+
expect(await loadRouteContentForEditing(engine, options)).toBe(model);
|
|
48
|
+
expect(request).toHaveBeenCalledTimes(3);
|
|
49
|
+
});
|
|
50
|
+
|
|
14
51
|
it('dedupes concurrent findOne requests with same key', async () => {
|
|
15
52
|
const request = vi.fn(async () => {
|
|
16
53
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -7,10 +7,18 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { observable } from '@formily/reactive';
|
|
11
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react';
|
|
11
12
|
import React from 'react';
|
|
12
13
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
13
14
|
import { FlowPage } from '../FlowPage';
|
|
15
|
+
import { RouteModel } from '../models/base/RouteModel';
|
|
16
|
+
import { loadRouteContentForEditing } from '../FlowModelRepository';
|
|
17
|
+
|
|
18
|
+
vi.mock('../FlowModelRepository', () => ({
|
|
19
|
+
loadRouteContentForEditing: vi.fn(),
|
|
20
|
+
isRouteContentConfirmed: () => false,
|
|
21
|
+
}));
|
|
14
22
|
|
|
15
23
|
type TestContextProperty = {
|
|
16
24
|
value?: unknown;
|
|
@@ -71,7 +79,6 @@ const createPageModel = (): TestPageModel => {
|
|
|
71
79
|
};
|
|
72
80
|
|
|
73
81
|
const mocks = vi.hoisted(() => ({
|
|
74
|
-
flowSettingsEnabled: false,
|
|
75
82
|
loadModel: vi.fn(),
|
|
76
83
|
loadOrCreateModel: vi.fn(),
|
|
77
84
|
rendererMobileStates: [] as boolean[],
|
|
@@ -83,6 +90,8 @@ const mocks = vi.hoisted(() => ({
|
|
|
83
90
|
contextMobileLayout: undefined as boolean | undefined,
|
|
84
91
|
}));
|
|
85
92
|
|
|
93
|
+
const editor = observable({ enabled: false });
|
|
94
|
+
|
|
86
95
|
vi.mock('@nocobase/flow-engine', async () => {
|
|
87
96
|
const actual = await vi.importActual<typeof import('@nocobase/flow-engine')>('@nocobase/flow-engine');
|
|
88
97
|
const ReactModule = await import('react');
|
|
@@ -95,14 +104,17 @@ vi.mock('@nocobase/flow-engine', async () => {
|
|
|
95
104
|
if (typeof props.model?.context.isMobileLayout === 'boolean') {
|
|
96
105
|
mocks.rendererMobileStates.push(props.model.context.isMobileLayout);
|
|
97
106
|
}
|
|
98
|
-
return ReactModule.createElement('
|
|
107
|
+
return ReactModule.createElement('input', { 'data-testid': 'flow-model-renderer' });
|
|
99
108
|
}),
|
|
100
109
|
useFlowEngine: vi.fn(() => ({
|
|
110
|
+
getModel: () => Object.create(RouteModel.prototype),
|
|
101
111
|
getModelClassAsync: vi.fn(async () => TestPageModelClass),
|
|
102
112
|
loadModel: mocks.loadModel,
|
|
103
113
|
loadOrCreateModel: mocks.loadOrCreateModel,
|
|
104
114
|
context: {
|
|
105
|
-
flowSettingsEnabled
|
|
115
|
+
get flowSettingsEnabled() {
|
|
116
|
+
return editor.enabled;
|
|
117
|
+
},
|
|
106
118
|
},
|
|
107
119
|
translate: vi.fn((value: string) => value),
|
|
108
120
|
})),
|
|
@@ -123,46 +135,6 @@ vi.mock('@nocobase/flow-engine', async () => {
|
|
|
123
135
|
};
|
|
124
136
|
});
|
|
125
137
|
|
|
126
|
-
vi.mock('ahooks', async () => {
|
|
127
|
-
const actual = await vi.importActual<typeof import('ahooks')>('ahooks');
|
|
128
|
-
const ReactModule = await import('react');
|
|
129
|
-
return {
|
|
130
|
-
...actual,
|
|
131
|
-
useRequest: vi.fn((service: () => Promise<unknown>) => {
|
|
132
|
-
const serviceRef = ReactModule.useRef(service);
|
|
133
|
-
const [state, setState] = ReactModule.useState<{
|
|
134
|
-
loading: boolean;
|
|
135
|
-
data?: unknown;
|
|
136
|
-
error?: unknown;
|
|
137
|
-
}>({
|
|
138
|
-
loading: true,
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
ReactModule.useEffect(() => {
|
|
142
|
-
let active = true;
|
|
143
|
-
serviceRef
|
|
144
|
-
.current()
|
|
145
|
-
.then((data) => {
|
|
146
|
-
if (active) {
|
|
147
|
-
setState({ loading: false, data, error: null });
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
.catch((error) => {
|
|
151
|
-
if (active) {
|
|
152
|
-
setState({ loading: false, data: undefined, error });
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
return () => {
|
|
157
|
-
active = false;
|
|
158
|
-
};
|
|
159
|
-
}, []);
|
|
160
|
-
|
|
161
|
-
return state;
|
|
162
|
-
}),
|
|
163
|
-
};
|
|
164
|
-
});
|
|
165
|
-
|
|
166
138
|
describe('FlowPage', () => {
|
|
167
139
|
beforeEach(() => {
|
|
168
140
|
mocks.loadModel.mockImplementation(async () => null);
|
|
@@ -170,7 +142,10 @@ describe('FlowPage', () => {
|
|
|
170
142
|
});
|
|
171
143
|
|
|
172
144
|
afterEach(() => {
|
|
173
|
-
|
|
145
|
+
act(() => {
|
|
146
|
+
editor.enabled = false;
|
|
147
|
+
});
|
|
148
|
+
vi.mocked(loadRouteContentForEditing).mockReset();
|
|
174
149
|
mocks.loadModel.mockReset();
|
|
175
150
|
mocks.loadOrCreateModel.mockReset();
|
|
176
151
|
mocks.rendererMobileStates = [];
|
|
@@ -182,6 +157,63 @@ describe('FlowPage', () => {
|
|
|
182
157
|
mocks.contextMobileLayout = undefined;
|
|
183
158
|
});
|
|
184
159
|
|
|
160
|
+
it('keeps the mounted route page and confirms storage only on the first edit', async () => {
|
|
161
|
+
mocks.model = createPageModel();
|
|
162
|
+
const { queryByTestId } = render(<FlowPage parentId="route" />);
|
|
163
|
+
await waitFor(() => expect(queryByTestId('flow-model-renderer')).toBeTruthy());
|
|
164
|
+
expect(mocks.loadOrCreateModel).toHaveBeenCalledWith(expect.objectContaining({ parentId: 'route' }), {
|
|
165
|
+
skipSave: true,
|
|
166
|
+
});
|
|
167
|
+
expect(loadRouteContentForEditing).not.toHaveBeenCalled();
|
|
168
|
+
|
|
169
|
+
const input = queryByTestId('flow-model-renderer') as HTMLInputElement;
|
|
170
|
+
fireEvent.change(input, { target: { value: 'draft' } });
|
|
171
|
+
let finish: () => void;
|
|
172
|
+
vi.mocked(loadRouteContentForEditing).mockImplementationOnce(
|
|
173
|
+
() =>
|
|
174
|
+
new Promise((resolve) => {
|
|
175
|
+
finish = () => resolve(mocks.model as unknown as Awaited<ReturnType<typeof loadRouteContentForEditing>>);
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
act(() => {
|
|
179
|
+
editor.enabled = true;
|
|
180
|
+
});
|
|
181
|
+
await waitFor(() => expect(loadRouteContentForEditing).toHaveBeenCalledOnce());
|
|
182
|
+
expect(input.closest('[inert]')).toBeTruthy();
|
|
183
|
+
expect(queryByTestId('flow-model-renderer')).toBe(input);
|
|
184
|
+
await act(async () => finish());
|
|
185
|
+
await waitFor(() => expect(input.closest('[inert]')).toBeNull());
|
|
186
|
+
act(() => {
|
|
187
|
+
editor.enabled = false;
|
|
188
|
+
});
|
|
189
|
+
act(() => {
|
|
190
|
+
editor.enabled = true;
|
|
191
|
+
});
|
|
192
|
+
expect(queryByTestId('flow-model-renderer')).toBe(input);
|
|
193
|
+
expect(input.value).toBe('draft');
|
|
194
|
+
expect(loadRouteContentForEditing).toHaveBeenCalledOnce();
|
|
195
|
+
expect(mocks.loadOrCreateModel).toHaveBeenCalledOnce();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('opens directly in the editor and keeps the confirmed page on subsequent toggles', async () => {
|
|
199
|
+
editor.enabled = true;
|
|
200
|
+
mocks.model = createPageModel();
|
|
201
|
+
vi.mocked(loadRouteContentForEditing).mockResolvedValue(
|
|
202
|
+
mocks.model as unknown as Awaited<ReturnType<typeof loadRouteContentForEditing>>,
|
|
203
|
+
);
|
|
204
|
+
const { findByTestId, getByTestId } = render(<FlowPage parentId="route" />);
|
|
205
|
+
const input = await findByTestId('flow-model-renderer');
|
|
206
|
+
act(() => {
|
|
207
|
+
editor.enabled = false;
|
|
208
|
+
});
|
|
209
|
+
act(() => {
|
|
210
|
+
editor.enabled = true;
|
|
211
|
+
});
|
|
212
|
+
expect(getByTestId('flow-model-renderer')).toBe(input);
|
|
213
|
+
expect(loadRouteContentForEditing).toHaveBeenCalledOnce();
|
|
214
|
+
expect(mocks.loadOrCreateModel).not.toHaveBeenCalled();
|
|
215
|
+
});
|
|
216
|
+
|
|
185
217
|
it('uses showFlowSettings from page model props', async () => {
|
|
186
218
|
mocks.model = createPageModel();
|
|
187
219
|
render(<FlowPage />);
|
|
@@ -239,7 +271,7 @@ describe('FlowPage', () => {
|
|
|
239
271
|
});
|
|
240
272
|
|
|
241
273
|
it('keeps configured page model cache behavior in flow settings mode', async () => {
|
|
242
|
-
|
|
274
|
+
editor.enabled = true;
|
|
243
275
|
mocks.model = createPageModel();
|
|
244
276
|
mocks.viewFilterByTk = 1;
|
|
245
277
|
|
|
@@ -7,33 +7,48 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { observer } from '@formily/reactive-react';
|
|
11
|
+
import { loadRouteContentForEditing } from '../../../FlowModelRepository';
|
|
12
|
+
import {
|
|
13
|
+
type CreateModelOptions,
|
|
14
|
+
FlowModel,
|
|
15
|
+
FlowModelRenderer,
|
|
16
|
+
observable,
|
|
17
|
+
type ParamObject,
|
|
18
|
+
tExpr,
|
|
19
|
+
} from '@nocobase/flow-engine';
|
|
11
20
|
import { Icon, type NocoBaseDesktopRoute } from '../../../../flow-compat';
|
|
12
21
|
import type { RouteRepository } from '../../../../RouteRepository';
|
|
13
|
-
import {
|
|
22
|
+
import { RouteContent, useRouteContentRequest } from '../../../useRouteContentRequest';
|
|
14
23
|
import _ from 'lodash';
|
|
15
24
|
import React from 'react';
|
|
16
25
|
import { SkeletonFallback } from '../../../components/SkeletonFallback';
|
|
17
26
|
import { TextAreaWithContextSelector } from '../../../components/TextAreaWithContextSelector';
|
|
18
27
|
import { BlockGridModel } from '../BlockGridModel';
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
const PageTabChildrenRenderer = observer(({ ctx, options }: PageTabChildrenProps) => {
|
|
30
|
+
const isRoute = ctx.model instanceof RootPageTabModel && ctx.model.props.route?.id != null;
|
|
31
|
+
const { model, loading, error, needsConfirmation } = useRouteContentRequest(
|
|
32
|
+
async (forEditing) => {
|
|
33
|
+
const model = forEditing
|
|
34
|
+
? await loadRouteContentForEditing(ctx.engine, options)
|
|
35
|
+
: await ctx.engine.loadOrCreateModel(options, { skipSave: isRoute || !ctx.flowSettingsEnabled });
|
|
24
36
|
model.context.addDelegate(ctx);
|
|
25
37
|
return model;
|
|
26
38
|
},
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
39
|
+
isRoute && ctx.flowSettingsEnabled,
|
|
40
|
+
[ctx.model.uid],
|
|
30
41
|
);
|
|
42
|
+
if (error && isRoute) throw error;
|
|
31
43
|
const margin = ctx?.isMobileLayout ? 8 : ctx?.themeToken.marginBlock;
|
|
32
|
-
if (
|
|
44
|
+
if (!model?.uid || (loading && !isRoute)) {
|
|
33
45
|
return <SkeletonFallback style={{ margin }} />;
|
|
34
46
|
}
|
|
35
|
-
|
|
36
|
-
}
|
|
47
|
+
const grid = <FlowModelRenderer model={model} fallback={<SkeletonFallback style={{ margin }} />} />;
|
|
48
|
+
return isRoute ? <RouteContent pending={needsConfirmation}>{grid}</RouteContent> : grid;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
type PageTabChildrenProps = { ctx: FlowModel['context']; options: CreateModelOptions };
|
|
37
52
|
|
|
38
53
|
/**
|
|
39
54
|
* 统一归一化 `desktopRoutes:updateOrCreate` 的返回值。
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { css, cx } from '@emotion/css';
|
|
10
11
|
import {
|
|
11
12
|
Collection,
|
|
12
13
|
DisplayItemModel,
|
|
@@ -14,6 +15,8 @@ import {
|
|
|
14
15
|
FieldModelRenderer,
|
|
15
16
|
FlowModelContext,
|
|
16
17
|
FormItem,
|
|
18
|
+
formItemStyle,
|
|
19
|
+
untracked,
|
|
17
20
|
} from '@nocobase/flow-engine';
|
|
18
21
|
import { get, castArray } from 'lodash';
|
|
19
22
|
import React from 'react';
|
|
@@ -22,6 +25,49 @@ import { DetailsGridModel } from './DetailsGridModel';
|
|
|
22
25
|
import { rebuildFieldSubModel } from '../../../internal/utils/rebuildFieldSubModel';
|
|
23
26
|
import { isToManyAssociationField } from '../../../internal/utils/modelUtils';
|
|
24
27
|
|
|
28
|
+
function mobileHorizontalItemStyle(marginBottom: number) {
|
|
29
|
+
return css`
|
|
30
|
+
&&.ant-form-item {
|
|
31
|
+
margin-bottom: ${formItemStyle.marginBottom}px;
|
|
32
|
+
|
|
33
|
+
@media (max-width: 575px) {
|
|
34
|
+
margin-bottom: ${marginBottom}px;
|
|
35
|
+
min-height: ${32 - marginBottom}px;
|
|
36
|
+
|
|
37
|
+
> .ant-form-item-row > .ant-form-item-label {
|
|
38
|
+
padding-bottom: 0;
|
|
39
|
+
line-height: max(20px, 1.4em);
|
|
40
|
+
|
|
41
|
+
> label {
|
|
42
|
+
height: auto;
|
|
43
|
+
line-height: max(20px, 1.4em);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
> .ant-form-item-row > .ant-form-item-control > .ant-form-item-control-input {
|
|
48
|
+
min-height: 20px;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const mobileSpacingByBlock = new Map([
|
|
56
|
+
['DetailsBlockModel', mobileHorizontalItemStyle(3)],
|
|
57
|
+
['GridCardBlockModel', mobileHorizontalItemStyle(8)],
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const mobilePlainTextStyle = css`
|
|
61
|
+
@media (max-width: 575px) {
|
|
62
|
+
> .ant-form-item-row
|
|
63
|
+
> .ant-form-item-control
|
|
64
|
+
> .ant-form-item-control-input
|
|
65
|
+
> .ant-form-item-control-input-content {
|
|
66
|
+
line-height: max(20px, 1.4em);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
|
|
25
71
|
/**
|
|
26
72
|
* 从 record 中取值
|
|
27
73
|
* @param record 当前行数据
|
|
@@ -217,8 +263,23 @@ export class DetailsItemModel extends DisplayItemModel<{
|
|
|
217
263
|
idx,
|
|
218
264
|
}),
|
|
219
265
|
);
|
|
266
|
+
const spacing =
|
|
267
|
+
this.context.isMobileLayout && mergedProps.layout === 'horizontal'
|
|
268
|
+
? mobileSpacingByBlock.get(this.context.blockModel.use)
|
|
269
|
+
: undefined;
|
|
270
|
+
const isPlainText =
|
|
271
|
+
spacing &&
|
|
272
|
+
modelForRender?.use === 'DisplayTextFieldModel' &&
|
|
273
|
+
(mergedProps.displayStyle ??
|
|
274
|
+
fieldModel.getStepParams('displayFieldSettings', 'displayStyle')?.displayStyle ??
|
|
275
|
+
// FieldModelRenderer writes back fork props after rendering. Subscribing here would render the item again.
|
|
276
|
+
untracked(() => modelForRender.props.displayStyle)) !== 'tag';
|
|
220
277
|
return (
|
|
221
|
-
<FormItem
|
|
278
|
+
<FormItem
|
|
279
|
+
{...mergedProps}
|
|
280
|
+
itemMarginClassName={spacing && cx(spacing, isPlainText && mobilePlainTextStyle)}
|
|
281
|
+
value={value}
|
|
282
|
+
>
|
|
222
283
|
<FieldModelRenderer model={modelForRender} />
|
|
223
284
|
</FormItem>
|
|
224
285
|
);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { FlowEngine, FlowModel, Tracker } from '@nocobase/flow-engine';
|
|
11
|
+
import { render } from '@testing-library/react';
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { expect, it, vi } from 'vitest';
|
|
14
|
+
import { DetailsItemModel } from '../DetailsItemModel';
|
|
15
|
+
|
|
16
|
+
it.each([
|
|
17
|
+
['DetailsBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '3px', '29px', true],
|
|
18
|
+
['GridCardBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '8px', '24px', true],
|
|
19
|
+
['GridCardBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'tag', '8px', '24px', false],
|
|
20
|
+
['DetailsBlockModel', true, 'horizontal', 'DisplayHtmlFieldModel', 'text', '3px', '29px', false],
|
|
21
|
+
['CCTaskCardDetailsModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '', '', false],
|
|
22
|
+
['DetailsBlockModel', false, 'horizontal', 'DisplayTextFieldModel', 'text', '', '', false],
|
|
23
|
+
['GridCardBlockModel', true, 'vertical', 'DisplayTextFieldModel', 'text', '', '', false],
|
|
24
|
+
] as const)(
|
|
25
|
+
'selects phone styles for %s, mobile=%s, %s, %s (%s)',
|
|
26
|
+
(block, mobile, layout, field, displayStyle, margin, minHeight, plainText) => {
|
|
27
|
+
const engine = new FlowEngine();
|
|
28
|
+
engine.registerModels({ [field]: FlowModel });
|
|
29
|
+
const model = new DetailsItemModel({ uid: 'detail-field', flowEngine: engine });
|
|
30
|
+
model.context.defineProperty('blockModel', { value: { use: block, props: { layout } } });
|
|
31
|
+
model.context.defineProperty('isMobileLayout', { value: mobile });
|
|
32
|
+
model.setProps('label', 'Name');
|
|
33
|
+
model.setStepParams('fieldSettings', 'init', { fieldPath: 'name' });
|
|
34
|
+
model.setSubModel('field', { use: field, props: { displayStyle } });
|
|
35
|
+
|
|
36
|
+
const { container } = render(React.cloneElement(model.renderItem(), {}, <span>Value</span>));
|
|
37
|
+
const item = container.querySelector<HTMLElement>('.ant-form-item');
|
|
38
|
+
const value = item.querySelector('.ant-form-item-control-input-content');
|
|
39
|
+
const rules = Array.from(document.styleSheets).flatMap((sheet) => Array.from(sheet.cssRules));
|
|
40
|
+
const narrowRules = rules.flatMap((rule) =>
|
|
41
|
+
rule instanceof CSSMediaRule && rule.conditionText === '(max-width: 575px)' ? Array.from(rule.cssRules) : [],
|
|
42
|
+
);
|
|
43
|
+
const declarations = (element: Element, property: string) =>
|
|
44
|
+
narrowRules
|
|
45
|
+
.flatMap((rule) =>
|
|
46
|
+
rule instanceof CSSStyleRule && element.matches(rule.selectorText)
|
|
47
|
+
? [rule.style.getPropertyValue(property)]
|
|
48
|
+
: [],
|
|
49
|
+
)
|
|
50
|
+
.filter(Boolean);
|
|
51
|
+
|
|
52
|
+
expect(item.style.marginBottom).toBe(margin ? '' : '12px');
|
|
53
|
+
expect(declarations(item, 'margin-bottom')).toEqual(margin ? [margin] : []);
|
|
54
|
+
expect(declarations(item, 'min-height')).toEqual(minHeight ? [minHeight] : []);
|
|
55
|
+
expect(declarations(value, 'line-height')).toEqual(plainText ? ['max(20px, 1.4em)'] : []);
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
it('does not subscribe the grid card item to field renderer prop writes', () => {
|
|
60
|
+
const engine = new FlowEngine();
|
|
61
|
+
engine.registerModels({ DisplayTextFieldModel: FlowModel });
|
|
62
|
+
const item = new DetailsItemModel({ uid: 'card-field', flowEngine: engine });
|
|
63
|
+
item.context.defineProperty('blockModel', { value: { use: 'GridCardBlockModel', props: { layout: 'horizontal' } } });
|
|
64
|
+
item.context.defineProperty('isMobileLayout', { value: true });
|
|
65
|
+
item.context.defineProperty('fieldIndex', { value: 0 });
|
|
66
|
+
item.setStepParams('fieldSettings', 'init', { fieldPath: 'name' });
|
|
67
|
+
const field = item.setSubModel('field', { use: 'DisplayTextFieldModel' });
|
|
68
|
+
const invalidate = vi.fn();
|
|
69
|
+
const tracker = new Tracker(invalidate);
|
|
70
|
+
try {
|
|
71
|
+
tracker.track(() => item.renderItem());
|
|
72
|
+
field.getFork('0').setProps({ value: 'First row' });
|
|
73
|
+
expect(invalidate).not.toHaveBeenCalled();
|
|
74
|
+
} finally {
|
|
75
|
+
tracker.dispose();
|
|
76
|
+
}
|
|
77
|
+
});
|