Skip to content

Commit

Permalink
[4059] Deselect descendants of collapsed tree items
Browse files Browse the repository at this point in the history
Bug: #4059
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid committed Nov 18, 2024
1 parent a2c4678 commit 8f4c1ff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

- https://github.com/eclipse-sirius/sirius-web/issues/1062[#1062] [explorer] It is now possible to expand or collapse items in the explorer without selecting them by clicking directly on the expand/collapse arrow icon.
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/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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 8f4c1ff

Please sign in to comment.