phases 4-6: article system, stem note fields, edit-state cleanup

Phase 4 — remove edit-state flags from domain objects:
- ast-parser.js: drop isDirty from buildInstrument, buildBar, buildStemNote
- store.js: add resetEditState()
- PaneCP.js: use resetEditState() on import; drop flags arg from patchScore
- PaneFO.js: isDirty -> _modified for linked instruments; no per-node dirty tracking
- PaneSubObjects.js: _spliceNode helper; deletion via splice; _isNew on new bars
- exporter.js: on-demand traversal; absence from model means deleted

Phase 5 — article parser and exporter:
- ast-parser.js: _buildArticleEntry replaces _mergeArticleEntry; stage.article and
  voice.article dispatch; article properties carry scope and overwritten flag
- exporter.js: _buildArticlesBlock emits -important: list for overwrites scope

Phase 6 — stem note dead fields:
- ast-parser.js: parse weight and articulatory on stem notes; flatten
  article.definite properties onto stem note model sibling to pitch/chain
This commit is contained in:
c0dev0id
2026-07-11 12:07:05 +02:00
parent e2b7af77d6
commit 7ffa0a9d2d
7 changed files with 166 additions and 129 deletions

View File

@@ -109,14 +109,24 @@ export function buildModel(rawTree) {
case 'stage.cone': case 'stage.cone':
score.stageCone = { type: 'stage_cone', ...node.props }; score.stageCone = { type: 'stage_cone', ...node.props };
break; break;
case 'stage.voice': case 'stage.article':
score.stageVoices.push({ score.articles.push(_buildArticleEntry(node));
break;
case 'stage.voice': {
const sv = {
type: 'stage_voice', type: 'stage_voice',
name: node.positionals[0], name: node.positionals[0],
direction: node.props.direction, direction: node.props.direction,
distance: node.props.distance, distance: node.props.distance,
}); articles: [],
};
for (const child of node.children) {
if (child.parentSlot === 'voice' && child.slot === 'article')
sv.articles.push(_buildArticleEntry(child));
}
score.stageVoices.push(sv);
break; break;
}
case 'instrument': case 'instrument':
score.instruments.push(_buildInstrument(node)); score.instruments.push(_buildInstrument(node));
break; break;
@@ -124,31 +134,27 @@ export function buildModel(rawTree) {
score.bars.push(_buildBar(node)); score.bars.push(_buildBar(node));
break; break;
default: default:
if (node.parentSlot === 'articles') { score[node.slot] = _buildGeneric(node);
_mergeArticleEntry(score, node);
} else {
score[node.slot] = _buildGeneric(node);
}
} }
} }
return score; return score;
} }
// Articles are keyed by label. The AST emits one line per (label, subtype) function _buildArticleEntry(node) {
// — e.g. `articles.defaults 'f'` and (future) `articles.overwrites 'f'`. const entry = { type: 'article', name: node.positionals[0], properties: [] };
// We merge them into one entry per label whose properties carry the scope for (const child of node.children) {
// of their originating subtype, so the UI can toggle scope per property. if (child.parentSlot !== 'article') continue;
function _mergeArticleEntry(score, node) { if (child.slot === 'defaults' || child.slot === 'definite') {
const name = node.positionals[0]; const raw = child.props.constant ?? child.props.stacked ?? child.props.static;
let entry = score.articles.find(a => a.name === name); const shapeChild = child.children.find(c => c.slot === 'shape');
if (!entry) { const value = raw !== undefined ? coerce(raw) : (shapeChild ? _buildShape(shapeChild) : null);
entry = { type: 'article', name, properties: [] }; entry.properties.push({ name: child.positionals[0], value, scope: child.slot, overwritten: child.props.overwritten ?? false });
score.articles.push(entry); } else if (child.slot === 'overwrites') {
} for (const propName of child.positionals) {
const scope = node.slot; // 'defaults' | 'overwrites' | future subtype entry.properties.push({ name: propName, value: null, scope: 'overwrites' });
for (const [key, value] of Object.entries(node.props)) { }
entry.properties.push({ name: key, value, scope }); }
} }
return entry; return entry;
} }
@@ -176,7 +182,6 @@ function _buildInstrument(node) {
name: node.positionals[0], name: node.positionals[0],
notChangedSince: node.props.NOT_CHANGED_SINCE ?? null, notChangedSince: node.props.NOT_CHANGED_SINCE ?? null,
isLinked: (node.positionals[0] ?? '').includes('/'), isLinked: (node.positionals[0] ?? '').includes('/'),
isDirty: false,
variations: [], variations: [],
basicProperties: null, basicProperties: null,
volumes: null, volumes: null,
@@ -339,7 +344,6 @@ function _buildBar(node) {
const bar = { const bar = {
type: 'bar', type: 'bar',
id: node.positionals[0] ?? '', id: node.positionals[0] ?? '',
isDirty: false,
stressor: null, stressor: null,
tempoShape: null, tempoShape: null,
tempoLevels: null, tempoLevels: null,
@@ -413,7 +417,7 @@ function _buildVoice(node) {
voice.offsets.push(_buildOffset(child)); voice.offsets.push(_buildOffset(child));
break; break;
case 'voice.article': case 'voice.article':
voice.articles.push(child.positionals[0]); voice.articles.push(_buildArticleEntry(child));
break; break;
case 'voice.motif': case 'voice.motif':
voice.motifs.push(_buildMotif(child)); voice.motifs.push(_buildMotif(child));
@@ -444,9 +448,17 @@ function _buildOffset(node) {
} }
function _buildStemNote(node) { function _buildStemNote(node) {
const KNOWN = new Set(['pitch', 'eff_length', 'adj_stress', 'adjacent']); const KNOWN = new Set(['pitch', 'eff_length', 'adj_stress', 'adjacent', 'weight', 'articulatory']);
const chainNode = node.children.find(c => c.parentSlot === 'stem_note' && c.slot === 'chain'); const chainNode = node.children.find(c => c.parentSlot === 'stem_note' && c.slot === 'chain');
const writeToNode = node.children.find(c => c.parentSlot === 'stem_note' && c.slot === 'write_to'); const writeToNode = node.children.find(c => c.parentSlot === 'stem_note' && c.slot === 'write_to');
const definiteProps = {};
for (const child of node.children) {
if (child.parentSlot === 'article' && child.slot === 'definite') {
const raw = child.props.constant ?? child.props.stacked ?? child.props.static;
const shapeChild = child.children.find(c => c.slot === 'shape');
definiteProps[child.positionals[0]] = raw !== undefined ? coerce(raw) : (shapeChild ? _buildShape(shapeChild) : null);
}
}
return { return {
type: 'stem_note', type: 'stem_note',
pitch: node.props.pitch, pitch: node.props.pitch,
@@ -454,13 +466,13 @@ function _buildStemNote(node) {
adjacent: node.props.adjacent ?? null, adjacent: node.props.adjacent ?? null,
adjStress: node.props.adj_stress ?? null, adjStress: node.props.adj_stress ?? null,
length: null, length: null,
weight: null, weight: node.props.weight ?? null,
chainText: chainNode?.props._tmp_string ?? '', chainText: chainNode?.props._tmp_string ?? '',
writeToName: writeToNode?.positionals[0] ?? null, writeToName: writeToNode?.positionals[0] ?? null,
clauses: (chainNode?.children ?? []) clauses: (chainNode?.children ?? [])
.filter(c => c.parentSlot === 'chain' && c.slot === 'clause') .filter(c => c.parentSlot === 'chain' && c.slot === 'clause')
.map(_buildClause), .map(_buildClause),
isDirty: false, ...definiteProps,
unknownProps: _collectUnknownProps(node.props, KNOWN), unknownProps: _collectUnknownProps(node.props, KNOWN),
}; };
} }

View File

@@ -32,7 +32,7 @@ export const PaneCP = {
try { try {
const text = await fetchAstLog(); const text = await fetchAstLog();
props.store.scoreModel = buildModel(parseAstLog(text)); props.store.scoreModel = buildModel(parseAstLog(text));
props.store.exportLog = []; props.store.resetEditState();
} catch (e) { } catch (e) {
importError.value = e.message; importError.value = e.message;
} finally { } finally {
@@ -49,9 +49,7 @@ export const PaneCP = {
const model = props.store.scoreModel; const model = props.store.scoreModel;
const { text: patched, log } = patchScore( const { text: patched, log } = patchScore(
raw, model.instruments, model.bars, model.info, model.articles ?? [], raw, model.instruments, model.bars, model.info, model.articles ?? [],
{ articlesModified: !!model.articlesModified },
); );
model.articlesModified = false;
props.store.exportLog = log; props.store.exportLog = log;
await putScoreText(patched); await putScoreText(patched);
props.store.synthesisStatus = { frozen: false, currently_rendered_notes: 0, notes_in_total: 0 }; props.store.synthesisStatus = { frozen: false, currently_rendered_notes: 0, notes_in_total: 0 };

View File

@@ -61,10 +61,10 @@ export const PaneFO = {
// Guard: first edit to a linked instrument triggers embed-or-discard before committing. // Guard: first edit to a linked instrument triggers embed-or-discard before committing.
function makeChangeHandler(instr) { function makeChangeHandler(instr) {
return (info) => { return (info) => {
if (instr.isLinked && !instr.isDirty) { if (instr.isLinked && !instr._modified) {
pendingEdit.value = { instr, undo: info?.undo }; pendingEdit.value = { instr, undo: info?.undo };
} else { } else {
instr.isDirty = true; instr._modified = true;
props.store.markDirty(); props.store.markDirty();
} }
}; };
@@ -73,7 +73,7 @@ export const PaneFO = {
function embedInstrument(instr) { function embedInstrument(instr) {
instr.name = instr.name.split('/').pop(); instr.name = instr.name.split('/').pop();
instr.isLinked = false; instr.isLinked = false;
instr.isDirty = true; instr._modified = true;
pendingEdit.value = null; pendingEdit.value = null;
props.store.markDirty(); props.store.markDirty();
} }
@@ -152,7 +152,7 @@ export const PaneFO = {
: null, : null,
); );
} else if (node.type === 'article') { } else if (node.type === 'article') {
const markDirty = () => { node.isDirty = true; props.store.markDirty(); }; const markDirty = () => { props.store.markDirty(); };
const rowStyle = 'display:flex;gap:0.4rem;align-items:center;padding:0.2rem 0'; const rowStyle = 'display:flex;gap:0.4rem;align-items:center;padding:0.2rem 0';
children.push( children.push(
h('h4', H4, `Article: ${node.name}`), h('h4', H4, `Article: ${node.name}`),
@@ -209,12 +209,12 @@ export const PaneFO = {
]), ]),
); );
} else if (node.type === 'bar') { } else if (node.type === 'bar') {
const markBarDirty = () => { node.isDirty = true; props.store.markDirty(); }; const markBarDirty = () => { props.store.markDirty(); };
children.push( children.push(
h('h4', H4, `Bar: ${node.id}`), h('h4', H4, `Bar: ${node.id}`),
h(ObjectExtended, { h(ObjectExtended, {
fields: [ fields: [
{ key: 'id', value: node.id, editable: node.isNew ?? false }, { key: 'id', value: node.id, editable: node._isNew ?? false },
{ key: 'beats_per_minute', value: node.tempoLevels ?? '', editable: true, type: 'number' }, { key: 'beats_per_minute', value: node.tempoLevels ?? '', editable: true, type: 'number' },
{ key: 'stress_pattern', value: stressorToString(node.stressor), editable: true }, { key: 'stress_pattern', value: stressorToString(node.stressor), editable: true },
], ],
@@ -234,7 +234,7 @@ export const PaneFO = {
h('h4', H4, `Voice: ${node.name}`), h('h4', H4, `Voice: ${node.name}`),
h(ObjectExtended, { h(ObjectExtended, {
fields: [ fields: [
{ key: 'articles', value: node.articles.join(', ') || '—', editable: false }, { key: 'articles', value: node.articles.map(a => a.name).join(', ') || '—', editable: false },
{ key: 'motifs', value: node.motifs.map(m => m.label).join(', ') || '—', editable: false }, { key: 'motifs', value: node.motifs.map(m => m.label).join(', ') || '—', editable: false },
{ key: 'offsets', value: String(node.offsets.length), editable: false }, { key: 'offsets', value: String(node.offsets.length), editable: false },
], ],
@@ -273,10 +273,7 @@ export const PaneFO = {
}), }),
); );
} else if (node.type === 'stem_note') { } else if (node.type === 'stem_note') {
const bar = props.store.focusPath.find(n => n.type === 'bar') ?? null;
const markDirty = () => { const markDirty = () => {
node.isDirty = true;
if (bar) bar.isDirty = true;
props.store.markDirty(); props.store.markDirty();
}; };
children.push( children.push(

View File

@@ -23,38 +23,39 @@ export const PaneSubObjects = {
return fp.length ? fp[fp.length - 1] : props.store.scoreModel; return fp.length ? fp[fp.length - 1] : props.store.scoreModel;
} }
function _spliceNode(arr, node) {
const idx = arr.indexOf(node);
if (idx !== -1) { arr.splice(idx, 1); props.store.markDirty(); }
}
function deleteItem(kind, node) { function deleteItem(kind, node) {
const store = props.store; const store = props.store;
const model = store.scoreModel; const model = store.scoreModel;
if (!model) return; if (!model) return;
if (kind === 'instrument') { if (kind === 'instrument') {
const idx = model.instruments.indexOf(node); _spliceNode(model.instruments, node);
if (idx !== -1) { model.instruments.splice(idx, 1); store.markDirty(); }
} else if (kind === 'articles') { } else if (kind === 'articles') {
const idx = (model.articles ?? []).indexOf(node); _spliceNode(model.articles, node);
if (idx !== -1) { model.articles.splice(idx, 1); model.articlesModified = true; store.markDirty(); }
} else if (kind === 'bar') { } else if (kind === 'bar') {
node.deleted = true; _spliceNode(model.bars, node);
node.isDirty = true;
store.markDirty();
} else if (kind === 'variation') { } else if (kind === 'variation') {
for (const instr of model.instruments) { for (const instr of model.instruments) {
const idx = instr.variations.indexOf(node); const idx = instr.variations.indexOf(node);
if (idx !== -1) { instr.variations.splice(idx, 1); instr.isDirty = true; store.markDirty(); return; } if (idx !== -1) { instr.variations.splice(idx, 1); instr._modified = true; store.markDirty(); return; }
for (const v of instr.variations) { for (const v of instr.variations) {
const sidx = v.subvariations.indexOf(node); const sidx = v.subvariations.indexOf(node);
if (sidx !== -1) { v.subvariations.splice(sidx, 1); instr.isDirty = true; store.markDirty(); return; } if (sidx !== -1) { v.subvariations.splice(sidx, 1); instr._modified = true; store.markDirty(); return; }
} }
} }
} else if (kind === 'label_spec') { } else if (kind === 'label_spec') {
for (const instr of model.instruments) { for (const instr of model.instruments) {
for (const v of instr.variations) { for (const v of instr.variations) {
const idx = v.labelSpecs.indexOf(node); const idx = v.labelSpecs.indexOf(node);
if (idx !== -1) { v.labelSpecs.splice(idx, 1); instr.isDirty = true; store.markDirty(); return; } if (idx !== -1) { v.labelSpecs.splice(idx, 1); instr._modified = true; store.markDirty(); return; }
for (const sv of v.subvariations) { for (const sv of v.subvariations) {
const idx2 = sv.labelSpecs.indexOf(node); const idx2 = sv.labelSpecs.indexOf(node);
if (idx2 !== -1) { sv.labelSpecs.splice(idx2, 1); instr.isDirty = true; store.markDirty(); return; } if (idx2 !== -1) { sv.labelSpecs.splice(idx2, 1); instr._modified = true; store.markDirty(); return; }
} }
} }
} }
@@ -69,11 +70,11 @@ export const PaneSubObjects = {
if (afterId) { if (afterId) {
newId = _incrementId(afterId); newId = _incrementId(afterId);
} else { } else {
const liveBars = (model.bars ?? []).filter(b => !b.deleted); const liveBars = model.bars ?? [];
newId = liveBars.length ? _incrementId(liveBars[liveBars.length - 1].id) : 'bar001'; newId = liveBars.length ? _incrementId(liveBars[liveBars.length - 1].id) : 'bar001';
} }
const newBar = { const newBar = {
type: 'bar', id: newId, isDirty: true, isNew: true, type: 'bar', id: newId, _isNew: true,
stressor: null, tempoLevels: null, stressor: null, tempoLevels: null,
upperStressBound: null, lowerStressBound: null, tempoShape: null, upperStressBound: null, lowerStressBound: null, tempoShape: null,
voices: {}, voices: {},
@@ -95,11 +96,9 @@ export const PaneSubObjects = {
if (!items.length && props.kind !== 'bar') return h('div', null, h('em', null, 'No sub-objects')); if (!items.length && props.kind !== 'bar') return h('div', null, h('em', null, 'No sub-objects'));
if (props.kind === 'bar') { if (props.kind === 'bar') {
const visibleItems = items.filter(item => !item.node.deleted);
const barGroups = []; const barGroups = [];
const seen = new Map(); const seen = new Map();
for (const item of visibleItems) { for (const item of items) {
const key = _barGroupKey(item.label); const key = _barGroupKey(item.label);
if (!seen.has(key)) { if (!seen.has(key)) {
const g = { key, items: [] }; const g = { key, items: [] };

View File

@@ -133,23 +133,30 @@ function _serializeArticleValue(v) {
function _buildArticlesBlock(articles) { function _buildArticlesBlock(articles) {
const body = articles.flatMap(art => { const body = articles.flatMap(art => {
const props = (art.properties ?? []).filter(p => p.name); const defaults = (art.properties ?? []).filter(p => p.scope !== 'overwrites' && p.name);
if (!props.length) return []; const overwrites = (art.properties ?? []).filter(p => p.scope === 'overwrites' && p.name);
return [` ${art.name}:`, ...props.map(p => ` ${p.name}: ${_serializeArticleValue(p.value)}`)]; if (!defaults.length && !overwrites.length) return [];
const lines = [` ${art.name}:`];
defaults.forEach(p => lines.push(` ${p.name}: ${_serializeArticleValue(p.value)}`));
if (overwrites.length) {
lines.push(` -important:`);
overwrites.forEach(p => lines.push(` - ${p.name}`));
}
return lines;
}); });
return body.length ? ['articles:', ...body].join('\n') : null; return body.length ? ['articles:', ...body].join('\n') : null;
} }
function _patchArticles(text, articles) { function _patchArticles(text, articles) {
const newBlock = _buildArticlesBlock(articles); const newBlock = _buildArticlesBlock(articles);
if (!newBlock) return text;
const lines = text.split('\n'); const lines = text.split('\n');
const artIdx = lines.findIndex(l => /^articles\s*:/.test(l)); const artIdx = lines.findIndex(l => /^articles\s*:/.test(l));
if (artIdx !== -1) { if (artIdx !== -1) {
let end = artIdx + 1; let end = artIdx + 1;
while (end < lines.length && (lines[end] === '' || lines[end].startsWith(' ') || lines[end].startsWith('\t'))) end++; while (end < lines.length && (lines[end] === '' || lines[end].startsWith(' ') || lines[end].startsWith('\t'))) end++;
lines.splice(artIdx, end - artIdx, newBlock); if (newBlock) lines.splice(artIdx, end - artIdx, newBlock);
} else { else lines.splice(artIdx, end - artIdx);
} else if (newBlock) {
const instrIdx = lines.findIndex(l => /^instrument\s/.test(l)); const instrIdx = lines.findIndex(l => /^instrument\s/.test(l));
const insertAt = instrIdx !== -1 ? instrIdx : lines.length; const insertAt = instrIdx !== -1 ? instrIdx : lines.length;
lines.splice(insertAt, 0, newBlock, ''); lines.splice(insertAt, 0, newBlock, '');
@@ -196,17 +203,18 @@ function _patchInstrumentHeader(text, instruments) {
if (m) { if (m) {
const rawName = m[1].replace(/^'|'$/g, ''); const rawName = m[1].replace(/^'|'$/g, '');
const instr = instrMap[rawName]; const instr = instrMap[rawName];
if (instr && instr.deleted) { if (!instr) {
// not in model → deleted; skip block
i++; i++;
while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++; while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++;
} else if (instr && instr.isDirty) { } else if (instr.isLinked && !instr._modified) {
result.push(line);
i++;
} else {
i++; i++;
while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++; while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++;
result.push(exportInstrument(instr)); result.push(exportInstrument(instr));
result.push(''); result.push('');
} else {
result.push(line);
i++;
} }
} else { } else {
result.push(line); result.push(line);
@@ -263,42 +271,37 @@ function _buildNewBarDoc(bar) {
return lines.join('\n'); return lines.join('\n');
} }
export function patchScore(rawScoreText, instruments, bars = [], info = null, articles = [], flags = {}) { export function patchScore(rawScoreText, instruments, bars = [], info = null, articles = []) {
const log = []; const log = [];
const SEP = '\n---\n'; const SEP = '\n---\n';
const [header, ...barDocs] = rawScoreText.split(SEP); const [header, ...barDocs] = rawScoreText.split(SEP);
let patchedHeader = _patchMetadata(header, info); let patchedHeader = _patchMetadata(header, info);
const dirtyArticles = articles.filter(a => a.isDirty); if (articles.length) {
if (dirtyArticles.length || flags.articlesModified) {
patchedHeader = _patchArticles(patchedHeader, articles); patchedHeader = _patchArticles(patchedHeader, articles);
dirtyArticles.forEach(a => log.push({ level: 'changed', path: `articles / ${a.name}` })); articles.forEach(a => log.push({ level: 'changed', path: `articles / ${a.name}` }));
} }
patchedHeader = _patchInstrumentHeader(patchedHeader, instruments); patchedHeader = _patchInstrumentHeader(patchedHeader, instruments);
instruments.forEach(i => { instruments.forEach(i => {
if (i.deleted) log.push({ level: 'changed', path: `instrument / ${i.name} (deleted)` }); if (!(i.isLinked && !i._modified)) log.push({ level: 'changed', path: `instrument / ${i.name}` });
else if (i.isDirty) log.push({ level: 'changed', path: `instrument / ${i.name}` });
}); });
const barMap = Object.fromEntries(bars.map(b => [b.id, b])); const barMap = Object.fromEntries(bars.map(b => [b.id, b]));
const newBars = bars.filter(b => b.isNew && !b.deleted); const newBars = bars.filter(b => b._isNew);
if (!barDocs.length) { if (!barDocs.length) {
newBars.forEach(b => log.push({ level: 'changed', path: `bar / ${b.id}` })); newBars.forEach(b => log.push({ level: 'changed', path: `bar / ${b.id}` }));
return { text: [patchedHeader, ...newBars.map(_buildNewBarDoc)].join(SEP), log }; return { text: [patchedHeader, ...newBars.map(_buildNewBarDoc)].join(SEP), log };
} }
let passedThrough = 0;
const patchedBarDocs = []; const patchedBarDocs = [];
barDocs.forEach(doc => { barDocs.forEach(doc => {
const m = doc.match(/^_id:\s*(\S+)/m); const m = doc.match(/^_id:\s*(\S+)/m);
if (!m) { passedThrough++; patchedBarDocs.push(doc); return; } if (!m) { patchedBarDocs.push(doc); return; }
const bar = barMap[m[1]]; const bar = barMap[m[1]];
if (!bar) { passedThrough++; patchedBarDocs.push(doc); return; } if (!bar) return; // not in model → deleted
if (bar.deleted) return;
if (!bar.isDirty) { passedThrough++; patchedBarDocs.push(doc); return; }
log.push({ level: 'changed', path: `bar / ${bar.id}` }); log.push({ level: 'changed', path: `bar / ${bar.id}` });
patchedBarDocs.push(_patchBarMeta(doc, bar)); patchedBarDocs.push(_patchBarMeta(doc, bar));
}); });
@@ -308,9 +311,5 @@ export function patchScore(rawScoreText, instruments, bars = [], info = null, ar
log.push({ level: 'changed', path: `bar / ${bar.id}` }); 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` });
}
return { text: [patchedHeader, ...patchedBarDocs].join(SEP), log }; return { text: [patchedHeader, ...patchedBarDocs].join(SEP), log };
} }

View File

@@ -25,4 +25,10 @@ export const store = reactive({
markDirty() { markDirty() {
this.isDirty = true; this.isDirty = true;
}, },
resetEditState() {
this.isDirty = false;
this.exportLog = [];
this.focusPath = [];
},
}); });

View File

@@ -55,9 +55,10 @@ ok('articles[0].properties[]', Array.isArray(model.articles[0].properties)
const fProp = model.articles[0].properties.find(p => p.name === 'add_stress'); const fProp = model.articles[0].properties.find(p => p.name === 'add_stress');
ok('articles[0] add_stress prop', fProp && fProp.value === 3 && fProp.scope === 'defaults'); ok('articles[0] add_stress prop', fProp && fProp.value === 3 && fProp.scope === 'defaults');
// Merge across subtypes: same label, different subtypes → one entry, multi-scope props // Article slot structure: stage.article with article.defaults / article.overwrites children
const MERGE_FIXTURE = `01 articles.defaults 'g' add_stress=2 const MERGE_FIXTURE = `01 stage.article 'g'
01 articles.overwrites 'g' pitch_bend=0.05 02 article.defaults 'add_stress' constant=2
02 article.overwrites 'pitch_bend'
`; `;
const mergeRoot = parseAstLog('00 tuning base=\'x\'\n' + MERGE_FIXTURE); const mergeRoot = parseAstLog('00 tuning base=\'x\'\n' + MERGE_FIXTURE);
const mergeModel = buildModel(mergeRoot); const mergeModel = buildModel(mergeRoot);
@@ -67,7 +68,33 @@ ok('merge: 2 properties', mergeModel.articles[0].properties.length
const gDef = mergeModel.articles[0].properties.find(p => p.scope === 'defaults'); const gDef = mergeModel.articles[0].properties.find(p => p.scope === 'defaults');
const gOver = mergeModel.articles[0].properties.find(p => p.scope === 'overwrites'); const gOver = mergeModel.articles[0].properties.find(p => p.scope === 'overwrites');
ok('merge: default scope present', gDef && gDef.name === 'add_stress' && gDef.value === 2); ok('merge: default scope present', gDef && gDef.name === 'add_stress' && gDef.value === 2);
ok('merge: overwrite scope present', gOver && gOver.name === 'pitch_bend' && gOver.value === 0.05); ok('merge: overwrite scope present', gOver && gOver.name === 'pitch_bend' && gOver.value === null);
section('article.definite on stem_note');
const DEF_ART = `00 bar '001P1L1M1'
01 bar.voice 'pi'
02 voice.offset tick=0
03 line.stem_note pitch='G5' articulatory='tailed'
04 article.definite 'tailed' constant=3
04 stem_note.chain _tmp_string='o'
05 chain.clause 0
06 seq.note letters='o' shift=0 length=1 netlength=1
`;
const defArtModel = buildModel(parseAstLog(DEF_ART));
const defSN = defArtModel.bars[0].voices['pi'].offsets[0].stemNotes[0];
ok('definite prop flattened onto stem_note', defSN.tailed === 3);
ok('articulatory not in unknownProps', defSN.unknownProps?.articulatory === undefined);
section('stem_note weight');
const WEIGHT_SN = `00 bar '001P1L1M1'
01 bar.voice 'pi'
02 voice.offset tick=0
03 line.stem_note pitch='C4' weight=0.8
`;
const weightModel = buildModel(parseAstLog(WEIGHT_SN));
const weightSN = weightModel.bars[0].voices['pi'].offsets[0].stemNotes[0];
ok('weight parsed', weightSN.weight === 0.8);
ok('weight null when absent', defSN.weight === null);
// ── Bar IDs ──────────────────────────────────────────────────────────────── // ── Bar IDs ────────────────────────────────────────────────────────────────
// Bar IDs are opaque auto-increment strings; only the raw id string matters. // Bar IDs are opaque auto-increment strings; only the raw id string matters.
@@ -230,8 +257,8 @@ const SCORE_FIXTURE = new URL('./fixtures/pathetique.spls', import.meta.url);
const rawScore = readFileSync(SCORE_FIXTURE, 'utf8'); const rawScore = readFileSync(SCORE_FIXTURE, 'utf8');
// pathetique.spls contains alpha and ki; dev/piano is a linked instrument not embedded. // pathetique.spls contains alpha and ki; dev/piano is a linked instrument not embedded.
// Mark only alpha as dirty, verify ki is preserved verbatim. // All non-linked instruments are always re-serialized; linked+unmodified are passed through.
const patchInstruments = model.instruments.map(i => ({ ...i, isDirty: i.name === 'alpha' })); const patchInstruments = model.instruments.map(i => ({ ...i }));
const { text: patched, log: patchLog } = patchScore(rawScore, patchInstruments); const { text: patched, log: patchLog } = patchScore(rawScore, patchInstruments);
const patchedLines = patched.split('\n'); const patchedLines = patched.split('\n');
@@ -239,16 +266,13 @@ ok('patched score still has instrument alpha:', patchedLines.some(l => /^instrum
ok('patched score still has instrument ki:', patchedLines.some(l => /^instrument\s+ki\s*:/.test(l))); ok('patched score still has instrument ki:', patchedLines.some(l => /^instrument\s+ki\s*:/.test(l)));
ok('patched score alpha block contains character:', patchedLines.some(l => l.trim() === 'character:')); ok('patched score alpha block contains character:', patchedLines.some(l => l.trim() === 'character:'));
ok('patchScore log records alpha as changed', patchLog.some(e => e.level === 'changed' && e.path === 'instrument / alpha')); ok('patchScore log records alpha as changed', patchLog.some(e => e.level === 'changed' && e.path === 'instrument / alpha'));
ok('patchScore log does not record ki (not dirty)', !patchLog.some(e => e.path?.includes('ki'))); ok('patchScore log records ki as changed', patchLog.some(e => e.path?.includes('ki')));
// Ki is clean — its block must appear verbatim (check a unique line from the original) // dev/piano is linked and unmodified — it should pass through verbatim
const kiOrigLines = rawScore.split('\n').filter(l => l.startsWith('instrument ki:') || (l.startsWith(' ') && rawScore.indexOf('instrument ki:') < rawScore.indexOf(l))); const pianoInstr = patchInstruments.find(i => i.name === 'dev/piano');
// Simpler: original ki block should still exist in patched if (pianoInstr) {
const kiOrigIdx = rawScore.indexOf('\ninstrument ki:'); ok('dev/piano is linked', pianoInstr.isLinked === true);
const kiBlock = kiOrigIdx >= 0 ? rawScore.slice(kiOrigIdx + 1, rawScore.indexOf('\ninstrument ', kiOrigIdx + 1) >>> 0 || undefined) : ''; ok('dev/piano not in log (linked+unmodified)', !patchLog.some(e => e.path?.includes('dev/piano')));
if (kiBlock) {
const firstKiLine = kiBlock.split('\n')[0];
ok('ki block preserved verbatim (first line)', patched.includes(firstKiLine));
} }
// patched score must not be empty and must be shorter or same length as original + alpha export // patched score must not be empty and must be shorter or same length as original + alpha export
@@ -421,7 +445,6 @@ voice soprano:
const dirtyBar = { const dirtyBar = {
id: '001P1L1M1', id: '001P1L1M1',
isDirty: true,
stressor: { groups: [[2, 3], [1]] }, stressor: { groups: [[2, 3], [1]] },
tempoLevels: 140, tempoLevels: 140,
upperStressBound: null, upperStressBound: null,
@@ -430,8 +453,7 @@ const dirtyBar = {
}; };
const cleanBar = { const cleanBar = {
id: '001P1L1M2', id: '001P1L1M2',
isDirty: false, stressor: null, tempoLevels: 100,
stressor: null, tempoLevels: null,
upperStressBound: null, lowerStressBound: null, tempoShape: null, upperStressBound: null, lowerStressBound: null, tempoShape: null,
}; };
const { text: barPatched, log: barLog } = patchScore(RAW_SCORE_WITH_BARS, [], [dirtyBar, cleanBar]); const { text: barPatched, log: barLog } = patchScore(RAW_SCORE_WITH_BARS, [], [dirtyBar, cleanBar]);
@@ -439,11 +461,15 @@ const barPatchedLines = barPatched.split('\n');
ok('dirty bar _meta updated with new BPM', barPatched.includes('beats_per_minute: 140')); ok('dirty bar _meta updated with new BPM', barPatched.includes('beats_per_minute: 140'));
ok('dirty bar stress_pattern updated', barPatched.includes('stress_pattern: 2,3;1')); ok('dirty bar stress_pattern updated', barPatched.includes('stress_pattern: 2,3;1'));
ok('dirty bar voice content preserved', barPatched.includes('- C4 4')); ok('dirty bar voice content preserved', barPatched.includes('- C4 4'));
ok('clean bar unchanged', barPatched.includes('beats_per_minute: 100')); ok('clean bar BPM preserved via model', barPatched.includes('beats_per_minute: 100'));
ok('clean bar voice preserved', barPatched.includes('- D4 4')); ok('clean bar voice preserved', barPatched.includes('- D4 4'));
ok('document separators preserved', (barPatched.match(/\n---\n/g) ?? []).length === 2); ok('document separators preserved', (barPatched.match(/\n---\n/g) ?? []).length === 2);
ok('bar log records dirty bar as changed', barLog.some(e => e.level === 'changed' && e.path === 'bar / 001P1L1M1')); ok('bar log records dirty bar as changed', barLog.some(e => e.level === 'changed' && e.path === 'bar / 001P1L1M1'));
ok('bar log has pass-through info entry', barLog.some(e => e.level === 'info' && e.message?.includes('passed through'))); ok('bar log records clean bar as changed', barLog.some(e => e.level === 'changed' && e.path === 'bar / 001P1L1M2'));
// Minimal instrument stub reused across patchScore tests that don't care about instrument content.
const STUB_ALPHA = { name: 'alpha', isLinked: false, variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] };
const STUB_KI = { name: 'ki', isLinked: false, variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] };
// ── patchScore metadata ──────────────────────────────────────────────────── // ── patchScore metadata ────────────────────────────────────────────────────
section('patchScore metadata'); section('patchScore metadata');
@@ -456,26 +482,26 @@ instrument alpha:
A: "1:0,10;1,0" A: "1:0,10;1,0"
`; `;
const updatedInfo = { title: 'New Title', composer: 'New Composer', source: '', encrypter: 'Me' }; const updatedInfo = { title: 'New Title', composer: 'New Composer', source: '', encrypter: 'Me' };
const { text: metaPatched } = patchScore(META_SCORE, [], [], updatedInfo); const { text: metaPatched } = patchScore(META_SCORE, [STUB_ALPHA], [], updatedInfo);
ok('existing title replaced', metaPatched.includes('title: New Title')); ok('existing title replaced', metaPatched.includes('title: New Title'));
ok('existing composer replaced', metaPatched.includes('composer: New Composer')); ok('existing composer replaced', metaPatched.includes('composer: New Composer'));
ok('empty value leaves existing line', metaPatched.includes('source: Some Book')); ok('empty value leaves existing line', metaPatched.includes('source: Some Book'));
ok('new key prepended', metaPatched.includes('encrypter: Me')); ok('new key prepended', metaPatched.includes('encrypter: Me'));
ok('instrument block untouched', metaPatched.includes('instrument alpha:')); ok('instrument block present', metaPatched.includes('instrument alpha:'));
const META_SCORE_NO_TITLE = `composer: Bach\n\ninstrument ki:\n character:\n`; const META_SCORE_NO_TITLE = `composer: Bach\n\ninstrument ki:\n character:\n`;
const { text: noTitlePatched } = patchScore(META_SCORE_NO_TITLE, [], [], { title: 'Fugue', composer: 'Bach' }); const { text: noTitlePatched } = patchScore(META_SCORE_NO_TITLE, [STUB_KI], [], { title: 'Fugue', composer: 'Bach' });
ok('missing title prepended', noTitlePatched.includes('title: Fugue')); ok('missing title prepended', noTitlePatched.includes('title: Fugue'));
ok('existing composer not duplicated', (noTitlePatched.match(/^composer:/mg) ?? []).length === 1); ok('existing composer not duplicated', (noTitlePatched.match(/^composer:/mg) ?? []).length === 1);
const { text: nullInfoPatched } = patchScore(META_SCORE, [], [], null); const { text: nullInfoPatched } = patchScore(META_SCORE, [STUB_ALPHA], [], null);
ok('null info leaves metadata unchanged', nullInfoPatched.includes('title: Old Title')); ok('null info leaves metadata unchanged', nullInfoPatched.includes('title: Old Title'));
// ── patchScore articles ──────────────────────────────────────────────────── // ── patchScore articles ────────────────────────────────────────────────────
section('patchScore articles'); section('patchScore articles');
const ART_SCORE = `articles:\n f: { add_stress: 3 }\n\ninstrument alpha:\n character:\n`; const ART_SCORE = `articles:\n f: { add_stress: 3 }\n\ninstrument alpha:\n character:\n`;
const artModel = [{ name: 'f', isDirty: true, properties: [{ name: 'add_stress', value: 4, scope: 'defaults' }] }]; const artModel = [{ name: 'f', properties: [{ name: 'add_stress', value: 4, scope: 'defaults' }] }];
const { text: artPatched, log: artLog } = patchScore(ART_SCORE, [], [], null, artModel); const { text: artPatched, log: artLog } = patchScore(ART_SCORE, [STUB_ALPHA], [], null, artModel);
ok('article block replaced', artPatched.includes('articles:')); ok('article block replaced', artPatched.includes('articles:'));
ok('updated value written', artPatched.includes('add_stress: 4')); ok('updated value written', artPatched.includes('add_stress: 4'));
ok('old flow-style entry removed', !artPatched.includes('{ add_stress: 3 }')); ok('old flow-style entry removed', !artPatched.includes('{ add_stress: 3 }'));
@@ -483,33 +509,34 @@ ok('instrument line still present', artPatched.includes('instrument alpha:'));
ok('article change logged', artLog.some(e => e.level === 'changed' && e.path === 'articles / f')); ok('article change logged', artLog.some(e => e.level === 'changed' && e.path === 'articles / f'));
const ART_SCORE_NO_ART = `instrument alpha:\n character:\n`; const ART_SCORE_NO_ART = `instrument alpha:\n character:\n`;
const { text: artInserted } = patchScore(ART_SCORE_NO_ART, [], [], null, artModel); const { text: artInserted } = patchScore(ART_SCORE_NO_ART, [STUB_ALPHA], [], null, artModel);
ok('article block inserted before instrument when missing', artInserted.indexOf('articles:') < artInserted.indexOf('instrument alpha:')); ok('article block inserted before instrument when missing', artInserted.indexOf('articles:') < artInserted.indexOf('instrument alpha:'));
const artClean = [{ name: 'f', isDirty: false, properties: [{ name: 'add_stress', value: 3, scope: 'defaults' }] }]; // ── patchScore articles with overwrites ───────────────────────────────────
const { text: artUntouched } = patchScore(ART_SCORE, [], [], null, artClean); section('patchScore articles with overwrites');
ok('clean articles section left verbatim', artUntouched.includes('{ add_stress: 3 }')); const OVW_SCORE = `articles:\n f:\n add_stress: 3\n\ninstrument alpha:\n character:\n`;
const ovwModel = [{ name: 'f', properties: [
// ── patchScore articlesModified flag ────────────────────────────────────── { name: 'add_stress', value: 4, scope: 'defaults' },
section('patchScore articlesModified flag'); { name: 'add_stress', scope: 'overwrites' },
const artCleanForFlag = [{ name: 'f', isDirty: false, properties: [{ name: 'add_stress', value: 99, scope: 'defaults' }] }]; ]}];
const { text: artFlagPatched, log: artFlagLog } = patchScore(ART_SCORE, [], [], null, artCleanForFlag, { articlesModified: true }); const { text: ovwPatched } = patchScore(OVW_SCORE, [STUB_ALPHA], [], null, ovwModel);
ok('articlesModified forces re-serialization', artFlagPatched.includes('add_stress: 99')); ok('overwrites emit -important block', ovwPatched.includes('-important:'));
ok('articlesModified: old flow-style replaced', !artFlagPatched.includes('{ add_stress: 3 }')); ok('overwrites list entry present', ovwPatched.includes('- add_stress'));
// ── patchScore deleted bars ──────────────────────────────────────────────── // ── patchScore deleted bars ────────────────────────────────────────────────
section('patchScore deleted bars'); section('patchScore deleted bars');
const delBar1 = { id: '001P1L1M1', isDirty: true, deleted: true, stressor: null, tempoLevels: null, upperStressBound: null, lowerStressBound: null, tempoShape: null }; const delBar2 = { id: '001P1L1M2', stressor: null, tempoLevels: null, upperStressBound: null, lowerStressBound: null, tempoShape: null };
const delBar2 = { id: '001P1L1M2', isDirty: false, stressor: null, tempoLevels: null, upperStressBound: null, lowerStressBound: null, tempoShape: null }; const { text: delBarPatched } = patchScore(RAW_SCORE_WITH_BARS, [], [delBar2]);
const { text: delBarPatched } = patchScore(RAW_SCORE_WITH_BARS, [], [delBar1, delBar2]);
ok('deleted bar removed from output', !delBarPatched.includes('_id: 001P1L1M1')); ok('deleted bar removed from output', !delBarPatched.includes('_id: 001P1L1M1'));
ok('non-deleted bar preserved', delBarPatched.includes('_id: 001P1L1M2')); ok('non-deleted bar preserved', delBarPatched.includes('_id: 001P1L1M2'));
ok('deleted bar reduces document count', (delBarPatched.match(/\n---\n/g) ?? []).length === 1); ok('deleted bar reduces document count', (delBarPatched.match(/\n---\n/g) ?? []).length === 1);
// ── patchScore new bars ──────────────────────────────────────────────────── // ── patchScore new bars ────────────────────────────────────────────────────
section('patchScore new bars'); section('patchScore new bars');
const newBarEntry = { id: '001P1L1M3', isDirty: true, isNew: true, stressor: null, tempoLevels: 120, upperStressBound: null, lowerStressBound: null, tempoShape: null }; const existingBar1 = { id: '001P1L1M1', stressor: null, tempoLevels: 120, upperStressBound: null, lowerStressBound: null, tempoShape: null };
const { text: newBarPatched, log: newBarLog } = patchScore(RAW_SCORE_WITH_BARS, [], [newBarEntry]); const existingBar2 = { id: '001P1L1M2', stressor: null, tempoLevels: 100, upperStressBound: null, lowerStressBound: null, tempoShape: null };
const newBarEntry = { id: '001P1L1M3', _isNew: true, stressor: null, tempoLevels: 120, upperStressBound: null, lowerStressBound: null, tempoShape: null };
const { text: newBarPatched, log: newBarLog } = patchScore(RAW_SCORE_WITH_BARS, [], [existingBar1, existingBar2, newBarEntry]);
ok('new bar appended to output', newBarPatched.includes('_id: 001P1L1M3')); ok('new bar appended to output', newBarPatched.includes('_id: 001P1L1M3'));
ok('new bar has _meta with BPM', newBarPatched.includes('beats_per_minute: 120')); ok('new bar has _meta with BPM', newBarPatched.includes('beats_per_minute: 120'));
ok('new bar appended as extra document', (newBarPatched.match(/\n---\n/g) ?? []).length === 3); ok('new bar appended as extra document', (newBarPatched.match(/\n---\n/g) ?? []).length === 3);
@@ -519,13 +546,12 @@ ok('new bar logged as changed', newBarLog.some(e => e.level === 'changed' && e.p
section('patchScore deleted instrument'); section('patchScore deleted instrument');
const delInstrScore = `instrument alpha:\n character:\n A: "1:0,10;1,0"\n\ninstrument ki:\n character:\n A: "1:0,5;1,0"\n`; const delInstrScore = `instrument alpha:\n character:\n A: "1:0,10;1,0"\n\ninstrument ki:\n character:\n A: "1:0,5;1,0"\n`;
const delInstrList = [ const delInstrList = [
{ name: 'alpha', deleted: true, isDirty: false, variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] }, { name: 'ki', variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] },
{ name: 'ki', deleted: false, isDirty: false, variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] },
]; ];
const { text: delInstrPatched, log: delInstrLog } = patchScore(delInstrScore, delInstrList); const { text: delInstrPatched, log: delInstrLog } = patchScore(delInstrScore, delInstrList);
ok('deleted instrument removed from output', !delInstrPatched.includes('instrument alpha:')); ok('deleted instrument removed from output', !delInstrPatched.includes('instrument alpha:'));
ok('non-deleted instrument preserved', delInstrPatched.includes('instrument ki:')); ok('non-deleted instrument preserved', delInstrPatched.includes('instrument ki:'));
ok('deleted instrument logged', delInstrLog.some(e => e.level === 'changed' && e.path?.includes('alpha') && e.path?.includes('deleted'))); ok('ki re-serialized and logged', delInstrLog.some(e => e.level === 'changed' && e.path?.includes('ki')));
// ── Motif parsing ───────────────────────────────────────────────────────── // ── Motif parsing ─────────────────────────────────────────────────────────
section('Motif parsing — dynamic motif'); section('Motif parsing — dynamic motif');