@nocobase/flow-engine 2.2.0-beta.16 → 2.2.0-beta.18
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/lib/components/FlowContextSelector.js +7 -1
- package/lib/components/subModel/LazyDropdown.js +21 -7
- package/lib/flowContext.d.ts +5 -1
- package/lib/flowContext.js +23 -6
- package/lib/resources/flowResource.js +1 -0
- package/lib/utils/associationObjectVariable.d.ts +10 -0
- package/lib/utils/associationObjectVariable.js +10 -7
- package/lib/utils/dateVariable.d.ts +22 -0
- package/lib/utils/dateVariable.js +123 -16
- package/lib/utils/index.d.ts +3 -3
- package/lib/utils/index.js +8 -0
- package/lib/utils/params-resolvers.d.ts +3 -0
- package/lib/utils/params-resolvers.js +10 -0
- package/lib/utils/variablesParams.js +5 -0
- package/lib/views/createViewMeta.d.ts +1 -0
- package/lib/views/createViewMeta.js +53 -22
- package/package.json +4 -4
- package/src/__tests__/createViewMeta.popup.test.ts +84 -1
- package/src/__tests__/flowContext.test.ts +8 -0
- package/src/__tests__/objectVariable.test.ts +6 -1
- package/src/__tests__/runjsFormSubmit.test.ts +45 -0
- package/src/components/FlowContextSelector.tsx +7 -1
- package/src/components/subModel/LazyDropdown.tsx +28 -13
- package/src/components/subModel/__tests__/LazyDropdown.test.tsx +202 -0
- package/src/components/variables/__tests__/FlowContextSelector.test.tsx +35 -0
- package/src/flowContext.ts +35 -5
- package/src/resources/__tests__/flowResource.test.ts +3 -0
- package/src/resources/flowResource.ts +1 -0
- package/src/utils/__tests__/dateVariable.test.ts +57 -4
- package/src/utils/__tests__/variablesParams.test.ts +28 -1
- package/src/utils/associationObjectVariable.ts +9 -6
- package/src/utils/dateVariable.ts +145 -18
- package/src/utils/index.ts +17 -2
- package/src/utils/params-resolvers.ts +12 -0
- package/src/utils/variablesParams.ts +10 -0
- package/src/views/createViewMeta.ts +52 -18
|
@@ -0,0 +1,202 @@
|
|
|
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 { act, render, screen, userEvent, waitFor } from '@nocobase/test/client';
|
|
11
|
+
import { ConfigProvider } from 'antd';
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
14
|
+
import { FlowEngineProvider } from '../../../provider';
|
|
15
|
+
import { FlowEngine } from '../../../flowEngine';
|
|
16
|
+
import LazyDropdown from '../LazyDropdown';
|
|
17
|
+
|
|
18
|
+
const setViewportHeight = (height: number) => {
|
|
19
|
+
Object.defineProperty(window, 'innerHeight', {
|
|
20
|
+
configurable: true,
|
|
21
|
+
value: height,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
describe('LazyDropdown', () => {
|
|
26
|
+
const originalInnerHeight = window.innerHeight;
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
setViewportHeight(originalInnerHeight);
|
|
30
|
+
vi.restoreAllMocks();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('uses the current viewport space when opening after the viewport height changes', async () => {
|
|
34
|
+
setViewportHeight(720);
|
|
35
|
+
const engine = new FlowEngine();
|
|
36
|
+
const user = userEvent.setup();
|
|
37
|
+
|
|
38
|
+
render(
|
|
39
|
+
<FlowEngineProvider engine={engine}>
|
|
40
|
+
<ConfigProvider>
|
|
41
|
+
<LazyDropdown
|
|
42
|
+
trigger={['click']}
|
|
43
|
+
menu={{
|
|
44
|
+
items: [{ key: 'field', label: 'Field' }],
|
|
45
|
+
}}
|
|
46
|
+
>
|
|
47
|
+
<button type="button">Open fields</button>
|
|
48
|
+
</LazyDropdown>
|
|
49
|
+
</ConfigProvider>
|
|
50
|
+
</FlowEngineProvider>,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const trigger = screen.getByRole('button', { name: 'Open fields' });
|
|
54
|
+
vi.spyOn(trigger, 'getBoundingClientRect').mockReturnValue({
|
|
55
|
+
bottom: 196,
|
|
56
|
+
height: 32,
|
|
57
|
+
left: 49,
|
|
58
|
+
right: 141,
|
|
59
|
+
top: 164,
|
|
60
|
+
width: 92,
|
|
61
|
+
x: 49,
|
|
62
|
+
y: 164,
|
|
63
|
+
toJSON: () => ({}),
|
|
64
|
+
});
|
|
65
|
+
setViewportHeight(460);
|
|
66
|
+
|
|
67
|
+
await user.click(trigger);
|
|
68
|
+
|
|
69
|
+
const menu = await screen.findByRole('menu');
|
|
70
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '256px', overflowY: 'auto' }));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('updates the available height while the dropdown is open', async () => {
|
|
74
|
+
setViewportHeight(720);
|
|
75
|
+
const engine = new FlowEngine();
|
|
76
|
+
const user = userEvent.setup();
|
|
77
|
+
|
|
78
|
+
render(
|
|
79
|
+
<FlowEngineProvider engine={engine}>
|
|
80
|
+
<ConfigProvider>
|
|
81
|
+
<LazyDropdown
|
|
82
|
+
trigger={['click']}
|
|
83
|
+
menu={{
|
|
84
|
+
items: [{ key: 'field', label: 'Field' }],
|
|
85
|
+
}}
|
|
86
|
+
>
|
|
87
|
+
<button type="button">Open fields</button>
|
|
88
|
+
</LazyDropdown>
|
|
89
|
+
</ConfigProvider>
|
|
90
|
+
</FlowEngineProvider>,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const trigger = screen.getByRole('button', { name: 'Open fields' });
|
|
94
|
+
vi.spyOn(trigger, 'getBoundingClientRect').mockReturnValue({
|
|
95
|
+
bottom: 196,
|
|
96
|
+
height: 32,
|
|
97
|
+
left: 49,
|
|
98
|
+
right: 141,
|
|
99
|
+
top: 164,
|
|
100
|
+
width: 92,
|
|
101
|
+
x: 49,
|
|
102
|
+
y: 164,
|
|
103
|
+
toJSON: () => ({}),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await user.click(trigger);
|
|
107
|
+
|
|
108
|
+
const menu = await screen.findByRole('menu');
|
|
109
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '400px', overflowY: 'auto' }));
|
|
110
|
+
|
|
111
|
+
act(() => {
|
|
112
|
+
setViewportHeight(460);
|
|
113
|
+
window.dispatchEvent(new Event('resize'));
|
|
114
|
+
});
|
|
115
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '256px', overflowY: 'auto' }));
|
|
116
|
+
|
|
117
|
+
act(() => {
|
|
118
|
+
setViewportHeight(720);
|
|
119
|
+
window.dispatchEvent(new Event('resize'));
|
|
120
|
+
});
|
|
121
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '400px', overflowY: 'auto' }));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('reserves the placement offset when the dropdown has an arrow', async () => {
|
|
125
|
+
setViewportHeight(460);
|
|
126
|
+
const engine = new FlowEngine();
|
|
127
|
+
const user = userEvent.setup();
|
|
128
|
+
|
|
129
|
+
render(
|
|
130
|
+
<FlowEngineProvider engine={engine}>
|
|
131
|
+
<ConfigProvider>
|
|
132
|
+
<LazyDropdown
|
|
133
|
+
arrow
|
|
134
|
+
trigger={['click']}
|
|
135
|
+
menu={{
|
|
136
|
+
items: [{ key: 'field', label: 'Field' }],
|
|
137
|
+
}}
|
|
138
|
+
>
|
|
139
|
+
<button type="button">Open fields</button>
|
|
140
|
+
</LazyDropdown>
|
|
141
|
+
</ConfigProvider>
|
|
142
|
+
</FlowEngineProvider>,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const trigger = screen.getByRole('button', { name: 'Open fields' });
|
|
146
|
+
vi.spyOn(trigger, 'getBoundingClientRect').mockReturnValue({
|
|
147
|
+
bottom: 196,
|
|
148
|
+
height: 32,
|
|
149
|
+
left: 49,
|
|
150
|
+
right: 141,
|
|
151
|
+
top: 164,
|
|
152
|
+
width: 92,
|
|
153
|
+
x: 49,
|
|
154
|
+
y: 164,
|
|
155
|
+
toJSON: () => ({}),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
await user.click(trigger);
|
|
159
|
+
|
|
160
|
+
const menu = await screen.findByRole('menu');
|
|
161
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '248px', overflowY: 'auto' }));
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('uses the space above when it is larger than the space below', async () => {
|
|
165
|
+
setViewportHeight(460);
|
|
166
|
+
const engine = new FlowEngine();
|
|
167
|
+
const user = userEvent.setup();
|
|
168
|
+
|
|
169
|
+
render(
|
|
170
|
+
<FlowEngineProvider engine={engine}>
|
|
171
|
+
<ConfigProvider>
|
|
172
|
+
<LazyDropdown
|
|
173
|
+
trigger={['click']}
|
|
174
|
+
menu={{
|
|
175
|
+
items: [{ key: 'field', label: 'Field' }],
|
|
176
|
+
}}
|
|
177
|
+
>
|
|
178
|
+
<button type="button">Open fields</button>
|
|
179
|
+
</LazyDropdown>
|
|
180
|
+
</ConfigProvider>
|
|
181
|
+
</FlowEngineProvider>,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const trigger = screen.getByRole('button', { name: 'Open fields' });
|
|
185
|
+
vi.spyOn(trigger, 'getBoundingClientRect').mockReturnValue({
|
|
186
|
+
bottom: 332,
|
|
187
|
+
height: 32,
|
|
188
|
+
left: 49,
|
|
189
|
+
right: 141,
|
|
190
|
+
top: 300,
|
|
191
|
+
width: 92,
|
|
192
|
+
x: 49,
|
|
193
|
+
y: 300,
|
|
194
|
+
toJSON: () => ({}),
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
await user.click(trigger);
|
|
198
|
+
|
|
199
|
+
const menu = await screen.findByRole('menu');
|
|
200
|
+
await waitFor(() => expect(menu).toHaveStyle({ maxHeight: '292px', overflowY: 'auto' }));
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -864,4 +864,39 @@ describe('FlowContextSelector', () => {
|
|
|
864
864
|
// It should only expand the node, not select it
|
|
865
865
|
expect(onChange).not.toHaveBeenCalled();
|
|
866
866
|
});
|
|
867
|
+
|
|
868
|
+
it('should expand but never select a node marked selectable=false', async () => {
|
|
869
|
+
const onChange = vi.fn();
|
|
870
|
+
const flowContext = createTestFlowContext();
|
|
871
|
+
const metaTree = [
|
|
872
|
+
{
|
|
873
|
+
name: 'date',
|
|
874
|
+
title: 'Date',
|
|
875
|
+
type: 'date',
|
|
876
|
+
paths: ['date'],
|
|
877
|
+
selectable: false,
|
|
878
|
+
children: [{ name: 'today', title: 'Today', type: 'date', paths: ['date', 'today'] }],
|
|
879
|
+
},
|
|
880
|
+
];
|
|
881
|
+
|
|
882
|
+
render(
|
|
883
|
+
<TestFlowContextWrapper context={flowContext}>
|
|
884
|
+
<FlowContextSelector metaTree={metaTree} onChange={onChange} />
|
|
885
|
+
</TestFlowContextWrapper>,
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
fireEvent.click(screen.getByRole('button'));
|
|
889
|
+
await waitFor(() => expect(screen.getByText('Date')).toBeInTheDocument());
|
|
890
|
+
|
|
891
|
+
fireEvent.click(screen.getByText('Date'));
|
|
892
|
+
fireEvent.click(screen.getByText('Date'));
|
|
893
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
894
|
+
|
|
895
|
+
await waitFor(() => expect(screen.getByText('Today')).toBeInTheDocument());
|
|
896
|
+
fireEvent.click(screen.getByText('Today'));
|
|
897
|
+
expect(onChange).toHaveBeenCalledWith(
|
|
898
|
+
'{{ ctx.date.today }}',
|
|
899
|
+
expect.objectContaining({ paths: ['date', 'today'] }),
|
|
900
|
+
);
|
|
901
|
+
});
|
|
867
902
|
});
|
package/src/flowContext.ts
CHANGED
|
@@ -50,11 +50,11 @@ import {
|
|
|
50
50
|
resolveModuleUrl,
|
|
51
51
|
} from './utils';
|
|
52
52
|
import { FlowExitAllException } from './utils/exceptions';
|
|
53
|
-
import { enqueueVariablesResolve, JSONValue } from './utils/params-resolvers';
|
|
53
|
+
import { buildFlowModelResolveDescriptor, enqueueVariablesResolve, JSONValue } from './utils/params-resolvers';
|
|
54
54
|
import type { RecordRef } from './utils/serverContextParams';
|
|
55
55
|
import { buildServerContextParams as _buildServerContextParams } from './utils/serverContextParams';
|
|
56
56
|
import { getDirtyAwareApiClient } from './utils/dirtyAwareApiClient';
|
|
57
|
-
import { inferRecordRef } from './utils/variablesParams';
|
|
57
|
+
import { inferRecordRef, inferViewRecordRef } from './utils/variablesParams';
|
|
58
58
|
import { FlowView, FlowViewer } from './views/FlowView';
|
|
59
59
|
import { RunJSContextRegistry, getModelClassName, type RunJSVersion } from './runjs-context/registry';
|
|
60
60
|
import { createEphemeralContext } from './utils/createEphemeralContext';
|
|
@@ -165,6 +165,10 @@ function inferSelectsFromUsage(paths: string[] = []): { generatedAppends?: strin
|
|
|
165
165
|
|
|
166
166
|
type Getter<T = any> = (ctx: FlowContext) => T | Promise<T>;
|
|
167
167
|
|
|
168
|
+
export type ResolveJsonTemplateOptions = {
|
|
169
|
+
contractModelUid?: string | number | null;
|
|
170
|
+
};
|
|
171
|
+
|
|
168
172
|
export type FlowContextDocRef = string | { url: string; title?: string };
|
|
169
173
|
|
|
170
174
|
export type FlowDeprecationDoc =
|
|
@@ -221,6 +225,8 @@ export interface MetaTreeNode {
|
|
|
221
225
|
// 变量禁用状态与原因(用于变量选择器 UI 展示)
|
|
222
226
|
disabled?: boolean | (() => boolean);
|
|
223
227
|
disabledReason?: string | (() => string | undefined);
|
|
228
|
+
// 允许节点仅用于展开子级,而不能作为变量值被选中
|
|
229
|
+
selectable?: boolean;
|
|
224
230
|
children?: MetaTreeNode[] | (() => Promise<MetaTreeNode[]>);
|
|
225
231
|
}
|
|
226
232
|
|
|
@@ -3044,7 +3050,7 @@ class BaseFlowEngineContext extends FlowContext {
|
|
|
3044
3050
|
* @deprecated use `resolveJsonTemplate` instead
|
|
3045
3051
|
*/
|
|
3046
3052
|
declare renderJson: (template: JSONValue) => Promise<any>;
|
|
3047
|
-
declare resolveJsonTemplate: (template: JSONValue) => Promise<any>;
|
|
3053
|
+
declare resolveJsonTemplate: (template: JSONValue, options?: ResolveJsonTemplateOptions) => Promise<any>;
|
|
3048
3054
|
declare getVar: (path: string) => Promise<any>;
|
|
3049
3055
|
declare request: (options: RequestOptions) => Promise<any>;
|
|
3050
3056
|
declare runjs: (code: string, variables?: Record<string, any>, options?: JSRunnerOptions) => Promise<any>;
|
|
@@ -3227,7 +3233,11 @@ export class FlowEngineContext extends BaseFlowEngineContext {
|
|
|
3227
3233
|
this.defineMethod('renderJson', function (template: any) {
|
|
3228
3234
|
return this.resolveJsonTemplate(template);
|
|
3229
3235
|
});
|
|
3230
|
-
|
|
3236
|
+
const resolveJsonTemplate = async function (
|
|
3237
|
+
this: BaseFlowEngineContext,
|
|
3238
|
+
template: any,
|
|
3239
|
+
options?: ResolveJsonTemplateOptions,
|
|
3240
|
+
) {
|
|
3231
3241
|
// 提取模板使用到的变量及其子路径
|
|
3232
3242
|
const used = extractUsedVariablePaths(template);
|
|
3233
3243
|
const usedVarNames = Object.keys(used || {});
|
|
@@ -3316,6 +3326,15 @@ export class FlowEngineContext extends BaseFlowEngineContext {
|
|
|
3316
3326
|
const inputFromMeta = await collectFromMeta();
|
|
3317
3327
|
const autoInput = { ...inputFromMeta } as Record<string, any>;
|
|
3318
3328
|
|
|
3329
|
+
const viewPaths = serverVarPaths.view || [];
|
|
3330
|
+
if (
|
|
3331
|
+
!autoInput.view &&
|
|
3332
|
+
viewPaths.some((path) => path === 'record' || path.startsWith('record.') || path.startsWith('record['))
|
|
3333
|
+
) {
|
|
3334
|
+
const recordRef = inferViewRecordRef(this);
|
|
3335
|
+
if (recordRef) autoInput.view = { record: recordRef };
|
|
3336
|
+
}
|
|
3337
|
+
|
|
3319
3338
|
// Special-case: formValues
|
|
3320
3339
|
// If server needs to resolve some formValues paths but meta params only cover association anchors
|
|
3321
3340
|
// (e.g. formValues.customer) and some top-level paths are missing (e.g. formValues.status),
|
|
@@ -3387,7 +3406,13 @@ export class FlowEngineContext extends BaseFlowEngineContext {
|
|
|
3387
3406
|
|
|
3388
3407
|
if (this.api) {
|
|
3389
3408
|
try {
|
|
3409
|
+
const contractRd = buildFlowModelResolveDescriptor(
|
|
3410
|
+
this as FlowRuntimeContext<FlowModel>,
|
|
3411
|
+
options?.contractModelUid,
|
|
3412
|
+
);
|
|
3390
3413
|
serverResolved = await enqueueVariablesResolve(this as FlowRuntimeContext<FlowModel>, {
|
|
3414
|
+
...(contractRd ? { contractRd } : {}),
|
|
3415
|
+
rd: buildFlowModelResolveDescriptor(this as FlowRuntimeContext<FlowModel>, this.model?.uid),
|
|
3391
3416
|
template,
|
|
3392
3417
|
contextParams: autoContextParams || {},
|
|
3393
3418
|
});
|
|
@@ -3399,7 +3424,8 @@ export class FlowEngineContext extends BaseFlowEngineContext {
|
|
|
3399
3424
|
}
|
|
3400
3425
|
|
|
3401
3426
|
return resolveExpressions(serverResolved, this);
|
|
3402
|
-
}
|
|
3427
|
+
};
|
|
3428
|
+
this.defineMethod('resolveJsonTemplate', resolveJsonTemplate);
|
|
3403
3429
|
|
|
3404
3430
|
// Helper: resolve a single ctx expression value via resolveJsonTemplate behavior.
|
|
3405
3431
|
// Example: await ctx.getVar('ctx.record.id')
|
|
@@ -4585,6 +4611,10 @@ export class FlowRunJSContext extends FlowContext {
|
|
|
4585
4611
|
constructor(delegate: FlowContext) {
|
|
4586
4612
|
super();
|
|
4587
4613
|
this.addDelegate(delegate);
|
|
4614
|
+
const submit = delegate.blockModel?.submitFromRunJs?.bind(delegate.blockModel);
|
|
4615
|
+
if (delegate.form && submit) {
|
|
4616
|
+
this.defineProperty('form', { value: { ...delegate.form, submit } });
|
|
4617
|
+
}
|
|
4588
4618
|
this.defineProperty('React', { value: React });
|
|
4589
4619
|
this.defineProperty('antd', { value: antd });
|
|
4590
4620
|
this.defineProperty('dayjs', {
|
|
@@ -79,6 +79,9 @@ describe('FlowResource - error handling', () => {
|
|
|
79
79
|
expect(r.getError()).toBeNull();
|
|
80
80
|
|
|
81
81
|
const err = new ResourceError({ response: { data: { error: { message: 'boom', code: 'X' } } } });
|
|
82
|
+
expect(err.data).toEqual({ message: 'boom', code: 'X' });
|
|
83
|
+
expect(err.message).toBe('boom');
|
|
84
|
+
expect(err.code).toBe('X');
|
|
82
85
|
const ret = r.setError(err);
|
|
83
86
|
expect(ret).toBe(r);
|
|
84
87
|
expect(r.error).toBe(err);
|
|
@@ -12,9 +12,12 @@ import {
|
|
|
12
12
|
decodeBase64Url,
|
|
13
13
|
encodeBase64Url,
|
|
14
14
|
isCompleteCtxDatePath,
|
|
15
|
+
isCtxDatePathPrefix,
|
|
15
16
|
isCtxDateExpression,
|
|
16
17
|
parseCtxDateExpression,
|
|
18
|
+
parseCtxDateExpressionConfig,
|
|
17
19
|
resolveCtxDatePath,
|
|
20
|
+
serializeCtxDateExpressionConfig,
|
|
18
21
|
serializeCtxDateValue,
|
|
19
22
|
} from '../dateVariable';
|
|
20
23
|
|
|
@@ -54,13 +57,60 @@ describe('dateVariable utils', () => {
|
|
|
54
57
|
number: 2,
|
|
55
58
|
});
|
|
56
59
|
|
|
57
|
-
const singleExpr = serializeCtxDateValue('2026-02-12')
|
|
60
|
+
const singleExpr = serializeCtxDateValue('2026-02-12');
|
|
61
|
+
if (!singleExpr) throw new Error('Expected exact date expression');
|
|
58
62
|
expect(parseCtxDateExpression(singleExpr)).toBe('2026-02-12');
|
|
59
63
|
|
|
60
|
-
const rangeExpr = serializeCtxDateValue(['2026-02-12', '2026-02-20'])
|
|
64
|
+
const rangeExpr = serializeCtxDateValue(['2026-02-12', '2026-02-20']);
|
|
65
|
+
if (!rangeExpr) throw new Error('Expected exact date range expression');
|
|
61
66
|
expect(parseCtxDateExpression(rangeExpr)).toEqual(['2026-02-12', '2026-02-20']);
|
|
62
67
|
});
|
|
63
68
|
|
|
69
|
+
it('serializes, parses and resolves formatted expressions', () => {
|
|
70
|
+
const expression = serializeCtxDateExpressionConfig({
|
|
71
|
+
kind: 'preset',
|
|
72
|
+
preset: 'today',
|
|
73
|
+
format: 'YYYY/MM/DD',
|
|
74
|
+
});
|
|
75
|
+
if (!expression) throw new Error('Expected formatted date expression');
|
|
76
|
+
|
|
77
|
+
expect(expression).toMatch(/^\{\{ ctx\.date\.format\.v[A-Za-z0-9_-]+\.preset\.today \}\}$/);
|
|
78
|
+
expect(parseCtxDateExpressionConfig(expression)).toEqual({
|
|
79
|
+
kind: 'preset',
|
|
80
|
+
preset: 'today',
|
|
81
|
+
format: 'YYYY/MM/DD',
|
|
82
|
+
});
|
|
83
|
+
// Keep the legacy parser contract for filter-form consumers.
|
|
84
|
+
expect(parseCtxDateExpression(expression)).toEqual({ type: 'today' });
|
|
85
|
+
|
|
86
|
+
const path = expression.replace('{{ ctx.', '').replace(' }}', '').split('.');
|
|
87
|
+
expect(resolveCtxDatePath(path)).toMatch(/^\d{4}\/\d{2}\/\d{2}$/);
|
|
88
|
+
expect(isCompleteCtxDatePath(path)).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('preserves significant whitespace in a custom Format', () => {
|
|
92
|
+
const expression = serializeCtxDateExpressionConfig({
|
|
93
|
+
kind: 'preset',
|
|
94
|
+
preset: 'today',
|
|
95
|
+
format: 'YYYY-MM-DD ',
|
|
96
|
+
});
|
|
97
|
+
if (!expression) throw new Error('Expected formatted date expression');
|
|
98
|
+
|
|
99
|
+
expect(parseCtxDateExpressionConfig(expression)?.format).toBe('YYYY-MM-DD ');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('formats exact ranges element by element', () => {
|
|
103
|
+
const expression = serializeCtxDateExpressionConfig({
|
|
104
|
+
kind: 'exact',
|
|
105
|
+
value: ['2026-02-12', '2026-02-20'],
|
|
106
|
+
format: 'YYYYMMDD',
|
|
107
|
+
});
|
|
108
|
+
if (!expression) throw new Error('Expected formatted date range expression');
|
|
109
|
+
const path = expression.replace('{{ ctx.', '').replace(' }}', '').split('.');
|
|
110
|
+
|
|
111
|
+
expect(resolveCtxDatePath(path)).toEqual(['20260212', '20260220']);
|
|
112
|
+
});
|
|
113
|
+
|
|
64
114
|
it('resolves preset/relative/exact path', () => {
|
|
65
115
|
expect(typeof resolveCtxDatePath(['date', 'preset', 'now'])).toBe('string');
|
|
66
116
|
|
|
@@ -72,11 +122,13 @@ describe('dateVariable utils', () => {
|
|
|
72
122
|
expect(typeof rel).toBe('string');
|
|
73
123
|
expect(rel).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
74
124
|
|
|
75
|
-
const singleExpr = serializeCtxDateValue('2026-02-12')
|
|
125
|
+
const singleExpr = serializeCtxDateValue('2026-02-12');
|
|
126
|
+
if (!singleExpr) throw new Error('Expected exact date expression');
|
|
76
127
|
const token = singleExpr.replace('{{ ctx.date.exact.single.date.', '').replace(' }}', '');
|
|
77
128
|
expect(resolveCtxDatePath(['date', 'exact', 'single', 'date', token])).toBe('2026-02-12');
|
|
78
129
|
|
|
79
|
-
const rangeExpr = serializeCtxDateValue(['2026-02-12', '2026-02-20'])
|
|
130
|
+
const rangeExpr = serializeCtxDateValue(['2026-02-12', '2026-02-20']);
|
|
131
|
+
if (!rangeExpr) throw new Error('Expected exact date range expression');
|
|
80
132
|
const parts = rangeExpr.replace('{{ ctx.date.exact.range.date.', '').replace(' }}', '').split('.');
|
|
81
133
|
expect(resolveCtxDatePath(['date', 'exact', 'range', 'date', parts[0], parts[1]])).toEqual([
|
|
82
134
|
'2026-02-12',
|
|
@@ -90,6 +142,7 @@ describe('dateVariable utils', () => {
|
|
|
90
142
|
expect(isCompleteCtxDatePath(['date', 'exact', 'single', 'date', 'vabc'])).toBe(true);
|
|
91
143
|
expect(isCompleteCtxDatePath(['date', 'exact', 'range', 'date', 'vabc', 'vdef'])).toBe(true);
|
|
92
144
|
expect(isCompleteCtxDatePath(['date', 'relative', 'next', 'day'])).toBe(false);
|
|
145
|
+
expect(isCtxDatePathPrefix(['date', 'format'])).toBe(true);
|
|
93
146
|
expect(isCompleteCtxDatePath(['user', 'name'])).toBe(false);
|
|
94
147
|
});
|
|
95
148
|
|
|
@@ -34,7 +34,8 @@ describe('variablesParams helpers', () => {
|
|
|
34
34
|
|
|
35
35
|
it('inferRecordRef fallback to collection.getFilterByTK when resource has no filterByTk', () => {
|
|
36
36
|
const engine = new FlowEngine();
|
|
37
|
-
const ds = engine.context.dataSourceManager.getDataSource('main')
|
|
37
|
+
const ds = engine.context.dataSourceManager.getDataSource('main');
|
|
38
|
+
if (!ds) throw new Error('main data source is required');
|
|
38
39
|
ds.addCollection({
|
|
39
40
|
name: 'users',
|
|
40
41
|
filterTargetKey: 'id',
|
|
@@ -108,6 +109,32 @@ describe('variablesParams helpers', () => {
|
|
|
108
109
|
});
|
|
109
110
|
});
|
|
110
111
|
|
|
112
|
+
it('collectContextParamsForTemplate infers view.record when its meta has no descriptor', async () => {
|
|
113
|
+
const ctx: any = {
|
|
114
|
+
getPropertyOptions: () => undefined,
|
|
115
|
+
view: {
|
|
116
|
+
inputArgs: {
|
|
117
|
+
collectionName: 'posts',
|
|
118
|
+
dataSourceKey: 'main',
|
|
119
|
+
filterByTk: 3,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const res = await collectContextParamsForTemplate(ctx, {
|
|
125
|
+
recordId: '{{ ctx.view.record.id }}',
|
|
126
|
+
viewType: '{{ ctx.view.type }}',
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(res).toEqual({
|
|
130
|
+
'view.record': {
|
|
131
|
+
collection: 'posts',
|
|
132
|
+
dataSourceKey: 'main',
|
|
133
|
+
filterByTk: 3,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
111
138
|
it('createRecordResolveOnServerWithLocal: no local record => always use server', () => {
|
|
112
139
|
const resolver = createRecordResolveOnServerWithLocal(
|
|
113
140
|
() => ({ name: 'posts', dataSourceKey: 'main' }) as any,
|
|
@@ -51,13 +51,14 @@ function findFieldByName(collection: Collection | null | undefined, name?: strin
|
|
|
51
51
|
* @param primaryKey 主键字段名
|
|
52
52
|
* @returns 解析出的主键值,无法解析时返回 undefined
|
|
53
53
|
*/
|
|
54
|
-
function
|
|
54
|
+
export function getAssociationFilterByTk(value: unknown, primaryKey: string | string[]) {
|
|
55
55
|
if (value == null) return undefined;
|
|
56
56
|
if (Array.isArray(primaryKey)) {
|
|
57
57
|
if (typeof value !== 'object' || !value) return undefined;
|
|
58
|
-
const
|
|
58
|
+
const record = value as Record<string, unknown>;
|
|
59
|
+
const out: Record<string, unknown> = {};
|
|
59
60
|
for (const k of primaryKey) {
|
|
60
|
-
const v =
|
|
61
|
+
const v = record[k];
|
|
61
62
|
if (typeof v === 'undefined' || v === null) return undefined;
|
|
62
63
|
out[k] = v;
|
|
63
64
|
}
|
|
@@ -65,7 +66,7 @@ function toFilterByTk(value: unknown, primaryKey: string | string[]) {
|
|
|
65
66
|
}
|
|
66
67
|
if (typeof value === 'string' || typeof value === 'number') return value;
|
|
67
68
|
if (typeof value === 'object') {
|
|
68
|
-
return (value as
|
|
69
|
+
return (value as Record<string, unknown>)[primaryKey];
|
|
69
70
|
}
|
|
70
71
|
return undefined;
|
|
71
72
|
}
|
|
@@ -149,7 +150,9 @@ export function createAssociationAwareObjectMetaFactory(
|
|
|
149
150
|
if (associationValue == null) continue;
|
|
150
151
|
|
|
151
152
|
if (Array.isArray(associationValue)) {
|
|
152
|
-
const ids = associationValue
|
|
153
|
+
const ids = associationValue
|
|
154
|
+
.map((item) => getAssociationFilterByTk(item, primaryKey))
|
|
155
|
+
.filter((v) => v != null);
|
|
153
156
|
if (ids.length) {
|
|
154
157
|
params[name] = {
|
|
155
158
|
collection: target,
|
|
@@ -158,7 +161,7 @@ export function createAssociationAwareObjectMetaFactory(
|
|
|
158
161
|
};
|
|
159
162
|
}
|
|
160
163
|
} else {
|
|
161
|
-
const id =
|
|
164
|
+
const id = getAssociationFilterByTk(associationValue, primaryKey);
|
|
162
165
|
if (id != null) {
|
|
163
166
|
params[name] = {
|
|
164
167
|
collection: target,
|