Skip to content

Commit 0365048

Browse files
committed
fix:修复代码面板导致表格列项目弹出框异常关闭的bug
1 parent 11c9bde commit 0365048

File tree

3 files changed

+77
-50
lines changed

3 files changed

+77
-50
lines changed

client/packages/barda-design/src/components/popover.tsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { SuspensionBox } from "./SuspensionBox";
21
import { Popover, PopoverProps } from "antd";
3-
import { Children, cloneElement, MouseEvent, ReactNode, useEffect, useState } from "react";
4-
import styled from "styled-components";
2+
import { EditorContext } from "barda/src/comps/editorState";
53
import { ActiveTextColor, GreyTextColor } from "constants/style";
64
import { trans } from "i18n/design";
75
import { PointIcon } from "icons";
6+
import { Children, cloneElement, MouseEvent, ReactNode, useContext, useEffect, useState } from "react";
7+
import styled from "styled-components";
8+
import { SuspensionBox } from "./SuspensionBox";
89

910
const Wedge = styled.div`
1011
height: 8px;
@@ -53,13 +54,25 @@ const SimplePopover = (props: {
5354
content: JSX.Element | React.ReactNode;
5455
}) => {
5556
const { visible, setVisible } = props;
57+
const editorState = useContext(EditorContext);
5658
const contentWithBox = (
57-
<SuspensionBox
58-
title={props.title}
59-
onClose={() => setVisible?.(false)}
60-
content={props.content}
61-
/>
59+
<SuspensionBox title={props.title} onClose={() => setVisible?.(false)} content={props.content} />
6260
);
61+
62+
const handleOpenChange = (open: boolean) => {
63+
if (!open && visible) {
64+
if (editorState.isCodeEditorPanelOpen) {
65+
return;
66+
}
67+
setTimeout(() => {
68+
if (!editorState.isCodeEditorPanelOpen) {
69+
setVisible?.(false);
70+
}
71+
}, 100);
72+
} else {
73+
setVisible?.(open);
74+
}
75+
};
6376
return (
6477
<Popover
6578
align={{
@@ -69,7 +82,7 @@ const SimplePopover = (props: {
6982
content={contentWithBox}
7083
trigger="click"
7184
open={visible}
72-
onOpenChange={setVisible}
85+
onOpenChange={handleOpenChange}
7386
placement="left"
7487
overlayStyle={{ width: "310px" }}
7588
>
@@ -119,7 +132,11 @@ const CustomPopover = (props: {
119132
);
120133
};
121134

122-
export type EditPopoverItemType = { text: ReactNode; onClick: () => void; type?: "delete" };
135+
export type EditPopoverItemType = {
136+
text: ReactNode;
137+
onClick: () => void;
138+
type?: "delete";
139+
};
123140

124141
export interface EditPopoverProps extends PopoverProps {
125142
children?: React.ReactElement;
@@ -178,9 +195,7 @@ const EditPopover = (props: EditPopoverProps) => {
178195
hide();
179196
}}
180197
>
181-
<HandleText $color={item.type === "delete" ? "#F73131" : "#333333"}>
182-
{item.text}
183-
</HandleText>
198+
<HandleText $color={item.type === "delete" ? "#F73131" : "#333333"}>{item.text}</HandleText>
184199
</Handle>
185200
))}
186201
{add && (
@@ -244,4 +259,4 @@ const EditPopover = (props: EditPopoverProps) => {
244259
</Popover>
245260
);
246261
};
247-
export { SimplePopover, CustomPopover, EditPopover };
262+
export { CustomPopover, EditPopover, SimplePopover };

client/packages/barda/src/comps/editorState.tsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type SelectSourceType = "editor" | "leftPanel" | "addComp" | "rightPanel"
4848

4949
/**
5050
* *所有编辑器状态都放置在此处,并且仍然是不可变的。
51-
*
51+
*
5252
* 注意:
5353
* 1.需要持久化的状态由comp维护,这里的状态不是持久化的。
5454
* 2.所有setter都不会更改当前editorState实例,而是生成新实例。
@@ -60,23 +60,23 @@ export type SelectSourceType = "editor" | "leftPanel" | "addComp" | "rightPanel"
6060
export class EditorState {
6161
readonly rootComp: ESRootCompType;
6262
readonly showPropertyPane: boolean = false; //显示属性面板
63-
readonly selectedCompNames: Set<string> = new Set(); //已选择的的组件名
63+
readonly selectedCompNames: Set<string> = new Set(); //已选择的的组件名
6464
readonly isDragging: boolean = false; //正在拖动标识
6565
readonly draggingCompType: string = "button"; //正在拖动的组件类型
6666
readonly forceShowGrid: boolean = false; // 强制显示网格线
6767
readonly disableInteract: boolean = false; // 禁用comp的交互(如点击按钮事件)
68-
readonly selectedBottomResName: string = ""; //选中的底部查询名
68+
readonly selectedBottomResName: string = ""; //选中的底部查询名
6969
readonly selectedBottomResType?: BottomResTypeEnum; //选中的底部查询类型
7070
readonly showResultCompName: string = "";
7171
readonly selectSource?: SelectSourceType; // the source of select type
7272
readonly isPasting: boolean = false; // 是否正在粘贴组件
73+
readonly isCodeEditorPanelOpen: boolean = false; // CodeEditorPanel是否打开
7374

7475
private __nameAndExposingInfoCache__: NameAndExposingInfo = {}; // 组件名称和暴露信息缓存
7576
private __uiCompByNameCache__: Record<string, any> = {}; // 按名称缓存的UI组件
7677

7778
private readonly setEditorState: (fn: (editorState: EditorState) => EditorState) => void;
7879

79-
8080
/**
8181
* 构造函数
8282
* @param rootComp 根组件
@@ -222,12 +222,7 @@ export class EditorState {
222222
const stateComInfoList = this.getTempStateCompInfoList();
223223
const transformerComInfoList = this.getTransformerCompInfoList();
224224
const dataResponderInfoList = this.getDataResponderInfoList();
225-
return [
226-
...queryComInfoList,
227-
...stateComInfoList,
228-
...transformerComInfoList,
229-
...dataResponderInfoList,
230-
];
225+
return [...queryComInfoList, ...stateComInfoList, ...transformerComInfoList, ...dataResponderInfoList];
231226
}
232227

233228
/**
@@ -379,12 +374,12 @@ export class EditorState {
379374

380375
/**
381376
* 获取选定的组件
382-
*
377+
*
383378
* 此方法用于从所有组件中筛选出选定的组件具体步骤如下:
384379
* 1. 获取所有组件的映射
385380
* 2. 使用lodash的pickBy方法筛选组件映射中符合选定条件的组件
386381
* 3. 筛选条件是组件的子元素名称在选定的组件名称列表中
387-
*
382+
*
388383
* @returns 返回一个对象,包含所有选定的组件
389384
*/
390385
selectedComps() {
@@ -397,10 +392,10 @@ export class EditorState {
397392

398393
/**
399394
* 选择容器组件
400-
*
395+
*
401396
* 此函数旨在确定当前选中的容器组件如果选中的组件为空,则返回当前UI组件的根组件
402397
* 如果选中的组件是容器类型,则返回该容器类型的子组件否则,返回第一个选中的组件或根组件
403-
*
398+
*
404399
* @returns {UIComponent} 当前选中的容器组件或根组件
405400
*/
406401
selectedContainer() {
@@ -419,28 +414,25 @@ export class EditorState {
419414

420415
/**
421416
* 根据组件键查找容器
422-
*
417+
*
423418
* 此方法旨在通过组件键(compKey)查找对应的容器,首先尝试在UI组件中查找,
424419
* 如果未找到,则继续在hooks组件中查找这种方式确保了查找过程的灵活性和高效性
425-
*
420+
*
426421
* @param compKey 组件的唯一键,用于标识特定的组件
427422
* @returns 返回找到的容器,如果没有找到则可能返回undefined或特定的默认值
428423
*/
429424
findContainer(compKey: string) {
430-
return (
431-
this.getUIComp().getComp()?.findContainer?.(compKey) ||
432-
this.getHooksComp().findContainer(compKey)
433-
);
425+
return this.getUIComp().getComp()?.findContainer?.(compKey) || this.getHooksComp().findContainer(compKey);
434426
}
435427

436428
/**
437429
* 查找UI父容器组件
438-
*
430+
*
439431
* 此方法旨在寻找指定组件的父容器组件它首先通过`getUIComp()`方法获取UI组件实例,
440432
* 并尝试在其上执行`findParentContainer`方法如果未找到,则通过`getHooksComp()`方法
441433
* 获取Hooks组件实例,并在其上执行相同方法这种冗余检查确保了在两个可能的组件实例中
442434
* 寻找到合适的父容器组件
443-
*
435+
*
444436
* @param compName 组件名称,用于标识需要查找父容器的目标组件
445437
* @param containerCompType 可选参数,指定父容器组件的类型如果不传入此参数,
446438
* 将返回任何类型的父容器组件
@@ -455,9 +447,9 @@ export class EditorState {
455447

456448
/**
457449
* 确定组件是否被选中,无论其是否处于多选状态。
458-
*
450+
*
459451
* 该方法主要基于组件名称检查当前状态下组件是否被选中。它首先通过 `getAllCompMap` 获取所有组件的映射,然后遍历该映射以查找名称匹配的组件。如果找到该组件且其已被选中(即名称存在于 `selectedCompNames` 中),则返回该组件。这允许快速检查组件的选中状态,而不受其他选中组件的影响。
460-
*
452+
*
461453
* @param compName 要检查的组件名称。
462454
* @returns 返回与当前组件名称对应的组件,无论其是否处于多选状态。
463455
*/
@@ -516,7 +508,7 @@ export class EditorState {
516508
this.changeStateFn((editorState) => {
517509
return {
518510
rootComp: compFn(editorState.rootComp),
519-
isPasting: false // 重置粘贴状态
511+
isPasting: false, // 重置粘贴状态
520512
};
521513
});
522514
}
@@ -547,6 +539,12 @@ export class EditorState {
547539
this.changeState({ isPasting });
548540
}
549541

542+
setCodeEditorPanelOpen(isOpen: boolean) {
543+
if (this.isCodeEditorPanelOpen !== isOpen) {
544+
this.changeState({ isCodeEditorPanelOpen: isOpen });
545+
}
546+
}
547+
550548
getUIComp() {
551549
return this.rootComp.children.ui;
552550
}

client/packages/barda/src/pages/editor/codeEditorPanel.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { PushpinFilled, PushpinOutlined } from "@ant-design/icons";
22
import { CodeEditorCloseIcon, CodeEditorOpenIcon, DragIcon } from "barda-design";
33
import { isEmpty } from "lodash";
44
import Trigger from "rc-trigger";
5-
import { ReactNode, useContext, useMemo, useRef, useState } from "react";
5+
import { ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
66
import Draggable from "react-draggable";
77
import { Resizable, ResizeCallbackData } from "react-resizable";
88
import { useWindowSize } from "react-use";
99
import styled from "styled-components";
10-
import { CompNameContext } from "../../comps/editorState";
10+
import { CompNameContext, EditorContext } from "../../comps/editorState";
1111
import { Layers } from "../../constants/Layers";
1212
import Handle from "../../layout/handler";
1313
import { getPanelStyle, savePanelStyle } from "../../util/localStorageUtil";
@@ -21,7 +21,7 @@ const Wrapper = styled.div`
2121
left: 50%;
2222
border-radius: 8px;
2323
background: #ffffff;
24-
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.3);
24+
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
2525
border: 1px solid #b6b6b6;
2626
`;
2727
const HeaderWrapper = styled.div`
@@ -101,7 +101,7 @@ export const CloseButton = styled.div`
101101

102102
const ButtonWrapper = styled.div`
103103
display: flex;
104-
`
104+
`;
105105

