Skip to content

Commit edfb3b6

Browse files
author
Thomas Hanke
committed
scroll waits till all images ae loaded
1 parent 85419ae commit edfb3b6

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

ckanext/markdown_view/assets/markdown_view.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ marked.use({ renderer });
1818

1919
document.addEventListener("DOMContentLoaded", async function () {
2020

21-
const pageUrl = document.getElementById('markdown_content').getAttribute('data-page-url');
21+
const contentDiv = document.getElementById("markdown_content");
22+
const pageUrl = contentDiv.getAttribute('data-page-url');
2223

24+
2325
try {
2426
const response = await fetch(pageUrl); // Path to raw Markdown file
2527
const blob = await response.blob(); // Unwrap to a blob...
@@ -52,12 +54,49 @@ document.addEventListener("DOMContentLoaded", async function () {
5254
parsedHTML = marked.parse(markdown.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, ""));
5355
parsedHTML = DOMPurify.sanitize(parsedHTML);
5456
}
55-
57+
5658
// Inject sanitized HTML into the page
57-
const contentDiv = document.getElementById("markdown_content");
5859
contentDiv.innerHTML = parsedHTML;
5960

60-
61+
// After content is injected, get all images
62+
const images = contentDiv.getElementsByTagName('img');
63+
let loadedImagesCount = 0;
64+
65+
// Function to scroll to the highlighted element
66+
const scrollToHighlighted = () => {
67+
const highlightedElement = document.getElementById('highlighted');
68+
if (highlightedElement) {
69+
highlightedElement.scrollIntoView({
70+
behavior: "smooth",
71+
block: "start",
72+
inline: "nearest"
73+
});
74+
}
75+
};
76+
77+
// If there are images, wait for all of them to load
78+
if (images.length > 0) {
79+
for (let img of images) {
80+
img.addEventListener('load', () => {
81+
loadedImagesCount++;
82+
// If all images are loaded, scroll
83+
if (loadedImagesCount === images.length) {
84+
scrollToHighlighted();
85+
}
86+
});
87+
88+
img.addEventListener('error', () => {
89+
// Handle image loading errors if necessary
90+
loadedImagesCount++;
91+
if (loadedImagesCount === images.length) {
92+
scrollToHighlighted();
93+
}
94+
});
95+
}
96+
} else {
97+
// If no images, scroll immediately
98+
scrollToHighlighted();
99+
}
61100
// Render math in the body
62101
renderMathInElement(document.body, {
63102
delimiters: [
@@ -68,20 +107,7 @@ document.addEventListener("DOMContentLoaded", async function () {
68107
],
69108
throwOnError: false
70109
});
71-
// Use requestAnimationFrame to ensure the scroll happens after rendering
72-
requestAnimationFrame(() => {
73-
// Delay scrolling slightly to allow other JS to affect rendering
74-
setTimeout(() => {
75-
const highlightedElement = document.getElementById('highlighted');
76-
if (highlightedElement) {
77-
highlightedElement.scrollIntoView({
78-
behavior: "smooth",
79-
block: "start",
80-
inline: "nearest"
81-
});
82-
}
83-
}, 500); // Adjust the delay as necessary
84-
});
110+
85111
} catch (error) {
86112
console.error("Failed to load Markdown content:", error);
87113
}

0 commit comments

Comments
 (0)