summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/components/Image.js
blob: 802d66add419659450b614b0cd506b6a3047e30c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const React = require('react');
const ReactRedux = require('react-redux');

const File = require('../models/File');
const FileShape = require('../propTypes/File');

/**
 * Local image. Using this component instead of <img>
 * avoid broken links when location changes.
 *
 * @type {ReactClass}
 */
const Image = React.createClass({
    propTypes: {
        currentFile: FileShape,
        src: React.PropTypes.oneOfType([
            React.PropTypes.string,
            FileShape
        ])
    },

    render() {
        let { src, currentFile, ...props } = this.props;
        delete props.dispatch;

        if (File.is(src)) {
            src = src.url;
        }

        src = currentFile.relative(src);
        return <img src={src} {...props} />;
    }
});

module.exports = ReactRedux.connect((state) => {
    return { currentFile: state.file };
})(Image);