@nocobase/flow-engine 2.1.0-beta.2 → 2.1.0-beta.21

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 (126) hide show
  1. package/LICENSE +201 -661
  2. package/README.md +79 -10
  3. package/lib/JSRunner.d.ts +10 -1
  4. package/lib/JSRunner.js +50 -5
  5. package/lib/ViewScopedFlowEngine.js +5 -1
  6. package/lib/components/FlowModelRenderer.d.ts +1 -1
  7. package/lib/components/FlowModelRenderer.js +10 -6
  8. package/lib/components/MobilePopup.js +6 -5
  9. package/lib/components/dnd/gridDragPlanner.js +6 -2
  10. package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.d.ts +3 -0
  11. package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.js +48 -9
  12. package/lib/components/settings/wrappers/contextual/FlowsFloatContextMenu.d.ts +19 -43
  13. package/lib/components/settings/wrappers/contextual/FlowsFloatContextMenu.js +339 -295
  14. package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +16 -2
  15. package/lib/components/settings/wrappers/contextual/useFloatToolbarPortal.d.ts +36 -0
  16. package/lib/components/settings/wrappers/contextual/useFloatToolbarPortal.js +272 -0
  17. package/lib/components/settings/wrappers/contextual/useFloatToolbarVisibility.d.ts +30 -0
  18. package/lib/components/settings/wrappers/contextual/useFloatToolbarVisibility.js +247 -0
  19. package/lib/components/subModel/AddSubModelButton.js +27 -1
  20. package/lib/components/subModel/utils.js +2 -2
  21. package/lib/data-source/index.js +6 -0
  22. package/lib/executor/FlowExecutor.js +31 -8
  23. package/lib/flowContext.js +31 -1
  24. package/lib/flowEngine.d.ts +151 -1
  25. package/lib/flowEngine.js +389 -15
  26. package/lib/flowSettings.d.ts +14 -6
  27. package/lib/flowSettings.js +34 -6
  28. package/lib/lazy-helper.d.ts +14 -0
  29. package/lib/lazy-helper.js +71 -0
  30. package/lib/locale/en-US.json +1 -0
  31. package/lib/locale/index.d.ts +2 -0
  32. package/lib/locale/zh-CN.json +1 -0
  33. package/lib/models/flowModel.d.ts +2 -1
  34. package/lib/models/flowModel.js +28 -9
  35. package/lib/reactive/observer.js +46 -16
  36. package/lib/runjs-context/registry.d.ts +1 -1
  37. package/lib/runjs-context/setup.js +20 -12
  38. package/lib/runjs-context/snippets/index.js +13 -2
  39. package/lib/runjs-context/snippets/scene/detail/set-field-style.snippet.d.ts +11 -0
  40. package/lib/runjs-context/snippets/scene/detail/set-field-style.snippet.js +50 -0
  41. package/lib/runjs-context/snippets/scene/table/set-cell-style.snippet.d.ts +11 -0
  42. package/lib/runjs-context/snippets/scene/table/set-cell-style.snippet.js +54 -0
  43. package/lib/scheduler/ModelOperationScheduler.d.ts +5 -1
  44. package/lib/scheduler/ModelOperationScheduler.js +3 -2
  45. package/lib/types.d.ts +47 -1
  46. package/lib/utils/index.d.ts +2 -2
  47. package/lib/utils/index.js +4 -0
  48. package/lib/utils/parsePathnameToViewParams.js +1 -1
  49. package/lib/utils/runjsTemplateCompat.js +1 -1
  50. package/lib/utils/runjsValue.js +41 -11
  51. package/lib/utils/schema-utils.d.ts +7 -1
  52. package/lib/utils/schema-utils.js +19 -0
  53. package/lib/views/FlowView.d.ts +7 -1
  54. package/lib/views/runViewBeforeClose.d.ts +10 -0
  55. package/lib/views/runViewBeforeClose.js +45 -0
  56. package/lib/views/useDialog.d.ts +2 -1
  57. package/lib/views/useDialog.js +20 -3
  58. package/lib/views/useDrawer.d.ts +2 -1
  59. package/lib/views/useDrawer.js +20 -3
  60. package/lib/views/usePage.d.ts +2 -1
  61. package/lib/views/usePage.js +10 -3
  62. package/package.json +6 -5
  63. package/src/JSRunner.ts +68 -4
  64. package/src/ViewScopedFlowEngine.ts +4 -0
  65. package/src/__tests__/JSRunner.test.ts +27 -1
  66. package/src/__tests__/flow-engine.test.ts +166 -0
  67. package/src/__tests__/flowContext.test.ts +65 -1
  68. package/src/__tests__/flowEngine.modelLoaders.test.ts +245 -0
  69. package/src/__tests__/flowSettings.test.ts +94 -15
  70. package/src/__tests__/renderHiddenInConfig.test.tsx +6 -6
  71. package/src/__tests__/runjsContext.test.ts +16 -0
  72. package/src/__tests__/runjsContextRuntime.test.ts +2 -0
  73. package/src/__tests__/runjsPreprocessDefault.test.ts +23 -0
  74. package/src/__tests__/runjsSnippets.test.ts +21 -0
  75. package/src/__tests__/viewScopedFlowEngine.test.ts +3 -3
  76. package/src/components/FlowModelRenderer.tsx +12 -6
  77. package/src/components/MobilePopup.tsx +4 -2
  78. package/src/components/__tests__/FlowModelRenderer.test.tsx +65 -2
  79. package/src/components/__tests__/flow-model-render-error-fallback.test.tsx +20 -10
  80. package/src/components/__tests__/gridDragPlanner.test.ts +88 -0
  81. package/src/components/dnd/gridDragPlanner.ts +8 -2
  82. package/src/components/settings/wrappers/contextual/DefaultSettingsIcon.tsx +63 -9
  83. package/src/components/settings/wrappers/contextual/FlowsFloatContextMenu.tsx +468 -440
  84. package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +18 -2
  85. package/src/components/settings/wrappers/contextual/__tests__/DefaultSettingsIcon.test.tsx +95 -0
  86. package/src/components/settings/wrappers/contextual/__tests__/FlowsFloatContextMenu.test.tsx +609 -0
  87. package/src/components/settings/wrappers/contextual/useFloatToolbarPortal.ts +358 -0
  88. package/src/components/settings/wrappers/contextual/useFloatToolbarVisibility.ts +281 -0
  89. package/src/components/subModel/AddSubModelButton.tsx +32 -2
  90. package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +142 -32
  91. package/src/components/subModel/utils.ts +1 -1
  92. package/src/data-source/index.ts +6 -0
  93. package/src/executor/FlowExecutor.ts +34 -9
  94. package/src/executor/__tests__/flowExecutor.test.ts +57 -0
  95. package/src/flowContext.ts +35 -3
  96. package/src/flowEngine.ts +445 -11
  97. package/src/flowSettings.ts +40 -6
  98. package/src/lazy-helper.tsx +57 -0
  99. package/src/locale/en-US.json +1 -0
  100. package/src/locale/zh-CN.json +1 -0
  101. package/src/models/__tests__/dispatchEvent.when.test.ts +214 -0
  102. package/src/models/flowModel.tsx +31 -10
  103. package/src/reactive/__tests__/observer.test.tsx +82 -0
  104. package/src/reactive/observer.tsx +87 -25
  105. package/src/runjs-context/registry.ts +1 -1
  106. package/src/runjs-context/setup.ts +22 -12
  107. package/src/runjs-context/snippets/index.ts +12 -1
  108. package/src/runjs-context/snippets/scene/detail/set-field-style.snippet.ts +30 -0
  109. package/src/runjs-context/snippets/scene/table/set-cell-style.snippet.ts +34 -0
  110. package/src/scheduler/ModelOperationScheduler.ts +14 -3
  111. package/src/types.ts +60 -0
  112. package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
  113. package/src/utils/__tests__/runjsValue.test.ts +11 -0
  114. package/src/utils/__tests__/utils.test.ts +62 -0
  115. package/src/utils/index.ts +2 -1
  116. package/src/utils/parsePathnameToViewParams.ts +2 -2
  117. package/src/utils/runjsTemplateCompat.ts +1 -1
  118. package/src/utils/runjsValue.ts +50 -11
  119. package/src/utils/schema-utils.ts +30 -1
  120. package/src/views/FlowView.tsx +11 -1
  121. package/src/views/__tests__/runViewBeforeClose.test.ts +30 -0
  122. package/src/views/__tests__/useDialog.closeDestroy.test.tsx +13 -12
  123. package/src/views/runViewBeforeClose.ts +19 -0
  124. package/src/views/useDialog.tsx +25 -3
  125. package/src/views/useDrawer.tsx +25 -3
  126. package/src/views/usePage.tsx +12 -3
