improve handling hashchange: preventdefault if newurl contains remark hash

This commit is contained in:
igoradamenko
2018-05-26 21:45:41 +03:00
parent 90cc29e19c
commit cdadc1e570
2 changed files with 10 additions and 4 deletions
+1
View File
@@ -70,6 +70,7 @@ export default class Root extends Component {
});
setTimeout(this.checkUrlHash);
window.addEventListener('hashchange', this.checkUrlHash);
});
}
+9 -4
View File
@@ -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]}` }), '*');
}
}
}