2023-05-13 07:42:54 +00:00
|
|
|
import React from 'react';
|
2023-04-27 02:36:49 +00:00
|
|
|
import ReactDom from 'react-dom';
|
2023-05-13 07:42:54 +00:00
|
|
|
import { UncontrolledTooltip } from 'reactstrap';
|
|
|
|
import classnames from 'classnames';
|
2023-05-18 05:52:49 +00:00
|
|
|
import { DiffViewer } from '@seafile/sdoc-editor';
|
2023-10-26 09:15:42 +00:00
|
|
|
import { seafileAPI } from '../../../utils/seafile-api';
|
|
|
|
import { gettext, historyRepoID } from '../../../utils/constants';
|
|
|
|
import Loading from '../../../components/loading';
|
|
|
|
import GoBack from '../../../components/common/go-back';
|
2023-05-13 07:42:54 +00:00
|
|
|
import SidePanel from './side-panel';
|
2023-10-26 09:15:42 +00:00
|
|
|
import { Utils } from '../../../utils/utils';
|
|
|
|
import toaster from '../../../components/toast';
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-10-26 09:15:42 +00:00
|
|
|
import '../../../css/layout.css';
|
2023-11-15 08:28:02 +00:00
|
|
|
import './index.css';
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 14:15:57 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2023-04-27 02:36:49 +00:00
|
|
|
class SdocFileHistory extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2023-05-13 07:42:54 +00:00
|
|
|
const isShowChanges = localStorage.getItem('seahub-sdoc-history-show-changes') === 'false' ? false : true;
|
2023-04-27 02:36:49 +00:00
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
2023-05-13 07:42:54 +00:00
|
|
|
isShowChanges,
|
|
|
|
currentVersion: {},
|
|
|
|
currentVersionContent: '',
|
|
|
|
lastVersionContent: '',
|
|
|
|
changes: [],
|
|
|
|
currentDiffIndex: 0,
|
2023-04-27 02:36:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-12-11 08:13:00 +00:00
|
|
|
getInitContent = (firstChildren) => {
|
|
|
|
if (firstChildren) {
|
|
|
|
return {
|
|
|
|
version: -1,
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
id: firstChildren.id,
|
|
|
|
type: 'title',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
id: firstChildren.children[0].id,
|
|
|
|
text: ''
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return this.getInitContentByPath();
|
|
|
|
};
|
|
|
|
|
|
|
|
getInitContentByPath = (path) => {
|
|
|
|
const name = path ? path.split('/')?.pop()?.slice(0, -5) : '';
|
|
|
|
return {
|
|
|
|
version: -1,
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
type: 'title',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
id: '2',
|
|
|
|
text: name
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-12-11 10:17:15 +00:00
|
|
|
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, '');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-12-09 09:39:43 +00:00
|
|
|
onSelectHistoryVersion = (currentVersion, lastVersion) => {
|
2023-05-13 07:42:54 +00:00
|
|
|
this.setState({ isLoading: true, currentVersion });
|
2023-06-19 09:35:30 +00:00
|
|
|
seafileAPI.getFileRevision(historyRepoID, currentVersion.commit_id, currentVersion.path).then(res => {
|
2023-05-13 07:42:54 +00:00
|
|
|
return seafileAPI.getFileContent(res.data);
|
|
|
|
}).then(res => {
|
|
|
|
const currentVersionContent = res.data;
|
2023-12-11 10:17:15 +00:00
|
|
|
this.updateLastVersionContent(currentVersionContent, currentVersion, lastVersion);
|
2023-05-13 07:42:54 +00:00
|
|
|
}).catch(error => {
|
|
|
|
const errorMessage = Utils.getErrorMsg(error, true);
|
|
|
|
toaster.danger(gettext(errorMessage));
|
|
|
|
this.setContent('', '');
|
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 07:42:54 +00:00
|
|
|
setContent = (currentVersionContent = '', lastVersionContent = '') => {
|
|
|
|
this.setState({ currentVersionContent, lastVersionContent, isLoading: false, changes: [], currentDiffIndex: 0 });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-12-11 10:17:15 +00:00
|
|
|
onShowChanges = (isShowChanges, lastVersion) => {
|
2023-11-15 08:28:02 +00:00
|
|
|
if (isShowChanges) {
|
|
|
|
const { currentVersionContent, currentVersion } = this.state;
|
2023-11-21 09:46:00 +00:00
|
|
|
this.setState({ isLoading: true, isShowChanges }, () => {
|
2023-05-18 05:52:49 +00:00
|
|
|
localStorage.setItem('seahub-sdoc-history-show-changes', isShowChanges + '');
|
2023-12-11 10:17:15 +00:00
|
|
|
this.updateLastVersionContent(currentVersionContent, currentVersion, lastVersion);
|
2023-05-18 05:52:49 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2023-11-21 09:46:00 +00:00
|
|
|
this.setState({ isLoading: true, isShowChanges }, () => {
|
|
|
|
this.setState({ isLoading: false, lastVersionContent: '' }, () => {
|
|
|
|
localStorage.setItem('seahub-sdoc-history-show-changes', isShowChanges + '');
|
|
|
|
});
|
2023-04-27 02:36:49 +00:00
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 07:42:54 +00:00
|
|
|
setDiffCount = (diff = { value: [], changes: [] }) => {
|
|
|
|
const { changes } = diff;
|
|
|
|
this.setState({ changes, currentDiffIndex: 0 });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 07:42:54 +00:00
|
|
|
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;
|
2023-04-27 02:36:49 +00:00
|
|
|
}
|
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 07:42:54 +00:00
|
|
|
lastChange = () => {
|
|
|
|
const { currentDiffIndex, changes } = this.state;
|
|
|
|
if (currentDiffIndex === 0) {
|
|
|
|
this.jumpToElement(changes.length - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.jumpToElement(currentDiffIndex - 1);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-05-13 07:42:54 +00:00
|
|
|
nextChange = () => {
|
|
|
|
const { currentDiffIndex, changes } = this.state;
|
|
|
|
if (currentDiffIndex === changes.length - 1) {
|
|
|
|
this.jumpToElement(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.jumpToElement(currentDiffIndex + 1);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-04-27 02:36:49 +00:00
|
|
|
|
2023-08-10 09:31:53 +00:00
|
|
|
renderChangesTip = () => {
|
|
|
|
const { isShowChanges, changes, currentDiffIndex, isLoading } = this.state;
|
|
|
|
if (isLoading) return null;
|
|
|
|
if (!isShowChanges) return null;
|
2023-05-13 07:42:54 +00:00
|
|
|
const changesCount = changes ? changes.length : 0;
|
2023-08-10 09:31:53 +00:00
|
|
|
if (changesCount === 0) {
|
|
|
|
return (
|
|
|
|
<div className="sdoc-file-history-header-right d-flex align-items-center">
|
|
|
|
<div className="sdoc-file-changes-container d-flex align-items-center pl-2 pr-2">
|
|
|
|
{gettext('No changes')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="sdoc-file-history-header-right d-flex align-items-center">
|
|
|
|
<div className="sdoc-file-changes-container d-flex align-items-center">
|
|
|
|
<div className="sdoc-file-changes-tip d-flex align-items-center justify-content-center pl-2 pr-2">
|
|
|
|
{`${gettext('Changes')} ${currentDiffIndex + 1}/${changesCount}`}
|
|
|
|
</div>
|
|
|
|
<div className="sdoc-file-changes-divider"></div>
|
|
|
|
<div
|
|
|
|
className="sdoc-file-changes-last d-flex align-items-center justify-content-center"
|
|
|
|
id="sdoc-file-changes-last"
|
|
|
|
onClick={this.lastChange}
|
|
|
|
>
|
|
|
|
<span className="fas fa-chevron-up"></span>
|
|
|
|
</div>
|
|
|
|
<div className="sdoc-file-changes-divider"></div>
|
|
|
|
<div
|
|
|
|
className="sdoc-file-changes-next d-flex align-items-center justify-content-center"
|
|
|
|
id="sdoc-file-changes-next"
|
|
|
|
onClick={this.nextChange}
|
|
|
|
>
|
|
|
|
<span className="fas fa-chevron-down"></span>
|
|
|
|
</div>
|
2023-11-15 08:28:02 +00:00
|
|
|
<UncontrolledTooltip placement="bottom" target="sdoc-file-changes-last" delay={0} fade={false}>
|
2023-08-10 09:31:53 +00:00
|
|
|
{gettext('Last modification')}
|
|
|
|
</UncontrolledTooltip>
|
2023-11-15 08:28:02 +00:00
|
|
|
<UncontrolledTooltip placement="bottom" target="sdoc-file-changes-next" delay={0} fade={false}>
|
2023-08-10 09:31:53 +00:00
|
|
|
{gettext('Next modification')}
|
|
|
|
</UncontrolledTooltip>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-08-10 09:31:53 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { currentVersion, isShowChanges, currentVersionContent, lastVersionContent, isLoading } = this.state;
|
2023-05-13 07:42:54 +00:00
|
|
|
|
2023-04-27 02:36:49 +00:00
|
|
|
return (
|
2023-05-13 07:42:54 +00:00
|
|
|
<div className="sdoc-file-history d-flex h-100 w-100 o-hidden">
|
|
|
|
<div className="sdoc-file-history-container d-flex flex-column">
|
|
|
|
<div className="sdoc-file-history-header pt-2 pb-2 pl-4 pr-4 d-flex justify-content-between w-100 o-hidden">
|
2023-08-10 09:31:53 +00:00
|
|
|
<div className={classnames('sdoc-file-history-header-left d-flex align-items-center o-hidden', { 'pr-4': isShowChanges })}>
|
2023-05-13 07:42:54 +00:00
|
|
|
<GoBack />
|
|
|
|
<div className="file-name text-truncate">{fileName}</div>
|
|
|
|
</div>
|
2023-08-10 09:31:53 +00:00
|
|
|
{this.renderChangesTip()}
|
2023-04-27 02:36:49 +00:00
|
|
|
</div>
|
2023-05-13 07:42:54 +00:00
|
|
|
<div className="sdoc-file-history-content f-flex flex-column" ref={ref => this.historyContentRef = ref}>
|
|
|
|
{isLoading ? (
|
|
|
|
<div className="sdoc-file-history-viewer d-flex align-items-center justify-content-center">
|
|
|
|
<Loading />
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<DiffViewer
|
|
|
|
currentContent={currentVersionContent}
|
|
|
|
lastContent={isShowChanges ? lastVersionContent : ''}
|
|
|
|
didMountCallback={this.setDiffCount}
|
|
|
|
/>
|
|
|
|
)}
|
2023-04-27 02:36:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-13 07:42:54 +00:00
|
|
|
<SidePanel
|
|
|
|
isShowChanges={isShowChanges}
|
|
|
|
currentVersion={currentVersion}
|
|
|
|
onSelectHistoryVersion={this.onSelectHistoryVersion}
|
|
|
|
onShowChanges={this.onShowChanges}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-27 02:36:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDom.render(<SdocFileHistory />, document.getElementById('wrapper'));
|