From 2bc1a7e37e0877dc5c34674ee1336d532a4096bc 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, 10 Jul 2024 14:39:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=A9=E7=BC=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editorTable/editorTable.ts | 82 ++++++++++++++++++++---- src/editorTable/languageFeature/hover.ts | 4 +- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/editorTable/editorTable.ts b/src/editorTable/editorTable.ts index a8d29d5..dc940ca 100644 --- a/src/editorTable/editorTable.ts +++ b/src/editorTable/editorTable.ts @@ -13,8 +13,8 @@ type ItemShape = string | boolean | number | null | TupleShape | MapShape | Arra type ArrayShape = ItemShape[]; type TupleShape = { - "__tuple__": true, - "items": ItemShape[], + __tuple__: true, + items: ItemShape[], }; type MapShape = { @@ -76,13 +76,31 @@ export class EditorObject { } public get(key: string): any { + let fieldInfo = this.getFieldInfo(key); + if (!fieldInfo) { + return undefined; + } if (key === 'name') { return this.name; } - return this.rawGet(key); + let raw = this.rawGet(key); + if (raw === undefined) { + return undefined; + } + let value = this.deserialize(raw); + if (fieldInfo.type === 'PLocalizeText') { + if (typeof value === 'number' || typeof value === 'string') { + value = y3.language.get(value) ?? value; + } + } + return value; } public set(key: string, value: ItemShape): boolean { + let fieldInfo = this.getFieldInfo(key); + if (!fieldInfo) { + return false; + } if (key === 'name') { if (typeof value === 'string') { this._name = value; @@ -91,14 +109,20 @@ export class EditorObject { return false; } } - return this.rawSet(key, value); + let raw = this.serialize(value); + if (fieldInfo.type === 'PLocalizeText') { + if (typeof raw === 'string') { + raw = y3.language.keyOf(raw, true); + } + } + return this.rawSet(key, raw); } private rawGet(key: string): ItemShape | undefined { return this.json?.get(key); } - private rawSet(key: string, value: ItemShape): boolean { + private rawSet(key: string, value: ItemShape | undefined): boolean { if (!this.json) { return false; } @@ -153,14 +177,46 @@ export class EditorObject { return this._fieldList; } - // private serialize(item: y3.json.Item): ItemShape { - // if (typeof item === 'string' || typeof item === 'boolean' || typeof item === 'number' || item === null) { - // return item; - // } else if (Array.isArray(item)) { - // return item.map((i) => this.serialize(i)); - // } else if (typeof item === 'object') { - // } - // } + private serialize(item: y3.json.Item, canBeTuple = true): ItemShape { + if (typeof item === 'string' || typeof item === 'boolean' || typeof item === 'number' || item === null) { + return item; + } else if (Array.isArray(item)) { + if (canBeTuple) { + return { + __tuple__: true, + items: item.map((i) => this.serialize(i, false)), + }; + } else { + return item.map((i) => this.serialize(i, canBeTuple)); + } + } else if (typeof item === 'object') { + let map: MapShape = {}; + for (const key in item) { + map[key] = this.serialize(item[key], canBeTuple); + } + return map; + } + throw new Error('不支持的数据类型:' + typeof item); + } + + private deserialize(item: ItemShape): y3.json.Item { + if (typeof item === 'string' || typeof item === 'boolean' || typeof item === 'number' || item === null) { + return item; + } else if (Array.isArray(item)) { + return item.map((i) => this.deserialize(i)); + } else if (typeof item === 'object') { + if (item.__tuple__) { + return this.deserialize(item.items); + } else { + let map: MapShape = {}; + for (const key in item) { + map[key] = this.deserialize((item as MapShape)[key]); + } + return map; + } + } + throw new Error('不支持的数据类型:' + typeof item); + } } async function loadObject(tableName: Table.NameCN, key: number) { diff --git a/src/editorTable/languageFeature/hover.ts b/src/editorTable/languageFeature/hover.ts index 02bfb8c..15386f8 100644 --- a/src/editorTable/languageFeature/hover.ts +++ b/src/editorTable/languageFeature/hover.ts @@ -60,7 +60,7 @@ class UnicodeProvider implements vscode.HoverProvider { return; } - return new vscode.Hover(new vscode.MarkdownString(node.value)); + return new vscode.Hover(new vscode.MarkdownString(`"${node.value}"`)); } } @@ -81,7 +81,7 @@ class TranslateProvider implements vscode.HoverProvider { return; } - return new vscode.Hover(new vscode.MarkdownString(text)); + return new vscode.Hover(new vscode.MarkdownString(`"${text}"`)); } }