Skip to content

Commit

Permalink
支持删除
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 3, 2024
1 parent bce1237 commit 551c067
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
55 changes: 38 additions & 17 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export class EditorTable<N extends Table.NameCN> 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) {
Expand All @@ -173,10 +179,38 @@ export class EditorTable<N extends Table.NameCN> 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<void> = new vscode.EventEmitter();

private initWatcher() {
Expand All @@ -186,34 +220,21 @@ export class EditorTable<N extends Table.NameCN> 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);
});
}

Expand Down
16 changes: 7 additions & 9 deletions src/editorTable/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,19 @@ 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;
}
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() {
Expand Down Expand Up @@ -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) => {
Expand Down
25 changes: 21 additions & 4 deletions src/tools/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
export async function removeFile(uri: vscode.Uri, relativePath?: string, options?: DeleteOptions): Promise<boolean>;
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;
Expand Down

0 comments on commit 551c067

Please sign in to comment.