fix(config-editor): preserve source YAML edits and empty keys

This commit is contained in:
Supra4E8C
2026-02-28 13:33:20 +08:00
Unverified
parent 72c0ef07ed
commit 5ccf1da86b
2 changed files with 15 additions and 7 deletions
+5 -1
View File
@@ -78,7 +78,11 @@ function setStringInDoc(doc: YamlDocument, path: YamlPath, value: unknown): void
doc.setIn(path, safe);
return;
}
if (docHas(doc, path)) doc.deleteIn(path);
// Preserve existing empty-string keys to avoid dropping template blocks/comments.
// Only keep the key when it already exists in the YAML.
if (docHas(doc, path)) {
doc.setIn(path, '');
}
}
function setIntFromStringInDoc(doc: YamlDocument, path: YamlPath, value: unknown): void {
+10 -6
View File
@@ -123,7 +123,8 @@ export function ConfigPage() {
const handleSave = async () => {
setSaving(true);
try {
const nextMergedYaml = applyVisualChangesToYaml(content);
// In source mode, save exactly what the user edited. In visual mode, materialize visual changes into YAML.
const nextMergedYaml = activeTab === 'source' ? content : applyVisualChangesToYaml(content);
const latestServerYaml = await configFileApi.fetchConfigYaml();
if (latestServerYaml === nextMergedYaml) {
@@ -156,10 +157,13 @@ export function ConfigPage() {
if (tab === activeTab) return;
if (tab === 'source') {
const nextContent = applyVisualChangesToYaml(content);
if (nextContent !== content) {
setContent(nextContent);
setDirty(true);
// Only rewrite YAML when there are pending visual changes; otherwise preserve raw YAML + comments.
if (visualDirty) {
const nextContent = applyVisualChangesToYaml(content);
if (nextContent !== content) {
setContent(nextContent);
setDirty(true);
}
}
} else {
loadVisualValuesFromYaml(content);
@@ -167,7 +171,7 @@ export function ConfigPage() {
setActiveTab(tab);
localStorage.setItem('config-management:tab', tab);
}, [activeTab, applyVisualChangesToYaml, content, loadVisualValuesFromYaml]);
}, [activeTab, applyVisualChangesToYaml, content, loadVisualValuesFromYaml, visualDirty]);
// Search functionality
const performSearch = useCallback((query: string, direction: 'next' | 'prev' = 'next') => {