diff --git a/frontend/src/pages/sdoc/sdoc-file-history/index.js b/frontend/src/pages/sdoc/sdoc-file-history/index.js index 8ffb26157d..1b7b23650a 100644 --- a/frontend/src/pages/sdoc/sdoc-file-history/index.js +++ b/frontend/src/pages/sdoc/sdoc-file-history/index.js @@ -88,34 +88,38 @@ class SdocFileHistory extends React.Component { }; }; + updateLastVersionContent = (currentVersionContent, currentVersion, lastVersion) => { + if (!lastVersion) { + this.setContent(currentVersionContent, ''); + return; + } + if (lastVersion === 'init') { + const lastVersionContent = currentVersionContent ? this.getInitContent(currentVersionContent.children[0]) : this.getInitContentByPath(currentVersion.path); + const validCurrentVersionContent = currentVersionContent || this.getInitContentByPath(currentVersion.path); + this.setContent(validCurrentVersionContent, lastVersionContent); + return; + } + seafileAPI.getFileRevision(historyRepoID, lastVersion.commit_id, lastVersion.path).then(res => { + return res.data ? seafileAPI.getFileContent(res.data) : { data: '' }; + }).then(res => { + const lastVersionContent = res.data; + const firstChildren = currentVersionContent.children[0]; + const validLastVersionContent = lastVersion && !lastVersionContent ? this.getInitContent(firstChildren) : lastVersionContent; + this.setContent(currentVersionContent, validLastVersionContent); + }).catch(error => { + const errorMessage = Utils.getErrorMsg(error, true); + toaster.danger(gettext(errorMessage)); + this.setContent(currentVersionContent, ''); + }); + }; + onSelectHistoryVersion = (currentVersion, lastVersion) => { this.setState({ isLoading: true, currentVersion }); seafileAPI.getFileRevision(historyRepoID, currentVersion.commit_id, currentVersion.path).then(res => { return seafileAPI.getFileContent(res.data); }).then(res => { const currentVersionContent = res.data; - if (lastVersion) { - if (lastVersion === 'init') { - const lastVersionContent = currentVersionContent ? this.getInitContent(currentVersionContent.children[0]) : this.getInitContentByPath(currentVersion.path); - const validCurrentVersionContent = currentVersionContent || this.getInitContentByPath(currentVersion.path); - this.setContent(validCurrentVersionContent, lastVersionContent); - return; - } - seafileAPI.getFileRevision(historyRepoID, lastVersion.commit_id, lastVersion.path).then(res => { - return res.data ? seafileAPI.getFileContent(res.data) : { data: '' }; - }).then(res => { - const lastVersionContent = res.data; - const firstChildren = currentVersionContent.children[0]; - const validLastVersionContent = lastVersion && !lastVersionContent ? this.getInitContent(firstChildren) : lastVersionContent; - this.setContent(currentVersionContent, validLastVersionContent); - }).catch(error => { - const errorMessage = Utils.getErrorMsg(error, true); - toaster.danger(gettext(errorMessage)); - this.setContent(currentVersionContent, ''); - }); - } else { - this.setContent(currentVersionContent, ''); - } + this.updateLastVersionContent(currentVersionContent, currentVersion, lastVersion); }).catch(error => { const errorMessage = Utils.getErrorMsg(error, true); toaster.danger(gettext(errorMessage)); @@ -127,21 +131,12 @@ class SdocFileHistory extends React.Component { this.setState({ currentVersionContent, lastVersionContent, isLoading: false, changes: [], currentDiffIndex: 0 }); }; - onShowChanges = (isShowChanges) => { + onShowChanges = (isShowChanges, lastVersion) => { if (isShowChanges) { const { currentVersionContent, currentVersion } = this.state; this.setState({ isLoading: true, isShowChanges }, () => { localStorage.setItem('seahub-sdoc-history-show-changes', isShowChanges + ''); - seafileAPI.getNextFileRevision(historyRepoID, currentVersion.id, currentVersion.path).then(res => { - return res.data ? seafileAPI.getFileContent(res.data) : { data: '' }; - }).then(res => { - const lastVersionContent = res.data; - this.setContent(currentVersionContent, lastVersionContent); - }).catch(error => { - const errorMessage = Utils.getErrorMsg(error, true); - toaster.danger(gettext(errorMessage)); - this.setContent(currentVersionContent, ''); - }); + this.updateLastVersionContent(currentVersionContent, currentVersion, lastVersion); }); return; } diff --git a/frontend/src/pages/sdoc/sdoc-file-history/side-panel.js b/frontend/src/pages/sdoc/sdoc-file-history/side-panel.js index a6da37aed6..73a929e238 100644 --- a/frontend/src/pages/sdoc/sdoc-file-history/side-panel.js +++ b/frontend/src/pages/sdoc/sdoc-file-history/side-panel.js @@ -142,13 +142,11 @@ class SidePanel extends Component { }); }; - onSelectHistoryVersion = (path) => { - const { isShowChanges } = this.props; + getLastVersion = (path, isShowChanges) => { const { historyGroups } = this.state; const [monthIndex, dayIndex, dailyIndex] = path; const monthHistoryGroup = historyGroups[monthIndex]; const dayHistoryGroup = monthHistoryGroup.children[dayIndex]; - const currentVersion = dayHistoryGroup.children[dailyIndex]; let lastVersion = ''; if (isShowChanges) { if (dayHistoryGroup.showDaily) { @@ -164,6 +162,17 @@ class SidePanel extends Component { lastVersion = 'init'; } } + return lastVersion; + }; + + onSelectHistoryVersion = (path) => { + const { historyGroups } = this.state; + const { isShowChanges } = this.props; + const [monthIndex, dayIndex, dailyIndex] = path; + const monthHistoryGroup = historyGroups[monthIndex]; + const dayHistoryGroup = monthHistoryGroup.children[dayIndex]; + const currentVersion = dayHistoryGroup.children[dailyIndex]; + const lastVersion = this.getLastVersion(path, isShowChanges); this.props.onSelectHistoryVersion(currentVersion, lastVersion); }; @@ -275,8 +284,22 @@ class SidePanel extends Component { }; onShowChanges = () => { - const { isShowChanges } = this.props; - this.props.onShowChanges(!isShowChanges); + const { isShowChanges, currentVersion } = this.props; + const { historyGroups } = this.state; + const nextShowChanges = !isShowChanges; + let lastVersion; + if (nextShowChanges) { + const { date } = currentVersion; + const momentDate = moment(date); + const month = momentDate.format('YYYY-MM'); + const day = momentDate.format('YYYY-MM-DD'); + const monthIndex = historyGroups.findIndex(item => item.month === month); + const dayIndex = historyGroups[monthIndex].children.findIndex(item => item.day === day); + const dailyIndex = historyGroups[monthIndex].children[dayIndex].children.findIndex(item => item.date === date); + const path = [monthIndex, dayIndex, dailyIndex]; + lastVersion = this.getLastVersion(path, nextShowChanges); + } + this.props.onShowChanges(nextShowChanges, lastVersion); }; render() {