当选择TreeView中的节点时,通常需要对节点进行某种操作,比如删除、编辑等。这时,需要为选择项关联一个命令。
首先,定义一个Command来响应编辑事件:
{
"command": "tempuploader.editTemplate",
"title": "Edit",
"icon": {
"light": "resources/light/edit.svg",
"dark": "resources/dark/edit.svg"
}
}
然后,将这个命令与选择的节点相关联,在menus中定义:
"view/item/context": [
{
"command": "tempuploader.editTemplate",
"when": "view == templateTree && viewItem == tempNode",
"group": "inline"
}
]
这里需要注意viewItem == tempNode,说明选中的节点contextvalue是tempNode,这个定义在节点的Class中:
export class TempNode extends TempNodeBasic {
constructor(
public readonly label: string,
public readonly tempName:string,
public readonly isFolder:boolean,
public children: any,
public readonly collapsibleState: vscode.TreeItemCollapsibleState
) {
super(label, tempName,isFolder,children, collapsibleState);
this.tooltip = `${this.label}`;
this.description = this.label;
}
contextValue = 'tempNode';
}
然后,在extensions中注册命令:
vscode.commands.registerCommand('tempuploader.editTemplate', (tempNode: TempNode) => {
vscode.window.showInformationMessage("编辑模板:" + tempNode.label);
}
网友评论