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 } 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 { findRange } from '@seafile/slate-react'; import { Nav, NavItem, NavLink, TabContent, TabPane } from 'reactstrap'; import classnames from 'classnames'; import HistoryList from './pages/review/history-list'; import { Value, Document, Block } from 'slate'; import ModalPortal from './components/modal-portal'; 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-hight-package'); const URL = require('url-parse'); var moment = require('moment'); class Draft extends React.Component { constructor(props) { super(props); this.state = { draftContent: '', draftOriginContent: '', draftInfo: {}, isLoading: true, isShowDiff: true, showDiffTip: false, activeTab: 'reviewInfo', commentsNumber: null, changedNodes: [], originRepoName: '', isShowCommentDialog: false, unresolvedComments: 0, activeItem: null, historyList: [], showReviewerDialog: false, reviewers: [], inResizing: false, rightPartWidth: 30, freezePublish: false }; 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 }); } getCommentsNumber = () => { seafileAPI.listComments(draftRepoID, draftFilePath).then((res) => { let number = res.data.total_count; let comments = res.data.comments; let unresolvedComments = 0; for (let i = 0; i < res.data.total_count; i++) { if (comments[i].resolved === false) { unresolvedComments++; } } this.setState({ commentsNumber: number, unresolvedComments: unresolvedComments, }); }); } addComment = (e) => { e.stopPropagation(); this.getQuote(); if (!this.quote) { return; } this.setState({ isShowCommentDialog: true }); } onCommentAdded = () => { this.getCommentsNumber(); this.toggleCommentDialog(); } toggleCommentDialog = () => { this.setState({ isShowCommentDialog: !this.state.isShowCommentDialog }); } getOriginRepoInfo = () => { seafileAPI.getRepoInfo(draftRepoID).then((res) => { this.setState({ originRepoName: res.data.repo_name }); }); } getDraftInfo = () => { seafileAPI.getFileInfo(draftRepoID, draftFilePath).then((res) => { this.setState({ draftInfo: res.data }); }); } getChangedNodes = () => { const nodes = this.refs.diffViewer.value.document.nodes; let keys = []; let lastDiffState = ''; nodes.map((node) => { if (node.data.get('diff_state') === 'diff-added' && lastDiffState !== 'diff-added') { keys.push(node.key); } else if (node.data.get('diff_state') === 'diff-removed' && lastDiffState !== 'diff-removed') { keys.push(node.key); } else if (node.data.get('diff_state') === 'diff-replaced' && lastDiffState !== 'diff-replaced') { keys.push(node.key); } lastDiffState = node.data.get('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 element = win.document.querySelector(`[data-key="${key}"]`); // 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.document.nodes; let key; nodes.map((node) => { if (node.data.get('old_index') == oldIndex && node.data.get('new_index') == newIndex) { key = node.key; } }); if (typeof(key) !== 'string') { nodes.map((node) => { if (node.text.indexOf(quote) > 0) { key = node.key; } }); } if (typeof(key) === 'string') { const win = window; let element = win.document.querySelector(`[data-key="${key}"]`); while (element.tagName === 'CODE') { 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; } } } 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 onResizeMove = this.state.inResizing ? this.onResizeMouseMove : null; const draftLink = siteRoot + 'lib/' + draftRepoID + '/file' + draftFilePath + '?mode=edit'; const freezePublish = (this.state.freezePublish || draftStatus === 'published') ? true : false; const canPublish = !this.state.freezePublish && draftFileExists; const time = moment(this.state.draftInfo.mtime * 1000).format('YYYY-MM-DD HH:mm'); const url = `${siteRoot}profile/${encodeURIComponent(this.state.draftInfo.last_modifier_email)}/`; return({gettext('Location')} | {this.props.originRepoName}{draftFilePath} |
---|