From e6d2846bed3931622977eb255067c94f5769ed9a Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 24 Nov 2022 20:21:03 +0100 Subject: [PATCH 1/4] Fix markdown anchor re-clicking The hashchange event did not fire on re-click of a active anchor. Instead, use the click event. Fixes: https://github.com/go-gitea/gitea/issues/21680 --- web_src/js/markup/anchors.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index cc2ed5db78e48..7ca3a87841243 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -2,10 +2,10 @@ import {svg} from '../svg.js'; const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6'; -function scrollToAnchor() { - if (document.querySelector(':target')) return; - if (!window.location.hash || window.location.hash.length <= 1) return; - const id = decodeURIComponent(window.location.hash.substring(1)); +function scrollToAnchor(hash) { + if (document.querySelector(':target')) return; // something else on the page matches + if (hash?.length <= 1) return; + const id = decodeURIComponent(hash.substring(1)); const el = document.getElementById(`user-content-${id}`); if (el) { el.scrollIntoView(); @@ -24,9 +24,11 @@ export function initMarkupAnchors() { a.classList.add('anchor'); a.setAttribute('href', `#${encodeURIComponent(originalId)}`); a.innerHTML = svg('octicon-link'); + a.addEventListener('click', (e) => { + scrollToAnchor(e.currentTarget.getAttribute('href')); + }); heading.prepend(a); } - scrollToAnchor(); - window.addEventListener('hashchange', scrollToAnchor); + scrollToAnchor(window.location.hash); } From df6c8aadb983246e164838c4c4fa131f4da55649 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 25 Nov 2022 09:22:06 +0100 Subject: [PATCH 2/4] add initial condition --- web_src/js/markup/anchors.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 7ca3a87841243..4c8a205d6e6f3 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -2,8 +2,9 @@ import {svg} from '../svg.js'; const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6'; -function scrollToAnchor(hash) { - if (document.querySelector(':target')) return; // something else on the page matches +function scrollToAnchor(hash, initial) { + // abort on page load if the browser has already scrolled to another anchor + if (initial && document.querySelector(':target')) return; if (hash?.length <= 1) return; const id = decodeURIComponent(hash.substring(1)); const el = document.getElementById(`user-content-${id}`); @@ -30,5 +31,5 @@ export function initMarkupAnchors() { heading.prepend(a); } - scrollToAnchor(window.location.hash); + scrollToAnchor(window.location.hash, true); } From 5746f9d78469c2406c02d40568b10a3ff6c06b82 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 25 Nov 2022 09:23:39 +0100 Subject: [PATCH 3/4] reword comment --- web_src/js/markup/anchors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index 4c8a205d6e6f3..f7b2b8a070f81 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -3,7 +3,7 @@ import {svg} from '../svg.js'; const headingSelector = '.markup h1, .markup h2, .markup h3, .markup h4, .markup h5, .markup h6'; function scrollToAnchor(hash, initial) { - // abort on page load if the browser has already scrolled to another anchor + // abort if the browser has already scrolled to another anchor during page load if (initial && document.querySelector(':target')) return; if (hash?.length <= 1) return; const id = decodeURIComponent(hash.substring(1)); From 91a34cc15700af7c621ee0f5c6ff493201c322e4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 25 Nov 2022 09:24:39 +0100 Subject: [PATCH 4/4] avoid implicit false --- web_src/js/markup/anchors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/markup/anchors.js b/web_src/js/markup/anchors.js index f7b2b8a070f81..53dfa2980c0a1 100644 --- a/web_src/js/markup/anchors.js +++ b/web_src/js/markup/anchors.js @@ -26,7 +26,7 @@ export function initMarkupAnchors() { a.setAttribute('href', `#${encodeURIComponent(originalId)}`); a.innerHTML = svg('octicon-link'); a.addEventListener('click', (e) => { - scrollToAnchor(e.currentTarget.getAttribute('href')); + scrollToAnchor(e.currentTarget.getAttribute('href'), false); }); heading.prepend(a); }