@nocobase/flow-engine 2.2.0-beta.6 → 2.2.0-beta.8

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.
Files changed (51) hide show
  1. package/lib/components/MobilePopup.style.js +16 -5
  2. package/lib/components/dnd/index.js +9 -2
  3. package/lib/components/settings/wrappers/contextual/FlowsFloatContextMenu.js +86 -32
  4. package/lib/components/settings/wrappers/contextual/useFloatToolbarVisibility.js +20 -0
  5. package/lib/flowContext.d.ts +1 -1
  6. package/lib/flowContext.js +32 -8
  7. package/lib/locale/en-US.json +1 -0
  8. package/lib/locale/index.d.ts +2 -0
  9. package/lib/locale/zh-CN.json +1 -0
  10. package/lib/resources/apiResource.js +2 -1
  11. package/lib/resources/baseRecordResource.js +6 -17
  12. package/lib/resources/multiRecordResource.js +13 -3
  13. package/lib/resources/singleRecordResource.js +7 -2
  14. package/lib/utils/dataSourceDirty.d.ts +20 -0
  15. package/lib/utils/dataSourceDirty.js +139 -0
  16. package/lib/utils/dirtyAwareApiClient.d.ts +11 -0
  17. package/lib/utils/dirtyAwareApiClient.js +378 -0
  18. package/lib/utils/index.d.ts +1 -0
  19. package/lib/utils/index.js +11 -0
  20. package/lib/utils/openViewRouteState.d.ts +28 -0
  21. package/lib/utils/openViewRouteState.js +125 -0
  22. package/lib/utils/parsePathnameToViewParams.d.ts +3 -0
  23. package/lib/utils/parsePathnameToViewParams.js +18 -1
  24. package/lib/views/ViewNavigation.js +5 -0
  25. package/package.json +4 -4
  26. package/src/__tests__/flowContext.test.ts +131 -0
  27. package/src/__tests__/flowEngine.dataSourceDirty.test.ts +51 -0
  28. package/src/__tests__/runjsRuntimeFeatures.test.ts +15 -2
  29. package/src/components/MobilePopup.style.ts +22 -6
  30. package/src/components/__tests__/MobilePopup.style.test.tsx +103 -0
  31. package/src/components/dnd/index.tsx +11 -2
  32. package/src/components/settings/wrappers/contextual/FlowsFloatContextMenu.tsx +105 -35
  33. package/src/components/settings/wrappers/contextual/__tests__/FlowsFloatContextMenu.test.tsx +381 -12
  34. package/src/components/settings/wrappers/contextual/useFloatToolbarVisibility.ts +28 -0
  35. package/src/flowContext.ts +45 -8
  36. package/src/locale/en-US.json +1 -0
  37. package/src/locale/zh-CN.json +1 -0
  38. package/src/resources/apiResource.ts +2 -1
  39. package/src/resources/baseRecordResource.ts +6 -23
  40. package/src/resources/multiRecordResource.ts +13 -3
  41. package/src/resources/singleRecordResource.ts +6 -1
  42. package/src/utils/__tests__/dirtyAwareApiClient.test.ts +392 -0
  43. package/src/utils/__tests__/openViewRouteState.test.ts +40 -0
  44. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +36 -0
  45. package/src/utils/dataSourceDirty.ts +126 -0
  46. package/src/utils/dirtyAwareApiClient.ts +430 -0
  47. package/src/utils/index.ts +10 -0
  48. package/src/utils/openViewRouteState.ts +107 -0
  49. package/src/utils/parsePathnameToViewParams.ts +23 -1
  50. package/src/views/ViewNavigation.ts +6 -1
  51. package/src/views/__tests__/ViewNavigation.test.ts +15 -0
