1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-15 07:52:14 +00:00
seahub/frontend/src/components/index-viewer.js

144 lines
4.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import MarkdownViewer from '@seafile/seafile-editor/dist/viewer/markdown-viewer';
2019-01-29 03:03:43 +00:00
import { repoID, slug, serviceURL, isPublicWiki } from '../utils/constants';
import { Utils } from '../utils/utils';
const viewerPropTypes = {
2019-01-28 08:48:03 +00:00
indexContent: PropTypes.string.isRequired,
onLinkClick: PropTypes.func.isRequired,
};
const contentClass = 'wiki-nav-content';
2019-01-29 03:03:43 +00:00
class IndexContentViewer extends React.Component {
2019-01-29 03:03:43 +00:00
constructor(props) {
super(props);
this.links = [];
}
componentDidMount() {
// Bind event when first loaded
this.links = document.querySelectorAll(`.${contentClass} a`);
this.links.forEach(link => {
link.addEventListener('click', this.onLinkClick);
});
}
componentWillReceiveProps() {
// Unbound event when updating
this.links.forEach(link => {
link.removeEventListener('click', this.onLinkClick);
});
}
componentDidUpdate() {
// Update completed, rebind event
this.links = document.querySelectorAll(`.${contentClass} a`);
this.links.forEach(link => {
link.addEventListener('click', this.onLinkClick);
});
}
componentWillUnmount() {
// Rebinding events when the component is destroyed
this.links.forEach(link => {
link.removeEventListener('click', this.onLinkClick);
});
}
onLinkClick = (event) => {
2019-01-29 03:03:43 +00:00
event.preventDefault();
event.stopPropagation();
let link = '';
if (event.target.tagName !== 'A') {
let target = event.target.parentNode;
while (target.tagName !== 'A') {
target = target.parentNode;
}
link = target.href;
2019-01-29 03:03:43 +00:00
} else {
link = event.target.href;
}
this.props.onLinkClick(link);
}
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;
let expression = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
let re = new RegExp(expression);
// Solving relative paths
if (!re.test(url)) {
item.data.href = serviceURL + "/published/" + slug + '/' + url;
}
2019-01-29 03:03:43 +00:00
// change file url
else if (Utils.isInternalMarkdownLink(url, repoID)) {
2019-01-29 03:03:43 +00:00
let path = Utils.getPathFromInternalMarkdownLink(url, repoID);
console.log(path);
2019-01-29 03:03:43 +00:00
// replace url
item.data.href = serviceURL + '/published/' + slug + path;
2019-01-29 03:03:43 +00:00
}
// change dir url
else if (Utils.isInternalDirLink(url, repoID)) {
2019-01-30 03:48:15 +00:00
let path = Utils.getPathFromInternalDirLink(url, repoID);
2019-01-29 03:03:43 +00:00
// replace url
item.data.href = serviceURL + '/published/' + slug + path;
}
2019-01-29 03:03:43 +00:00
}
}
return item;
}
modifyValueBeforeRender = (value) => {
let nodes = value.document.nodes;
let newNodes = Utils.changeMarkdownNodes(nodes, this.changeInlineNode);
value.document.nodes = newNodes;
return value;
}
2019-01-28 08:48:03 +00:00
onContentRendered = () => {
// todo
}
render() {
return (
2019-01-29 03:03:43 +00:00
<div className={contentClass}>
<MarkdownViewer
markdownContent={this.props.indexContent}
onContentRendered={this.onContentRendered}
2019-01-29 03:03:43 +00:00
modifyValueBeforeRender={this.modifyValueBeforeRender}
/>
</div>
);
}
}
IndexContentViewer.propTypes = viewerPropTypes;
export default IndexContentViewer;