import React from 'react'; import ReactDom from 'react-dom'; import { UncontrolledTooltip } from 'reactstrap'; import classnames from 'classnames'; import { DiffViewer } from '@seafile/sdoc-editor'; import { seafileAPI } from '../../../utils/seafile-api'; import { gettext, historyRepoID } from '../../../utils/constants'; import Loading from '../../../components/loading'; import GoBack from '../../../components/common/go-back'; import SidePanel from './side-panel'; import { Utils } from '../../../utils/utils'; import toaster from '../../../components/toast'; import '../../../css/layout.css'; import './index.css'; const { serviceURL, avatarURL, siteRoot } = window.app.config; const { username, name } = window.app.pageOptions; const { repoID, fileName, filePath, docUuid, assetsUrl } = window.fileHistory.pageOptions; window.seafile = { repoID, docPath: filePath, docName: fileName, docUuid, isOpenSocket: false, serviceUrl: serviceURL, name, username, avatarURL, siteRoot, assetsUrl, }; class SdocFileHistory extends React.Component { constructor(props) { super(props); const isShowChanges = localStorage.getItem('seahub-sdoc-history-show-changes') === 'false' ? false : true; this.state = { isLoading: true, isShowChanges, currentVersion: {}, currentVersionContent: '', lastVersionContent: '', changes: [], currentDiffIndex: 0, }; } onSelectHistoryVersion = (currentVersion, isShowChanges) => { 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 (isShowChanges) { seafileAPI.getLastFileRevision(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, ''); }); } else { this.setContent(currentVersionContent, ''); } }).catch(error => { const errorMessage = Utils.getErrorMsg(error, true); toaster.danger(gettext(errorMessage)); this.setContent('', ''); }); }; setContent = (currentVersionContent = '', lastVersionContent = '') => { this.setState({ currentVersionContent, lastVersionContent, isLoading: false, changes: [], currentDiffIndex: 0 }); }; onShowChanges = (isShowChanges) => { 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, ''); }); }); return; } this.setState({ isLoading: true, isShowChanges }, () => { this.setState({ isLoading: false, lastVersionContent: '' }, () => { localStorage.setItem('seahub-sdoc-history-show-changes', isShowChanges + ''); }); }); }; setDiffCount = (diff = { value: [], changes: [] }) => { const { changes } = diff; this.setState({ changes, currentDiffIndex: 0 }); }; jumpToElement = (currentDiffIndex) => { this.setState({ currentDiffIndex }, () => { const { currentDiffIndex, changes } = this.state; const change = changes[currentDiffIndex]; const changeElement = document.querySelectorAll(`[data-id=${change}]`)[0]; if (changeElement) { this.historyContentRef.scrollTop = changeElement.offsetTop - 10; } }); }; lastChange = () => { const { currentDiffIndex, changes } = this.state; if (currentDiffIndex === 0) { this.jumpToElement(changes.length - 1); return; } this.jumpToElement(currentDiffIndex - 1); }; nextChange = () => { const { currentDiffIndex, changes } = this.state; if (currentDiffIndex === changes.length - 1) { this.jumpToElement(0); return; } this.jumpToElement(currentDiffIndex + 1); }; renderChangesTip = () => { const { isShowChanges, changes, currentDiffIndex, isLoading } = this.state; if (isLoading) return null; if (!isShowChanges) return null; const changesCount = changes ? changes.length : 0; if (changesCount === 0) { return (
{gettext('No changes')}
); } return (
{`${gettext('Changes')} ${currentDiffIndex + 1}/${changesCount}`}
{gettext('Last modification')} {gettext('Next modification')}
); }; render() { const { currentVersion, isShowChanges, currentVersionContent, lastVersionContent, isLoading } = this.state; return (
{fileName}
{this.renderChangesTip()}
this.historyContentRef = ref}> {isLoading ? (
) : ( )}
); } } ReactDom.render(, document.getElementById('wrapper'));