Example list on each example page

This commit is contained in:
Tim Schaub
2018-05-12 14:04:19 -06:00
parent d33026d12c
commit ac9b0c7c9f
4 changed files with 134 additions and 26 deletions

View File

@@ -30,6 +30,6 @@ const map = new Map({
target: 'map', target: 'map',
view: new View({ view: new View({
center: [0, 0], center: [0, 0],
zoom: 1 zoom: 2
}) })
}); });

View File

@@ -1,36 +1,47 @@
import React, {Component, Fragment} from 'react'; import React, {Component, Fragment} from 'react';
import {object} from 'prop-types'; import {object} from 'prop-types';
import injectSheet from 'react-jss'; import injectSheet from 'react-jss';
import ExampleList from './ExampleList';
class Example extends Component { class Example extends Component {
render() { render() {
const example = this.props.data.sitePage.context; const example = this.props.data.sitePage.context;
return ( return (
<Fragment> <section className={this.props.classes.wrapper}>
<div className={this.props.classes.nav}>
<ExampleList active={example.slug} />
</div>
<div className={this.props.classes.content}>
<h1>{example.frontmatter.title}</h1> <h1>{example.frontmatter.title}</h1>
<iframe <iframe
className={this.props.classes.embed} className={this.props.classes.embed}
src={example.embedUrl} src={example.embedUrl}
frameBorder="0" frameBorder="0"
/> />
<aside className={this.props.classes.aside}>
<p>
<a href={example.embedUrl}>stand-alone version</a>
</p>
</aside>
<h3>script</h3> <h3>script</h3>
<pre> <pre className={this.props.classes.pre}>
<code>{example.js}</code> <code>{example.js}</code>
</pre> </pre>
<h3>markup</h3> <h3>markup</h3>
<pre> <pre className={this.props.classes.pre}>
<code>{example.html}</code> <code>{example.html}</code>
</pre> </pre>
{example.css && ( {example.css && (
<Fragment> <Fragment>
<h3>style</h3> <h3>style</h3>
<pre> <pre className={this.props.classes.pre}>
<code>{example.css}</code> <code>{example.css}</code>
</pre> </pre>
</Fragment> </Fragment>
)} )}
</Fragment> </div>
</section>
); );
} }
} }
@@ -58,11 +69,28 @@ export const query = graphql`
`; `;
const styles = { const styles = {
wrapper: {
display: 'flex'
},
nav: {
marginRight: '1em'
},
content: {
minWidth: 300,
flexGrow: 1
},
pre: {
overflow: 'auto'
},
embed: { embed: {
margin: 0, margin: 0,
padding: 0, padding: 0,
height: 350, height: 350,
width: '100%' width: '100%'
},
aside: {
textAlign: 'right',
fontSize: '0.75em'
} }
}; };

View File

@@ -0,0 +1,62 @@
import React, {Component} from 'react';
import {object} from 'prop-types';
import injectSheet from 'react-jss';
import Link from 'gatsby-link';
class ExampleList extends Component {
constructor(props) {
super(props);
this.state = {
index: null
};
}
componentDidMount() {
fetch('../index.json')
.then(response => response.json())
.then(index => {
this.setState({index});
});
}
renderList() {
const index = this.state.index;
if (!index) {
return '...';
}
const list = [];
for (const id in index) {
const example = index[id];
list.push(
<li key={id}>
<Link to={example.slug}>{example.title}</Link>
</li>
);
}
return <ul className={this.props.classes.list}>{list}</ul>;
}
render() {
return (
<div className={this.props.classes.wrapper}>{this.renderList()}</div>
);
}
}
ExampleList.propTypes = {
classes: object.isRequired
};
const styles = {
wrapper: {
minWidth: '10em'
},
list: {
margin: 0,
listStyle: 'none'
}
};
export default injectSheet(styles)(ExampleList);

View File

@@ -50,7 +50,15 @@ exports.createPages = async (
{graphql, boundActionCreators}, {graphql, boundActionCreators},
{sourceInstanceName, baseCss = ''} {sourceInstanceName, baseCss = ''}
) => { ) => {
const {createPage} = boundActionCreators; const {createPage, createRedirect} = boundActionCreators;
createRedirect({
fromPath: `/examples/`,
isPermanent: false,
redirectInBrowser: true,
toPath: `/examples/map/`
});
const {data} = await graphql(` const {data} = await graphql(`
{ {
allFile( allFile(
@@ -100,6 +108,7 @@ exports.createPages = async (
const embedDirName = 'example-embeds'; const embedDirName = 'example-embeds';
const embedDir = path.join(__dirname, '..', '..', 'public', embedDirName); const embedDir = path.join(__dirname, '..', '..', 'public', embedDirName);
const exampleDir = path.join(__dirname, '..', '..', 'public', 'examples');
rollupCache = await bundle.write({ rollupCache = await bundle.write({
format: 'es', format: 'es',
@@ -108,6 +117,7 @@ exports.createPages = async (
}); });
const writes = []; const writes = [];
const index = {};
for (const name in examples) { for (const name in examples) {
const node = examples[name].md; const node = examples[name].md;
@@ -162,6 +172,10 @@ exports.createPages = async (
writes.push(writeFile(path.join(embedDir, embedName), embed)); writes.push(writeFile(path.join(embedDir, embedName), embed));
const slug = markdownNode.fields.slug; const slug = markdownNode.fields.slug;
index[name] = {
title: markdownNode.frontmatter.title,
slug
};
createPage({ createPage({
path: slug, path: slug,
@@ -177,5 +191,9 @@ exports.createPages = async (
}); });
} }
writes.push(
writeFile(path.join(exampleDir, 'index.json'), JSON.stringify(index))
);
await Promise.all(writes); await Promise.all(writes);
}; };