2019-01-04 10:30:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { seafileAPI } from './utils/seafile-api';
|
|
|
|
import { Utils } from './utils/utils';
|
2020-12-26 09:45:48 +00:00
|
|
|
import { serviceURL, mediaUrl } from './utils/constants';
|
2019-03-28 02:59:43 +00:00
|
|
|
import SharedFileView from './components/shared-file-view/shared-file-view';
|
|
|
|
import SharedFileViewTip from './components/shared-file-view/shared-file-view-tip';
|
2019-01-04 10:30:03 +00:00
|
|
|
import Loading from './components/loading';
|
|
|
|
import MarkdownViewer from '@seafile/seafile-editor/dist/viewer/markdown-viewer';
|
2019-07-16 02:01:09 +00:00
|
|
|
import toaster from './components/toast';
|
2019-01-04 10:30:03 +00:00
|
|
|
|
2019-03-28 02:59:43 +00:00
|
|
|
const { repoID, sharedToken, rawPath, err } = window.shared.pageOptions;
|
2019-01-04 10:30:03 +00:00
|
|
|
|
|
|
|
class SharedFileViewMarkdown extends React.Component {
|
2019-03-28 02:59:43 +00:00
|
|
|
render() {
|
|
|
|
return <SharedFileView content={<FileContent />} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileContent extends React.Component {
|
2019-01-04 10:30:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
markdownContent: '',
|
2019-03-28 02:59:43 +00:00
|
|
|
loading: !err
|
2019-01-04 10:30:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
seafileAPI.getFileContent(rawPath).then((res) => {
|
|
|
|
this.setState({
|
|
|
|
markdownContent: res.data,
|
|
|
|
loading: false
|
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-07-19 15:50:22 +00:00
|
|
|
});
|
2019-01-04 10:30:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 07:41:01 +00:00
|
|
|
changeImageURL = (innerNode) => {
|
|
|
|
if (innerNode.type == 'image') {
|
|
|
|
let imageUrl = innerNode.data.src;
|
|
|
|
|
|
|
|
const re = new RegExp(serviceURL + '/lib/' + repoID +'/file.*raw=1');
|
2019-11-05 09:46:06 +00:00
|
|
|
|
|
|
|
// different repo
|
2019-01-24 07:41:01 +00:00
|
|
|
if (!re.test(imageUrl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get image path
|
2020-03-24 12:12:29 +00:00
|
|
|
let imagePath = imageUrl.substring(serviceURL.length);
|
|
|
|
let index = imagePath.indexOf('/file');
|
|
|
|
let index2 = imagePath.indexOf('?');
|
|
|
|
imagePath = imagePath.substring(index + 5, index2);
|
2019-01-24 07:41:01 +00:00
|
|
|
// change image url
|
2020-04-22 12:46:19 +00:00
|
|
|
innerNode.data.src = serviceURL + '/view-image-via-share-link/?token=' + sharedToken + '&path=' + Utils.encodePath(imagePath);
|
2019-01-24 07:41:01 +00:00
|
|
|
}
|
|
|
|
return innerNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifyValueBeforeRender = (value) => {
|
2020-07-07 10:27:16 +00:00
|
|
|
return Utils.changeMarkdownNodes(value, this.changeImageURL);
|
2019-01-24 07:41:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:30:03 +00:00
|
|
|
render() {
|
2019-03-28 02:59:43 +00:00
|
|
|
if (err) {
|
|
|
|
return <SharedFileViewTip />;
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:30:03 +00:00
|
|
|
if (this.state.loading) {
|
2019-01-31 09:37:02 +00:00
|
|
|
return <Loading />;
|
2019-01-04 10:30:03 +00:00
|
|
|
}
|
2019-03-28 02:59:43 +00:00
|
|
|
|
2019-01-04 10:30:03 +00:00
|
|
|
return (
|
2019-03-28 02:59:43 +00:00
|
|
|
<div className="shared-file-view-body">
|
|
|
|
<div className="md-view">
|
2019-11-05 09:46:06 +00:00
|
|
|
<MarkdownViewer
|
2020-12-26 09:45:48 +00:00
|
|
|
scriptSource={mediaUrl + 'js/mathjax/tex-svg.js'}
|
2019-03-28 02:59:43 +00:00
|
|
|
markdownContent={this.state.markdownContent}
|
|
|
|
showTOC={false}
|
|
|
|
serviceURL={serviceURL}
|
2019-01-04 10:30:03 +00:00
|
|
|
sharedToken={sharedToken}
|
2019-03-28 02:59:43 +00:00
|
|
|
repoID={repoID}
|
|
|
|
modifyValueBeforeRender={this.modifyValueBeforeRender}
|
2019-01-04 10:30:03 +00:00
|
|
|
/>
|
2019-03-28 02:59:43 +00:00
|
|
|
</div>
|
2019-01-04 10:30:03 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render (
|
|
|
|
<SharedFileViewMarkdown />,
|
|
|
|
document.getElementById('wrapper')
|
2019-11-05 09:46:06 +00:00
|
|
|
);
|