Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-4541 refactor: tree #2326

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type DatabaseObjectInfo {
type NavigatorNodeInfo {
# Node ID - generally a full path to the node from root of tree
id: ID!
# # Node URI - a unique path to a node including all parent nodes
# uri: ID! @since(version: "23.3.1")
# Node URI - a unique path to a node including all parent nodes
uri: ID! @since(version: "23.3.1")
# Node human readable name
name: String
#Node full name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
.treeNodeNested {
box-sizing: border-box;
padding-left: 8px;
display: none;

&.root {
padding: 0;
display: block;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { createDataContext } from '@cloudbeaver/core-data-context';

export const DATA_CONTEXT_NAV_NODE_ID = createDataContext<string>('nav-node-id');
1 change: 1 addition & 0 deletions webapp/packages/core-navigation-tree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './NodesManager/extensions/IObjectNavNodeProvider';
export * from './NodesManager/DBObjectResource';
export * from './NodesManager/DATA_CONTEXT_ACTIVE_NODE';
export * from './NodesManager/DATA_CONTEXT_NAV_NODE';
export * from './NodesManager/DATA_CONTEXT_NAV_NODE_ID';
export * from './NodesManager/DATA_CONTEXT_NAV_NODES';
export * from './NodesManager/NavNodeInfoResource';
export * from './NodesManager/NavNodeManagerService';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

export type TreeDataTransformer<T> = (nodeId: string, data: T) => T;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { TreeDataTransformer } from './TreeDataTransformer';

export function applyTransforms<T>(id: string, data: T, transformers?: TreeDataTransformer<T>[]) {
if (!transformers) {
return data;
}

for (const transformer of transformers) {
data = transformer(id, data);
}

return data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { INode } from '../INode';
import type { INodeState } from '../INodeState';
import type { TreeDataTransformer } from './TreeDataTransformer';

export function rootNodeStateTransformer(root: string): TreeDataTransformer<INodeState> {
return function rootNodeStateTransformer(nodeId, data) {
if (nodeId === root) {
return {
...data,
expanded: true,
};
}

return data;
};
}

export function rootNodeTransformer(root: string): TreeDataTransformer<INode> {
return function rootNodeTransformer(nodeId, data) {
if (nodeId === root) {
return {
...data,
leaf: false,
};
}

return data;
};
}
14 changes: 14 additions & 0 deletions webapp/packages/plugin-navigation-tree/src/TreeNew/INode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

export interface INode {
name: string;
tooltip?: string;
icon?: string;
leaf?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

export interface INodeComponentBaseProps {
nodeId: string;
offsetHeight: number;
}

export interface INodeComponentProps extends INodeComponentBaseProps {
childrenRenderer: React.FC<INodeComponentBaseProps>;
}

export type NodeComponent = React.FC<INodeComponentProps>;

export type INodeRenderer = (nodeId: string) => NodeComponent | null;
12 changes: 12 additions & 0 deletions webapp/packages/plugin-navigation-tree/src/TreeNew/INodeState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

export interface INodeState {
selected: boolean;
expanded: boolean;
}
51 changes: 51 additions & 0 deletions webapp/packages/plugin-navigation-tree/src/TreeNew/Node.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';
import { useContext } from 'react';

import { TreeNode } from '@cloudbeaver/core-blocks';

import { TreeContext } from './contexts/TreeContext';
import { TreeDataContext } from './contexts/TreeDataContext';
import type { NodeComponent } from './INodeRenderer';
import { NodeControl } from './NodeControl';
import { useNodeDnD } from './useNodeDnD';

export const Node: NodeComponent = observer(function Node({ nodeId, offsetHeight, childrenRenderer }) {
const tree = useContext(TreeContext)!;
const data = useContext(TreeDataContext)!;

const { expanded, selected } = data.getState(nodeId);

const dndData = useNodeDnD(nodeId, () => {
if (!selected) {
tree.selectNode(nodeId, true);
}
});

function handleOpen() {
return tree.openNode(nodeId);
}

function handleToggleExpand() {
return tree.expandNode(nodeId, !expanded);
}

function handleSelect() {
tree.selectNode(nodeId, !selected);
}

const NavigationTreeChildrenNew = childrenRenderer;

return (
<TreeNode selected={selected} expanded={expanded} onExpand={handleToggleExpand} onOpen={handleOpen} onSelect={handleSelect}>
<NodeControl ref={dndData.setTargetRef} nodeId={nodeId} />
{expanded && <NavigationTreeChildrenNew nodeId={nodeId} offsetHeight={offsetHeight + tree.getNodeHeight(nodeId)} />}
</TreeNode>
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';
import { useContext, useId } from 'react';

import { getComputed, TreeNodeNested } from '@cloudbeaver/core-blocks';

import { NodeSizeCacheContext } from './contexts/NodeSizeCacheContext';
import { TreeDataContext } from './contexts/TreeDataContext';
import { TreeVirtualizationContext } from './contexts/TreeVirtualizationContext';
import { NodeRenderer } from './NodeRenderer';

interface Props {
nodeId: string;
offsetHeight: number;
root?: boolean;
}

const OVERSCAN = 128;

function getPositionWithOverscan(position: number, forward: boolean) {
if (forward) {
return position - (position % OVERSCAN) + OVERSCAN;
}

return position - (position % OVERSCAN);
}

const NodeChildrenObserved = observer<Props>(function NodeChildren({ nodeId, offsetHeight, root }) {
const data = useContext(TreeDataContext)!;
const optimization = useContext(TreeVirtualizationContext)!;
const sizeCache = useContext(NodeSizeCacheContext)!;
const firstId = useId();
const lastId = useId();
const viewPortFrom = getComputed(() => getPositionWithOverscan(optimization.viewPort.from, false)) - offsetHeight;
const viewPortTo = getComputed(() => getPositionWithOverscan(optimization.viewPort.to, true)) - offsetHeight;

const children = data.getChildren(nodeId);

function renderChildren() {
let offset = 0;
let postFillHeight = 0;

const elements = [];

for (let i = 0; i < children.length; i++) {
const child = children[i];
const size = sizeCache.getSize(child);

if (offset + size < viewPortFrom) {
offset += size;
} else if (offset < viewPortTo) {
if (offset > 0 && elements.length === 0) {
elements.push(<div key={firstId} style={{ height: offset }} />);
}

elements.push(<NodeRenderer key={child} nodeId={child} offsetHeight={offset + offsetHeight} childrenRenderer={NodeChildrenObserved} />);
offset += size;
} else {
postFillHeight += size;
}
}

if (postFillHeight > 0) {
elements.push(<div key={lastId} style={{ height: postFillHeight }} />);
}

return elements;
}

return <TreeNodeNested root={root}>{renderChildren()}</TreeNodeNested>;
});

export const NodeChildren = NodeChildrenObserved;
36 changes: 36 additions & 0 deletions webapp/packages/plugin-navigation-tree/src/TreeNew/NodeControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';
import { forwardRef, useContext } from 'react';

import { TreeNodeControl, TreeNodeExpand, TreeNodeIcon, TreeNodeName } from '@cloudbeaver/core-blocks';

import { TreeContext } from './contexts/TreeContext';
import { TreeDataContext } from './contexts/TreeDataContext';

interface Props {
nodeId: string;
}

export const NodeControl = observer<Props, HTMLDivElement>(
forwardRef(function NodeControl({ nodeId }, ref) {
const data = useContext(TreeDataContext)!;
const tree = useContext(TreeContext)!;

const node = data.getNode(nodeId);
const height = tree.getNodeHeight(nodeId);

return (
<TreeNodeControl ref={ref} style={{ height }}>
<TreeNodeExpand leaf={node.leaf} />
<TreeNodeIcon icon={node.icon} />
<TreeNodeName title={node.tooltip}>{node.name}</TreeNodeName>
</TreeNodeControl>
);
}),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';
import { useContext } from 'react';

import { TreeContext } from './contexts/TreeContext';
import type { NodeComponent } from './INodeRenderer';
import { Node } from './Node';

export const NodeRenderer: NodeComponent = observer(function NodeRenderer(props) {
const tree = useContext(TreeContext)!;
const NodeComponent = tree.getNodeComponent(props.nodeId) ?? Node;

return <NodeComponent {...props} />;
});
Loading
Loading