Skip to content

Commit d13434c

Browse files
ringodstleunen
authored andcommitted
Support for selecting a single file in a multi-file gist. (#2)
1 parent 0921c2d commit d13434c

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Get the id from the gist url `https://gist.github.com/{your_name}/{id}` and set
88

99
## Example
1010

11+
Single-file gist:
12+
1113
```js
1214
var React = require('react');
1315
var Gist = require('react-gist');
@@ -18,11 +20,24 @@ React.render(
1820
);
1921
```
2022

23+
Multi-file gist:
24+
25+
```js
26+
var React = require('react');
27+
var Gist = require('react-gist');
28+
29+
React.render(
30+
<Gist id='5995ea726914f280afb3' file='Chef-Dockerfile' />,
31+
document.body
32+
);
33+
```
34+
2135
## Usage
2236

2337
### `<Gist id={string} />`
2438

2539
- `id` {string} Id of the gist
40+
- `file` {string} Name of a specific file in a multi-file gist
2641

2742
## demo
2843

demo/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ var React = require('react');
22
var Gist = require('../index');
33

44
var g = React.render(
5-
<Gist id='5104372' />,
5+
<Gist id='bedde975e6e92a77e2321487ba45f313' />,
66
document.body
77
);
88

99
// update the gist after 5 seconds
1010
setTimeout(function() {
11-
g.setProps({id: '4702818'});
11+
g.setProps({id: '5995ea726914f280afb3', file: 'Chef-Dockerfile'});
1212
}, 5000);

index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ var React = require('react');
44

55
var Gist = React.createClass({
66
propTypes: {
7-
id: React.PropTypes.string.isRequired
7+
id: React.PropTypes.string.isRequired,
8+
file: React.PropTypes.string
89
},
910

1011
shouldComponentUpdate: function(nextProps) {
11-
return this.props.id !== nextProps.id;
12+
return (this.props.id !== nextProps.id) || (this.props.file != nextProps.file);
1213
},
1314

1415
componentDidMount: function() {
@@ -18,14 +19,23 @@ var Gist = React.createClass({
1819
this._updateIframeContent();
1920
},
2021

22+
_calculateUrl: function() {
23+
if (this.props.file) {
24+
return 'https://gist.github.com/' + this.props.id + '.js?file=' + this.props.file
25+
} else {
26+
return 'https://gist.github.com/' + this.props.id + '.js'
27+
}
28+
},
29+
2130
_updateIframeContent: function() {
2231
var iframe = this.refs.iframe.getDOMNode();
2332

2433
var doc = iframe.document;
2534
if (iframe.contentDocument) doc = iframe.contentDocument;
2635
else if (iframe.contentWindow) doc = iframe.contentWindow.document;
2736

28-
var gistScript = '<script type="text/javascript" src="https://gist.github.com/' + this.props.id + '.js"></script>';
37+
var gistLink = this._calculateUrl()
38+
var gistScript = '<script type="text/javascript" src="' + gistLink + '"></script>';
2939
var styles = '<style>*{font-size:12px;}</style>';
3040
var resizeScript = 'onload="parent.document.getElementById(\'gist-' + this.props.id + '\').style.height=document.body.scrollHeight + \'px\'"';
3141
var iframeHtml = '<html><head><base target="_parent">'+styles+'</head><body '+resizeScript+'>'+gistScript+'</body></html>';

0 commit comments

Comments
 (0)