From c78dfd7c09646f3ea689ba76d7bc42183343d135 Mon Sep 17 00:00:00 2001 From: shanshuirenjia <978987373@qq.com> Date: Fri, 27 May 2022 14:08:38 +0800 Subject: [PATCH 1/2] repair refresh page scroll bug --- frontend/src/components/wiki-markdown-viewer.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/frontend/src/components/wiki-markdown-viewer.js b/frontend/src/components/wiki-markdown-viewer.js index d9c55b325f..ddb8870c93 100644 --- a/frontend/src/components/wiki-markdown-viewer.js +++ b/frontend/src/components/wiki-markdown-viewer.js @@ -70,6 +70,15 @@ class WikiMarkdownViewer extends React.Component { this.titlesInfo = markdownViewer.titlesInfo; } + getTitlesInfo = () => { + let titlesInfo = []; + let headingList = document.querySelectorAll('h2[id^="user-content"], h3[id^="user-content"]'); + for (let i = 0; i < headingList.length; i++) { + titlesInfo.push(headingList[i].offsetTop); + } + this.titlesInfo = titlesInfo; + } + onLinkClick = (event) => { event.preventDefault(); event.stopPropagation(); @@ -89,6 +98,10 @@ class WikiMarkdownViewer extends React.Component { onScrollHandler = () => { const contentScrollTop = this.markdownContainer.current.scrollTop + 180; let titlesLength = this.titlesInfo.length; + if (titlesLength === 0) { + this.getTitlesInfo(); + titlesLength = this.titlesInfo.length; + } let activeTitleIndex; if (contentScrollTop <= this.titlesInfo[0]) { activeTitleIndex = 0; From 507e318a5fa5f17e9f034888453f40a551018937 Mon Sep 17 00:00:00 2001 From: shanshuirenjia <978987373@qq.com> Date: Fri, 27 May 2022 15:08:50 +0800 Subject: [PATCH 2/2] optimize code --- frontend/src/components/wiki-markdown-viewer.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/wiki-markdown-viewer.js b/frontend/src/components/wiki-markdown-viewer.js index ddb8870c93..fcfd317585 100644 --- a/frontend/src/components/wiki-markdown-viewer.js +++ b/frontend/src/components/wiki-markdown-viewer.js @@ -39,6 +39,8 @@ class WikiMarkdownViewer extends React.Component { this.links.forEach(link => { link.addEventListener('click', this.onLinkClick); }); + + this.getTitlesInfo(); } componentWillReceiveProps(nextProps) { @@ -57,6 +59,9 @@ class WikiMarkdownViewer extends React.Component { this.links.forEach(link => { link.addEventListener('click', this.onLinkClick); }); + if (this.titlesInfo.length === 0) { + this.getTitlesInfo(); + } } componentWillUnmount() { @@ -66,10 +71,6 @@ class WikiMarkdownViewer extends React.Component { }); } - onContentRendered = (markdownViewer) => { - this.titlesInfo = markdownViewer.titlesInfo; - } - getTitlesInfo = () => { let titlesInfo = []; let headingList = document.querySelectorAll('h2[id^="user-content"], h3[id^="user-content"]'); @@ -98,10 +99,6 @@ class WikiMarkdownViewer extends React.Component { onScrollHandler = () => { const contentScrollTop = this.markdownContainer.current.scrollTop + 180; let titlesLength = this.titlesInfo.length; - if (titlesLength === 0) { - this.getTitlesInfo(); - titlesLength = this.titlesInfo.length; - } let activeTitleIndex; if (contentScrollTop <= this.titlesInfo[0]) { activeTitleIndex = 0; @@ -183,7 +180,6 @@ class WikiMarkdownViewer extends React.Component { scriptSource={mediaUrl + 'js/mathjax/tex-svg.js'} markdownContent={this.props.markdownContent} activeTitleIndex={this.state.activeTitleIndex} - onContentRendered={this.onContentRendered} modifyValueBeforeRender={this.modifyValueBeforeRender} /> ); @@ -195,7 +191,6 @@ class WikiMarkdownViewer extends React.Component { scriptSource={mediaUrl + 'js/mathjax/tex-svg.js'} markdownContent={this.props.markdownContent} activeTitleIndex={this.state.activeTitleIndex} - onContentRendered={this.onContentRendered} /> ); }