From 551c067a9a0c26041e236e2344adc0af59f067c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 3 Jul 2024 19:40:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editorTable/editorTable.ts | 55 +++++++++++++++++++++++----------- src/editorTable/treeView.ts | 16 +++++----- src/tools/fs.ts | 25 +++++++++++++--- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/editorTable/editorTable.ts b/src/editorTable/editorTable.ts index 195f6ae..8a91e65 100644 --- a/src/editorTable/editorTable.ts +++ b/src/editorTable/editorTable.ts @@ -147,6 +147,12 @@ export class EditorTable extends vscode.Disposable { return this._listCache; } + public async delete(key: number) { + let uri = vscode.Uri.joinPath(this.uri, `${key}.json`); + await y3.fs.removeFile(uri); + this.changeTable('delete', key); + } + private _listActions: ['create' | 'delete', number][] = []; private resortList() { if (this._listActions.length === 0) { @@ -173,10 +179,38 @@ export class EditorTable extends vscode.Disposable { } @throttle(200) - private callOnDidChange() { + private notifyChange() { this._onDidChange.fire(); } + private changeTable(action: 'create' | 'delete' | 'change', id: number) { + switch (action) { + case 'create': { + if (!this._listCache) { + return; + } + this._listActions.push(['create', id]); + break; + } + case 'delete': { + if (!this._listCache) { + return; + } + this._objectCache[id] = undefined; + this._listActions.push(['delete', id]); + break; + } + case 'change': { + if (!this._objectCache[id]) { + return; + } + this._objectCache[id] = undefined; + break; + } + } + this.notifyChange(); + } + private _onDidChange: vscode.EventEmitter = new vscode.EventEmitter(); private initWatcher() { @@ -186,34 +220,21 @@ export class EditorTable extends vscode.Disposable { if (id === undefined) { return; } - if (!this._objectCache[id]) { - return; - } - this._objectCache[id] = undefined; - this.callOnDidChange(); + this.changeTable('change', id); }); this.watcher.onDidCreate((fileUri) => { let id = getFileID(fileUri.path); if (id === undefined) { return; } - if (!this._listCache) { - return; - } - this._listActions.push(['create', id]); - this.callOnDidChange(); + this.changeTable('create', id); }); this.watcher.onDidDelete((fileUri) => { let id = getFileID(fileUri.path); if (id === undefined) { return; } - if (!this._listCache) { - return; - } - this._objectCache[id] = undefined; - this._listActions.push(['delete', id]); - this.callOnDidChange(); + this.changeTable('delete', id); }); } diff --git a/src/editorTable/treeView.ts b/src/editorTable/treeView.ts index d0b1b4a..07cc2b7 100644 --- a/src/editorTable/treeView.ts +++ b/src/editorTable/treeView.ts @@ -182,6 +182,7 @@ class TreeView extends vscode.Disposable { vscode.env.clipboard.writeText(fileNode.object.name); }); + // 复制UID vscode.commands.registerCommand("y3-helper.copyTableItemUID", (fileNode: FileNode) => { if (!fileNode.object) { return; @@ -189,6 +190,11 @@ class TreeView extends vscode.Disposable { vscode.env.clipboard.writeText(fileNode.object.key.toString()); }); + // 删除 + vscode.commands.registerCommand("y3-helper.deleteEditorTableItem", (fileNode: FileNode) => { + let table = editorTable.open(fileNode.tableName); + table.delete(fileNode.key); + }); } async refresh() { @@ -216,15 +222,7 @@ export async function init() { // 右键菜单的命令注册 - // vscode.commands.registerCommand("y3-helper.deleteEditorTableItem", (fileNode: FileNode) => { - // try { - // vscode.workspace.fs.delete(fileNode.resourceUri); - // } - // catch (error) { - // vscode.window.showErrorMessage("删除失败,错误为" + error); - // } - // //editorTableDataProvider.refresh(); - // }); + // vscode.commands.registerCommand("y3-helper.addNewEditorTableItem", async (fileNode: FileNode) => { diff --git a/src/tools/fs.ts b/src/tools/fs.ts index a533ea6..dc5e4a4 100644 --- a/src/tools/fs.ts +++ b/src/tools/fs.ts @@ -47,12 +47,29 @@ export async function writeFile(uri: vscode.Uri, ...args: any[]) { } } -export async function removeFile(uri: vscode.Uri, relativePath?: string) { - if (relativePath) { - uri = vscode.Uri.joinPath(uri, relativePath); +interface DeleteOptions { + /** + * 递归删除文件夹 + */ + recursive?: boolean; + /** + * 尝试移动到回收站 + */ + useTrash?: boolean; +} + +export async function removeFile(uri: vscode.Uri, options?: DeleteOptions): Promise; +export async function removeFile(uri: vscode.Uri, relativePath?: string, options?: DeleteOptions): Promise; +export async function removeFile(uri: vscode.Uri, ...args: any[]) { + let options: DeleteOptions | undefined; + if (typeof args[0] === 'string') { + uri = vscode.Uri.joinPath(uri, args[0]); + options = args[1]; + } else { + options = args[0]; } try { - await vscode.workspace.fs.delete(uri); + await vscode.workspace.fs.delete(uri, options); return true; } catch { return false;