Skip to content

Commit 53c3522

Browse files
committed
fix(web): 配置页编辑已有配置时保留原配置,仅应用用户修改
1 parent 45aa94c commit 53c3522

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎web/js/app.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ function renderConfigEditor(item){
10381038
const current = item || {};
10391039
$('configEditorTitle').textContent = `${editing ? '编辑' : '新增'}${def.label}`;
10401040
$('configEditorHint').textContent = def.hint;
1041-
$('configFields').innerHTML = def.fields.map(field => fieldHTML(field, current)).join('');
1041+
$('configFields').innerHTML = def.fields.map(field => fieldHTML(field, current, !editing)).join('');
10421042
const resetTrafficBtn = $('resetTrafficBtn');
10431043
const canResetTraffic = editing && S.admin.selectedType === 'servers';
10441044
resetTrafficBtn.style.display = canResetTraffic ? '' : 'none';
@@ -1076,8 +1076,8 @@ function refreshClientCmd(){
10761076
const pass = form.password ? form.password.value : '';
10771077
textEl.textContent = clientInstallCommand(user, pass);
10781078
}
1079-
function fieldHTML(field, item){
1080-
const value = item[field.name] ?? field.default ?? '';
1079+
function fieldHTML(field, item, showDefault){
1080+
const value = item[field.name] ?? (showDefault ? (field.default ?? '') : '');
10811081
if(field.type === 'checkbox'){
10821082
return `<label class="check-row"><input name="${esc(field.name)}" type="checkbox" ${value ? 'checked' : ''} /> <span>${esc(field.label)}</span></label>`;
10831083
}
@@ -1097,6 +1097,7 @@ function clearConfigForm(){
10971097
}
10981098
function formConfigItem(){
10991099
const elements = $('configForm').elements;
1100+
const existing = S.admin.selectedIndex >= 0 ? configItems()[S.admin.selectedIndex] : null;
11001101
const item = {};
11011102
activeConfigDef().fields.forEach(field => {
11021103
const el = elements[field.name];
@@ -1110,10 +1111,13 @@ function formConfigItem(){
11101111
if(!Number.isFinite(value)) value = field.default ?? 0;
11111112
if(field.min != null) value = Math.max(field.min, value);
11121113
if(field.max != null) value = Math.min(field.max, value);
1114+
if(existing && !(field.name in existing) && el.value === '') return;
11131115
item[field.name] = value;
11141116
return;
11151117
}
1116-
item[field.name] = field.keepRaw ? el.value : el.value.trim();
1118+
const value = field.keepRaw ? el.value : el.value.trim();
1119+
if(existing && !(field.name in existing) && value === '') return;
1120+
item[field.name] = value;
11171121
});
11181122
return item;
11191123
}
@@ -1127,7 +1131,9 @@ async function saveConfigItem(key, index, item){
11271131
S.admin.saving = true;
11281132
S.suppressStatsReloadUntil = Date.now() + 8000;
11291133
try{
1130-
const data = await api(configItemPath(key, index), { method: index >= 0 ? 'PUT' : 'POST', body: JSON.stringify(item) });
1134+
const original = index >= 0 ? configItems()[index] : null;
1135+
const body = original ? { ...original, ...item } : item;
1136+
const data = await api(configItemPath(key, index), { method: index >= 0 ? 'PUT' : 'POST', body: JSON.stringify(body) });
11311137
if(data.config) S.admin.config = normalizeAdminConfig(data.config);
11321138
S.suppressStatsReloadUntil = Date.now() + 8000;
11331139
return data;

0 commit comments

Comments
 (0)