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

feat: support treeview treeItem.checkboxstate #4214

Merged
merged 6 commits into from
Dec 11, 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
9 changes: 9 additions & 0 deletions packages/components/src/recycle-tree/tree/TreeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
MetadataChangeType,
TreeNodeEvent,
TreeNodeType,
TreeViewItemCheckboxInfo,
WatchEvent,
} from '../types';

Expand Down Expand Up @@ -243,6 +244,14 @@ export class TreeNode implements ITreeNode {
return this._path;
}

get checkboxInfo(): TreeViewItemCheckboxInfo | undefined {
return {
checked: false,
tooltip: '',
accessibilityInformation: this.accessibilityInformation,
};
}

get accessibilityInformation(): IAccessibilityInformation {
return {
label: this.name,
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/recycle-tree/types/tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ export interface IAccessibilityInformation {
role?: string;
}

export interface TreeViewItemCheckboxInfo {
checked: boolean;
tooltip?: string;
accessibilityInformation?: IAccessibilityInformation;
}

export interface ITreeNode {
/**
* 唯一标识id
Expand Down Expand Up @@ -41,6 +47,10 @@ export interface ITreeNode {
* 父节点,但为undefined时,表示该节点为根节点
*/
readonly parent: ICompositeTreeNode | undefined;
/**
* 节点的选择信息
*/
readonly checkboxInfo?: TreeViewItemCheckboxInfo;
/**
* 节点无障碍信息
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import cls from 'classnames';
import React, { CSSProperties, DragEvent, FC, MouseEvent, ReactNode, useCallback, useEffect, useState } from 'react';
import React, {
CSSProperties,
DragEvent,
FC,
FormEvent,
MouseEvent,
ReactNode,
useCallback,
useEffect,
useState,
} from 'react';

import { ClasslistComposite, INodeRendererProps, Loading, PromptHandle, TreeNodeType } from '@opensumi/ide-components';
import {
CheckBox,
ClasslistComposite,
INodeRendererProps,
Loading,
PromptHandle,
TreeNodeType,
} from '@opensumi/ide-components';
import { getIcon, useDesignStyles } from '@opensumi/ide-core-browser';
import { TitleActionList } from '@opensumi/ide-core-browser/lib/components/actions';
import { MenuId } from '@opensumi/ide-core-browser/lib/menu/next';
Expand All @@ -21,6 +38,7 @@ export interface ITreeViewNodeProps {
decorations?: ClasslistComposite;
onTwistierClick?: (ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode, type: TreeNodeType) => void;
onClick: (ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode, type: TreeNodeType) => void;
onChange: (item: ExtensionTreeNode | ExtensionCompositeTreeNode) => void;
onContextMenu?: (ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode, type: TreeNodeType) => void;
onDragStart?: (ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode) => void;
onDragEnter?: (ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode) => void;
Expand All @@ -36,6 +54,7 @@ export type TreeViewNodeRenderedProps = ITreeViewNodeProps & INodeRendererProps;
export const TreeViewNode: FC<TreeViewNodeRenderedProps> = ({
item,
onClick,
onChange,
onContextMenu,
itemType,
leftPadding = 8,
Expand Down Expand Up @@ -138,6 +157,14 @@ export const TreeViewNode: FC<TreeViewNodeRenderedProps> = ({
[item, onDrop],
);

const handleCheckBoxChange = useCallback(
(event: FormEvent<HTMLInputElement>) => {
onChange(item);
event.stopPropagation();
},
[item, onChange],
);

const isDirectory = itemType === TreeNodeType.CompositeTreeNode;
const paddingLeft = isDirectory
? `${defaultLeftPadding + (item.depth || 0) * (leftPadding || 0)}px`
Expand Down Expand Up @@ -167,6 +194,24 @@ export const TreeViewNode: FC<TreeViewNodeRenderedProps> = ({
<div className={cls(styles.file_icon, node.icon)} style={{ maxHeight: TREE_VIEW_NODE_HEIGHT }}></div>
);

const renderCheckbox = (node: ExtensionCompositeTreeNode | ExtensionTreeNode) => {
if (node.checkboxInfo === undefined) {
return null;
}

return (
<CheckBox
data-node-id={node.treeItemId}
readOnly
onChange={(event) => handleCheckBoxChange(event)}
checked={!!node.checkboxInfo.checked}
id={node.treeItemId}
role={node.checkboxInfo.accessibilityInformation?.role}
title={node.checkboxInfo.tooltip}
/>
);
};

const renderDisplayName = (node: ExtensionCompositeTreeNode | ExtensionTreeNode) => {
const displayName = () => {
if (node.highlights) {
Expand Down Expand Up @@ -277,6 +322,7 @@ export const TreeViewNode: FC<TreeViewNodeRenderedProps> = ({
>
<div className={cls(styles.tree_view_node_content)}>
{renderTwice(item)}
{renderCheckbox(item)}
{renderIcon(item)}
<div className={styles.tree_view_node_overflow_wrap}>
{renderDisplayName(item)}
Expand Down
14 changes: 14 additions & 0 deletions packages/extension/src/browser/components/extension-tree-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ export const ExtensionTabBarTreeView = ({
[canSelectMany, model],
);

const handleCheckBoxChange = useCallback(
(item: ExtensionTreeNode | ExtensionCompositeTreeNode) => {
const { handleCheckBoxChange } = model;
if (item) {
handleCheckBoxChange(item);
}
},
[model],
);

const handleContextMenu = useCallback(
(ev: MouseEvent, node: ExtensionTreeNode | ExtensionCompositeTreeNode) => {
const { handleContextMenu } = model;
Expand Down Expand Up @@ -201,6 +211,7 @@ export const ExtensionTabBarTreeView = ({
isVisible={isVisible}
handleTreeReady={handleTreeReady}
handleItemClicked={handleItemClicked}
handleCheckBoxChange={handleCheckBoxChange}
handleTwistierClick={handleTwistierClick}
handleContextMenu={handleContextMenu}
handleDragStart={handleDragStart}
Expand All @@ -226,6 +237,7 @@ interface TreeViewProps {
model: ExtensionTreeViewModel;
handleTreeReady(handle: IRecycleTreeHandle): void;
handleItemClicked(ev: MouseEvent, item: ExtensionTreeNode | ExtensionCompositeTreeNode, type: TreeNodeType): void;
handleCheckBoxChange(item: ExtensionTreeNode | ExtensionCompositeTreeNode): void;
handleTwistierClick(ev: MouseEvent, item: ExtensionCompositeTreeNode): void;
handleContextMenu(ev: MouseEvent, node: ExtensionTreeNode | ExtensionCompositeTreeNode): void;
handleDragStart(ev: MouseEvent, node: ExtensionTreeNode | ExtensionCompositeTreeNode): void;
Expand Down Expand Up @@ -255,6 +267,7 @@ const TreeView = memo(
dataProvider,
handleTreeReady,
handleItemClicked,
handleCheckBoxChange,
handleTwistierClick,
handleContextMenu,
handleDragStart,
Expand Down Expand Up @@ -301,6 +314,7 @@ const TreeView = memo(
itemType={props.itemType}
decorations={model.decorations.getDecorations(props.item as any)}
onClick={handleItemClicked}
onChange={handleCheckBoxChange}
onTwistierClick={handleTwistierClick}
onContextMenu={handleContextMenu}
onDragStart={handleDragStart}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Autowired, INJECTOR_TOKEN, Injectable, Injector, Optional } from '@opensumi/di';
import { ITreeNodeOrCompositeTreeNode, Tree } from '@opensumi/ide-components';
import { CompositeTreeNode, ITreeNodeOrCompositeTreeNode, Tree } from '@opensumi/ide-components';
import { IRPCProtocol } from '@opensumi/ide-connection';
import {
BinaryBuffer,
Expand Down Expand Up @@ -414,6 +414,7 @@ export class TreeViewDataProvider extends Tree {
item.contextValue || '',
item.id,
actions,
item.checkboxInfo,
item.accessibilityInformation,
expanded,
item.resourceUri,
Expand All @@ -437,6 +438,7 @@ export class TreeViewDataProvider extends Tree {
item.contextValue || '',
item.id,
actions,
item.checkboxInfo,
item.accessibilityInformation,
item.resourceUri,
);
Expand Down Expand Up @@ -586,6 +588,43 @@ export class TreeViewDataProvider extends Tree {
super.dispose();
this.treeItemId2TreeNode.clear();
}

markAsChecked(
node: ExtensionTreeNode | ExtensionCompositeTreeNode,
checked: boolean,
manageCheckboxStateManually?: boolean,
): void {
function findParentsToChange(child: ITreeNodeOrCompositeTreeNode, nodes: ITreeNodeOrCompositeTreeNode[]): void {
if (
child.parent?.checkboxInfo !== undefined &&
child.parent.checkboxInfo.checked !== checked &&
(!checked ||
!child.parent.children?.some((candidate) => candidate !== child && candidate.checkboxInfo?.checked === false))
) {
nodes.push(child.parent);
findParentsToChange(child.parent, nodes);
}
}

function findChildrenToChange(parent: ITreeNodeOrCompositeTreeNode, nodes: ITreeNodeOrCompositeTreeNode[]): void {
if (CompositeTreeNode.is(parent)) {
parent.children?.forEach((child) => {
if (child.checkboxInfo !== undefined && child.checkboxInfo.checked !== checked) {
nodes.push(child);
}
findChildrenToChange(child, nodes);
});
}
}

const nodesToChange = [node];
if (!manageCheckboxStateManually) {
findParentsToChange(node, nodesToChange);
findChildrenToChange(node, nodesToChange);
}
nodesToChange.forEach((n) => (n.checkboxInfo!.checked = checked));
this.proxy?.$checkStateChanged(this.treeViewId, [{ treeItemId: node.treeItemId, checked }]);
}
}

export class TreeViewDragAndDropController extends Disposable implements ITreeViewDragAndDropController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,4 +1083,14 @@ export class ExtensionTreeViewModel {
}
});
}

handleCheckBoxChange = async (item: ExtensionCompositeTreeNode | ExtensionTreeNode) => {
if (item) {
this.treeViewDataProvider.markAsChecked(
item,
!item.checkboxInfo!.checked,
this.treeViewOptions.manageCheckboxStateManually,
);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CompositeTreeNode, ITree, TreeNode } from '@opensumi/ide-components';
import { MenuNode } from '@opensumi/ide-core-browser/lib/menu/next';
import { IAccessibilityInformation, Uri, UriComponents, isObject, isString } from '@opensumi/ide-core-common';

import { ITreeItemLabel } from '../../../../common/vscode';
import { ITreeItemLabel, TreeViewItemCheckboxInfo } from '../../../../common/vscode';
import { ICommand } from '../../../../common/vscode/models';
import { TreeViewDataProvider } from '../main.thread.treeview';

Expand Down Expand Up @@ -58,6 +58,7 @@ export class ExtensionCompositeTreeNode extends CompositeTreeNode {
public contextValue: string = '',
public treeItemId: string = '',
public actions: MenuNode[],
private _checkboxInfo?: TreeViewItemCheckboxInfo,
private _accessibilityInformation?: IAccessibilityInformation,
expanded?: boolean,
sourceUri?: UriComponents,
Expand Down Expand Up @@ -92,6 +93,10 @@ export class ExtensionCompositeTreeNode extends CompositeTreeNode {
return this._displayName;
}

get checkboxInfo() {
return this._checkboxInfo;
}

get accessibilityInformation() {
return {
role: this._accessibilityInformation?.role || 'treeitem',
Expand Down Expand Up @@ -147,6 +152,7 @@ export class ExtensionTreeNode extends TreeNode {
public contextValue: string = '',
public treeItemId: string = '',
public actions: MenuNode[],
private _checkboxInfo?: TreeViewItemCheckboxInfo,
private _accessibilityInformation?: IAccessibilityInformation,
private sourceUri?: UriComponents,
) {
Expand Down Expand Up @@ -180,6 +186,10 @@ export class ExtensionTreeNode extends TreeNode {
return this._displayName;
}

get checkboxInfo() {
return this._checkboxInfo;
}

get accessibilityInformation() {
return {
role: this._accessibilityInformation?.role || 'treeitem',
Expand Down
25 changes: 25 additions & 0 deletions packages/extension/src/common/vscode/treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IExtHostTreeView {
$setExpanded(treeViewId: string, treeItemId: string, expanded: boolean): Promise<any>;
$setSelection(treeViewId: string, treeItemHandles: string[]): void;
$setVisible(treeViewId: string, visible: boolean): void;
$checkStateChanged(treeViewId: string, items: { treeItemId: string; checked: boolean }[]): Promise<void>;
$resolveTreeItem(treeViewId: string, treeItemId: string, token: CancellationToken): Promise<TreeViewItem | undefined>;
$handleDrop(
destinationViewId: string,
Expand Down Expand Up @@ -77,6 +78,17 @@ export interface ITreeItemLabel {
strikethrough?: boolean;
}

export interface TreeViewItemCheckboxInfo {
checked: boolean;
tooltip?: string;
accessibilityInformation?: IAccessibilityInformation;
}

export enum TreeItemCheckboxState {
Unchecked = 0,
Checked = 1,
}

export class TreeViewItem {
id: string;

Expand All @@ -98,6 +110,8 @@ export class TreeViewItem {

contextValue?: string;

checkboxInfo?: TreeViewItemCheckboxInfo;

command?: ICommand;

accessibilityInformation?: IAccessibilityInformation;
Expand All @@ -116,6 +130,12 @@ export interface TreeView<T> extends vscode.TreeView<T> {
* 当节点可见性变化时触发的事件
*/
readonly onDidChangeVisibility: Event<vscode.TreeViewVisibilityChangeEvent>;

/**
* 表示元素已被选中或未选中的事件。
*/
readonly onDidChangeCheckboxState: Event<vscode.TreeCheckboxChangeEvent<T>>;

/**
* 当节点选中时触发的事件
*/
Expand Down Expand Up @@ -174,6 +194,11 @@ export interface ViewBadge {
}

export interface TreeViewBaseOptions {
/**
* 手动管理复选框状态
*/
manageCheckboxStateManually?: boolean;

/**
* 是否展示折叠所有功能(panel上功能)
*/
Expand Down
Loading
Loading