From 8cf4146a48e3b8c71cc0fa1498f2d881bca1ac3c Mon Sep 17 00:00:00 2001 From: Robin Nicole Date: Fri, 18 Jul 2025 14:05:37 +0200 Subject: [PATCH 1/2] fix: error when hovering on metarelations --- src/js/action/draw.js | 20 +++++++++++++++----- src/js/utils/misc.js | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/js/action/draw.js b/src/js/action/draw.js index 690f9e0b..5d192eca 100644 --- a/src/js/action/draw.js +++ b/src/js/action/draw.js @@ -26,6 +26,7 @@ import { draw_slur, isSlurDownward, get_by_id, + get_by_oldid, handleFlip } from '../utils/misc' @@ -475,8 +476,13 @@ function addHoverClassToChildren(element, isRoot, isPrimary, draw_context, mei_g // Add hover class to the meta-relation itself if (!isRoot) element.classList.add(isPrimary ? 'extrarelationhover' : 'relationhover') - // Get the corresponding MEI node using the oldid attribute - let meiNode = get_by_id(mei_graph, element.getAttribute('oldid') || element.id) + let meiNode = get_by_oldid(mei_graph, element.getAttribute('oldid')) + if (!meiNode) { + let id = element.id.slice(element.id.match(/^\d/)) + meiNode = get_by_id(mei_graph, id) + } + + if (!meiNode || !meiNode.length) return // Recursively add hover class to the children let primaries = relation_primaries(mei_graph, meiNode).map( @@ -504,9 +510,13 @@ function removeHoverClassToChildren(element, isRoot, isPrimary, draw_context, me // Remove hover class from the meta-relation itself if (!isRoot) element.classList.remove(isPrimary ? 'extrarelationhover' : 'relationhover') - // Get the corresponding MEI node using the oldid attribute - // TODO: might be undefined and produce error - let meiNode = get_by_id(mei_graph, element.getAttribute('oldid') || element.id) + let meiNode = get_by_oldid(mei_graph, element.getAttribute('oldid')) + if (!meiNode) { + let id = element.id.slice(element.id.match(/^\d/)) + meiNode = get_by_id(mei_graph, id) + } + + if (!meiNode || !meiNode.length) return // Recursively remove hover class from the children let primaries = relation_primaries(mei_graph, meiNode).map( diff --git a/src/js/utils/misc.js b/src/js/utils/misc.js index 3f150c3a..66edab20 100644 --- a/src/js/utils/misc.js +++ b/src/js/utils/misc.js @@ -294,6 +294,8 @@ export function note_coords(note) { // Gets all elements from the doc with the oldid export function get_by_oldid(doc, id) { + if (!id) + return null if (id[0] == '#') { id = id.slice(1) } var elems = doc.querySelectorAll('[*|oldid=\'' + id + '\']') if (elems) { From 3e7cb38548b8db863b5b6e95e6b78091b409225a Mon Sep 17 00:00:00 2001 From: Robin Nicole Date: Fri, 18 Jul 2025 15:48:38 +0200 Subject: [PATCH 2/2] chore: modularise a piece of code --- src/js/action/draw.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/js/action/draw.js b/src/js/action/draw.js index 5d192eca..dc0ed396 100644 --- a/src/js/action/draw.js +++ b/src/js/action/draw.js @@ -467,6 +467,25 @@ export function draw_metarelation(draw_context, mei_graph, g_elem) { return added } +/** + * Gets the element by old ID, and gets it by ID if not found + * + * @param {MEI_graph} mei_graph The graph to look the element into + * @param {element} el The element to look for + * + * @return {MEI_node} The node in the MEI of the element + */ +function get_by_oldid_and_id(mei_graph, el) { + + let meiNode = get_by_oldid(mei_graph, el.getAttribute('oldid')) + if (!meiNode) { + let id = el.id.slice(el.id.match(/^\d/)) + meiNode = get_by_id(mei_graph, id) + } + + return meiNode +} + // Recursively add hover class to elements and their children function addHoverClassToChildren(element, isRoot, isPrimary, draw_context, mei_graph) { if (!element) return @@ -476,11 +495,7 @@ function addHoverClassToChildren(element, isRoot, isPrimary, draw_context, mei_g // Add hover class to the meta-relation itself if (!isRoot) element.classList.add(isPrimary ? 'extrarelationhover' : 'relationhover') - let meiNode = get_by_oldid(mei_graph, element.getAttribute('oldid')) - if (!meiNode) { - let id = element.id.slice(element.id.match(/^\d/)) - meiNode = get_by_id(mei_graph, id) - } + let meiNode = get_by_oldid_and_id(mei_graph, element) if (!meiNode || !meiNode.length) return @@ -510,11 +525,7 @@ function removeHoverClassToChildren(element, isRoot, isPrimary, draw_context, me // Remove hover class from the meta-relation itself if (!isRoot) element.classList.remove(isPrimary ? 'extrarelationhover' : 'relationhover') - let meiNode = get_by_oldid(mei_graph, element.getAttribute('oldid')) - if (!meiNode) { - let id = element.id.slice(element.id.match(/^\d/)) - meiNode = get_by_id(mei_graph, id) - } + let meiNode = get_by_oldid_and_id(mei_graph, element) if (!meiNode || !meiNode.length) return