From cdadc1e5704b2ccfd99168e9f4f1b8734e65686b Mon Sep 17 00:00:00 2001 From: igoradamenko Date: Sat, 26 May 2018 21:45:41 +0300 Subject: [PATCH] improve handling hashchange: preventdefault if newurl contains remark hash --- web/app/components/root/root.jsx | 1 + web/app/embed.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/web/app/components/root/root.jsx b/web/app/components/root/root.jsx index 77b31e5c..4744281c 100644 --- a/web/app/components/root/root.jsx +++ b/web/app/components/root/root.jsx @@ -70,6 +70,7 @@ export default class Root extends Component { }); setTimeout(this.checkUrlHash); + window.addEventListener('hashchange', this.checkUrlHash); }); } diff --git a/web/app/embed.js b/web/app/embed.js index a411f6c2..43cf3428 100644 --- a/web/app/embed.js +++ b/web/app/embed.js @@ -1,4 +1,4 @@ -import { BASE_URL, NODE_ID } from 'common/constants'; +import { BASE_URL, NODE_ID, COMMENT_NODE_CLASSNAME_PREFIX } from 'common/constants'; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); @@ -69,13 +69,18 @@ function init() { // so let's check it and prevent our scrolling if it so if (data.scrollTo + iframeTop === 0) return; - window.scrollTo(window.pageXOffset, data.scrollTo + iframe.getBoundingClientRect().top); + window.scrollTo(window.pageXOffset, data.scrollTo + iframeTop); } } catch (e) {} } - function postHashToIframe() { - iframe.contentWindow.postMessage(JSON.stringify({ hash: location.hash }), '*'); + function postHashToIframe(e) { + if (!e && location.hash.indexOf(`#${COMMENT_NODE_CLASSNAME_PREFIX}`) === 0) { + iframe.contentWindow.postMessage(JSON.stringify({ hash: location.hash }), '*'); + } else if (e && e.newURL.includes(`#${COMMENT_NODE_CLASSNAME_PREFIX}`)) { + e.preventDefault(); + iframe.contentWindow.postMessage(JSON.stringify({ hash: `#${e.newURL.split('#')[1]}` }), '*'); + } } }