Skip to content

Commit ff5f6ff

Browse files
author
linyuan.yang
committed
环境变量
1 parent 13f1c1e commit ff5f6ff

1 file changed

Lines changed: 39 additions & 15 deletions

File tree

packages/app/src/tools/env-vars/EnvVarsView.vue

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ interface EditDialogState {
2323
originalName: string
2424
name: string
2525
value: string
26-
typeName: 'REG_SZ' | 'REG_EXPAND_SZ'
2726
originalTypeName: 'REG_SZ' | 'REG_EXPAND_SZ'
2827
/** PATH 列表编辑器的条目;null 表示非列表模式 */
2928
pathEntries: string[] | null
@@ -143,9 +142,12 @@ const nameConflict = computed(() => {
143142
return existing ? existing.name : null
144143
})
145144
146-
const typeDowngrade = computed(() => {
145+
/** 保存时的注册表类型:编辑保持原类型;新建按值里是否含 %VAR% 引用自动判断。 */
146+
const effectiveTypeName = computed<'REG_SZ' | 'REG_EXPAND_SZ'>(() => {
147147
const state = dialog.value
148-
return !!state && state.originalTypeName === 'REG_EXPAND_SZ' && state.typeName === 'REG_SZ'
148+
if (!state) return 'REG_SZ'
149+
if (state.mode === 'edit') return state.originalTypeName
150+
return /%[A-Za-z_][\w() .\-]*%/.test(effectiveValue.value) ? 'REG_EXPAND_SZ' : 'REG_SZ'
149151
})
150152
151153
async function load(target: EnvVarScope, options: { silent?: boolean } = {}) {
@@ -190,6 +192,21 @@ function canExpand(entry: EnvVarEntry) {
190192
return entry.rawValue.length > EXPAND_THRESHOLD || entry.rawValue.includes('\n')
191193
}
192194
195+
/** PATH 这类分号分隔的列表变量,按条展示比一整段原文可读。 */
196+
function isListVar(entry: EnvVarEntry) {
197+
return entry.name.trim().toUpperCase() === 'PATH' && entry.rawValue.includes(';')
198+
}
199+
200+
function listEntries(entry: EnvVarEntry) {
201+
return entry.rawValue.split(';')
202+
}
203+
204+
/** 展开后值有变化才展示;长文本收起时不展示,避免两段大段重复文本。 */
205+
function showExpandedHelper(entry: EnvVarEntry) {
206+
if (!entry.expandedValue || entry.expandedValue === entry.rawValue) return false
207+
return !canExpand(entry) || expanded.value.has(entry.name)
208+
}
209+
193210
function toggleExpand(entry: EnvVarEntry) {
194211
const next = new Set(expanded.value)
195212
if (!next.delete(entry.name)) next.add(entry.name)
@@ -233,7 +250,6 @@ function openCreate() {
233250
originalName: '',
234251
name: '',
235252
value: '',
236-
typeName: 'REG_SZ',
237253
originalTypeName: 'REG_SZ',
238254
pathEntries: null,
239255
useListEditor: false,
@@ -249,7 +265,6 @@ function openEdit(entry: EnvVarEntry) {
249265
originalName: entry.name,
250266
name: entry.name,
251267
value: entry.rawValue,
252-
typeName: entry.typeName,
253268
originalTypeName: entry.typeName,
254269
pathEntries: isPath ? entry.rawValue.split(';') : null,
255270
useListEditor: isPath,
@@ -268,7 +283,7 @@ async function saveDialog() {
268283
dialogError.value = ''
269284
try {
270285
const name = state.name.trim()
271-
await setEnvVar(tab.value as EnvVarScope, name, effectiveValue.value, state.typeName)
286+
await setEnvVar(tab.value as EnvVarScope, name, effectiveValue.value, effectiveTypeName.value)
272287
dialog.value = null
273288
showStatus(`已保存 ${name},新启动的程序会立即生效`)
274289
await load(tab.value as EnvVarScope, { silent: true })
@@ -431,8 +446,14 @@ onUnmounted(() => {
431446
<span class="badge" :class="{ expand: entry.typeName === 'REG_EXPAND_SZ' }">{{ typeLabel(entry.typeName) }}</span>
432447
</div>
433448
<div class="var-value">
434-
<p class="value-text" :class="{ clamped: canExpand(entry) && !expanded.has(entry.name) }">{{ entry.rawValue || '(空)' }}</p>
435-
<p v-if="entry.expandedValue && entry.expandedValue !== entry.rawValue" class="helper">
449+
<template v-if="isListVar(entry)">
450+
<p class="helper list-count">共 {{ listEntries(entry).length }} 条路径</p>
451+
<ul class="value-text path-items" :class="{ clamped: canExpand(entry) && !expanded.has(entry.name) }">
452+
<li v-for="(item, index) in listEntries(entry)" :key="index">{{ item || '(空)' }}</li>
453+
</ul>
454+
</template>
455+
<p v-else class="value-text" :class="{ clamped: canExpand(entry) && !expanded.has(entry.name) }">{{ entry.rawValue || '(空)' }}</p>
456+
<p v-if="showExpandedHelper(entry)" class="helper">
436457
含 %VAR% 引用,展开后:{{ entry.expandedValue }}
437458
</p>
438459
<button
@@ -477,13 +498,12 @@ onUnmounted(() => {
477498
>
478499
<p v-if="nameConflict" class="warning">已存在同名变量(Windows 变量名不区分大小写),保存将覆盖 {{ nameConflict }}。</p>
479500

480-
<label for="env-var-type">类型</label>
481-
<select id="env-var-type" v-model="dialog.typeName" class="input">
482-
<option value="REG_SZ">文本(REG_SZ)</option>
483-
<option value="REG_EXPAND_SZ">展开型(REG_EXPAND_SZ)</option>
484-
</select>
485-
<p v-if="typeDowngrade" class="error">当前类型为展开型,改为文本将丢失 %VAR% 引用的展开语义,程序会读到原样的 %…% 字符串。</p>
486-
<p v-else-if="dialog.typeName === 'REG_EXPAND_SZ'" class="helper">值中的 %VAR% 引用会在程序读取时自动展开为实际路径。</p>
501+
<p v-if="dialog.mode === 'edit' && dialog.originalTypeName === 'REG_EXPAND_SZ'" class="helper">
502+
该变量为展开型(REG_EXPAND_SZ),值中的 %VAR% 引用会在程序读取时自动展开,保存时保持该类型。
503+
</p>
504+
<p v-else-if="effectiveTypeName === 'REG_EXPAND_SZ'" class="helper">
505+
值中含 %VAR% 引用,将以展开型(REG_EXPAND_SZ)保存,程序读取时自动展开为实际路径。
506+
</p>
487507

488508
<label for="env-var-value">值</label>
489509
<template v-if="showListEditor">
@@ -589,6 +609,10 @@ onUnmounted(() => {
589609
.var-value { min-width: 0; }
590610
.value-text { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; font: 13px ui-monospace, SFMono-Regular, Consolas, monospace; }
591611
.value-text.clamped { display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 3; }
612+
.list-count { margin-top: 0; }
613+
.path-items { margin: 0; padding: 0; list-style: none; }
614+
.path-items li { position: relative; padding: 1px 0 1px 14px; overflow-wrap: anywhere; }
615+
.path-items li::before { content: ''; position: absolute; left: 2px; top: 0.75em; width: 5px; height: 5px; border-radius: 50%; background: var(--fg-muted); opacity: 0.5; }
592616
.link-btn { margin-top: 2px; padding: 0; border: none; background: transparent; color: var(--primary); font-size: 13px; cursor: pointer; }
593617
.var-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
594618
.copy-btn { min-height: 30px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 5px; background: transparent; color: var(--fg); font-size: 13px; cursor: pointer; }

0 commit comments

Comments
 (0)