Skip to content

Commit

Permalink
Add highlighting of active melody when displaying EsAC data.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigsapp committed Sep 9, 2024
1 parent 7ef67dc commit fb10bc0
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 1 deletion.
1 change: 1 addition & 0 deletions _includes/vhv-scripts/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ function replaceEditorContentWithHumdrumFile(text, page) {
if (!text) {
text = getTextFromEditor();
}
clearAllAceMarkers();
if (!text) {
console.log("No content to convert to Humdrum");
return;
Expand Down
117 changes: 117 additions & 0 deletions _includes/vhv-scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function setupAceEditor(idtag) {
if (!FreezeRendering) {
displayNotation();
}
updateMelodyStartLineForEsac();
}
} else {
highlightNoteInScore(event)
Expand Down Expand Up @@ -111,7 +112,123 @@ function setupAceEditor(idtag) {
}

insertSplashMusic();
}



//////////////////////////////
//
// updateMelodyStartLineForEsac
//

function updateMelodyStartLineForEsac() {
if (EditorMode !== "esac") {
clearAllAceMarkers();
}

let cursorPosition = EDITOR.getCursorPosition();
let currentLine = cursorPosition.row;
let startLine = Math.max(0, currentLine - 50);
let endLine = Math.min(EDITOR.session.getLength() - 1, currentLine + 50);
let lines = EDITOR.session.getLines(startLine, endLine + 1);
let regex = /^[A-Z0-9_-]+\s*$/;

function findRange(lines, startLine, currentLine, regex) {
let start = -1;
let end = -1;

// Check if the current line matches the regex
if (regex.test(lines[currentLine - startLine])) {
start = currentLine; // Set the start line to the current line
} else {
// Search backward from current line to find first line matching regex
for (let i = currentLine - startLine; i >= 0; i--) {
if (regex.test(lines[i])) {
start = i + startLine; // Set start to first matching line above cursor
break;
}
}
}

// If no start line is found, return early
//if (start === -1) {
// return { start: start, end: end };
//}

// Search forward from the line after the start line to find the next line matching the regex
let startj = 0;
if (start < 0) {
for (let j = 0; j < lines.length; j++) {
if (regex.test(lines[j])) {
startj = j + 1;
start = j; // Set to the next
break;
}
}
}

for (let j = startj; j < lines.length; j++) {
if (regex.test(lines[j])) {
end = (j + startLine) - 1; // Set end to the line before the next matching line
break;
}
}

// If no next matching line is found, set the end to the last line in the window
if (end < 0) {
end = endLine; // Set end to the last line in the window (inclusive)
}
if (start < 0) {
start = 0;
}

return { start: start, end: end };
}

clearAllAceMarkers();

// Find the range of lines to extract within the 50-line window
let range = findRange(lines, startLine, currentLine, regex);
// console.log("RANGE START = ", range.start, "RANGE END = ", range.end);

if (range.start !== -1) {
addAceEditorMarker(range.start);
} else {
console.warn("Cannot find starting line of EsAC data.");
}
}



//////////////////////////////
//
// addAceEditorMarker --
//

function addAceEditorMarker(line) {
console.warn("ADDING MARKER TO LINE", line);
let range = ace.require('ace/range').Range;
let value = EDITOR.session.addMarker(new range(line, 0, line, 1), "esac-highlight", "fullLine", true);
EDITOR.renderer.updateFull(); // Forces the editor to refresh/render
console.error("MARKER RETURN", value);
}



//////////////////////////////
//
// clearAllAceMarks --
//

function clearAllAceMarkers() {
let markers = EDITOR.session.getMarkers(true);
for (let markerId in markers) {
if (markers.hasOwnProperty(markerId)) {
if (markers[markerId].type === "fullLine") {
EDITOR.session.removeMarker(markerId);
}
}
}
}


Expand Down
4 changes: 4 additions & 0 deletions _includes/vhv-scripts/utility-ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ function getMode(text) {
}
if (text.match(/^\s*</)) {
return "xml";
clearAllAceMarkers();
} else if (text.substring(0, 2000).match(/Group memberships:/)) {
clearAllAceMarkers();
return "musedata";
} else if (text.substring(0, 2000).match(/^[A-Za-z0-9+\/\s]+$/)) {
clearAllAceMarkers();
return "mime";
} else if (text.substring(0, 2000).match(/^CUT\[/m)) {
return "esac";
} else {
clearAllAceMarkers();
return "humdrum";
}
}
Expand Down
Loading

1 comment on commit fb10bc0

@craigsapp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example:

Screenshot 2024-09-08 at 9 04 59 PM

Pink highlighting at line 108208 (and not 108221 or 108233) indicates that that melody is being shown in notation on the right side.

Please sign in to comment.