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 } 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 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-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', commentsList: [], changedNodes: [], originRepoName: '', isShowCommentDialog: false, 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 }); } 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.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 (
{this.state.isShowDiff ? : }
); } listReviewers = () => { seafileAPI.listDraftReviewers(draftID).then((res) => { this.setState({ reviewers: res.data.reviewers }); }); } onSwitchShowDiff = () => { if (!this.state.isShowDiff) { let that = this; setTimeout(() => { that.getChangedNodes(); }, 100); } this.setState({ isShowDiff: !this.state.isShowDiff, }); } toggleDiffTip = () => { this.setState({ showDiffTip: !this.state.showDiffTip }); } toggleAddReviewerDialog = () => { if (this.state.showReviewerDialog) { this.listReviewers(); } this.setState({ showReviewerDialog: !this.state.showReviewerDialog }); } showDiffButton = () => { return (
{gettext('View diff')}
); } onPublishDraft = () => { seafileAPI.publishDraft(draftID).then(res => { this.setState({ freezePublish: !this.state.freezePublish }); }); } initialDiffViewerContent = () => { seafileAPI.listFileHistoryRecords(draftRepoID, draftFilePath, 1, 25).then((res) => { this.setState({ historyList: res.data.data, totalReversionCount: res.data.total_count }); if (res.data.data.length > 1) { axios.all([ seafileAPI.getFileRevision(draftRepoID, res.data.data[0].commit_id, draftFilePath), seafileAPI.getFileRevision(draftRepoID, res.data.data[1].commit_id, draftFilePath) ]).then(axios.spread((res1, res2) => { axios.all([seafileAPI.getFileContent(res1.data), seafileAPI.getFileContent(res2.data)]).then(axios.spread((content1,content2) => { this.setState({ draftContent: content1.data, draftOriginContent: content2.data }); })); })); } else { seafileAPI.getFileRevision(draftRepoID, res.data.data[0].commit_id, draftFilePath).then((res) => { seafileAPI.getFileContent(res.data).then((content) => { this.setState({ draftContent: content.data, draftOriginContent: '' }); }); }); } }); } setDiffViewerContent = (newContent, prevContent) => { this.setState({ draftContent: newContent, draftOriginContent: prevContent, isLoading: false, }); } setURL = (newurl) => { let url = new URL(window.location.href); url.set('hash', newurl); window.location.href = url.toString(); } tabItemClick = (tab) => { if (this.state.activeTab !== tab) { if (tab !== 'history' && window.location.hash) { this.setURL('#'); } if (tab == 'reviewInfo') { this.initialContent(); } else if (tab == 'history') { this.initialDiffViewerContent(); } this.setState({ activeTab: tab }); } } showNavItem = (showTab) => { const commentsNumber = this.state.commentsList.length; switch(showTab) { case 'info': return ( { this.tabItemClick('reviewInfo');}} > ); case 'comments': return ( {this.tabItemClick('comments');}} > {commentsNumber > 0 &&
{commentsNumber}
}
); case 'history': return ( { this.tabItemClick('history');}} > ); } } setBtnPosition = (e) => { const nativeSelection = window.getSelection(); if (!nativeSelection.rangeCount) { this.range = null; return; } if (nativeSelection.isCollapsed === false) { const nativeRange = nativeSelection.getRangeAt(0); const focusNode = nativeSelection.focusNode; if ((focusNode.tagName === 'I') || (focusNode.nodeType !== 3 && focusNode.getAttribute('class') === 'language-type')) { // fix select last paragraph let fragment = nativeRange.cloneContents(); let startNode = fragment.firstChild.firstChild; if (!startNode) { return; } let newNativeRange = document.createRange(); newNativeRange.setStartBefore(startNode); newNativeRange.setEndAfter(startNode); this.range = findRange(newNativeRange, this.refs.diffViewer); } else { this.range = findRange(nativeRange, this.refs.diffViewer); } if (!this.range) { return; } let rect = nativeRange.getBoundingClientRect(); // fix Safari bug if (navigator.userAgent.indexOf('Chrome') < 0 && navigator.userAgent.indexOf('Safari') > 0) { if (nativeRange.collapsed && rect.top == 0 && rect.height == 0) { if (nativeRange.startOffset == 0) { nativeRange.setEnd(nativeRange.endContainer, 1); } else { nativeRange.setStart(nativeRange.startContainer, nativeRange.startOffset - 1); } rect = nativeRange.getBoundingClientRect(); if (rect.top == 0 && rect.height == 0) { if (nativeRange.getClientRects().length) { rect = nativeRange.getClientRects()[0]; } } } } let style = this.refs.commentbtn.style; style.top = `${rect.top - 100 + this.refs.viewContent.scrollTop}px`; } else { let style = this.refs.commentbtn.style; style.top = '-1000px'; } } getQuote = () => { let range = this.range; if (!range) return; this.quote = ''; const { document } = this.refs.diffViewer.value; let { anchor, focus } = range; const anchorText = document.getNode(anchor.key); const focusText = document.getNode(focus.key); const anchorInline = document.getClosestInline(anchor.key); const focusInline = document.getClosestInline(focus.key); // COMPAT: If the selection is at the end of a non-void inline node, and // there is a node after it, put it in the node after instead. This // standardizes the behavior, since it's indistinguishable to the user. if (anchorInline && anchor.offset == anchorText.text.length) { const block = document.getClosestBlock(anchor.key); const nextText = block.getNextText(anchor.key); if (nextText) { range = range.moveAnchorTo(nextText.key, 0); } } if (focusInline && focus.offset == focusText.text.length) { const block = document.getClosestBlock(focus.key); const nextText = block.getNextText(focus.key); if (nextText) { range = range.moveFocusTo(nextText.key, 0); } } let fragment = document.getFragmentAtRange(range); let nodes = this.removeNullNode(fragment.nodes); let newFragment = Document.create({ nodes: nodes }); let newValue = Value.create({ document: newFragment }); this.quote = serialize(newValue.toJSON()); let blockPath = document.createSelection(range).anchor.path.slice(0, 1); let node = document.getNode(blockPath); this.newIndex = node.data.get('new_index'); this.oldIndex = node.data.get('old_index'); } removeNullNode = (oldNodes) => { let newNodes = []; oldNodes.map((node) => { if (!node.text.trim()) return; const childNodes = node.nodes; if ((childNodes && childNodes.size === 1) || (!childNodes)) { newNodes.push(node); } else if (childNodes.size > 1) { let nodes = this.removeNullNode(childNodes); let newNode = Block.create({ nodes: nodes, data: node.data, key: node.key, type: node.type }); newNodes.push(newNode); } }); return newNodes; } onResizeMouseUp = () => { if (this.state.inResizing) { this.setState({ inResizing: false }); } } onResizeMouseDown = () => { this.setState({ inResizing: true }); }; onResizeMouseMove = (e) => { let rate = 100 - e.nativeEvent.clientX / this.refs.main.clientWidth * 100; if (rate < 20 || rate > 60) { this.setState({ inResizing: false }); return null; } this.setState({ rightPartWidth: rate }); }; componentDidMount() { this.getOriginRepoInfo(); this.getDraftInfo(); this.listReviewers(); this.listComments(); this.initialContent(); document.addEventListener('selectionchange', this.setBtnPosition); } componentWillUnmount() { document.removeEventListener('selectionchange', this.setBtnPosition); } renderDiffButton = () => { switch(draftStatus) { case 'open': if (!draftFileExists || !originFileExists) { return; } return this.showDiffButton(); case 'published': if (!originFileExists) { return; } return this.showDiffButton(); } } renderNavItems = () => { switch (draftStatus) { case 'open': if (!draftFileExists) { return ( ); } return ( ); case 'published': if (!originFileExists) { return ( ); } return ( ); } } renderContent = () => { switch(draftStatus) { case 'open': if (!draftFileExists) { 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 } = this.state; 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 && filePermission == 'rw'; const time = moment(draftInfo.mtime * 1000).format('YYYY-MM-DD HH:mm'); const url = `${siteRoot}profile/${encodeURIComponent(draftInfo.last_modifier_email)}/`; return(
{this.state.isLoading ?
:
{this.renderContent()}
}
{this.renderNavItems()}
{draftFileExists && } {(this.state.isShowDiff === true && this.state.changedNodes.length > 0) && }
{this.state.showReviewerDialog && } {this.state.isShowCommentDialog && }
); } } class SidePanelReviewers extends React.Component { constructor(props) { super(props); } render() { const { reviewers } = this.props; return (
{gettext('Reviewers')}
{reviewers.length > 0 ? reviewers.map((item, index = 0, arr) => { return (
{item.user_name}
); }) : {gettext('No reviewer yet.')} }
); } } const sidePanelReviewersPropTypes = { reviewers: PropTypes.array.isRequired, toggleAddReviewerDialog: PropTypes.func.isRequired }; SidePanelReviewers.propTypes = sidePanelReviewersPropTypes; class SidePanelAuthor extends React.Component { render() { return (
{gettext('Author')}
{author}
); } } class SidePanelOrigin extends React.Component { constructor(props) { super(props); } render() { return (
{gettext('Location')}{this.props.originRepoName}{draftFilePath}
); } } const SidePanelOriginPropTypes = { originRepoName: PropTypes.string.isRequired }; SidePanelOrigin.propTypes = SidePanelOriginPropTypes; class UnresolvedComments extends React.Component { constructor(props) { super(props); } render() { const { commentsList } = this.props; let unresolvedNumber = 0; if (commentsList) { for (let i = 0, count = commentsList.length; i < count; i++) { if (commentsList[i].resolved === false) { unresolvedNumber++; } } } return (
{gettext('Comments')}
{gettext('Unresolved comments:')}{' '}{unresolvedNumber}
); } } const UnresolvedCommentsPropTypes = { commentsList: PropTypes.array.isRequired, }; UnresolvedComments.propTypes = UnresolvedCommentsPropTypes; class SidePanelChanges extends React.Component { constructor(props) { super(props); } render() { return (
{gettext('Changes')}
{gettext('Number of changes:')}{' '}{this.props.changedNumber} {this.props.changedNumber > 0 &&
{ this.props.scrollToChangedNode('down');}}> { this.props.scrollToChangedNode('up');}}>
}
); } } const sidePanelChangesPropTypes = { changedNumber: PropTypes.number.isRequired, scrollToChangedNode: PropTypes.func.isRequired }; SidePanelChanges.propTypes = sidePanelChangesPropTypes; ReactDOM.render ( , document.getElementById('wrapper') );