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 551c067 commit 1350e0e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export class EditorObject {
}
return this._name;
}

public async rename(name: string): Promise<boolean> {
let id = y3.language.keyOf(name);
let obj = JSON.parse(this.json);
obj.name = id;
let newJson = JSON.stringify(obj, null, 4);
this._name = undefined;
this._raw = undefined;
return await y3.fs.writeFile(this.uri, newJson);
}
}

async function loadObject(tableName: Table.NameCN, key: number) {
Expand Down Expand Up @@ -149,7 +159,9 @@ export class EditorTable<N extends Table.NameCN> extends vscode.Disposable {

public async delete(key: number) {
let uri = vscode.Uri.joinPath(this.uri, `${key}.json`);
await y3.fs.removeFile(uri);
await y3.fs.removeFile(uri, {
useTrash: true,
}) ?? await y3.fs.removeFile(uri);
this.changeTable('delete', key);
}

Expand Down
12 changes: 6 additions & 6 deletions src/editorTable/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ class Language extends vscode.Disposable {
this.updateFile();
}

keyOf(value: string): string {
keyOf(value: string): string | number {
if (!this._reverse) {
this._reverse = {};
for (let key in this._language) {
this._reverse[this._language[key]] = key;
}
}
if (this._reverse[value]) {
return this._reverse[value];
return parseInt(this._reverse[value]) ?? this._reverse[value];
} else {
let key = this.makeKey(value);
this.set(key, value);
this.set(key.toString(), value);
return key;
}
}

private makeKey(value: string): string {
return hash(value).toString();
private makeKey(value: string): number {
return hash(value);
}

@throttle(1000)
Expand Down Expand Up @@ -113,7 +113,7 @@ export function set(key: string | number, value: string) {
/**
* 获取中文文本对应的key,如果不存在会新建
*/
export function keyOf(value: string | number): string {
export function keyOf(value: string | number): string | number {
if (typeof value === 'number') {
value = value.toString();
}
Expand Down
23 changes: 23 additions & 0 deletions src/editorTable/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ class TreeView extends vscode.Disposable {
let table = editorTable.open(fileNode.tableName);
table.delete(fileNode.key);
});

// 重命名
vscode.commands.registerCommand("y3-helper.renameEditorTableItem", async (fileNode: FileNode) => {
if (!fileNode.object) {
return;
}
const inputOptions: vscode.InputBoxOptions = {
prompt: '修改后的新名称',
value: fileNode.object.name,
placeHolder: '新名称',
validateInput: (text: string) => {
if (text.length === 0) {
return "输入的内容为空";
}
return null;
}
};
let value = await vscode.window.showInputBox(inputOptions);
if (!value) {
return;
}
await fileNode.object.rename(value);
});
}

async refresh() {
Expand Down

0 comments on commit 1350e0e

Please sign in to comment.