|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import Prism from 'prismjs'; |
| 4 | + |
| 5 | +import { getNextStory, getPreviousStory } from 'glutenberg'; |
| 6 | +import markdown from '../markdown'; |
| 7 | + |
| 8 | +class Tabs extends Component { |
| 9 | + constructor() { |
| 10 | + super( ...arguments ); |
| 11 | + this.state = { |
| 12 | + activeTab: 0, |
| 13 | + }; |
| 14 | + } |
| 15 | + |
| 16 | + selectTab( index ) { |
| 17 | + return () => { |
| 18 | + this.setState( { activeTab: index } ); |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + componentDidUpdate() { |
| 23 | + Prism.highlightAll(); |
| 24 | + } |
| 25 | + |
| 26 | + render() { |
| 27 | + const { tabs } = this.props; |
| 28 | + const activeTab = tabs[ this.state.activeTab ]; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div> |
| 32 | + <div> |
| 33 | + { tabs.map( ( tab, index ) => ( |
| 34 | + <button |
| 35 | + key={ index } |
| 36 | + onClick={ this.selectTab( index ) } |
| 37 | + className={ index === this.state.activeTab ? 'is-active' : '' } |
| 38 | + > |
| 39 | + { tab.name } |
| 40 | + </button> |
| 41 | + ) ) } |
| 42 | + { activeTab && <div dangerouslySetInnerHTML={ { __html: activeTab.content } } /> } |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function MarkdownContent( { content } ) { |
| 50 | + const blocks = markdown( content ); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div> |
| 54 | + { blocks.map( ( block, index ) => { |
| 55 | + if ( block.type === 'raw' ) { |
| 56 | + return <div key={ index } dangerouslySetInnerHTML={ { __html: block.content } } />; |
| 57 | + } |
| 58 | + if ( block.type === 'codetabs' ) { |
| 59 | + return <Tabs key={ index } tabs={ block.tabs } />; |
| 60 | + } |
| 61 | + |
| 62 | + return null; |
| 63 | + } ) } |
| 64 | + </div> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +class Page extends Component { |
| 69 | + componentDidMount() { |
| 70 | + Prism.highlightAll(); |
| 71 | + } |
| 72 | + |
| 73 | + render() { |
| 74 | + const { story } = this.props; |
| 75 | + const nextStory = getNextStory( story.id ); |
| 76 | + const previousStory = getPreviousStory( story.id ); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + { !! story.Component && <story.Component /> } |
| 81 | + { !! story.markdown && <MarkdownContent content={ story.markdown } /> } |
| 82 | + |
| 83 | + <div className="navigation"> |
| 84 | + { !! previousStory && ( |
| 85 | + <p className="nav-older"> |
| 86 | + <Link to={ previousStory.path }>{ '←' } { previousStory.title }</Link> |
| 87 | + </p> |
| 88 | + ) } |
| 89 | + { !! nextStory && ( |
| 90 | + <p className="nav-newer"> |
| 91 | + <Link to={ nextStory.path }>{ nextStory.title } { '→' }</Link> |
| 92 | + </p> |
| 93 | + ) } |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export default Page; |
0 commit comments