Add delete buttons and bar add/create to sub-objects pane

This commit is contained in:
c0dev0id
2026-06-30 13:18:29 +02:00
parent e011c9e7d8
commit 66c1f2c151
8 changed files with 261 additions and 39 deletions

View File

@@ -220,7 +220,10 @@ function patchInstrumentHeader(text, instruments) {
if (m) {
const rawName = m[1].replace(/^'|'$/g, '');
const instr = instrMap[rawName];
if (instr && instr.isDirty) {
if (instr && instr.deleted) {
i++;
while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++;
} else if (instr && instr.isDirty) {
i++;
while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++;
result.push(exportInstrument(instr));
@@ -280,7 +283,15 @@ function patchBarMeta(doc, bar) {
return out.join('\n');
}
export function patchScore(rawScoreText, instruments, bars = [], info = null, articles = []) {
function buildNewBarDoc(bar) {
const lines = [`_id: ${bar.id}`, '_meta:'];
const sp = stressorToString(bar.stressor);
if (sp) lines.push(` stress_pattern: ${sp}`);
if (bar.tempoLevels != null) lines.push(` beats_per_minute: ${bar.tempoLevels}`);
return lines.join('\n');
}
export function patchScore(rawScoreText, instruments, bars = [], info = null, articles = [], flags = {}) {
const log = [];
const SEP = '\n---\n';
const [header, ...barDocs] = rawScoreText.split(SEP);
@@ -288,28 +299,43 @@ export function patchScore(rawScoreText, instruments, bars = [], info = null, ar
let patchedHeader = patchMetadata(header, info);
const dirtyArticles = articles.filter(a => a.isDirty);
if (dirtyArticles.length) {
if (dirtyArticles.length || flags.articlesModified) {
patchedHeader = patchArticles(patchedHeader, articles);
for (const a of dirtyArticles) log.push({ level: 'changed', path: `articles / ${a.name}` });
}
patchedHeader = patchInstrumentHeader(patchedHeader, instruments);
for (const i of instruments.filter(i => i.isDirty)) log.push({ level: 'changed', path: `instrument / ${i.name}` });
if (!barDocs.length) return { text: patchedHeader, log };
for (const i of instruments) {
if (i.deleted) log.push({ level: 'changed', path: `instrument / ${i.name} (deleted)` });
else if (i.isDirty) log.push({ level: 'changed', path: `instrument / ${i.name}` });
}
const barMap = {};
for (const bar of bars) barMap[bar.id] = bar;
if (!barDocs.length) {
const newBarDocs = bars.filter(b => b.isNew && !b.deleted).map(b => buildNewBarDoc(b));
for (const b of bars.filter(b => b.isNew && !b.deleted)) log.push({ level: 'changed', path: `bar / ${b.id}` });
return { text: [patchedHeader, ...newBarDocs].join(SEP), log };
}
let passedThrough = 0;
const patchedBarDocs = barDocs.map(doc => {
const patchedBarDocs = [];
for (const doc of barDocs) {
const m = doc.match(/^_id:\s*(\S+)/m);
if (!m) { passedThrough++; return doc; }
if (!m) { passedThrough++; patchedBarDocs.push(doc); continue; }
const bar = barMap[m[1]];
if (!bar?.isDirty) { passedThrough++; return doc; }
if (!bar) { passedThrough++; patchedBarDocs.push(doc); continue; }
if (bar.deleted) continue;
if (!bar.isDirty) { passedThrough++; patchedBarDocs.push(doc); continue; }
log.push({ level: 'changed', path: `bar / ${bar.id}` });
return patchBarMeta(doc, bar);
});
patchedBarDocs.push(patchBarMeta(doc, bar));
}
for (const bar of bars.filter(b => b.isNew && !b.deleted)) {
patchedBarDocs.push(buildNewBarDoc(bar));
log.push({ level: 'changed', path: `bar / ${bar.id}` });
}
if (passedThrough > 0) {
log.push({ level: 'info', message: `${passedThrough} bar document${passedThrough !== 1 ? 's' : ''} passed through unchanged` });