import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import { Button } from 'reactstrap'; /* eslint-disable */ import Prism from 'prismjs'; /* eslint-enable */ import { siteRoot, gettext, draftOriginFilePath, draftFilePath, author, authorAvatar, originFileExists, draftFileExists, draftID, draftFileName, draftRepoID, draftStatus, draftPublishVersion, originFileVersion, filePermission, serviceURL } from './utils/constants'; import { seafileAPI } from './utils/seafile-api'; import axios from 'axios'; import DiffViewer from '@seafile/seafile-editor/dist/viewer/diff-viewer'; import { serialize } from '@seafile/seafile-editor/dist/utils/slate2markdown/serialize'; import Loading from './components/loading'; import ReviewComments from './components/review-list-view/review-comments'; import ReviewCommentDialog from './components/review-list-view/review-comment-dialog.js'; import { Tooltip } from 'reactstrap'; import AddReviewerDialog from './components/dialog/add-reviewer-dialog.js'; import { ReactEditor } from '@seafile/slate-react'; import { Nav, NavItem, NavLink, TabContent, TabPane } from 'reactstrap'; import classnames from 'classnames'; import HistoryList from './pages/review/history-list'; import { Range, Editor } from 'slate'; import ModalPortal from './components/modal-portal'; import reviewComment from './models/review-comment.js'; import './assets/css/fa-solid.css'; import './assets/css/fa-regular.css'; import './assets/css/fontawesome.css'; import './css/layout.css'; import './css/toolbar.css'; import './css/dirent-detail.css'; import './css/draft.css'; require('@seafile/seafile-editor/dist/editor/code-highlight-package'); const URL = require('url-parse'); var moment = require('moment'); const { toSlateRange: findRange, toDOMNode } = ReactEditor; class Draft extends React.Component { constructor(props) { super(props); this.state = { draftContent: '', draftOriginContent: '', draftInfo: {}, isLoading: true, isShowDiff: true, showDiffTip: false, activeTab: 'reviewInfo', commentsList: [], changedNodes: [], originRepoName: '', isShowCommentDialog: false, activeItem: null, historyList: [], showReviewerDialog: false, reviewers: [], inResizing: false, rightPartWidth: 30, draftStatus: draftStatus, }; this.quote = ''; this.newIndex = null; this.oldIndex = null; this.changeIndex = -1; this.range = null; } initialContent = () => { switch(draftStatus) { case 'open': if (!draftFileExists) { this.setState({ isLoading: false, isShowDiff: false }); return; } if (!originFileExists) { seafileAPI.getFileDownloadLink(draftRepoID, draftFilePath) .then(res => { seafileAPI.getFileContent(res.data) .then(res => { this.setState({ draftContent: res.data, draftOriginContent: res.data, isLoading: false, isShowDiff: false }); }); }); return; } const hash = window.location.hash; if (hash.indexOf('#history-') === 0) { const currentCommitID = hash.slice(9, 49); const preCommitID = hash.slice(50, 90); let preItemFilePath, currentItemFilePath; this.setState({ isLoading: false, activeTab: 'history', }); seafileAPI.listFileHistoryRecords(draftRepoID, draftFilePath, 1, 25).then((res) => { const historyList = res.data.data; this.setState({ historyList: historyList, totalReversionCount: res.data.total_count }); for (let i = 0, length = historyList.length; i < length; i++) { if (preCommitID === historyList[i].commit_id) { this.setState({ activeItem: i }); preItemFilePath = historyList[i].path; } if (currentCommitID === historyList[i].commit_id) { currentItemFilePath = historyList[i].path; } if (preItemFilePath && currentItemFilePath) break; } axios.all([ seafileAPI.getFileRevision(draftRepoID, currentCommitID, currentItemFilePath), seafileAPI.getFileRevision(draftRepoID, preCommitID, preItemFilePath) ]).then(axios.spread((res1, res2) => { axios.all([seafileAPI.getFileContent(res1.data), seafileAPI.getFileContent(res2.data)]).then(axios.spread((content1, content2) => { this.setDiffViewerContent(content2.data, content1.data); })); })); return; }); } else { axios.all([ seafileAPI.getFileDownloadLink(draftRepoID, draftFilePath), seafileAPI.getFileDownloadLink(draftRepoID, draftOriginFilePath) ]).then(axios.spread((res1, res2) => { axios.all([ seafileAPI.getFileContent(res1.data), seafileAPI.getFileContent(res2.data) ]).then(axios.spread((draftContent, draftOriginContent) => { this.setState({ draftContent: draftContent.data, draftOriginContent: draftOriginContent.data, isLoading: false }); let that = this; setTimeout(() => { that.getChangedNodes(); }, 100); })); })); } break; case 'published': if (!originFileExists) { this.setState({ isLoading: false, isShowDiff: false }); return; } let dl0 = siteRoot + 'repo/' + draftRepoID + '/' + draftPublishVersion + '/download?' + 'p=' + draftOriginFilePath; let dl = siteRoot + 'repo/' + draftRepoID + '/' + originFileVersion + '/download?' + 'p=' + draftOriginFilePath; axios.all([ seafileAPI.getFileContent(dl0), seafileAPI.getFileContent(dl) ]).then(axios.spread((draftContent, draftOriginContent) => { this.setState({ draftContent: draftContent.data, draftOriginContent: draftOriginContent.data, isLoading: false, }); })); break; } } onHistoryItemClick = (currentItem, preItem, activeItem) => { const preCommitID = preItem.commit_id; const currentCommitID = currentItem.commit_id; const url = 'history-' + preCommitID + '-' + currentCommitID; this.setURL(url); this.setState({ activeItem: activeItem, isLoading: true, }); axios.all([ seafileAPI.getFileRevision(draftRepoID, currentCommitID, currentItem.path), seafileAPI.getFileRevision(draftRepoID, preCommitID, preItem.path) ]).then(axios.spread((res1, res2) => { axios.all([seafileAPI.getFileContent(res1.data), seafileAPI.getFileContent(res2.data)]).then(axios.spread((content1,content2) => { this.setDiffViewerContent(content1.data, content2.data); })); })); } onHistoryListChange = (historyList) => { this.setState({historyList: historyList }); } listComments = () => { seafileAPI.listComments(draftRepoID, draftFilePath).then((res) => { let commentsList = []; res.data.comments.forEach((item) => { commentsList.push(new reviewComment(item)); }); this.setState({ commentsList: commentsList }); }); } addComment = (e) => { e.stopPropagation(); this.getQuote(); if (!this.quote) return; this.setState({ isShowCommentDialog: true }); } onCommentAdded = () => { this.listComments(); this.toggleCommentDialog(); } toggleCommentDialog = () => { this.setState({ isShowCommentDialog: !this.state.isShowCommentDialog }); } getOriginRepoInfo = () => { seafileAPI.getRepoInfo(draftRepoID).then((res) => { this.setState({ originRepoName: res.data.repo_name }); }); } getDraftInfo = () => { if (draftStatus === 'open') { seafileAPI.getFileInfo(draftRepoID, draftFilePath).then((res) => { this.setState({ draftInfo: res.data }); }); } } getChangedNodes = () => { const nodes = this.refs.diffViewer.value; let keys = []; let lastDiffState = ''; nodes.map((node, index) => { const diff_state = node.data['diff_state']; if (diff_state === 'diff-added' && lastDiffState !== 'diff-added') { keys.push(index); } else if (diff_state === 'diff-removed' && lastDiffState !== 'diff-removed') { keys.push(index); } else if (diff_state === 'diff-replaced' && lastDiffState !== 'diff-replaced') { keys.push(index); } lastDiffState = node.data.diff_state; }); this.setState({ changedNodes: keys }); } scrollToChangedNode = (scroll) => { if (this.state.changedNodes.length == 0) return; if (scroll === 'up') { this.changeIndex++; } else { this.changeIndex--; } if (this.changeIndex > this.state.changedNodes.length - 1) { this.changeIndex = 0; } if (this.changeIndex < 0) { this.changeIndex = this.state.changedNodes.length - 1; } const win = window; let key = this.state.changedNodes[this.changeIndex]; let node = window.viewer.children[key]; let element = toDOMNode(window.viewer, node); // fix code-block or tables while (element.className.indexOf('diff-') === -1 && element.tagName !== 'BODY') { element = element.parentNode; } const scroller = this.findScrollContainer(element, win); const isWindow = scroller == win.document.body || scroller == win.document.documentElement; if (isWindow) { win.scrollTo(0, element.offsetTop); } else { scroller.scrollTop = element.offsetTop; } } findScrollContainer = (element, window) => { let parent = element.parentNode; const OVERFLOWS = ['auto', 'overlay', 'scroll']; let scroller; while (!scroller) { if (!parent.parentNode) break; const style = window.getComputedStyle(parent); const { overflowY } = style; if (OVERFLOWS.includes(overflowY)) { scroller = parent; break; } parent = parent.parentNode; } if (!scroller) { return window.document.body; } return scroller; } scrollToQuote = (newIndex, oldIndex, quote) => { const nodes = this.refs.diffViewer.value; let commentNode = nodes.find((node) => { if (node.data['old_index'] == oldIndex && node.data['new_index'] == newIndex) { return node } }); if (commentNode) { const element = toDOMNode(window.viewer, commentNode); if (!element) return; const win = window; const scroller = this.findScrollContainer(element, win); const isWindow = scroller == win.document.body || scroller == win.document.documentElement; if (isWindow) { win.scrollTo(0, element.offsetTop); } else { scroller.scrollTop = element.offsetTop; } } } showDiffViewer = () => { return (
{gettext('Draft has been deleted.')}
; } return this.showDiffViewer(); case 'published': if (!originFileExists) { return{gettext('Original file has been deleted.')}
; } return this.showDiffViewer(); } } render() { const { draftInfo, reviewers, originRepoName, draftStatus } = this.state; const onResizeMove = this.state.inResizing ? this.onResizeMouseMove : null; const draftLink = siteRoot + 'lib/' + draftRepoID + '/file' + draftFilePath + '?mode=edit'; const showPublishedButton = this.state.draftStatus == 'published'; const showPublishButton = this.state.draftStatus == 'open' && filePermission == 'rw'; const showEditButton = this.state.draftStatus == 'open' && filePermission == 'rw'; const time = moment(draftInfo.mtime * 1000).format('YYYY-MM-DD HH:mm'); const url = `${siteRoot}profile/${encodeURIComponent(draftInfo.last_modifier_email)}/`; return({gettext('Location')} | {draftStatus === 'open' ? {originRepoName}{draftFilePath} : {filePath} } |
---|