* 核心:修改 timeline YAML 中的指定字段,保留注释
(presetName, periodKey, field, value)
| 4439 | * 核心:修改 timeline YAML 中的指定字段,保留注释 |
| 4440 | */ |
| 4441 | function updateTimelineField(presetName, periodKey, field, value) { |
| 4442 | const editor = document.getElementById('timeline-editor'); |
| 4443 | let yaml = editor.value; |
| 4444 | const lines = yaml.split('\n'); |
| 4445 | |
| 4446 | // 1. 定位预设/custom 的起始行 |
| 4447 | const isCustom = presetName === 'custom'; |
| 4448 | let sectionStart = -1; |
| 4449 | let sectionIndent = 0; |
| 4450 | |
| 4451 | if (isCustom) { |
| 4452 | // 找 custom: 顶层 key |
| 4453 | for (let i = 0; i < lines.length; i++) { |
| 4454 | if (/^custom:\s*/.test(lines[i])) { |
| 4455 | sectionStart = i; |
| 4456 | sectionIndent = 0; |
| 4457 | break; |
| 4458 | } |
| 4459 | } |
| 4460 | } else { |
| 4461 | // 找 presets: 下的 presetName: |
| 4462 | let inPresets = false; |
| 4463 | for (let i = 0; i < lines.length; i++) { |
| 4464 | const line = lines[i]; |
| 4465 | if (/^presets:\s*/.test(line)) { |
| 4466 | inPresets = true; |
| 4467 | continue; |
| 4468 | } |
| 4469 | if (inPresets && /^\S/.test(line) && !line.startsWith('#')) { |
| 4470 | break; // left presets block |
| 4471 | } |
| 4472 | if (inPresets) { |
| 4473 | const m = line.match(/^(\s+)(\S+):\s*/); |
| 4474 | if (m && m[2] === presetName) { |
| 4475 | sectionStart = i; |
| 4476 | sectionIndent = m[1].length; |
| 4477 | break; |
| 4478 | } |
| 4479 | } |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | if (sectionStart < 0) return; |
| 4484 | |
| 4485 | // 2. 找到 section 结束行 |
| 4486 | let sectionEnd = lines.length; |
| 4487 | for (let i = sectionStart + 1; i < lines.length; i++) { |
| 4488 | const line = lines[i]; |
| 4489 | if (line.trim() === '' || line.trim().startsWith('#')) continue; |
| 4490 | const indent = line.search(/\S/); |
| 4491 | if (indent <= sectionIndent) { |
| 4492 | sectionEnd = i; |
| 4493 | break; |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | // 3. 在 section 内定位 periodKey 子区域 |
| 4498 | let targetStart, targetEnd; |
no test coverage detected