File size: 1,951 Bytes
7f88cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Debug from 'debug'
import PropTypes from 'prop-types'
import React from 'react'
import ReactDOM from 'react-dom'
import ImageToolDemo from './src/ImageToolDemo'
import HotspotImageDemo from './src/HotspotImageDemo'
import history from './src/history'
import {createRoute, createScope, RouterProvider, RouteScope, StateLink} from '@sanity/state-router'
import IMAGES from './src/data/testImages'

Debug.disable('')
Debug.enable(process.env.DEBUG)

const DEFAULT_IMAGE_INDEX = 4

const routes = createRoute('/*', [
  createRoute('/:demoName/*', (params) => {
    return params.demoName ? createScope(params.demoName, createRoute('/:imageIndex')) : []
  }),
])

class Root extends React.Component {
  static contextTypes = {
    router: PropTypes.object,
  }
  renderDemo(demoName) {
    const routerState = this.context.router.state
    const demoParams = routerState[demoName] || {}
    if (demoName === 'hotspotimage') {
      return <HotspotImageDemo src={IMAGES[demoParams.imageIndex || DEFAULT_IMAGE_INDEX]} />
    }
    if (demoName === 'imagetool') {
      return <ImageToolDemo src={IMAGES[demoParams.imageIndex || DEFAULT_IMAGE_INDEX]} />
    }
    return <div>No such demo</div>
  }
  render() {
    const {router} = this.context
    return (
      <div>
        <StateLink state={{demoName: 'imagetool'}}>ImageTool</StateLink>
        <StateLink state={{demoName: 'hotspotimage'}}>Hotspot Image</StateLink>
        {router.state.demoName && (
          <RouteScope scope={router.state.demoName}>
            {this.renderDemo(router.state.demoName)}
          </RouteScope>
        )}
      </div>
    )
  }
}

function handleNavigate(nexturl) {
  history.push(nexturl)
}
function render(location) {
  ReactDOM.render(
    <RouterProvider router={routes} location={location} onNavigate={handleNavigate}>
      <Root />
    </RouterProvider>,
    document.getElementById('content')
  )
}

history.listen(render)
render(history.location)