@nocobase/client-v2 2.2.0-beta.6 → 2.2.0-beta.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/client-v2",
3
- "version": "2.2.0-beta.6",
3
+ "version": "2.2.0-beta.7",
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.0-beta.6",
31
- "@nocobase/flow-engine": "2.2.0-beta.6",
32
- "@nocobase/sdk": "2.2.0-beta.6",
33
- "@nocobase/shared": "2.2.0-beta.6",
34
- "@nocobase/utils": "2.2.0-beta.6",
30
+ "@nocobase/evaluators": "2.2.0-beta.7",
31
+ "@nocobase/flow-engine": "2.2.0-beta.7",
32
+ "@nocobase/sdk": "2.2.0-beta.7",
33
+ "@nocobase/shared": "2.2.0-beta.7",
34
+ "@nocobase/utils": "2.2.0-beta.7",
35
35
  "ahooks": "^3.7.2",
36
36
  "antd": "5.24.2",
37
37
  "antd-style": "3.7.1",
@@ -46,5 +46,5 @@
46
46
  "react-i18next": "^11.15.1",
47
47
  "react-router-dom": "^6.30.1"
48
48
  },
49
- "gitHead": "dc9246516f8a3efd6056f22fe7b3bc947bd4575b"
49
+ "gitHead": "e6a3fa8963a73cd9ddfc1273d71b0012483e1ad8"
50
50
  }
