Skip to content

Commit f18624d

Browse files
committed
feat: update quick pick option description (#5)
1 parent b4dbf13 commit f18624d

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

client/src/lib/constant.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ export const JSON_SPACE = 4
2828
// 自定义国际化内容选项
2929
export const CUSTOM_PICK_OPTION = '自定义'
3030

31+
// 自定义 pick option 描述信息
32+
export const CUSTOM_PICK_OPTION_DESC = '自定义 id,这会默认选择第一个翻译结果'
33+
3134
// 选择国际化文本 placeholder
32-
export const CUSTOM_PICK_PLACEHOLDER = '请选择用来替换的国际化文本 id 内容'
35+
export const CUSTOM_PICK_PLACEHOLDER = '请选择用来替换的国际化代码 id 内容'
3336

3437
// 自定义国际化输入框 placeholder
35-
export const CUSTOM_INPUT_PLACEHOLDER = '请输入自定义的国际化 id 内容'
38+
export const CUSTOM_INPUT_PLACEHOLDER = '请输入自定义的国际化代码 id 内容'
3639

3740
// 自定义 intl id 校验失败信息
38-
export const INVALID_CUSTOM_ID_MESSAGE = '国际化 id 只能由大写字符或下划线组成'
41+
export const INVALID_CUSTOM_ID_MESSAGE = '国际化代码 id 只能由大写字符或下划线组成'
3942

4043
// intl id 中的非法字符
4144
export const INVALID_INTL_ID_CHARACTER = /[^A-Za-z\s]/ig

client/src/lib/util.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { window as Window, ProgressLocation } from 'vscode'
1+
import { window as Window, ProgressLocation, QuickPickItem } from 'vscode'
22
import { DocumentSelector } from 'vscode-languageclient'
3-
import { getZhTranslation } from './baidu'
3+
import { getBaiduZhTranslation } from './translate/baidu'
44

55
import
66
{
77
ActivationLanguage,
88
CUSTOM_INPUT_PLACEHOLDER,
99
CUSTOM_INTL_ID_REGX,
1010
CUSTOM_PICK_OPTION,
11+
CUSTOM_PICK_OPTION_DESC,
1112
CUSTOM_PICK_PLACEHOLDER,
1213
getIntlMessage,
1314
INVALID_CUSTOM_ID_MESSAGE,
@@ -79,7 +80,7 @@ export const getTranslateResultsWithProgress = async (searchText: string): Promi
7980
{
8081
// 获取供选择的 Options
8182
// 这是最终的返回结果
82-
const result = await getZhTranslation(searchText)
83+
const result = await getBaiduZhTranslation(searchText)
8384
Array.isArray(result) && result.length && TranslationResultMap.set(searchText, result)
8485
return result
8586
})
@@ -130,9 +131,35 @@ export const getIntlIdWithQuickPick = async (
130131
intlConfig?: Record<string, string>,
131132
): Promise<[string | undefined, string | undefined]> =>
132133
{
133-
const _intlIdOptions = translateResults.map(re => getCleanIntlId(re).toUpperCase().split(' ').join('_'))
134+
// intl id 对应 translation result 的 map
135+
// intlId -> translateResult
136+
const intlResultMap = new Map<string, string>()
134137

135-
let intlId = await Window.showQuickPick([..._intlIdOptions, CUSTOM_PICK_OPTION], { placeHolder: CUSTOM_PICK_PLACEHOLDER })
138+
// 包含描述信息和细节的 quick pick
139+
// 参考 https://stackoverflow.com/questions/62312943/vscode-use-quickpick-list-items-with-description
140+
const quickPickOptions: QuickPickItem[] = translateResults.map(re =>
141+
{
142+
const intlId = getCleanIntlId(re).toUpperCase().split(' ').join('_')
143+
intlResultMap.set(intlId, re)
144+
return {
145+
label: intlId,
146+
detail: `对应翻译结果:${intlResultMap.get(intlId) || '无'}`
147+
}
148+
})
149+
150+
// 自定义选项
151+
const customPickOption: QuickPickItem = { label: CUSTOM_PICK_OPTION, description: CUSTOM_PICK_OPTION_DESC }
152+
153+
const pickedIntlOption = await Window.showQuickPick([...quickPickOptions, customPickOption], { placeHolder: CUSTOM_PICK_PLACEHOLDER })
154+
155+
// 选中的 option 对应的 intl id
156+
let intlId = pickedIntlOption?.label
157+
158+
// 自定义 id 会选择第一个翻译结果
159+
// 否则取 intlId 对应的翻译结果
160+
const translateResult = !!intlId
161+
? (intlId !== CUSTOM_PICK_OPTION && intlResultMap.get(intlId)) ? intlResultMap.get(intlId) : translateResults && translateResults[0]
162+
: undefined
136163

137164
// 用户选择的 id 有可能已存在,我们会给它加上数字用以区分
138165
if (intlConfig && intlId && (intlId in intlConfig) && intlId !== CUSTOM_PICK_OPTION)
@@ -145,8 +172,7 @@ export const getIntlIdWithQuickPick = async (
145172
intlId = `${intlId}_${getIntlIdCount(idCount)}`
146173
}
147174

148-
// 具体的英文翻译文本 doesn't matter
149-
return [intlId, translateResults && translateResults[0]]
175+
return [intlId, translateResult]
150176
}
151177

152178
/**

0 commit comments

Comments
 (0)