@@ -0,0 +1,358 @@
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 { useCallback, useEffect, useRef, useState } from 'react';
11
+ import type { CSSProperties, RefObject } from 'react';
12
+
13
+ const APP_CONTAINER_SELECTOR = '#nocobase-app-container';
14
+ const DRAWER_CONTENT_WRAPPER_SELECTOR = '.ant-drawer-content-wrapper';
15
+ const DRAWER_CONTENT_SELECTOR = '.ant-drawer-content';
16
+ const DRAWER_ROOT_SELECTOR = '.ant-drawer-root';
17
+ const MODAL_SELECTOR = '.ant-modal';
18
+ const MODAL_WRAP_SELECTOR = '.ant-modal-wrap';
19
+ const MODAL_ROOT_SELECTOR = '.ant-modal-root';
20
+
21
+ type ToolbarPortalPositioningMode = 'fixed' | 'absolute';
22
+
23
+ interface ToolbarPortalHostConfig {
24
+ mountElement: HTMLElement;
25
+ positioningElement: HTMLElement;
26
+ positioningMode: ToolbarPortalPositioningMode;
27
+ }
28
+
29
+ export interface ToolbarPortalRenderSnapshot {
30
+ mountElement: HTMLElement;
31
+ positioningMode: ToolbarPortalPositioningMode;
32
+ }
33
+
34
+ export interface ToolbarPortalRect {
35
+ top: number;
36
+ left: number;
37
+ width: number;
38
+ height: number;
39
+ }
40
+
41
+ interface ToolbarPortalInset {
42
+ top: number;
43
+ left: number;
44
+ right: number;
45
+ bottom: number;
46
+ }
47
+
48
+ interface UseFloatToolbarPortalOptions {
49
+ active: boolean;
50
+ containerRef: RefObject<HTMLDivElement>;
51
+ toolbarContainerRef: RefObject<HTMLDivElement>;
52
+ toolbarStyle?: CSSProperties;
53
+ }
54
+
55
+ interface UseFloatToolbarPortalResult {
56
+ portalRect: ToolbarPortalRect;
57
+ portalRenderSnapshot: ToolbarPortalRenderSnapshot | null;
58
+ getPopupContainer: (triggerNode?: HTMLElement) => HTMLElement;
59
+ updatePortalRect: () => void;
60
+ schedulePortalRectUpdate: () => void;
61
+ }
62
+
63
+ const defaultPortalRect: ToolbarPortalRect = {
64
+ top: 0,
65
+ left: 0,
66
+ width: 0,
67
+ height: 0,
68
+ };
69
+
70
+ const getClosestElement = (hostEl: HTMLElement | null, selector: string) =>
71
+ hostEl?.closest(selector) as HTMLElement | null;
72
+
73
+ const createAbsolutePortalHostConfig = (element: HTMLElement): ToolbarPortalHostConfig => ({
74
+ mountElement: element,
75
+ positioningElement: element,
76
+ positioningMode: 'absolute',
77
+ });
78
+
79
+ const popupPortalHostResolvers: Array<(hostEl: HTMLElement | null) => HTMLElement | null> = [
80
+ (hostEl) => getClosestElement(hostEl, DRAWER_CONTENT_WRAPPER_SELECTOR),
81
+ (hostEl) => getClosestElement(hostEl, MODAL_WRAP_SELECTOR),
82
+ (hostEl) => getClosestElement(hostEl, MODAL_SELECTOR),
83
+ (hostEl) => {
84
+ const drawerContent = getClosestElement(hostEl, DRAWER_CONTENT_SELECTOR);
85
+ return drawerContent ? getClosestElement(drawerContent, DRAWER_CONTENT_WRAPPER_SELECTOR) || drawerContent : null;
86
+ },
87
+ (hostEl) => getClosestElement(hostEl, DRAWER_ROOT_SELECTOR),
88
+ (hostEl) => getClosestElement(hostEl, MODAL_ROOT_SELECTOR),
89
+ ];
90
+
91
+ const getPopupPortalHostConfig = (hostEl: HTMLElement | null): ToolbarPortalHostConfig | null => {
92
+ for (const resolveHost of popupPortalHostResolvers) {
93
+ const popupHost = resolveHost(hostEl);
94
+ if (popupHost) {
95
+ return createAbsolutePortalHostConfig(popupHost);
96
+ }
97
+ }
98
+
99
+ return null;
100
+ };
101
+
102
+ const getToolbarPortalHostConfig = (hostEl: HTMLElement | null): ToolbarPortalHostConfig | null => {
103
+ if (typeof document === 'undefined') {
104
+ return null;
105
+ }
106
+
107
+ const popupRootConfig = getPopupPortalHostConfig(hostEl);
108
+ if (popupRootConfig) {
109
+ return popupRootConfig;
110
+ }
111
+
112
+ const appContainer = document.querySelector(APP_CONTAINER_SELECTOR) as HTMLElement | null;
113
+ if (appContainer) {
114
+ return createAbsolutePortalHostConfig(appContainer);
115
+ }
116
+
117
+ return {
118
+ mountElement: document.body,
119
+ positioningElement: document.body,
120
+ positioningMode: 'fixed',
121
+ };
122
+ };
123
+
124
+ const parseToolbarInsetValue = (value: CSSProperties['top']) => {
125
+ if (typeof value === 'number' && Number.isFinite(value)) {
126
+ return value;
127
+ }
128
+
129
+ if (typeof value === 'string') {
130
+ const trimmedValue = value.trim();
131
+ if (/^-?\d+(\.\d+)?(px)?$/.test(trimmedValue)) {
132
+ return Number.parseFloat(trimmedValue);
133
+ }
134
+ }
135
+
136
+ return 0;
137
+ };
138
+
139
+ const resolveToolbarPortalInset = (toolbarStyle?: CSSProperties): ToolbarPortalInset => {
140
+ return {
141
+ top: parseToolbarInsetValue(toolbarStyle?.top),
142
+ left: parseToolbarInsetValue(toolbarStyle?.left),
143
+ right: parseToolbarInsetValue(toolbarStyle?.right),
144
+ bottom: parseToolbarInsetValue(toolbarStyle?.bottom),
145
+ };
146
+ };
147
+
148
+ const getAbsolutePositioningElement = (
149
+ toolbarEl: HTMLElement | null,
150
+ portalHostConfig: ToolbarPortalHostConfig | null,
151
+ ) => {
152
+ if (!portalHostConfig || portalHostConfig.positioningMode !== 'absolute') {
153
+ return portalHostConfig?.positioningElement || null;
154
+ }
155
+
156
+ const offsetParent = toolbarEl?.offsetParent;
157
+ if (
158
+ offsetParent instanceof HTMLElement &&
159
+ offsetParent !== document.body &&
160
+ offsetParent !== document.documentElement
161
+ ) {
162
+ return offsetParent;
163
+ }
164
+
165
+ return portalHostConfig.positioningElement;
166
+ };
167
+
168
+ const calculatePortalRect = (
169
+ hostEl: HTMLElement | null,
170
+ portalHostConfig: ToolbarPortalHostConfig | null,
171
+ toolbarStyle?: CSSProperties,
172
+ toolbarEl?: HTMLElement | null,
173
+ ): ToolbarPortalRect => {
174
+ if (!hostEl) {
175
+ return defaultPortalRect;
176
+ }
177
+
178
+ const inset = resolveToolbarPortalInset(toolbarStyle);
179
+ const hostRect = hostEl.getBoundingClientRect();
180
+
181
+ let rect: ToolbarPortalRect;
182
+ if (!portalHostConfig || portalHostConfig.positioningMode === 'fixed') {
183
+ rect = {
184
+ top: hostRect.top,
185
+ left: hostRect.left,
186
+ width: hostRect.width,
187
+ height: hostRect.height,
188
+ };
189
+ } else {
190
+ const positioningElement = getAbsolutePositioningElement(toolbarEl || null, portalHostConfig);
191
+ const portalHostRect =
192
+ positioningElement?.getBoundingClientRect() || portalHostConfig.positioningElement.getBoundingClientRect();
193
+ const scrollTop = positioningElement?.scrollTop ?? portalHostConfig.positioningElement.scrollTop;
194
+ const scrollLeft = positioningElement?.scrollLeft ?? portalHostConfig.positioningElement.scrollLeft;
195
+
196
+ rect = {
197
+ top: hostRect.top - portalHostRect.top + scrollTop,
198
+ left: hostRect.left - portalHostRect.left + scrollLeft,
199
+ width: hostRect.width,
200
+ height: hostRect.height,
201
+ };
202
+ }
203
+
204
+ return {
205
+ top: rect.top + inset.top,
206
+ left: rect.left + inset.left,
207
+ width: Math.max(0, rect.width - inset.left - inset.right),
208
+ height: Math.max(0, rect.height - inset.top - inset.bottom),
209
+ };
210
+ };
211
+
212
+ export const omitToolbarPortalInsetStyle = (toolbarStyle?: CSSProperties): CSSProperties | undefined => {
213
+ if (!toolbarStyle) {
214
+ return undefined;
215
+ }
216
+
217
+ const nextStyle = { ...toolbarStyle };
218
+ delete nextStyle.top;
219
+ delete nextStyle.left;
220
+ delete nextStyle.right;
221
+ delete nextStyle.bottom;
222
+ return nextStyle;
223
+ };
224
+
225
+ export const useFloatToolbarPortal = ({
226
+ active,
227
+ containerRef,
228
+ toolbarContainerRef,
229
+ toolbarStyle,
230
+ }: UseFloatToolbarPortalOptions): UseFloatToolbarPortalResult => {
231
+ const [portalRect, setPortalRect] = useState<ToolbarPortalRect>(defaultPortalRect);
232
+ const [portalRenderSnapshot, setPortalRenderSnapshot] = useState<ToolbarPortalRenderSnapshot | null>(null);
233
+ const portalHostConfigRef = useRef<ToolbarPortalHostConfig | null>(null);
234
+ const portalRafIdRef = useRef<number | null>(null);
235
+
236
+ const updatePortalRect = useCallback(() => {
237
+ const hostElement = containerRef.current;
238
+ if (!hostElement) {
239
+ return;
240
+ }
241
+
242
+ const nextPortalHostConfig = getToolbarPortalHostConfig(hostElement);
243
+ portalHostConfigRef.current = nextPortalHostConfig;
244
+ setPortalRenderSnapshot((prevSnapshot) => {
245
+ if (!nextPortalHostConfig) {
246
+ return prevSnapshot === null ? prevSnapshot : null;
247
+ }
248
+
249
+ if (
250
+ prevSnapshot?.mountElement === nextPortalHostConfig.mountElement &&
251
+ prevSnapshot?.positioningMode === nextPortalHostConfig.positioningMode
252
+ ) {
253
+ return prevSnapshot;
254
+ }
255
+
256
+ return {
257
+ mountElement: nextPortalHostConfig.mountElement,
258
+ positioningMode: nextPortalHostConfig.positioningMode,
259
+ };
260
+ });
261
+
262
+ const nextRect = calculatePortalRect(hostElement, nextPortalHostConfig, toolbarStyle, toolbarContainerRef.current);
263
+ setPortalRect((prevRect) => {
264
+ if (
265
+ prevRect.top === nextRect.top &&
266
+ prevRect.left === nextRect.left &&
267
+ prevRect.width === nextRect.width &&
268
+ prevRect.height === nextRect.height
269
+ ) {
270
+ return prevRect;
271
+ }
272
+ return nextRect;
273
+ });
274
+ }, [containerRef, toolbarContainerRef, toolbarStyle]);
275
+
276
+ const schedulePortalRectUpdate = useCallback(() => {
277
+ if (portalRafIdRef.current !== null) {
278
+ return;
279
+ }
280
+
281
+ portalRafIdRef.current = window.requestAnimationFrame(() => {
282
+ portalRafIdRef.current = null;
283
+ updatePortalRect();
284
+ });
285
+ }, [updatePortalRect]);
286
+
287
+ const getPopupContainer = useCallback(
288
+ (triggerNode?: HTMLElement) => {
289
+ const fallbackContainer =
290
+ triggerNode?.ownerDocument?.body ||
291
+ containerRef.current?.ownerDocument?.body ||
292
+ (typeof document !== 'undefined' ? document.body : null);
293
+
294
+ return (portalHostConfigRef.current?.mountElement ||
295
+ getToolbarPortalHostConfig(triggerNode || containerRef.current)?.mountElement ||
296
+ fallbackContainer) as HTMLElement;
297
+ },
298
+ [containerRef],
299
+ );
300
+
301
+ useEffect(() => {
302
+ if (!active) {
303
+ return;
304
+ }
305
+
306
+ updatePortalRect();
307
+
308
+ const handleViewportChange = () => {
309
+ schedulePortalRectUpdate();
310
+ };
311
+
312
+ const container = containerRef.current;
313
+ const mountElement = portalHostConfigRef.current?.mountElement;
314
+ const positioningElement = portalHostConfigRef.current?.positioningElement;
315
+ const resizeObserver =
316
+ typeof ResizeObserver !== 'undefined' && container
317
+ ? new ResizeObserver(() => {
318
+ schedulePortalRectUpdate();
319
+ })
320
+ : null;
321
+
322
+ if (container) {
323
+ resizeObserver?.observe(container);
324
+ }
325
+ if (mountElement && mountElement !== container) {
326
+ resizeObserver?.observe(mountElement);
327
+ }
328
+ if (positioningElement && positioningElement !== container && positioningElement !== mountElement) {
329
+ resizeObserver?.observe(positioningElement);
330
+ }
331
+
332
+ window.addEventListener('resize', handleViewportChange);
333
+ window.addEventListener('scroll', handleViewportChange, true);
334
+
335
+ return () => {
336
+ resizeObserver?.disconnect();
337
+ window.removeEventListener('resize', handleViewportChange);
338
+ window.removeEventListener('scroll', handleViewportChange, true);
339
+ };
340
+ }, [active, containerRef, schedulePortalRectUpdate, updatePortalRect]);
341
+
342
+ useEffect(() => {
343
+ return () => {
344
+ if (portalRafIdRef.current !== null) {
345
+ window.cancelAnimationFrame(portalRafIdRef.current);
346
+ }
347
+ portalHostConfigRef.current = null;
348
+ };
349
+ }, []);
350
+
351
+ return {
352
+ portalRect,
353
+ portalRenderSnapshot,
354
+ getPopupContainer,
355
+ updatePortalRect,
356
+ schedulePortalRectUpdate,
357
+ };
358
+ };
@@ -0,0 +1,281 @@
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 { useCallback, useEffect, useRef, useState } from 'react';
11
+ import type { MouseEvent as ReactMouseEvent, RefObject } from 'react';
12
+
13
+ const TOOLBAR_HIDE_DELAY = 180;
14
+ const CHILD_FLOAT_MENU_ACTIVITY_EVENT = 'nb-float-menu-child-activity';
15
+
16
+ interface UseFloatToolbarVisibilityOptions {
17
+ modelUid: string;
18
+ containerRef: RefObject<HTMLDivElement>;
19
+ toolbarContainerRef: RefObject<HTMLDivElement>;
20
+ updatePortalRect: () => void;
21
+ schedulePortalRectUpdate: () => void;
22
+ }
23
+
24
+ interface UseFloatToolbarVisibilityResult {
25
+ isToolbarVisible: boolean;
26
+ shouldRenderToolbar: boolean;
27
+ handleSettingsMenuOpenChange: (open: boolean) => void;
28
+ handleChildHover: (e: ReactMouseEvent) => void;
29
+ handleHostMouseEnter: () => void;
30
+ handleHostMouseLeave: (e: ReactMouseEvent<HTMLDivElement>) => void;
31
+ handleToolbarMouseEnter: () => void;
32
+ handleToolbarMouseLeave: (e: ReactMouseEvent<HTMLDivElement>) => void;
33
+ handleResizeDragStart: () => void;
34
+ handleResizeDragEnd: () => void;
35
+ }
36
+
37
+ const isNodeWithin = (target: EventTarget | null, container: HTMLElement | null): boolean => {
38
+ return target instanceof Node && !!container?.contains(target);
39
+ };
40
+
41
+ const getToolbarModelUidFromTarget = (target: EventTarget | null): string | null => {
42
+ if (!(target instanceof Element)) {
43
+ return null;
44
+ }
45
+
46
+ return target.closest('.nb-toolbar-container[data-model-uid]')?.getAttribute('data-model-uid') || null;
47
+ };
48
+
49
+ const isNodeWithinDescendantFloatToolbar = (
50
+ target: EventTarget | null,
51
+ container: HTMLElement | null,
52
+ currentModelUid: string,
53
+ ): boolean => {
54
+ const targetModelUid = getToolbarModelUidFromTarget(target);
55
+ if (!container || !targetModelUid || targetModelUid === currentModelUid) {
56
+ return false;
57
+ }
58
+
59
+ return Array.from(
60
+ container.querySelectorAll<HTMLElement>('[data-has-float-menu="true"][data-float-menu-model-uid]'),
61
+ ).some(
62
+ (hostElement) =>
63
+ hostElement !== container && hostElement.getAttribute('data-float-menu-model-uid') === targetModelUid,
64
+ );
65
+ };
66
+
67
+ export const useFloatToolbarVisibility = ({
68
+ modelUid,
69
+ containerRef,
70
+ toolbarContainerRef,
71
+ updatePortalRect,
72
+ schedulePortalRectUpdate,
73
+ }: UseFloatToolbarVisibilityOptions): UseFloatToolbarVisibilityResult => {
74
+ const [hideMenu, setHideMenu] = useState(false);
75
+ const [isHostHovered, setIsHostHovered] = useState(false);
76
+ const [isToolbarHovered, setIsToolbarHovered] = useState(false);
77
+ const [isDraggingToolbar, setIsDraggingToolbar] = useState(false);
78
+ const [isToolbarPinned, setIsToolbarPinned] = useState(false);
79
+ const [isHidePending, setIsHidePending] = useState(false);
80
+ const [activeChildToolbarIds, setActiveChildToolbarIds] = useState<string[]>([]);
81
+ const hideToolbarTimerRef = useRef<number | null>(null);
82
+ const reportedChildActivityToAncestorsRef = useRef(false);
83
+
84
+ const hasActiveChildToolbar = activeChildToolbarIds.length > 0;
85
+ const isToolbarVisible =
86
+ !hideMenu && !hasActiveChildToolbar && (isHostHovered || isToolbarHovered || isDraggingToolbar || isToolbarPinned);
87
+ const shouldRenderToolbar = isToolbarVisible || isToolbarPinned || isDraggingToolbar;
88
+ const isToolbarInteractionActive =
89
+ isHostHovered || isToolbarHovered || isDraggingToolbar || isToolbarPinned || isHidePending;
90
+
91
+ const clearHideToolbarTimer = useCallback(() => {
92
+ if (hideToolbarTimerRef.current !== null) {
93
+ window.clearTimeout(hideToolbarTimerRef.current);
94
+ hideToolbarTimerRef.current = null;
95
+ }
96
+ setIsHidePending(false);
97
+ }, []);
98
+
99
+ const scheduleHideToolbar = useCallback(() => {
100
+ clearHideToolbarTimer();
101
+ setIsHidePending(true);
102
+ hideToolbarTimerRef.current = window.setTimeout(() => {
103
+ hideToolbarTimerRef.current = null;
104
+ setIsHidePending(false);
105
+ if (isDraggingToolbar || isToolbarPinned) {
106
+ return;
107
+ }
108
+ setIsHostHovered(false);
109
+ setIsToolbarHovered(false);
110
+ }, TOOLBAR_HIDE_DELAY);
111
+ }, [clearHideToolbarTimer, isDraggingToolbar, isToolbarPinned]);
112
+
113
+ const handleSettingsMenuOpenChange = useCallback((open: boolean) => {
114
+ setIsToolbarPinned(open);
115
+ }, []);
116
+
117
+ useEffect(() => {
118
+ const hostElement = containerRef.current;
119
+ if (!hostElement) {
120
+ return;
121
+ }
122
+
123
+ const handleChildToolbarActivity = (event: Event) => {
124
+ const customEvent = event as CustomEvent<{ active?: boolean; modelUid?: string }>;
125
+ if (!(customEvent.target instanceof HTMLElement) || customEvent.target === hostElement) {
126
+ return;
127
+ }
128
+
129
+ const childModelUid = customEvent.detail?.modelUid;
130
+ if (!childModelUid) {
131
+ return;
132
+ }
133
+
134
+ setActiveChildToolbarIds((prevIds) => {
135
+ return customEvent.detail?.active
136
+ ? prevIds.includes(childModelUid)
137
+ ? prevIds
138
+ : [...prevIds, childModelUid]
139
+ : prevIds.filter((id) => id !== childModelUid);
140
+ });
141
+ };
142
+
143
+ hostElement.addEventListener(CHILD_FLOAT_MENU_ACTIVITY_EVENT, handleChildToolbarActivity as EventListener);
144
+ return () => {
145
+ hostElement.removeEventListener(CHILD_FLOAT_MENU_ACTIVITY_EVENT, handleChildToolbarActivity as EventListener);
146
+ };
147
+ }, [containerRef]);
148
+
149
+ useEffect(() => {
150
+ const hostElement = containerRef.current;
151
+ if (!hostElement || reportedChildActivityToAncestorsRef.current === isToolbarInteractionActive) {
152
+ return;
153
+ }
154
+
155
+ reportedChildActivityToAncestorsRef.current = isToolbarInteractionActive;
156
+ hostElement.dispatchEvent(
157
+ new CustomEvent(CHILD_FLOAT_MENU_ACTIVITY_EVENT, {
158
+ bubbles: true,
159
+ detail: { active: isToolbarInteractionActive, modelUid },
160
+ }),
161
+ );
162
+ }, [containerRef, isToolbarInteractionActive, modelUid]);
163
+
164
+ useEffect(() => {
165
+ const hostElement = containerRef.current;
166
+
167
+ return () => {
168
+ if (hostElement && reportedChildActivityToAncestorsRef.current) {
169
+ hostElement.dispatchEvent(
170
+ new CustomEvent(CHILD_FLOAT_MENU_ACTIVITY_EVENT, {
171
+ bubbles: true,
172
+ detail: { active: false, modelUid },
173
+ }),
174
+ );
175
+ reportedChildActivityToAncestorsRef.current = false;
176
+ }
177
+ clearHideToolbarTimer();
178
+ };
179
+ }, [clearHideToolbarTimer, containerRef, modelUid]);
180
+
181
+ useEffect(() => {
182
+ if (isToolbarPinned) {
183
+ clearHideToolbarTimer();
184
+ updatePortalRect();
185
+ }
186
+ }, [clearHideToolbarTimer, isToolbarPinned, updatePortalRect]);
187
+
188
+ const handleChildHover = useCallback(
189
+ (e: ReactMouseEvent) => {
190
+ const target = e.target as HTMLElement;
191
+ const childWithMenu = target.closest('[data-has-float-menu]');
192
+ const isCurrentHostTarget = !childWithMenu || childWithMenu === containerRef.current;
193
+
194
+ if (isCurrentHostTarget) {
195
+ clearHideToolbarTimer();
196
+ setIsHostHovered(true);
197
+ }
198
+
199
+ setHideMenu(!!childWithMenu && childWithMenu !== containerRef.current);
200
+ },
201
+ [clearHideToolbarTimer, containerRef],
202
+ );
203
+
204
+ const handleHostMouseEnter = useCallback(() => {
205
+ clearHideToolbarTimer();
206
+ setHideMenu(false);
207
+ updatePortalRect();
208
+ setIsHostHovered(true);
209
+ }, [clearHideToolbarTimer, updatePortalRect]);
210
+
211
+ const handleHostMouseLeave = useCallback(
212
+ (e: ReactMouseEvent<HTMLDivElement>) => {
213
+ if (isToolbarPinned) {
214
+ setIsHostHovered(false);
215
+ return;
216
+ }
217
+ if (isNodeWithin(e.relatedTarget, toolbarContainerRef.current)) {
218
+ clearHideToolbarTimer();
219
+ setIsHostHovered(false);
220
+ setIsToolbarHovered(true);
221
+ return;
222
+ }
223
+ if (isNodeWithinDescendantFloatToolbar(e.relatedTarget, containerRef.current, modelUid)) {
224
+ clearHideToolbarTimer();
225
+ setHideMenu(false);
226
+ setIsHostHovered(true);
227
+ return;
228
+ }
229
+ scheduleHideToolbar();
230
+ },
231
+ [clearHideToolbarTimer, containerRef, isToolbarPinned, modelUid, scheduleHideToolbar, toolbarContainerRef],
232
+ );
233
+
234
+ const handleToolbarMouseEnter = useCallback(() => {
235
+ clearHideToolbarTimer();
236
+ updatePortalRect();
237
+ setIsHostHovered(false);
238
+ setIsToolbarHovered(true);
239
+ }, [clearHideToolbarTimer, updatePortalRect]);
240
+
241
+ const handleToolbarMouseLeave = useCallback(
242
+ (e: ReactMouseEvent<HTMLDivElement>) => {
243
+ if (isToolbarPinned) {
244
+ setIsToolbarHovered(false);
245
+ return;
246
+ }
247
+ setIsToolbarHovered(false);
248
+ if (isNodeWithin(e.relatedTarget, containerRef.current)) {
249
+ clearHideToolbarTimer();
250
+ setIsHostHovered(true);
251
+ return;
252
+ }
253
+ scheduleHideToolbar();
254
+ },
255
+ [clearHideToolbarTimer, containerRef, isToolbarPinned, scheduleHideToolbar],
256
+ );
257
+
258
+ const handleResizeDragStart = useCallback(() => {
259
+ updatePortalRect();
260
+ setIsDraggingToolbar(true);
261
+ schedulePortalRectUpdate();
262
+ }, [schedulePortalRectUpdate, updatePortalRect]);
263
+
264
+ const handleResizeDragEnd = useCallback(() => {
265
+ setIsDraggingToolbar(false);
266
+ schedulePortalRectUpdate();
267
+ }, [schedulePortalRectUpdate]);
268
+
269
+ return {
270
+ isToolbarVisible,
271
+ shouldRenderToolbar,
272
+ handleSettingsMenuOpenChange,
273
+ handleChildHover,
274
+ handleHostMouseEnter,
275
+ handleHostMouseLeave,
276
+ handleToolbarMouseEnter,
277
+ handleToolbarMouseLeave,
278
+ handleResizeDragStart,
279
+ handleResizeDragEnd,
280
+ };
281
+ };
@@ -9,7 +9,7 @@
9
9
 
