From 7673833b6342cfe55a6f7cf11f5c7005bf862148 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Fri, 19 Jul 2024 22:16:02 +0200 Subject: [PATCH] feat(web4): scroll comments into view (#142) Enables "scroll into view" for proposal comments functionality for https://github.com/NEAR-DevHub/neardevhub-bos/pull/868 Example link to comment being scrolled into view: https://devhublink.testnet.page/proposal/127?accountId=theori.near&blockHeight=121684702 --- src/web4/handler.rs | 4 ++++ src/web4/scroll_comment_into_view.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/web4/scroll_comment_into_view.js diff --git a/src/web4/handler.rs b/src/web4/handler.rs index 7763fcb..1b7a1d3 100644 --- a/src/web4/handler.rs +++ b/src/web4/handler.rs @@ -105,6 +105,7 @@ pub fn web4_get(contract: &Contract, request: Web4Request) -> Web4Response { let title = html_escape::encode_text(&title).to_string(); let description = html_escape::encode_text(&description).to_string(); + let scroll_comment_into_view_js = include_str!("./scroll_comment_into_view.js"); let body = format!( r#" @@ -152,6 +153,9 @@ pub fn web4_get(contract: &Contract, request: Web4Request) -> Web4Response { + "#, url = redirect_path diff --git a/src/web4/scroll_comment_into_view.js b/src/web4/scroll_comment_into_view.js new file mode 100644 index 0000000..fa321db --- /dev/null +++ b/src/web4/scroll_comment_into_view.js @@ -0,0 +1,21 @@ +(async function () { + const urlSearchParams = new URLSearchParams(location.search); + const accountId = urlSearchParams.get("accountId"); + const blockHeight = urlSearchParams.get("blockHeight"); + if (accountId && blockHeight) { + for (let n = 0; n < 20; n++) { + const linkElementSelector = `#${accountId.replace( + /[^a-z0-9]/g, + "" + )}${blockHeight}`; + + const linkElement = document.querySelector(linkElementSelector); + if (linkElement) { + linkElement.scrollIntoView(); + console.log("scrolled into view"); + break; + } + await new Promise((resolve) => setTimeout(() => resolve(), 500)); + } + } + })(); \ No newline at end of file