From f27d1e1cc34b60b2a9b0ebf4894f729f3ccc6545 Mon Sep 17 00:00:00 2001 From: Pierre-Charles David Date: Tue, 29 Oct 2024 15:21:08 +0100 Subject: [PATCH] [4059] Deselect descendants of collapsed tree items Bug: https://github.com/eclipse-sirius/sirius-web/issues/4059 Signed-off-by: Pierre-Charles David --- CHANGELOG.adoc | 2 + .../src/views/explorer/ExplorerView.tsx | 47 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 40bb582ebe0..a120a23f8aa 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -59,6 +59,8 @@ This provider is in charge of getting the icon list of the representation. This was first fixed in 2022.3.0 but broken in 2024.3.0; it is now fixed again. - https://github.com/eclipse-sirius/sirius-web/issues/4280[#4280] [diagram] Fix direct edit with F2 when the palette is opened - https://github.com/eclipse-sirius/sirius-web/issues/4302[#4302] [diagram] Fix edges label flashing +- https://github.com/eclipse-sirius/sirius-web/issues/4059[#4059] [explorer] When a part of the explorer is collapsed, all items inside the collapsed sub-tree are deselected. +This prevents the automatic re-opening of the collapsed sections to reveal the selected items. === New Features diff --git a/packages/sirius-web/frontend/sirius-web-application/src/views/explorer/ExplorerView.tsx b/packages/sirius-web/frontend/sirius-web-application/src/views/explorer/ExplorerView.tsx index 0268d6811c4..64194fd68b2 100644 --- a/packages/sirius-web/frontend/sirius-web-application/src/views/explorer/ExplorerView.tsx +++ b/packages/sirius-web/frontend/sirius-web-application/src/views/explorer/ExplorerView.tsx @@ -10,9 +10,11 @@ * Contributors: * Obeo - initial API and implementation *******************************************************************************/ -import { WorkbenchViewComponentProps } from '@eclipse-sirius/sirius-components-core'; +import { useSelection, WorkbenchViewComponentProps } from '@eclipse-sirius/sirius-components-core'; import { FilterBar, + GQLTree, + GQLTreeItem, TreeFilter, TreeToolBar, TreeToolBarContext, @@ -44,8 +46,36 @@ const useStyles = makeStyles()((theme: Theme) => ({ const isTreeRefreshedEventPayload = (payload: GQLTreeEventPayload): payload is GQLTreeRefreshedEventPayload => payload && payload.__typename === 'TreeRefreshedEventPayload'; +const findTreeItemById = (items: GQLTreeItem[], id: string) => { + for (const child of items) { + if (child.id === id) { + return child; + } else if (child.hasChildren) { + const descendant = findTreeItemById(child.children, id); + if (descendant) { + return descendant; + } + } + } + return null; +}; + +const findSelectedDescendants = (tree: GQLTree, rootIds: string[], selectedIds: string[]) => { + const result = []; + for (const rootId of rootIds) { + const root = findTreeItemById(tree.children, rootId); + for (const selectedId of selectedIds) { + if (findTreeItemById(root.children, selectedId)) { + result.push(selectedId); + } + } + } + return result; +}; + export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewComponentProps) => { const { classes: styles } = useStyles(); + const { selection, setSelection } = useSelection(); const initialState: ExplorerViewState = { synchronizedWithSelection: true, @@ -146,7 +176,22 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon /> ); } + const onExpandedElementChange = (expanded: string[], maxDepth: number) => { + // Remove all descendants of collapsed elements from the selection + const expandedBefore = state.expanded[state.activeTreeDescriptionId] || []; + const collapsedElements = [...expandedBefore].filter((item) => !expanded.includes(item)); + if (collapsedElements) { + const selectedDescendants = findSelectedDescendants( + state.tree, + collapsedElements, + selection.entries.map((entry) => entry.id) + ); + setSelection({ + entries: selection.entries.filter((entry) => !selectedDescendants.includes(entry.id)), + }); + } + setState((prevState) => ({ ...prevState, expanded: {