Skip to content

Commit

Permalink
[4059] Generalize to all TreeViews
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 Oct 29, 2024
1 parent 934ce17 commit 16c6af5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { useSelection, WorkbenchViewComponentProps } from '@eclipse-sirius/sirius-components-core';
import { WorkbenchViewComponentProps } from '@eclipse-sirius/sirius-components-core';
import {
FilterBar,
GQLTree,
GQLTreeItem,
TreeFilter,
TreeToolBar,
TreeToolBarContext,
Expand Down Expand Up @@ -46,36 +44,8 @@ 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 @@ -178,20 +148,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ const getExpandAllTreePathQuery = gql`
}
`;

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, rootId: string, selectedIds: string[]) => {
const result = [];
const root = findTreeItemById(tree.children, rootId);
for (const selectedId of selectedIds) {
if (findTreeItemById(root.children, selectedId)) {
result.push(selectedId);
}
}
return result;
};

export const TreeView = ({
editingContextId,
readOnly,
Expand All @@ -72,7 +97,7 @@ export const TreeView = ({
expanded: expanded,
maxDepth: maxDepth,
});
const { selection } = useSelection();
const { selection, setSelection } = useSelection();

const [getTreePath, { loading: treePathLoading, data: treePathData, error: treePathError }] = useLazyQuery<
GQLGetTreePathData,
Expand Down Expand Up @@ -163,9 +188,21 @@ export const TreeView = ({
const { expanded, maxDepth } = state;

if (expanded.includes(id)) {
// Actually a collapse

const newExpanded = [...expanded];
newExpanded.splice(newExpanded.indexOf(id), 1);

// Remove all descendants of collapsed element from the selection
const selectedDescendants = findSelectedDescendants(
tree,
id,
selection.entries.map((entry) => entry.id)
);
setSelection({
entries: selection.entries.filter((entry) => !selectedDescendants.includes(entry.id)),
});

setState((prevState) => ({
...prevState,
expanded: newExpanded,
Expand Down

0 comments on commit 16c6af5

Please sign in to comment.