From ed7322daac82d927066f9a04525f7f8a38a0ec25 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 11 Aug 2021 21:01:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E5=88=86?= =?UTF-8?q?=E7=BB=84=E7=9B=91=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/modals/GroupDetail/Panel.tsx | 63 +++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/web/src/components/modals/GroupDetail/Panel.tsx b/web/src/components/modals/GroupDetail/Panel.tsx index 97a784c8..ead57f0d 100644 --- a/web/src/components/modals/GroupDetail/Panel.tsx +++ b/web/src/components/modals/GroupDetail/Panel.tsx @@ -1,17 +1,19 @@ -import React, { useCallback, useMemo, useRef } from 'react'; +import React, { useCallback, useMemo, useRef, useState } from 'react'; import { GroupPanelType, useGroupInfo, GroupPanel as GroupPanelInfo, showToasts, + t, } from 'tailchat-shared'; -import { Tree } from 'antd'; +import { Button, Tree } from 'antd'; import type { NodeDragEventParams } from 'rc-tree/lib/contextTypes'; import type { DataNode, EventDataNode } from 'antd/lib/tree'; import type { Key } from 'rc-tree/lib/interface'; import type { AllowDrop } from 'rc-tree/lib/Tree'; import _cloneDeep from 'lodash/cloneDeep'; import _isNil from 'lodash/isNil'; +import _isEqual from 'lodash/isEqual'; /** * 将一维的面板列表构筑成立体的数组 @@ -83,12 +85,22 @@ const GroupPanelTree: React.FC = React.memo((props) => { const handleAllowDrop = useCallback( ({ dropNode, dropPosition }: Parameters[0]) => { - if (dropPosition === 0 && draggingNode.current?.isLeaf === false) { - // 不允许容器之间产生父子节点 - return false; + if (draggingNode.current?.isLeaf === true) { + // 如果正在拖拽的节点是面板 + if (dropPosition === 0) { + return !dropNode.isLeaf; + } + return true; + } else { + // 正在拖拽的节点是分组 + if (dropPosition === 0) { + // 不允许容器之间产生父子节点 + return false; + } + + // 仅允许拖拽到分组的上下 + return !dropNode.isLeaf; } - - return dropNode.isLeaf === draggingNode.current?.isLeaf; }, [] ); @@ -116,8 +128,8 @@ const GroupPanelTree: React.FC = React.memo((props) => { ); if (draggingNode.current?.isLeaf === true && info.node.isLeaf === true) { - // 如果是面板且目标也是面板 - // 则更新它的父节点id + // 如果拖拽节点是面板且目标也是面板 + // 则更新它的父节点id为目标节点的父节点id info.dragNodesKeys // 获取所有的移动节点的位置 .map((key) => newGroupPanels.findIndex((panel) => panel.id === key)) @@ -126,6 +138,21 @@ const GroupPanelTree: React.FC = React.memo((props) => { .forEach((pos) => { newGroupPanels[pos].parentId = dropGroupPanel.parentId; }); + } else if ( + draggingNode.current?.isLeaf === true && + info.dropToGap === false && + info.node.isLeaf === false + ) { + // 拖拽节点是面板拖动到组节点上 + // 则更新它的父节点id为目标节点的id + info.dragNodesKeys + // 获取所有的移动节点的位置 + .map((key) => newGroupPanels.findIndex((panel) => panel.id === key)) + // 过滤掉没找到的 + .filter((index) => index !== -1) + .forEach((pos) => { + newGroupPanels[pos].parentId = dropGroupPanel.id; + }); } if (info.node.dragOverGapTop === true) { @@ -172,14 +199,28 @@ export const GroupPanel: React.FC<{ const groupId = props.groupId; const groupInfo = useGroupInfo(groupId); const groupPanels = groupInfo?.panels ?? []; + const [editingGroupPanels, setEditingGroupPanels] = useState(groupPanels); const handleChange = useCallback((newGroupPanels: GroupPanelInfo[]) => { - console.log('newGroupPanels', newGroupPanels); + setEditingGroupPanels(newGroupPanels); }, []); + const handleSave = useCallback(() => { + console.log('editingGroupPanels', editingGroupPanels); + }, [editingGroupPanels]); + return (
- + + + {!_isEqual(groupPanels, editingGroupPanels) && ( + + )}
); });