@@ -97,6 +97,7 @@ interface InternalPageItemRecord extends PluginSettingsPageItemOptions {
97
97
  export class PluginSettingsManager<TApp extends BaseApplication<any> = BaseApplication<any>> {
98
98
  protected menus: Record<string, InternalMenuItemRecord> = {};
99
99
  protected pages: Record<string, InternalPageItemRecord> = {};
100
+ protected pluginSettingsLinks: Record<string, string> = {};
100
101
  protected aclSnippets: string[] = [];
101
102
  public app: TApp;
102
103
 
@@ -294,6 +295,58 @@ export class PluginSettingsManager<TApp extends BaseApplication<any> = BaseAppli
294
295
  return !!(this.menus[name] || this.pages[name]);
295
296
  }
296
297
 
298
+ /**
299
+ * 绑定插件管理列表中的 plugin name 到实际 settings 入口。
300
+ *
301
+ * 插件管理列表使用 applicationPlugins.name 判断是否显示 Settings 入口,
302
+ * 但部分插件会把设置页挂到共享菜单或历史路径下。
303
+ *
304
+ * @param pluginName 插件管理列表中的插件名
305
+ * @param settingsName menu 或 page 名称
306
+ * @returns {void}
307
+ */
308
+ setPluginSettingsLink(pluginName: string, settingsName: string) {
309
+ if (!pluginName) {
310
+ throw new Error('Plugin settings link pluginName is required');
311
+ }
312
+
313
+ if (!settingsName) {
314
+ throw new Error('Plugin settings link settingsName is required');
315
+ }
316
+
317
+ this.pluginSettingsLinks[pluginName] = settingsName;
318
+ }
319
+
320
+ /**
321
+ * 获取插件管理列表中的 plugin name 对应的 settings 名称。
322
+ *
323
+ * @param pluginName 插件管理列表中的插件名
324
+ * @returns {string} settings 名称
325
+ */
326
+ getPluginSettingsName(pluginName: string) {
327
+ return this.pluginSettingsLinks[pluginName] || pluginName;
328
+ }
329
+
330
+ /**
331
+ * 判断插件管理列表中的插件是否有可跳转的 settings 入口。
332
+ *
333
+ * @param pluginName 插件管理列表中的插件名
334
+ * @returns {boolean} 是否已注册 settings 入口
335
+ */
336
+ hasPluginSettings(pluginName: string) {
337
+ return this.has(this.getPluginSettingsName(pluginName));
338
+ }
339
+
340
+ /**
341
+ * 获取插件管理列表中的插件对应的 settings 路径。
342
+ *
343
+ * @param pluginName 插件管理列表中的插件名
344
+ * @returns {string} settings 绝对路径
345
+ */
346
+ getPluginSettingsRoutePath(pluginName: string) {
347
+ return this.getRoutePath(this.getPluginSettingsName(pluginName));
348
+ }
349
+
297
350
  /**
298
351
  * 获取单个 menu 或 page 的只读快照。
299
352
  *
@@ -78,6 +78,19 @@ describe('PluginSettingsManager v2', () => {
78
78
  expect(app.router.get('admin.settings.demo.advanced')).toMatchObject({ path: 'advanced' });
79
79
  });
80
80
 
81
+ it('should resolve plugin settings links for plugin manager entries', () => {
82
+ const app = createMockClient();
83
+
84
+ app.pluginSettingsManager.addMenuItem({ key: 'shared', title: 'Shared' });
85
+ app.pluginSettingsManager.addPageTabItem({ menuKey: 'shared', key: 'target', title: 'Target' });
86
+ app.pluginSettingsManager.setPluginSettingsLink('demo-plugin', 'shared.target');
87
+
88
+ expect(app.pluginSettingsManager.has('demo-plugin')).toBe(false);
89
+ expect(app.pluginSettingsManager.hasPluginSettings('demo-plugin')).toBe(true);
90
+ expect(app.pluginSettingsManager.getPluginSettingsName('demo-plugin')).toBe('shared.target');
91
+ expect(app.pluginSettingsManager.getPluginSettingsRoutePath('demo-plugin')).toBe('/admin/settings/shared/target');
92
+ });
93
+
81
94
  it('should render string icon on both menu and page tab via renderIcon', () => {
82
95
  // Previously `renderPage` spread the raw `icon` string straight to the antd
83
96
  // Menu item, which displayed "LockOutlinedTitle" as text. Both `renderMenuItem`
@@ -18,6 +18,19 @@ class TestAclPlugin extends Plugin {
18
18
  }
19
19
  }
20
20
 
21
+ class TestSettingsLinkPlugin extends Plugin {
22
+ async load() {
23
+ this.app.pluginSettingsManager.addMenuItem({ key: 'shared-settings', title: 'Shared settings' });
24
+ this.app.pluginSettingsManager.addPageTabItem({
25
+ menuKey: 'shared-settings',
26
+ key: 'target',
27
+ title: 'Target settings',
28
+ Component: () => <div>Target settings page</div>,
29
+ });
30
+ this.app.pluginSettingsManager.setPluginSettingsLink('demo-plugin', 'shared-settings.target');
31
+ }
32
+ }
33
+
21
34
  type MockClientApplication = ReturnType<typeof createMockClient>;
22
35
 
23
36
  const renderApp = (app: MockClientApplication) => {
@@ -35,9 +48,9 @@ const waitForGetRequests = async (app: MockClientApplication, urls: string[]) =>
35
48
  );
36
49
  };
37
50
 
38
- const setupApp = (pmList: any[]) => {
51
+ const setupApp = (pmList: any[], plugins: Array<typeof Plugin> = []) => {
39
52
  const app = createMockClient({
40
- plugins: [NocoBaseBuildInPlugin, TestAclPlugin],
53
+ plugins: [NocoBaseBuildInPlugin, TestAclPlugin, ...plugins],
41
54
  router: { type: 'memory', initialEntries: ['/admin/settings/plugin-manager'] },
42
55
  });
43
56
 
@@ -174,4 +187,33 @@ describe('plugin-manager page', () => {
174
187
  expect(removeCall?.params).toMatchObject({ filterByTk: 'demo-plugin' });
175
188
  });
176
189
  });
190
+
191
+ it('shows Settings for a plugin linked to a different settings page', async () => {
192
+ const app = setupApp(
193
+ [
194
+ {
195
+ name: 'demo-plugin',
196
+ packageName: '@nocobase/demo-plugin',
197
+ displayName: 'Demo plugin',
198
+ description: 'A demo',
199
+ enabled: true,
200
+ builtIn: false,
201
+ removable: false,
202
+ version: '0.1.0',
203
+ isCompatible: true,
204
+ keywords: [],
205
+ },
206
+ ],
207
+ [TestSettingsLinkPlugin],
208
+ );
209
+
210
+ renderApp(app);
211
+ await waitForGetRequests(app, ['/auth:check', 'roles:check', 'pm:list']);
212
+
213
+ const card = await screen.findByRole('button', { name: 'Demo plugin' });
214
+ const settingsLink = within(card.closest('.ant-card') as HTMLElement).getByText('Settings');
215
+ fireEvent.click(settingsLink);
216
+
217
+ expect(await screen.findByText('Target settings page')).toBeInTheDocument();
218
+ });
177
219
  });
@@ -14,7 +14,7 @@ import { message } from 'antd';
14
14
  import { AdminSettingsLayoutModel as ClientV2AdminSettingsLayoutModel } from '../settings-center';
15
15
  import { AdminSettingsLayoutModel as ClientV1AdminSettingsLayoutModel } from '../../../client/src/pm/AdminSettingsLayoutModel';
16
16
  import { NocoBaseBuildInPlugin } from '../nocobase-buildin-plugin';
17
- import { matchSettingsRoute } from '../settings-center/utils';
17
+ import { matchSettingsRoute, sortTopLevelSettings } from '../settings-center/utils';
18
18
 
19
19
  class TestAclPlugin extends Plugin {
20
20
  async load() {
@@ -168,6 +168,12 @@ describe('settings center', () => {
168
168
  });
169
169
  });
170
170
 
171
+ it('should sort system-settings with other top-level settings by normal ordering', () => {
172
+ const settings = [{ name: 'system-settings' }, { name: 'api-keys' }, { name: 'backups' }] as any;
173
+
174
+ expect(sortTopLevelSettings(settings).map((item) => item.name)).toEqual(['api-keys', 'backups', 'system-settings']);
175
+ });
176
+
171
177
  it('should redirect /admin/settings to system-settings by default', async () => {
172
178
  const app = createMockClient({
173
179
  plugins: [NocoBaseBuildInPlugin, TestAclPlugin],
@@ -139,6 +139,64 @@ describe('normalizeDataScopeFilter', () => {
139
139
  expect(resource.removeFilterGroup).not.toHaveBeenCalled();
140
140
  });
141
141
 
142
+ it('dataScope handler preserves current role as server-side variable', async () => {
143
+ const engine = new FlowEngine();
144
+ const resource = {
145
+ addFilterGroup: vi.fn(),
146
+ removeFilterGroup: vi.fn(),
147
+ };
148
+ const ctx = engine.context;
149
+ ctx.defineProperty('api', { value: { auth: { role: '__union__' } } });
150
+ ctx.defineProperty('user', {
151
+ value: {
152
+ roles: [{ name: 'admin' }, { name: 'member' }],
153
+ },
154
+ });
155
+ ctx.defineProperty('model', {
156
+ value: {
157
+ uid: 'table-1',
158
+ resource,
159
+ },
160
+ });
161
+ const params = {
162
+ filter: {
163
+ logic: '$and',
164
+ items: [{ path: 'roles.name', operator: '$includes', value: '{{ ctx.role }}' }],
165
+ },
166
+ };
167
+
168
+ await (dataScope as { handler: (ctx: typeof ctx, params: typeof params) => Promise<void> }).handler(ctx, params);
169
+
170
+ expect(resource.addFilterGroup).toHaveBeenCalledWith('table-1', {
171
+ $and: [{ roles: { name: { $includes: '{{$nRole}}' } } }],
172
+ });
173
+ expect(resource.removeFilterGroup).not.toHaveBeenCalled();
174
+ });
175
+
176
+ it('setTargetDataScope handler preserves current role as server-side variable', async () => {
177
+ const resource = {
178
+ addFilterGroup: vi.fn(),
179
+ removeFilterGroup: vi.fn(),
180
+ hasData: vi.fn(() => false),
181
+ refresh: vi.fn(),
182
+ };
183
+ const ctx = createSetTargetDataScopeContext(resource, { resolvedValue: ['admin', 'member'] });
184
+ const params = {
185
+ targetBlockUid: 'target-1',
186
+ filter: {
187
+ logic: '$and',
188
+ items: [{ path: 'roles.name', operator: '$includes', value: '{{ctx.role}}' }],
189
+ },
190
+ };
191
+
192
+ await (setTargetDataScope as any).handler(ctx, params);
193
+
194
+ expect(resource.addFilterGroup).toHaveBeenCalledWith('setTargetDataScope_action-1', {
195
+ $and: [{ roles: { name: { $includes: '{{$nRole}}' } } }],
196
+ });
197
+ expect(resource.removeFilterGroup).not.toHaveBeenCalled();
198
+ });
199
+
142
200
  it('setTargetDataScope handler sends null for empty variable dependencies', async () => {
143
201
  const resource = {
144
202
  addFilterGroup: vi.fn(),
@@ -12,6 +12,12 @@ import { transformFilter } from '@nocobase/utils/client';
12
12
  import _ from 'lodash';
13
13
 
14
14
  const PRESERVE_NULL = { __nocobaseDataScopeNull__: true };
15
+ const SERVER_CURRENT_ROLE_VARIABLE = '{{$nRole}}';
16
+ const CURRENT_ROLE_EXPRESSION_RE = /^\s*\{\{\s*ctx\.role\s*\}\}\s*$/;
17
+
18
+ function isCurrentRoleExpression(value: unknown) {
19
+ return typeof value === 'string' && CURRENT_ROLE_EXPRESSION_RE.test(value);
20
+ }
15
21
 
16
22
  function isPreserveNull(value: any) {
17
23
  return (
@@ -45,6 +51,10 @@ function markEmptyVariableValues(rawNode: any, resolvedNode: any) {
45
51
  }
46
52
 
47
53
  if ('path' in rawNode && 'operator' in rawNode) {
54
+ if (isCurrentRoleExpression(rawNode.value)) {
55
+ resolvedNode.value = SERVER_CURRENT_ROLE_VARIABLE;
56
+ return;
57
+ }
48
58
  if (
49
59
  isVariableExpression(rawNode.value) &&
50
60
  (resolvedNode.value === undefined || resolvedNode.value === null || resolvedNode.value === '')
@@ -113,6 +113,48 @@ export interface FieldAssignRulesEditorProps {
113
113
 
114
114
  const ALL_ASSIGN_MODES: AssignMode[] = ['default', 'override', 'assign'];
115
115
 
116
+ function cloneCascaderOption(option: FieldAssignCascaderOption): FieldAssignCascaderOption {
117
+ return {
118
+ ...option,
119
+ children: option.children?.map((child) => cloneCascaderOption(child)),
120
+ };
121
+ }
122
+
123
+ function mergeCascaderOptionInto(options: FieldAssignCascaderOption[], source: FieldAssignCascaderOption) {
124
+ const value = source?.value ? String(source.value) : '';
125
+ if (!value) return;
126
+
127
+ const existing = options.find((item) => String(item?.value || '') === value);
128
+ if (!existing) {
129
+ options.push(cloneCascaderOption(source));
130
+ return;
131
+ }
132
+
133
+ if (!existing.label && source.label) {
134
+ existing.label = source.label;
135
+ }
136
+ if (source.isLeaf === false || existing.children?.length || source.children?.length) {
137
+ existing.isLeaf = false;
138
+ }
139
+ if (source.loading) {
140
+ existing.loading = source.loading;
141
+ }
142
+ if (source.children?.length) {
143
+ existing.children = mergeCascaderOptions(existing.children || [], source.children);
144
+ }
145
+ }
146
+
147
+ function mergeCascaderOptions(
148
+ configured: FieldAssignCascaderOption[],
149
+ allFields: FieldAssignCascaderOption[],
150
+ ): FieldAssignCascaderOption[] {
151
+ const result = (Array.isArray(configured) ? configured : []).map((item) => cloneCascaderOption(item));
152
+ for (const item of Array.isArray(allFields) ? allFields : []) {
153
+ mergeCascaderOptionInto(result, item);
154
+ }
155
+ return result;
156
+ }
157
+
116
158
  function isAssignMode(mode: unknown): mode is AssignMode {
117
159
  return mode === 'default' || mode === 'override' || mode === 'assign';
118
160
  }
@@ -171,13 +213,21 @@ export const FieldAssignRulesEditor: React.FC<FieldAssignRulesEditorProps> = (pr
171
213
  },
172
214
  [normalizeItemMode, onChange],
173
215
  );
216
+
217
+ const normalizeFieldOptions = React.useCallback((options: FieldAssignRulesEditorProps['fieldOptions']) => {
218
+ return Array.isArray(options)
219
+ ? (options as FieldAssignCascaderOption[]).map((option) => cloneCascaderOption(option))
220
+ : [];
221
+ }, []);
174
222
  const [cascaderOptions, setCascaderOptions] = React.useState<FieldAssignCascaderOption[]>(() =>
175
- Array.isArray(fieldOptions) ? (fieldOptions as FieldAssignCascaderOption[]) : [],
223
+ normalizeFieldOptions(fieldOptions),
176
224
  );
225
+ const loadedCascaderOptionsRef = React.useRef<WeakSet<FieldAssignCascaderOption>>(new WeakSet());
177
226
 
178
227
  React.useEffect(() => {
179
- setCascaderOptions(Array.isArray(fieldOptions) ? (fieldOptions as FieldAssignCascaderOption[]) : []);
180
- }, [fieldOptions]);
228
+ loadedCascaderOptionsRef.current = new WeakSet();
229
+ setCascaderOptions(normalizeFieldOptions(fieldOptions));
230
+ }, [fieldOptions, normalizeFieldOptions, rootCollection, maxAssociationFieldDepth]);
181
231
 
182
232
  const getRuleKey = React.useCallback((item: FieldAssignRuleItem, index: number) => item?.key || String(index), []);
183
233
  const [titleFieldDraftMap, setTitleFieldDraftMap] = React.useState<Record<string, string | undefined>>({});
@@ -597,23 +647,26 @@ export const FieldAssignRulesEditor: React.FC<FieldAssignRulesEditorProps> = (pr
597
647
  const opts = selectedOptions || [];
598
648
  const target = opts[opts.length - 1];
599
649
  if (!target) return;
600
- if (target.children && Array.isArray(target.children) && target.children.length) return;
601
650
  if (target.isLeaf) return;
651
+ if (loadedCascaderOptionsRef.current.has(target)) return;
602
652
 
603
653
  const segments = opts.map((o) => String(o?.value)).filter(Boolean);
604
654
  const targetCollection = resolveTargetCollectionBySegments(segments);
605
655
  if (!targetCollection) {
606
- target.isLeaf = true;
656
+ target.isLeaf = !(target.children && target.children.length);
657
+ loadedCascaderOptionsRef.current.add(target);
607
658
  setCascaderOptions((prev) => [...prev]);
608
659
  return;
609
660
  }
610
661
 
611
662
  const children = buildChildrenFromCollection(targetCollection, segments.length);
612
663
  if (!children.length) {
613
- target.isLeaf = true;
664
+ target.isLeaf = !(target.children && target.children.length);
614
665
  } else {
615
- target.children = children;
666
+ target.children = mergeCascaderOptions(target.children || [], children);
667
+ target.isLeaf = false;
616
668
  }
669
+ loadedCascaderOptionsRef.current.add(target);
617
670
  setCascaderOptions((prev) => [...prev]);
618
671
  },
619
672
  [buildChildrenFromCollection, resolveTargetCollectionBySegments],
@@ -629,12 +682,14 @@ export const FieldAssignRulesEditor: React.FC<FieldAssignRulesEditorProps> = (pr
629
682
  const hit = options.find((o) => String(o?.value) === seg);
630
683
  if (!hit) return;
631
684
  selected.push(hit);
685
+ if (!hit.isLeaf && !loadedCascaderOptionsRef.current.has(hit)) {
686
+ await loadCascaderData(selected);
687
+ }
632
688
  if (hit.children?.length) {
633
689
  options = hit.children;
634
690
  continue;
635
691
  }
636
692
  if (hit.isLeaf) return;
637
- await loadCascaderData(selected);
638
693
  options = hit.children || [];
639
694
  }
640
695
  },
@@ -665,7 +720,9 @@ export const FieldAssignRulesEditor: React.FC<FieldAssignRulesEditorProps> = (pr
665
720
  await preloadCascaderPath(segs);
666
721
  }
667
722
  };
668
- void run();
723
+ run().catch((error) => {
724
+ console.warn('[FieldAssignRulesEditor] Failed to preload cascader path', error);
725
+ });
669
726
  return () => {
670
727
  cancelled = true;
671
728
  };