From 159c71367d4b64ca4bc54f7681df26c3b52be9e4 Mon Sep 17 00:00:00 2001 From: rdrew Date: Tue, 10 Nov 2026 11:06:41 -0400 Subject: [PATCH] annotation spaces --- js/fix-annotation-spaces.js | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/js/fix-annotation-spaces.js b/js/fix-annotation-spaces.js index 21afbcf..5f1322b 100644 --- a/js/fix-annotation-spaces.js +++ b/js/fix-annotation-spaces.js @@ -1,19 +1,30 @@ -(function (Drupal, once) { - Drupal.behaviors.fixMiradorAnnotationSpaces = { - attach: function (context, settings) { - // Find all Mirador annotation HTML content wrappers - const annotationElements = once( - "fix-annotation-spaces", - ".mirador-third-party-html", - context - ); +(function () { + // Guard against running multiple observers if the script is loaded more than once + if (window.__miradorSpaceFixInitialized) return; + window.__miradorSpaceFixInitialized = true; - annotationElements.forEach((el) => { - // Replace both the literal ' ' string and the non-breaking space character (\u00A0) + const observer = new MutationObserver((mutations) => { + // Target any third-party HTML content blocks in Mirador that haven't been cleaned yet + const elements = document.querySelectorAll( + ".mirador-third-party-html:not([data-fixed])" + ); + + if (elements.length > 0) { + elements.forEach((el) => { + // Swap non-breaking spaces for normal breakable spaces el.innerHTML = el.innerHTML .replace(/ /g, " ") .replace(/\u00A0/g, " "); + + // Mark element so we don't re-process it on subsequent mutations + el.setAttribute("data-fixed", "true"); }); - }, - }; -})(Drupal, once); + } + }); + + // Start observing the document body for dynamically added Mirador elements + observer.observe(document.body, { + childList: true, + subtree: true, + }); +})();