Skip to content

Commit

Permalink
更新物编
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 2, 2024
1 parent 35b3b2e commit 73c0c41
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 37 deletions.
58 changes: 57 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@
"when": "view == y3-helper.editorTableView",
"group": "navigation"
}
],
"view/item/context": [
{
"command": "y3-helper.openFile",
"when": "view == y3-helper.editorTableView && viewItem == json",
"group": "navigation"
},
{
"command": "y3-helper.revealInFileExplorer",
"when": "view == y3-helper.editorTableView",
"group": "navigation"
},
{
"command": "y3-helper.deleteEditorTableItem",
"when": "view == y3-helper.editorTableView && viewItem == json",
"group": "modification"
},
{
"command": "y3-helper.addNewEditorTableItem",
"when": "view == y3-helper.editorTableView && viewItem == directory",
"group": "modification"
},
{
"command": "y3-helper.copyTableItemUID",
"when": "view == y3-helper.editorTableView && viewItem == json",
"group": "copy"
},
{
"command": "y3-helper.copyTableItemName",
"when": "view == y3-helper.editorTableView && viewItem == json",
"group": "copy"
}
]
},
"viewsContainers": {
Expand Down Expand Up @@ -86,13 +118,29 @@
"category": "y3-helper",
"icon": "$(refresh)"
},
{
"command": "y3-helper.addNewEditorTableItem",
"title": "Y3:新建物编项目"
},
{
"command": "y3-helper.deleteEditorTableItem",
"title": "Y3:删除物编项目"
},
{
"command": "y3-helper.copyTableItemUID",
"title": "Y3:复制物编项目UID"
},
{
"command": "y3-helper.copyTableItemName",
"title": "Y3:复制物编项目的名称"
},
{
"command": "y3-helper.downloadPresetUI",
"title": "Y3:下载预设资源(UI)"
},
{
"command": "y3-helper.addNewDataInCSV",
"title": "Y3:在CSV表格中添加新物编数据"
"title": "Y3:添加新物编数据到CSV表格中"
},
{
"command": "y3-helper.addUIDandNameToCSVfromProject",
Expand All @@ -105,6 +153,14 @@
{
"command": "y3-helper.modifyNameInCSV",
"title": "Y3:修改CSV表格中的物编项目的名称"
},
{
"command": "y3-helper.openFile",
"title": "Y3:打开文件"
},
{
"command": "y3-helper.revealInFileExplorer",
"title": "Y3:在系统文件浏览器中打开"
}
],
"configuration": {
Expand Down
4 changes: 2 additions & 2 deletions src/editorTable/CSVimporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class CSVimporter
let jsonFilePath=targetPath.fsPath+'\\'+uid+'.json';
if(!isInDirectory(targetPath.fsPath,uid+'.json')){
console.log("没有检测到对应物品的Json,从模板新建了Json文件存储物编数据:" + jsonFilePath);
let templateJson = await fs.readFileSync(path.dirname(__dirname) + "\\template\\json_template\\" + tableType + ".json");
let templateJson = await fs.readFileSync(path.join(__dirname, "../../template/json_template/" + tableType + ".json"));
await fs.writeFileSync(jsonFilePath,templateJson);
}

Expand Down Expand Up @@ -282,7 +282,7 @@ export class CSVimporter

try {
// 将更新后的数据写回文件
await fs.writeFileSync(jsonFilePath, toUnicodeIgnoreASCII(JSON.stringify(jsonData, null, 2)), 'utf8');
fs.writeFileSync(jsonFilePath, toUnicodeIgnoreASCII(JSON.stringify(jsonData, null, 2)), 'utf8');
}
catch (err)
{
Expand Down
49 changes: 38 additions & 11 deletions src/editorTable/editorTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { env } from '../env';
import { isPathValid, isJson, getFileNameByVscodeUri } from '../utility';
import { encode } from 'punycode';
import { englishPathToChinese } from '../constants';
import { EditorTableItemInfo } from './types';


export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode> {
Expand Down Expand Up @@ -79,34 +80,51 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode
if (name !== undefined && typeof name === "string") {
label = name + "(" + label.substring(0, label.length - 5) + ")";//显示为"这是一个单位(134219828)"的格式
}
let uid = editorTableJson['uid'];
if (isNaN(uid)) {
return;
}

const fileNode = new FileNode(
label,
stat.isDirectory() ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
stat.isDirectory() ? vscode.Uri.file(filePath) : vscode.Uri.file(filePath),
stat.isDirectory(),
uid,
name
);
fileNodes.push(fileNode);
}
else if (stat.isDirectory()) {
if (label in this.englishPathToChinese) {
label = this.englishPathToChinese[label];
const fileNode = new FileNode(
label,
stat.isDirectory() ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
stat.isDirectory() ? vscode.Uri.file(filePath) : vscode.Uri.file(filePath),
stat.isDirectory()
);
fileNodes.push(fileNode);
}
else {
return Promise.resolve(fileNodes);
return;
}
}
const fileNode = new FileNode(
label,
stat.isDirectory() ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
stat.isDirectory() ? vscode.Uri.file(filePath) : vscode.Uri.file(filePath),
stat.isDirectory()
);
fileNodes.push(fileNode);

});