@@ -0,0 +1,107 @@
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
+ const OPEN_VIEW_ROUTE_MODES = ['drawer', 'dialog', 'embed'] as const;
11
+ const OPEN_VIEW_ROUTE_SIZES = ['small', 'medium', 'large'] as const;
12
+ const OPEN_VIEW_ROUTE_STATE_TOKEN_ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
13
+ const OPEN_VIEW_ROUTE_STATE_TOKEN_LENGTH = 8;
14
+
15
+ export type OpenViewRouteMode = (typeof OPEN_VIEW_ROUTE_MODES)[number];
16
+ export type OpenViewRouteSize = (typeof OPEN_VIEW_ROUTE_SIZES)[number];
17
+
18
+ export type OpenViewRouteState = {
19
+ mode?: OpenViewRouteMode;
20
+ size?: OpenViewRouteSize;
21
+ };
22
+
23
+ export const RUNJS_OPEN_VIEW_ROUTE_STATE = Symbol.for('nocobase.runjs.openViewRouteState');
24
+
25
+ function isOpenViewRouteMode(value: unknown): value is OpenViewRouteMode {
26
+ return typeof value === 'string' && (OPEN_VIEW_ROUTE_MODES as readonly string[]).includes(value);
27
+ }
28
+
29
+ function isOpenViewRouteSize(value: unknown): value is OpenViewRouteSize {
30
+ return typeof value === 'string' && (OPEN_VIEW_ROUTE_SIZES as readonly string[]).includes(value);
31
+ }
32
+
33
+ export function createOpenViewRouteState(input?: { mode?: unknown; size?: unknown }): OpenViewRouteState | undefined {
34
+ const state: OpenViewRouteState = {};
35
+
36
+ if (isOpenViewRouteMode(input?.mode)) {
37
+ state.mode = input.mode;
38
+ }
39
+ if (isOpenViewRouteSize(input?.size)) {
40
+ state.size = input.size;
41
+ }
42
+
43
+ return state.mode || state.size ? state : undefined;
44
+ }
45
+
46
+ function hashString(value: string) {
47
+ let hash = 2166136261;
48
+ for (let i = 0; i < value.length; i++) {
49
+ hash ^= value.charCodeAt(i);
50
+ hash = Math.imul(hash, 16777619);
51
+ }
52
+ return hash >>> 0;
53
+ }
54
+
55
+ function stateToCode(state: OpenViewRouteState) {
56
+ const modeIndex = state.mode ? OPEN_VIEW_ROUTE_MODES.indexOf(state.mode) + 1 : 0;
57
+ const sizeIndex = state.size ? OPEN_VIEW_ROUTE_SIZES.indexOf(state.size) + 1 : 0;
58
+ const code = modeIndex * 4 + sizeIndex;
59
+ return code > 0 ? code : undefined;
60
+ }
61
+
62
+ function codeToState(code: number): OpenViewRouteState | undefined {
63
+ const modeIndex = Math.floor(code / 4);
64
+ const sizeIndex = code % 4;
65
+ return createOpenViewRouteState({
66
+ mode: modeIndex ? OPEN_VIEW_ROUTE_MODES[modeIndex - 1] : undefined,
67
+ size: sizeIndex ? OPEN_VIEW_ROUTE_SIZES[sizeIndex - 1] : undefined,
68
+ });
69
+ }
70
+
71
+ function tokenForCode(viewUid: string, code: number) {
72
+ let seed = hashString(`${viewUid}:${code}`);
73
+ let token = '';
74
+ for (let i = 0; i < OPEN_VIEW_ROUTE_STATE_TOKEN_LENGTH; i++) {
75
+ seed = Math.imul(seed ^ (code + i * 17), 16777619) >>> 0;
76
+ token += OPEN_VIEW_ROUTE_STATE_TOKEN_ALPHABET[seed % OPEN_VIEW_ROUTE_STATE_TOKEN_ALPHABET.length];
77
+ }
78
+ return token;
79
+ }
80
+
81
+ export function isOpenViewRouteStateToken(value: unknown): value is string {
82
+ return (
83
+ typeof value === 'string' &&
84
+ value.length === OPEN_VIEW_ROUTE_STATE_TOKEN_LENGTH &&
85
+ [...value].every((char) => OPEN_VIEW_ROUTE_STATE_TOKEN_ALPHABET.includes(char))
86
+ );
87
+ }
88
+
89
+ export function encodeOpenViewRouteState(viewUid: string, input?: { mode?: unknown; size?: unknown }) {
90
+ const state = createOpenViewRouteState(input);
91
+ const code = state ? stateToCode(state) : undefined;
92
+ return code ? tokenForCode(viewUid, code) : undefined;
93
+ }
94
+
95
+ export function decodeOpenViewRouteState(viewUid: string, token: unknown) {
96
+ if (!isOpenViewRouteStateToken(token)) {
97
+ return undefined;
98
+ }
99
+
100
+ for (let code = 1; code < 16; code++) {
101
+ if (tokenForCode(viewUid, code) === token) {
102
+ return codeToState(code);
103
+ }
104
+ }
105
+
106
+ return undefined;
107
+ }
@@ -7,6 +7,8 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ import { decodeOpenViewRouteState, type OpenViewRouteState } from './openViewRouteState';
11
+
10
12
  export interface ViewParam {
11
13
  /** 视图唯一标识符,一般为某个 Model 实例的 uid */
12
14
  viewUid: string;
@@ -16,6 +18,8 @@ export interface ViewParam {
16
18
  filterByTk?: string | Record<string, string | number>;
17
19
  /** source Id */
18
20
  sourceId?: string;
21
+ /** RunJS ctx.openView runtime display overrides decoded from URL. */
22
+ openViewRouteState?: OpenViewRouteState;
19
23
  }
20
24
 
21
25
  export interface ParsePathnameToViewParamsOptions {
@@ -109,7 +113,25 @@ export const parsePathnameToViewParams = (
109
113
  }
110
114
  }
111
115
  // 处理参数
112
- else if (currentView && i + 1 < segments.length) {
116
+ else if (currentView) {
117
+ if (segment === 'opts') {
118
+ if (i + 1 < segments.length) {
119
+ const routeState = decodeOpenViewRouteState(currentView.viewUid, segments[i + 1]);
120
+ if (routeState) {
121
+ currentView.openViewRouteState = routeState;
122
+ }
123
+ i += 2;
124
+ } else {
125
+ i++;
126
+ }
127
+ continue;
128
+ }
129
+
130
+ if (i + 1 >= segments.length) {
131
+ i++;
132
+ continue;
133
+ }
134
+
113
135
  const rawValue = segments[i + 1];
114
136
  // 尝试对路径段进行解码
115
137
  let decoded: string = rawValue;
@@ -9,7 +9,7 @@
9
9
 
10
10
  import { FlowEngineContext } from '../flowContext';
11
11
  import { define, observable } from '../reactive';
12
- import { ViewParam as SharedViewParam } from '../utils';
12
+ import { encodeOpenViewRouteState, ViewParam as SharedViewParam } from '../utils';
13
13
 
14
14
  type ViewParams = Omit<SharedViewParam, 'viewUid'> & { viewUid?: string };
15
15
 
@@ -79,6 +79,11 @@ export function generatePathnameFromViewParams(
79
79
  // 添加视图 UID
80
80
  segments.push(viewParam.viewUid);
81
81
 
82
+ const openViewRouteStateToken = encodeOpenViewRouteState(viewParam.viewUid, viewParam.openViewRouteState);
83
+ if (openViewRouteStateToken) {
84
+ segments.push('opts', openViewRouteStateToken);
85
+ }
86
+
82
87
  // 添加参数
83
88
  if (viewParam.tabUid) {
84
89
  segments.push('tab', viewParam.tabUid);
@@ -7,6 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ import { encodeOpenViewRouteState } from '../../utils/openViewRouteState';
10
11
  import { ViewNavigation, generatePathnameFromViewParams } from '../ViewNavigation';
11
12
 
12
13
  describe('ViewNavigation', () => {
@@ -220,6 +221,20 @@ describe('generatePathnameFromViewParams', () => {
220
221
  expect(generatePathnameFromViewParams([{ viewUid: 'xxx' }, { viewUid: 'yyy' }])).toBe('/admin/xxx/view/yyy');
221
222
  });
222
223
 
224
+ it('should generate RunJS openView route state params after the matching view uid', () => {
225
+ const token = encodeOpenViewRouteState('yyy', { mode: 'dialog', size: 'large' });
226
+ if (!token) {
227
+ throw new Error('Expected openView route state token.');
228
+ }
229
+ const pathname = generatePathnameFromViewParams([
230
+ { viewUid: 'xxx' },
231
+ { viewUid: 'yyy', openViewRouteState: { mode: 'dialog', size: 'large' }, filterByTk: '1' },
232
+ ]);
233
+
234
+ expect(token).toMatch(/^[A-Za-z]{8}$/);
235
+ expect(pathname).toBe(`/admin/xxx/view/yyy/opts/${token}/filterbytk/1`);
236
+ });
237
+
223
238
  it('should generate complex path with all parameters', () => {
224
239
  expect(
225
240
  generatePathnameFromViewParams([