@nocobase/flow-engine 2.1.0-alpha.3 → 2.1.0-alpha.30
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/LICENSE +201 -661
- package/README.md +79 -10
- package/lib/JSRunner.d.ts +10 -1
- package/lib/JSRunner.js +50 -5
- package/lib/ViewScopedFlowEngine.js +5 -1
- package/lib/components/FieldModelRenderer.js +2 -2
- package/lib/components/FlowModelRenderer.d.ts +3 -1
- package/lib/components/FlowModelRenderer.js +12 -6
- package/lib/components/MobilePopup.js +6 -5
- package/lib/components/dnd/gridDragPlanner.d.ts +59 -2
- package/lib/components/dnd/gridDragPlanner.js +601 -21
- package/lib/components/dnd/index.d.ts +19 -1
- package/lib/components/dnd/index.js +243 -23
- package/lib/components/settings/wrappers/component/SelectWithTitle.d.ts +2 -1
- package/lib/components/settings/wrappers/component/SelectWithTitle.js +14 -12
- package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.d.ts +3 -0
- package/lib/components/settings/wrappers/contextual/DefaultSettingsIcon.js +68 -10
- package/lib/components/settings/wrappers/contextual/FlowsFloatContextMenu.d.ts +23 -43
- package/lib/components/settings/wrappers/contextual/FlowsFloatContextMenu.js +352 -295
- package/lib/components/settings/wrappers/contextual/StepSettingsDialog.js +16 -2
- package/lib/components/settings/wrappers/contextual/useFloatToolbarPortal.d.ts +36 -0
- package/lib/components/settings/wrappers/contextual/useFloatToolbarPortal.js +274 -0
- package/lib/components/settings/wrappers/contextual/useFloatToolbarVisibility.d.ts +30 -0
- package/lib/components/settings/wrappers/contextual/useFloatToolbarVisibility.js +315 -0
- package/lib/components/subModel/AddSubModelButton.js +27 -1
- package/lib/components/subModel/index.d.ts +1 -0
- package/lib/components/subModel/index.js +19 -0
- package/lib/components/subModel/utils.d.ts +1 -1
- package/lib/components/subModel/utils.js +2 -2
- package/lib/data-source/index.d.ts +73 -0
- package/lib/data-source/index.js +211 -1
- package/lib/executor/FlowExecutor.js +31 -8
- package/lib/flowContext.d.ts +2 -0
- package/lib/flowContext.js +31 -1
- package/lib/flowEngine.d.ts +151 -1
- package/lib/flowEngine.js +389 -15
- package/lib/flowI18n.js +2 -1
- package/lib/flowSettings.d.ts +14 -6
- package/lib/flowSettings.js +34 -6
- package/lib/lazy-helper.d.ts +14 -0
- package/lib/lazy-helper.js +71 -0
- package/lib/locale/en-US.json +1 -0
- package/lib/locale/index.d.ts +2 -0
- package/lib/locale/zh-CN.json +1 -0
- package/lib/models/DisplayItemModel.d.ts +1 -1
- package/lib/models/EditableItemModel.d.ts +1 -1
- package/lib/models/FilterableItemModel.d.ts +1 -1
- package/lib/models/flowModel.d.ts +13 -10
- package/lib/models/flowModel.js +78 -18
- package/lib/provider.js +38 -23
- package/lib/reactive/observer.js +46 -16
- package/lib/runjs-context/registry.d.ts +1 -1
- package/lib/runjs-context/setup.js +20 -12
- package/lib/runjs-context/snippets/index.js +13 -2
- package/lib/runjs-context/snippets/scene/detail/set-field-style.snippet.d.ts +11 -0
- package/lib/runjs-context/snippets/scene/detail/set-field-style.snippet.js +50 -0
- package/lib/runjs-context/snippets/scene/table/set-cell-style.snippet.d.ts +11 -0
- package/lib/runjs-context/snippets/scene/table/set-cell-style.snippet.js +54 -0
- package/lib/scheduler/ModelOperationScheduler.d.ts +5 -1
- package/lib/scheduler/ModelOperationScheduler.js +3 -2
- package/lib/types.d.ts +47 -1
- package/lib/utils/createCollectionContextMeta.js +6 -2
- package/lib/utils/index.d.ts +2 -2
- package/lib/utils/index.js +4 -0
- package/lib/utils/parsePathnameToViewParams.js +1 -1
- package/lib/utils/runjsTemplateCompat.js +1 -1
- package/lib/utils/runjsValue.js +41 -11
- package/lib/utils/schema-utils.d.ts +7 -1
- package/lib/utils/schema-utils.js +19 -0
- package/lib/views/FlowView.d.ts +7 -1
- package/lib/views/runViewBeforeClose.d.ts +10 -0
- package/lib/views/runViewBeforeClose.js +45 -0
- package/lib/views/useDialog.d.ts +2 -1
- package/lib/views/useDialog.js +20 -3
- package/lib/views/useDrawer.d.ts +2 -1
- package/lib/views/useDrawer.js +20 -3
- package/lib/views/usePage.d.ts +2 -1
- package/lib/views/usePage.js +10 -3
- package/package.json +6 -5
- package/src/JSRunner.ts +68 -4
- package/src/ViewScopedFlowEngine.ts +4 -0
- package/src/__tests__/JSRunner.test.ts +27 -1
- package/src/__tests__/flow-engine.test.ts +166 -0
- package/src/__tests__/flowContext.test.ts +65 -1
- package/src/__tests__/flowEngine.modelLoaders.test.ts +245 -0
- package/src/__tests__/flowSettings.test.ts +94 -15
- package/src/__tests__/objectVariable.test.ts +24 -0
- package/src/__tests__/provider.test.tsx +24 -2
- package/src/__tests__/renderHiddenInConfig.test.tsx +6 -6
- package/src/__tests__/runjsContext.test.ts +16 -0
- package/src/__tests__/runjsContextRuntime.test.ts +2 -0
- package/src/__tests__/runjsPreprocessDefault.test.ts +23 -0
- package/src/__tests__/runjsSnippets.test.ts +21 -0
- package/src/__tests__/viewScopedFlowEngine.test.ts +3 -3
- package/src/components/FieldModelRenderer.tsx +2 -1
- package/src/components/FlowModelRenderer.tsx +18 -6
- package/src/components/MobilePopup.tsx +4 -2
- package/src/components/__tests__/FlowModelRenderer.test.tsx +65 -2
- package/src/components/__tests__/dnd.test.ts +44 -0
- package/src/components/__tests__/flow-model-render-error-fallback.test.tsx +20 -10
- package/src/components/__tests__/gridDragPlanner.test.ts +512 -3
- package/src/components/dnd/__tests__/DndProvider.test.tsx +98 -0
- package/src/components/dnd/gridDragPlanner.ts +743 -19
- package/src/components/dnd/index.tsx +291 -27
- package/src/components/settings/wrappers/component/SelectWithTitle.tsx +21 -9
- package/src/components/settings/wrappers/contextual/DefaultSettingsIcon.tsx +88 -10
- package/src/components/settings/wrappers/contextual/FlowsFloatContextMenu.tsx +487 -440
- package/src/components/settings/wrappers/contextual/StepSettingsDialog.tsx +18 -2
- package/src/components/settings/wrappers/contextual/__tests__/DefaultSettingsIcon.test.tsx +189 -3
- package/src/components/settings/wrappers/contextual/__tests__/FlowsFloatContextMenu.test.tsx +778 -0
- package/src/components/settings/wrappers/contextual/useFloatToolbarPortal.ts +360 -0
- package/src/components/settings/wrappers/contextual/useFloatToolbarVisibility.ts +361 -0
- package/src/components/subModel/AddSubModelButton.tsx +32 -2
- package/src/components/subModel/__tests__/AddSubModelButton.test.tsx +142 -32
- package/src/components/subModel/index.ts +1 -0
- package/src/components/subModel/utils.ts +1 -1
- package/src/data-source/__tests__/index.test.ts +34 -1
- package/src/data-source/index.ts +258 -2
- package/src/executor/FlowExecutor.ts +34 -9
- package/src/executor/__tests__/flowExecutor.test.ts +57 -0
- package/src/flowContext.ts +37 -3
- package/src/flowEngine.ts +445 -11
- package/src/flowI18n.ts +2 -1
- package/src/flowSettings.ts +40 -6
- package/src/lazy-helper.tsx +57 -0
- package/src/locale/en-US.json +1 -0
- package/src/locale/zh-CN.json +1 -0
- package/src/models/DisplayItemModel.tsx +1 -1
- package/src/models/EditableItemModel.tsx +1 -1
- package/src/models/FilterableItemModel.tsx +1 -1
- package/src/models/__tests__/dispatchEvent.when.test.ts +214 -0
- package/src/models/__tests__/flowModel.test.ts +19 -3
- package/src/models/flowModel.tsx +119 -33
- package/src/provider.tsx +41 -25
- package/src/reactive/__tests__/observer.test.tsx +82 -0
- package/src/reactive/observer.tsx +87 -25
- package/src/runjs-context/registry.ts +1 -1
- package/src/runjs-context/setup.ts +22 -12
- package/src/runjs-context/snippets/index.ts +12 -1
- package/src/runjs-context/snippets/scene/detail/set-field-style.snippet.ts +30 -0
- package/src/runjs-context/snippets/scene/table/set-cell-style.snippet.ts +34 -0
- package/src/scheduler/ModelOperationScheduler.ts +14 -3
- package/src/types.ts +60 -0
- package/src/utils/__tests__/createCollectionContextMeta.test.ts +48 -0
- package/src/utils/__tests__/parsePathnameToViewParams.test.ts +7 -0
- package/src/utils/__tests__/runjsValue.test.ts +11 -0
- package/src/utils/__tests__/utils.test.ts +62 -0
- package/src/utils/createCollectionContextMeta.ts +6 -2
- package/src/utils/index.ts +2 -1
- package/src/utils/parsePathnameToViewParams.ts +2 -2
- package/src/utils/runjsTemplateCompat.ts +1 -1
- package/src/utils/runjsValue.ts +50 -11
- package/src/utils/schema-utils.ts +30 -1
- package/src/views/FlowView.tsx +11 -1
- package/src/views/__tests__/runViewBeforeClose.test.ts +30 -0
- package/src/views/__tests__/useDialog.closeDestroy.test.tsx +13 -12
- package/src/views/runViewBeforeClose.ts +19 -0
- package/src/views/useDialog.tsx +25 -3
- package/src/views/useDrawer.tsx +25 -3
- package/src/views/usePage.tsx +12 -3
|
@@ -0,0 +1,360 @@
|
|
|
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 MENU_SUBMENU_POPUP_SELECTOR = '.ant-menu-submenu-popup';
|
|
15
|
+
const DRAWER_CONTENT_WRAPPER_SELECTOR = '.ant-drawer-content-wrapper';
|
|
16
|
+
const DRAWER_CONTENT_SELECTOR = '.ant-drawer-content';
|
|
17
|
+
const DRAWER_ROOT_SELECTOR = '.ant-drawer-root';
|
|
18
|
+
const MODAL_SELECTOR = '.ant-modal';
|
|
19
|
+
const MODAL_WRAP_SELECTOR = '.ant-modal-wrap';
|
|
20
|
+
const MODAL_ROOT_SELECTOR = '.ant-modal-root';
|
|
21
|
+
|
|
22
|
+
type ToolbarPortalPositioningMode = 'fixed' | 'absolute';
|
|
23
|
+
|
|
24
|
+
interface ToolbarPortalHostConfig {
|
|
25
|
+
mountElement: HTMLElement;
|
|
26
|
+
positioningElement: HTMLElement;
|
|
27
|
+
positioningMode: ToolbarPortalPositioningMode;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ToolbarPortalRenderSnapshot {
|
|
31
|
+
mountElement: HTMLElement;
|
|
32
|
+
positioningMode: ToolbarPortalPositioningMode;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ToolbarPortalRect {
|
|
36
|
+
top: number;
|
|
37
|
+
left: number;
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ToolbarPortalInset {
|
|
43
|
+
top: number;
|
|
44
|
+
left: number;
|
|
45
|
+
right: number;
|
|
46
|
+
bottom: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface UseFloatToolbarPortalOptions {
|
|
50
|
+
active: boolean;
|
|
51
|
+
containerRef: RefObject<HTMLDivElement>;
|
|
52
|
+
toolbarContainerRef: RefObject<HTMLDivElement>;
|
|
53
|
+
toolbarStyle?: CSSProperties;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface UseFloatToolbarPortalResult {
|
|
57
|
+
portalRect: ToolbarPortalRect;
|
|
58
|
+
portalRenderSnapshot: ToolbarPortalRenderSnapshot | null;
|
|
59
|
+
getPopupContainer: (triggerNode?: HTMLElement) => HTMLElement;
|
|
60
|
+
updatePortalRect: () => void;
|
|
61
|
+
schedulePortalRectUpdate: () => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const defaultPortalRect: ToolbarPortalRect = {
|
|
65
|
+
top: 0,
|
|
66
|
+
left: 0,
|
|
67
|
+
width: 0,
|
|
68
|
+
height: 0,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const getClosestElement = (hostEl: HTMLElement | null, selector: string) =>
|
|
72
|
+
hostEl?.closest(selector) as HTMLElement | null;
|
|
73
|
+
|
|
74
|
+
const createAbsolutePortalHostConfig = (element: HTMLElement): ToolbarPortalHostConfig => ({
|
|
75
|
+
mountElement: element,
|
|
76
|
+
positioningElement: element,
|
|
77
|
+
positioningMode: 'absolute',
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const popupPortalHostResolvers: Array<(hostEl: HTMLElement | null) => HTMLElement | null> = [
|
|
81
|
+
(hostEl) => getClosestElement(hostEl, MENU_SUBMENU_POPUP_SELECTOR),
|
|
82
|
+
(hostEl) => getClosestElement(hostEl, DRAWER_CONTENT_WRAPPER_SELECTOR),
|
|
83
|
+
(hostEl) => getClosestElement(hostEl, MODAL_WRAP_SELECTOR),
|
|
84
|
+
(hostEl) => getClosestElement(hostEl, MODAL_SELECTOR),
|
|
85
|
+
(hostEl) => {
|
|
86
|
+
const drawerContent = getClosestElement(hostEl, DRAWER_CONTENT_SELECTOR);
|
|
87
|
+
return drawerContent ? getClosestElement(drawerContent, DRAWER_CONTENT_WRAPPER_SELECTOR) || drawerContent : null;
|
|
88
|
+
},
|
|
89
|
+
(hostEl) => getClosestElement(hostEl, DRAWER_ROOT_SELECTOR),
|
|
90
|
+
(hostEl) => getClosestElement(hostEl, MODAL_ROOT_SELECTOR),
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
const getPopupPortalHostConfig = (hostEl: HTMLElement | null): ToolbarPortalHostConfig | null => {
|
|
94
|
+
for (const resolveHost of popupPortalHostResolvers) {
|
|
95
|
+
const popupHost = resolveHost(hostEl);
|
|
96
|
+
if (popupHost) {
|
|
97
|
+
return createAbsolutePortalHostConfig(popupHost);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return null;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const getToolbarPortalHostConfig = (hostEl: HTMLElement | null): ToolbarPortalHostConfig | null => {
|
|
105
|
+
if (typeof document === 'undefined') {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const popupRootConfig = getPopupPortalHostConfig(hostEl);
|
|
110
|
+
if (popupRootConfig) {
|
|
111
|
+
return popupRootConfig;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const appContainer = document.querySelector(APP_CONTAINER_SELECTOR) as HTMLElement | null;
|
|
115
|
+
if (appContainer) {
|
|
116
|
+
return createAbsolutePortalHostConfig(appContainer);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
mountElement: document.body,
|
|
121
|
+
positioningElement: document.body,
|
|
122
|
+
positioningMode: 'fixed',
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const parseToolbarInsetValue = (value: CSSProperties['top']) => {
|
|
127
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (typeof value === 'string') {
|
|
132
|
+
const trimmedValue = value.trim();
|
|
133
|
+
if (/^-?\d+(\.\d+)?(px)?$/.test(trimmedValue)) {
|
|
134
|
+
return Number.parseFloat(trimmedValue);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return 0;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const resolveToolbarPortalInset = (toolbarStyle?: CSSProperties): ToolbarPortalInset => {
|
|
142
|
+
return {
|
|
143
|
+
top: parseToolbarInsetValue(toolbarStyle?.top),
|
|
144
|
+
left: parseToolbarInsetValue(toolbarStyle?.left),
|
|
145
|
+
right: parseToolbarInsetValue(toolbarStyle?.right),
|
|
146
|
+
bottom: parseToolbarInsetValue(toolbarStyle?.bottom),
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const getAbsolutePositioningElement = (
|
|
151
|
+
toolbarEl: HTMLElement | null,
|
|
152
|
+
portalHostConfig: ToolbarPortalHostConfig | null,
|
|
153
|
+
) => {
|
|
154
|
+
if (!portalHostConfig || portalHostConfig.positioningMode !== 'absolute') {
|
|
155
|
+
return portalHostConfig?.positioningElement || null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const offsetParent = toolbarEl?.offsetParent;
|
|
159
|
+
if (
|
|
160
|
+
offsetParent instanceof HTMLElement &&
|
|
161
|
+
offsetParent !== document.body &&
|
|
162
|
+
offsetParent !== document.documentElement
|
|
163
|
+
) {
|
|
164
|
+
return offsetParent;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return portalHostConfig.positioningElement;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const calculatePortalRect = (
|
|
171
|
+
hostEl: HTMLElement | null,
|
|
172
|
+
portalHostConfig: ToolbarPortalHostConfig | null,
|
|
173
|
+
toolbarStyle?: CSSProperties,
|
|
174
|
+
toolbarEl?: HTMLElement | null,
|
|
175
|
+
): ToolbarPortalRect => {
|
|
176
|
+
if (!hostEl) {
|
|
177
|
+
return defaultPortalRect;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const inset = resolveToolbarPortalInset(toolbarStyle);
|
|
181
|
+
const hostRect = hostEl.getBoundingClientRect();
|
|
182
|
+
|
|
183
|
+
let rect: ToolbarPortalRect;
|
|
184
|
+
if (!portalHostConfig || portalHostConfig.positioningMode === 'fixed') {
|
|
185
|
+
rect = {
|
|
186
|
+
top: hostRect.top,
|
|
187
|
+
left: hostRect.left,
|
|
188
|
+
width: hostRect.width,
|
|
189
|
+
height: hostRect.height,
|
|
190
|
+
};
|
|
191
|
+
} else {
|
|
192
|
+
const positioningElement = getAbsolutePositioningElement(toolbarEl || null, portalHostConfig);
|
|
193
|
+
const portalHostRect =
|
|
194
|
+
positioningElement?.getBoundingClientRect() || portalHostConfig.positioningElement.getBoundingClientRect();
|
|
195
|
+
const scrollTop = positioningElement?.scrollTop ?? portalHostConfig.positioningElement.scrollTop;
|
|
196
|
+
const scrollLeft = positioningElement?.scrollLeft ?? portalHostConfig.positioningElement.scrollLeft;
|
|
197
|
+
|
|
198
|
+
rect = {
|
|
199
|
+
top: hostRect.top - portalHostRect.top + scrollTop,
|
|
200
|
+
left: hostRect.left - portalHostRect.left + scrollLeft,
|
|
201
|
+
width: hostRect.width,
|
|
202
|
+
height: hostRect.height,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
top: rect.top + inset.top,
|
|
208
|
+
left: rect.left + inset.left,
|
|
209
|
+
width: Math.max(0, rect.width - inset.left - inset.right),
|
|
210
|
+
height: Math.max(0, rect.height - inset.top - inset.bottom),
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const omitToolbarPortalInsetStyle = (toolbarStyle?: CSSProperties): CSSProperties | undefined => {
|
|
215
|
+
if (!toolbarStyle) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const nextStyle = { ...toolbarStyle };
|
|
220
|
+
delete nextStyle.top;
|
|
221
|
+
delete nextStyle.left;
|
|
222
|
+
delete nextStyle.right;
|
|
223
|
+
delete nextStyle.bottom;
|
|
224
|
+
return nextStyle;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export const useFloatToolbarPortal = ({
|
|
228
|
+
active,
|
|
229
|
+
containerRef,
|
|
230
|
+
toolbarContainerRef,
|
|
231
|
+
toolbarStyle,
|
|
232
|
+
}: UseFloatToolbarPortalOptions): UseFloatToolbarPortalResult => {
|
|
233
|
+
const [portalRect, setPortalRect] = useState<ToolbarPortalRect>(defaultPortalRect);
|
|
234
|
+
const [portalRenderSnapshot, setPortalRenderSnapshot] = useState<ToolbarPortalRenderSnapshot | null>(null);
|
|
235
|
+
const portalHostConfigRef = useRef<ToolbarPortalHostConfig | null>(null);
|
|
236
|
+
const portalRafIdRef = useRef<number | null>(null);
|
|
237
|
+
|
|
238
|
+
const updatePortalRect = useCallback(() => {
|
|
239
|
+
const hostElement = containerRef.current;
|
|
240
|
+
if (!hostElement) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const nextPortalHostConfig = getToolbarPortalHostConfig(hostElement);
|
|
245
|
+
portalHostConfigRef.current = nextPortalHostConfig;
|
|
246
|
+
setPortalRenderSnapshot((prevSnapshot) => {
|
|
247
|
+
if (!nextPortalHostConfig) {
|
|
248
|
+
return prevSnapshot === null ? prevSnapshot : null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (
|
|
252
|
+
prevSnapshot?.mountElement === nextPortalHostConfig.mountElement &&
|
|
253
|
+
prevSnapshot?.positioningMode === nextPortalHostConfig.positioningMode
|
|
254
|
+
) {
|
|
255
|
+
return prevSnapshot;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
mountElement: nextPortalHostConfig.mountElement,
|
|
260
|
+
positioningMode: nextPortalHostConfig.positioningMode,
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const nextRect = calculatePortalRect(hostElement, nextPortalHostConfig, toolbarStyle, toolbarContainerRef.current);
|
|
265
|
+
setPortalRect((prevRect) => {
|
|
266
|
+
if (
|
|
267
|
+
prevRect.top === nextRect.top &&
|
|
268
|
+
prevRect.left === nextRect.left &&
|
|
269
|
+
prevRect.width === nextRect.width &&
|
|
270
|
+
prevRect.height === nextRect.height
|
|
271
|
+
) {
|
|
272
|
+
return prevRect;
|
|
273
|
+
}
|
|
274
|
+
return nextRect;
|
|
275
|
+
});
|
|
276
|
+
}, [containerRef, toolbarContainerRef, toolbarStyle]);
|
|
277
|
+
|
|
278
|
+
const schedulePortalRectUpdate = useCallback(() => {
|
|
279
|
+
if (portalRafIdRef.current !== null) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
portalRafIdRef.current = window.requestAnimationFrame(() => {
|
|
284
|
+
portalRafIdRef.current = null;
|
|
285
|
+
updatePortalRect();
|
|
286
|
+
});
|
|
287
|
+
}, [updatePortalRect]);
|
|
288
|
+
|
|
289
|
+
const getPopupContainer = useCallback(
|
|
290
|
+
(triggerNode?: HTMLElement) => {
|
|
291
|
+
const fallbackContainer =
|
|
292
|
+
triggerNode?.ownerDocument?.body ||
|
|
293
|
+
containerRef.current?.ownerDocument?.body ||
|
|
294
|
+
(typeof document !== 'undefined' ? document.body : null);
|
|
295
|
+
|
|
296
|
+
return (portalHostConfigRef.current?.mountElement ||
|
|
297
|
+
getToolbarPortalHostConfig(triggerNode || containerRef.current)?.mountElement ||
|
|
298
|
+
fallbackContainer) as HTMLElement;
|
|
299
|
+
},
|
|
300
|
+
[containerRef],
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
useEffect(() => {
|
|
304
|
+
if (!active) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
updatePortalRect();
|
|
309
|
+
|
|
310
|
+
const handleViewportChange = () => {
|
|
311
|
+
schedulePortalRectUpdate();
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const container = containerRef.current;
|
|
315
|
+
const mountElement = portalHostConfigRef.current?.mountElement;
|
|
316
|
+
const positioningElement = portalHostConfigRef.current?.positioningElement;
|
|
317
|
+
const resizeObserver =
|
|
318
|
+
typeof ResizeObserver !== 'undefined' && container
|
|
319
|
+
? new ResizeObserver(() => {
|
|
320
|
+
schedulePortalRectUpdate();
|
|
321
|
+
})
|
|
322
|
+
: null;
|
|
323
|
+
|
|
324
|
+
if (container) {
|
|
325
|
+
resizeObserver?.observe(container);
|
|
326
|
+
}
|
|
327
|
+
if (mountElement && mountElement !== container) {
|
|
328
|
+
resizeObserver?.observe(mountElement);
|
|
329
|
+
}
|
|
330
|
+
if (positioningElement && positioningElement !== container && positioningElement !== mountElement) {
|
|
331
|
+
resizeObserver?.observe(positioningElement);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
window.addEventListener('resize', handleViewportChange);
|
|
335
|
+
window.addEventListener('scroll', handleViewportChange, true);
|
|
336
|
+
|
|
337
|
+
return () => {
|
|
338
|
+
resizeObserver?.disconnect();
|
|
339
|
+
window.removeEventListener('resize', handleViewportChange);
|
|
340
|
+
window.removeEventListener('scroll', handleViewportChange, true);
|
|
341
|
+
};
|
|
342
|
+
}, [active, containerRef, schedulePortalRectUpdate, updatePortalRect]);
|
|
343
|
+
|
|
344
|
+
useEffect(() => {
|
|
345
|
+
return () => {
|
|
346
|
+
if (portalRafIdRef.current !== null) {
|
|
347
|
+
window.cancelAnimationFrame(portalRafIdRef.current);
|
|
348
|
+
}
|
|
349
|
+
portalHostConfigRef.current = null;
|
|
350
|
+
};
|
|
351
|
+
}, []);
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
portalRect,
|
|
355
|
+
portalRenderSnapshot,
|
|
356
|
+
getPopupContainer,
|
|
357
|
+
updatePortalRect,
|
|
358
|
+
schedulePortalRectUpdate,
|
|
359
|
+
};
|
|
360
|
+
};
|