diff --git a/components/flatEditor.js b/components/flatEditor.js index 2a4f2c4..c8ae57d 100644 --- a/components/flatEditor.js +++ b/components/flatEditor.js @@ -264,7 +264,7 @@ function FlatEditor({ .ready() .then(() => { if (score.scoreId === 'blank' && orig) { - let result = pitchesToRests(JSON.parse(orig)); + let result = pitchesToRests(JSON.parse(orig)); // TODO: watch out here for the uuid issues if (trim) { result = trimScore(result, trim); } @@ -293,15 +293,37 @@ function FlatEditor({ embed.loadJSON(result).then(() => { embed.off('cursorPosition'); embed.on('cursorPosition', (ev) => { + // FIXME probably we need to debounce this event. when the paste has succeeded, don't notice that resulting change of cursor and try to do this again. console.log('cursorPos', ev) // selectedMeasure console.log('json.current', json.current) - if (json.current && selectedMeasure && selectedMeasure.current && JSON.stringify(selectedMeasure.current)!== JSON && json.current !== '{}') { + if (json.current && selectedMeasure && selectedMeasure.current && JSON.stringify(selectedMeasure.current)!== '{}' && json.current !== '{}') { console.log('selectedMeasure.current', selectedMeasure.current) const scoreData = JSON.parse(json.current); const correctedSelection = correctMeasure(JSON.parse(JSON.stringify(selectedMeasure.current))); console.log('correctedSelection', correctedSelection) scoreData['score-partwise'].part[0].measure[ev.measureIdx] = correctedSelection + //FIXME + if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx], 'attributes')) { + console.log('no attributes', scoreData['score-partwise'].part[0].measure[ev.measureIdx]) + scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes = [{}]; + + } + if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0], '$adagio-location')) { + scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0]['$adagio-location'] = { + "timePos": 0, + "dpq": 1 + } + } + if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0], '$adagio-time')) { + scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0]['$adagio-time'] = { + "beats": "4", + "beat-type": "4" + } + } + if (Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx], 'barline')) { + delete scoreData['score-partwise'].part[0].measure[ev.measureIdx].barline; + } selectedMeasure.current = {}; const toLoad = JSON.stringify(scoreData); console.log('erroneous toLoad', toLoad); diff --git a/lib/flat.js b/lib/flat.js index 70624fb..42f084b 100644 --- a/lib/flat.js +++ b/lib/flat.js @@ -11,6 +11,8 @@ function colorMap (color) { const pitchesToRests = (pieceScoreJSON) => { + // overwrite divisions + // ensure that duration and timePos reflect the updated divisions const getMeasureTimeSignature = (measure, current) => { let duration = 8; // default to 8 because i reasoned it might be a quarter in some cases let maxRests = 4; // bc 4 is a common denominator for musicians @@ -18,6 +20,7 @@ const pitchesToRests = (pieceScoreJSON) => { if (measure.attributes) { measure.attributes.forEach((attribute) => { if (attribute.divisions) { + console.log('p2Rests::getMTimeSig:divisions', attribute.divisions) duration = attribute.divisions; updated = true; } @@ -49,6 +52,9 @@ const pitchesToRests = (pieceScoreJSON) => { // } composeScoreJSON['score-partwise'].part[0].measure.forEach((measure) => { + if (measure.attributes && measure.attributes[0].divisions) { + measure.attributes[0].divisions = 2; + } currentTimeSig = getMeasureTimeSignature(measure, currentTimeSig); // FIXME: this overwrites later measures with the default if (measure.direction) { measure.direction.forEach((directionObj) => { diff --git a/lib/variations.js b/lib/variations.js index 64e99ac..68bd3db 100644 --- a/lib/variations.js +++ b/lib/variations.js @@ -50,8 +50,11 @@ function correctMeasure(measure) { // console.log("origDivisions", alteredMeasure.attributes[0].divisions); // console.log("inside correctMeasure measure", JSON.parse(JSON.stringify(alteredMeasure))); + let modify = false; // check if reconstruction necessary - let modify = origDivisions != defaultDiv && (alteredMeasure.attributes[0].divisions = defaultDiv); + if (Object.hasOwn(alteredMeasure, 'attributes') && Array.isArray(alteredMeasure.attributes) && alteredMeasure.attributes.length > 0) { + modify = origDivisions != defaultDiv && (alteredMeasure.attributes[0].divisions = defaultDiv); + } // only modify if necessary modify && (alteredMeasure.note.forEach((note) => { @@ -62,7 +65,7 @@ function correctMeasure(measure) { // an explicit property describing whether a beat is a rest makes life easier // (idt you can *always* use null in the way you can in a lang like C, so // this is more out of an abundance of caution) - alteredMeasure.note.forEach((note) => { + alteredMeasure?.note?.forEach((note) => { note.isRest = (Boolean) (note.rest ?? false); }); // console.log(`outgoing corrected Measure w/ isRest property`, [...alteredMeasure.note]);