10
10
  import { Switch } from 'antd';
11
11
  import _ from 'lodash';
12
- import React, { useMemo } from 'react';
12
+ import React, { useEffect, useMemo } from 'react';
13
13
  import { FlowModelContext } from '../../flowContext';
14
14
  import { FlowModel } from '../../models';
15
15
  import { CreateModelOptions, ModelConstructor } from '../../types';
@@ -542,6 +542,22 @@ const AddSubModelButtonCore = function AddSubModelButton({
542
542
  [model, subModelKey, subModelType],
543
543
  );
544
544
 
545
+ React.useEffect(() => {
546
+ const handleSubModelChanged = () => {
547
+ setRefreshTick((x) => x + 1);
548
+ };
549
+
550
+ model.emitter?.on('onSubModelAdded', handleSubModelChanged);
551
+ model.emitter?.on('onSubModelRemoved', handleSubModelChanged);
552
+ model.emitter?.on('onSubModelReplaced', handleSubModelChanged);
553
+
554
+ return () => {
555
+ model.emitter?.off('onSubModelAdded', handleSubModelChanged);
556
+ model.emitter?.off('onSubModelRemoved', handleSubModelChanged);
557
+ model.emitter?.off('onSubModelReplaced', handleSubModelChanged);
558
+ };
559
+ }, [model]);
560
+
545
561
  // 点击处理逻辑
546
562
  const onClick = async (info: any) => {
547
563
  const clickedItem = info.originalItem || info;
@@ -594,7 +610,7 @@ const AddSubModelButtonCore = function AddSubModelButton({
594
610
  let addedModel: FlowModel | undefined;
595
611
 
596
612
  try {
597
- addedModel = model.flowEngine.createModel({
613
+ addedModel = await model.flowEngine.createModelAsync({
598
614
  ..._.cloneDeep(createOpts),
599
615
  parentId: model.uid,
600
616
  subKey: subModelKey,
@@ -651,6 +667,20 @@ const AddSubModelButtonCore = function AddSubModelButton({
651
667
  [finalItems, model, subModelKey, subModelType],
652
668
  );
653
669
 
670
+ useEffect(() => {
671
+ const handleSubModelChange = () => {
672
+ setRefreshTick((x) => x + 1);
673
+ };
674
+
675
+ model.emitter.on('onSubModelAdded', handleSubModelChange);
676
+ model.emitter.on('onSubModelRemoved', handleSubModelChange);
677
+
678
+ return () => {
679
+ model.emitter.off('onSubModelAdded', handleSubModelChange);
680
+ model.emitter.off('onSubModelRemoved', handleSubModelChange);
681
+ };
682
+ }, [model]);
683
+
654
684
  return (
655
685
  <LazyDropdown
656
686
  menu={{