return Promise.resolve(fileNodes);
}
}

class FileNode extends vscode.TreeItem {
export class FileNode extends vscode.TreeItem {
constructor(
public label: string,
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly resourceUri: vscode.Uri,
public readonly isDirectory: boolean
public readonly isDirectory: boolean,
public readonly uid?: number,// 物编项目的uid
public readonly name?:string // 物编项目的名称
) {
super(label, collapsibleState);
this.resourceUri = resourceUri;
Expand All @@ -116,6 +134,15 @@ class FileNode extends vscode.TreeItem {
title: '打开文件',
arguments: [resourceUri]
};
if (this.isDirectory) {
this.contextValue = 'directory';
}
else if (isJson(resourceUri.fsPath)) {
this.contextValue = 'json';
}
else {
this.contextValue = 'otherTypes';
}
}
}

Expand Down
20 changes: 17 additions & 3 deletions src/editorTable/editorTableUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,28 @@ export async function searchAllEditorTableItemInCSV(query: string):Promise< vsco
return Promise.resolve(res);
}

let editorTableItemMap: Map<number, EditorTableItemInfo> = new Map<number,EditorTableItemInfo>();

/**
* 获取项目中的所有物编数据,返回一个HashMap可通过uid查询对应物编数据
* @returns
*/
export function getEditorTableItemMap(): Map<number, EditorTableItemInfo>
{
return editorTableItemMap;
}

export function updateEditorTableItemMap(){
editorTableItemMap = getMapOfEditorTableItemInfoInProject();
}
/**
* 获取项目中的所有物编数据,返回一个HashMap可通过uid查询对应物编数据
*/
export function getMapOfEditorTableItemInfoInProject(): {[key:number]: EditorTableItemInfo } {
let res: { [key: number]: EditorTableItemInfo } = [];
function getMapOfEditorTableItemInfoInProject(): Map<number, EditorTableItemInfo> {
let res: Map<number, EditorTableItemInfo> = new Map<number, EditorTableItemInfo>();
let items: EditorTableItemInfo[] = getAllEditorTableItemInfoInProject();
for (let item of items) {
res[item.uid] = item;
res.set(item.uid, item);
}
return res;
}
Expand Down
5 changes: 4 additions & 1 deletion src/editorTable/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './editorTableUtility';
export * from './types';
export * from './types';
export * from './editorTableProvider';
export * from './CSVimporter';
export * from './CSVeditor';
17 changes: 17 additions & 0 deletions src/editorTable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import { EditorTableType } from '../constants';
import { isFileValid, isJson} from '../utility';
import * as fs from 'fs';
import { toUnicodeIgnoreASCII } from '../utility';


/**
Expand Down Expand Up @@ -46,4 +47,20 @@ export class EditorTableItemInfo{
}
return editorTableJson;
}
/**
* 根据jsonUri 写入物编数据Json
* @param jsonObject
* @returns
*/
public setJson(jsonObject:any):boolean {
try {
fs.writeFileSync(this.jsonUri.fsPath, toUnicodeIgnoreASCII(JSON.stringify(jsonObject, null, 2)), 'utf8');
return true;
}
catch (err) {
vscode.window.showErrorMessage('保存Json文件时出错 Error writing file:');
console.error('保存Json文件时出错', err);
return false;
}
}
}
Loading

0 comments on commit 73c0c41

Please sign in to comment.