2018-01-08 15:07:26 +00:00
|
|
|
import { path } from 'ramda'
|
2017-04-28 15:47:01 +00:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import ReactDOM from 'react-dom'
|
2017-07-02 17:12:02 +00:00
|
|
|
import marked from 'Engine/marked'
|
2017-10-13 13:47:16 +00:00
|
|
|
import './Dictionary.css'
|
2018-01-30 14:16:32 +00:00
|
|
|
import Overlay from './Overlay'
|
2017-04-28 15:47:01 +00:00
|
|
|
|
|
|
|
// On ajoute à la section la possibilité d'ouvrir un panneau d'explication des termes.
|
|
|
|
// Il suffit à la section d'appeler une fonction fournie en lui donnant du JSX
|
|
|
|
export let AttachDictionary = dictionary => Decorated =>
|
2018-05-09 16:41:57 +00:00
|
|
|
class withDictionary extends Component {
|
2017-04-28 15:47:01 +00:00
|
|
|
state = {
|
|
|
|
term: null,
|
|
|
|
explanation: null
|
|
|
|
}
|
|
|
|
componentDidMount() {
|
2018-05-09 16:41:57 +00:00
|
|
|
// eslint-disable-next-line
|
2017-04-28 15:47:01 +00:00
|
|
|
let decoratedNode = ReactDOM.findDOMNode(this.decorated)
|
|
|
|
decoratedNode.addEventListener('click', e => {
|
|
|
|
let term = e.target.dataset['termDefinition'],
|
2018-01-08 15:07:26 +00:00
|
|
|
explanation = path([term, 'description'], dictionary)
|
2018-01-03 15:54:19 +00:00
|
|
|
this.setState({ explanation, term })
|
2017-04-28 15:47:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
renderExplanationMarkdown(explanation, term) {
|
|
|
|
return marked(`### Mécanisme: ${term}\n\n${explanation}`)
|
|
|
|
}
|
2018-01-03 15:54:19 +00:00
|
|
|
render() {
|
|
|
|
let { explanation, term } = this.state
|
2017-04-28 15:47:01 +00:00
|
|
|
return (
|
2018-05-09 16:41:57 +00:00
|
|
|
<div>
|
2018-01-03 15:54:19 +00:00
|
|
|
<Decorated
|
|
|
|
ref={decorated => (this.decorated = decorated)}
|
|
|
|
{...this.props}
|
|
|
|
/>
|
|
|
|
{explanation && (
|
2018-01-30 14:16:32 +00:00
|
|
|
<Overlay
|
|
|
|
onOuterClick={() =>
|
|
|
|
this.setState({ term: null, explanation: null })
|
2018-05-09 16:41:57 +00:00
|
|
|
}>
|
2018-01-03 15:54:19 +00:00
|
|
|
<div
|
|
|
|
id="dictionaryPanel"
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: this.renderExplanationMarkdown(explanation, term)
|
|
|
|
}}
|
|
|
|
/>
|
2018-01-30 14:16:32 +00:00
|
|
|
</Overlay>
|
2018-01-03 15:54:19 +00:00
|
|
|
)}
|
2017-04-28 15:47:01 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|