import React from 'react'; import PropTypes from 'prop-types'; import { repoID, slug, serviceURL, isPublicWiki } from '../utils/constants'; import { Utils } from '../utils/utils'; import { Value } from 'slate'; import { deserialize } from '@seafile/seafile-editor/dist/utils/slate2markdown'; import'../css/index-viewer.css'; const viewerPropTypes = { indexContent: PropTypes.string.isRequired, onLinkClick: PropTypes.func.isRequired, }; class TreeNode { constructor({ name, href, parentNode }) { this.name = name; this.href = href; this.parentNode = parentNode || null; this.children = []; } setParent(parentNode) { this.parentNode = parentNode; } addChildren(nodeList) { nodeList.forEach((node) => { node.setParent(this); }); this.children = nodeList; } } class IndexContentViewer extends React.Component { constructor(props) { super(props); this.links = []; this.treeRoot = new TreeNode({ name: '', href: '' }); } componentWillMount() { this.getRootNode(); } componentDidMount() { this.bindClickEvent(); } componentWillReceiveProps() { this.removeClickEvent(); } componentDidUpdate() { this.bindClickEvent(); } componentWillUnmount() { this.removeClickEvent(); } bindClickEvent = () => { const contentClass = 'wiki-nav-content'; this.links = document.querySelectorAll(`.${contentClass} a`); this.links.forEach(link => { link.addEventListener('click', this.onLinkClick); }); } removeClickEvent = () => { this.links.forEach(link => { link.removeEventListener('click', this.onLinkClick); }); } onLinkClick = (event) => { event.preventDefault(); event.stopPropagation(); const link = this.getLink(event.target); if (link) this.props.onLinkClick(link); } getLink = (node) => { const tagName = node.tagName; if (!tagName || tagName === 'HTML') return; if (tagName === 'A') { return node.href; } else { return this.getLink(node.parentNode); } } changeInlineNode = (item) => { if (item.object == 'inline') { let url; // change image url if (item.type == 'image' && isPublicWiki) { url = item.data.src; const re = new RegExp(serviceURL + '/lib/' + repoID +'/file.*raw=1'); // different repo if (!re.test(url)) { return; } // get image path let index = url.indexOf('/file'); let index2 = url.indexOf('?'); const imagePath = url.substring(index + 5, index2); // replace url item.data.src = serviceURL + '/view-image-via-public-wiki/?slug=' + slug + '&path=' + imagePath; } else if (item.type == 'link') { url = item.data.href; /* eslint-disable */ let expression = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/ /* eslint-enable */ let re = new RegExp(expression); // Solving relative paths if (!re.test(url)) { item.data.href = serviceURL + '/published/' + slug + '/' + url; } // change file url else if (Utils.isInternalMarkdownLink(url, repoID)) { let path = Utils.getPathFromInternalMarkdownLink(url, repoID); // replace url item.data.href = serviceURL + '/published/' + slug + path; } // change dir url else if (Utils.isInternalDirLink(url, repoID)) { let path = Utils.getPathFromInternalDirLink(url, repoID); // replace url item.data.href = serviceURL + '/published/' + slug + path; } } } return item; } getRootNode = () => { let value = deserialize(this.props.indexContent); value = value.toJSON(); const newNodes = Utils.changeMarkdownNodes(value.document.nodes, this.changeInlineNode); newNodes.map((node) => { if (node.type === 'unordered_list' || node.type === 'ordered_list') { this.treeRoot = this.transSlateToTree(node.nodes, this.treeRoot); } }); } transSlateToTree = (slateNodes, treeRoot) => { let treeNodes = slateNodes.map((slateNode) => { const inline = slateNode.nodes[0].nodes[0]; if (slateNode.nodes.length === 2 && slateNode.nodes[1].type === 'unordered_list') { let treeNode = new TreeNode({ name: inline.nodes[0].leaves[0].text, href: inline.data.href }); return this.transSlateToTree(slateNode.nodes[1].nodes, treeNode); } else { return new TreeNode({ name: inline.nodes[0].leaves[0].text, href: inline.data.href }); } }); treeRoot.addChildren(treeNodes); return treeRoot; } render() { return (