106106
export const CodeEditorPanel = (props: {
107107
editor: ReactNode;
@@ -125,6 +125,15 @@ export const CodeEditorPanel = (props: {
125125
const [pinned, setpinned] = useState(false);
126126

127127
const compName = useContext(CompNameContext);
128+
const editorState = useContext(EditorContext);
129+
130+
const updateEditorState = useCallback(() => {
131+
editorState.setCodeEditorPanelOpen(visible);
132+
}, [visible]);
133+
134+
useEffect(() => {
135+
updateEditorState();
136+
}, [updateEditorState]);
128137

129138
return (
130139
<Trigger
@@ -160,12 +169,12 @@ export const CodeEditorPanel = (props: {
160169
height={size.h}
161170
onResize={(event, { size }) => setSize({ w: size.width, h: size.height })}
162171
onResizeStop={(e: React.SyntheticEvent, data: ResizeCallbackData) => {
163-
const targetRect = draggableRef.current?.getBoundingClientRect()
164-
const newHeight = targetRect && targetRect?.top < 0 ? targetRect.bottom + targetRect?.top : size.h
165-
setSize({ w: size.w, h: newHeight })
166-
savePanelStyle({ ...panelStyle, codeEditor: { w: size.w, h: newHeight } })
167-
}
168-
}
172+
const targetRect = draggableRef.current?.getBoundingClientRect();
173+
const newHeight =
174+
targetRect && targetRect?.top < 0 ? targetRect.bottom + targetRect?.top : size.h;
175+
setSize({ w: size.w, h: newHeight });
176+
savePanelStyle({ ...panelStyle, codeEditor: { w: size.w, h: newHeight } });
177+
}}
169178
handle={Handle}
170179
resizeHandles={["s", "n", "w", "e", "sw", "nw", "se", "ne"]}
171180
minConstraints={[480, 360]}
@@ -183,7 +192,12 @@ export const CodeEditorPanel = (props: {
183192
<CloseButton onClick={() => setpinned(!pinned)}>
184193
{pinned ? <PushpinFilled rotate={-45} /> : <PushpinOutlined />}
185194
</CloseButton>
186-
<CloseButton onClick={() => { setVisible(false); setpinned(false) }}>
195+
<CloseButton
196+
onClick={() => {
197+
setVisible(false);
198+
setpinned(false);
199+
}}
200+
>
187201
<CodeEditorCloseIcon />
188202
</CloseButton>
189203
</ButtonWrapper>

0 commit comments

Comments
 (0)