forked from ElemeFE/element
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ElemeFE#78 from e1emeb0t/dev
Rewrite Markdown parser & introduce Transition
- Loading branch information
Showing
18 changed files
with
389 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as Markdown } from './markdown'; | ||
export { default as Transition } from './transition'; | ||
export { default as Component } from './component'; | ||
export { default as PropTypes } from './props'; | ||
export { default as View } from './view'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { transform } from 'babel-standalone'; | ||
import highlight from 'highlight.js'; | ||
import marked from 'marked'; | ||
|
||
import Transition from '../transition'; | ||
|
||
export default class Canvas extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = {}; | ||
} | ||
|
||
componentWillMount() { | ||
marked.setOptions({ | ||
highlight: code => { | ||
return highlight.highlightAuto(code).value; | ||
} | ||
}); | ||
} | ||
|
||
componentDidMount() { | ||
this.renderSource(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.renderSource(); | ||
} | ||
|
||
getHeight() { | ||
return Math.max(this.refs.highlight.offsetHeight, this.refs.description && this.refs.description.offsetHeight || 0); | ||
} | ||
|
||
blockControl() { | ||
this.setState({ | ||
showBlock: !this.state.showBlock | ||
}); | ||
} | ||
|
||
renderSource() { | ||
const Element = require('../../src'); | ||
|
||
const div = this.refs.source; | ||
const args = ['context', 'React'], argv = [this.props.context, React]; | ||
|
||
for (const key in Element) { | ||
args.push(key); | ||
argv.push(Element[key]); | ||
} | ||
|
||
args.push(this.component); | ||
|
||
if (div instanceof HTMLElement) { | ||
ReactDOM.unmountComponentAtNode(div); | ||
} | ||
|
||
ReactDOM.render(new Function(...args).apply(null, argv), div); | ||
} | ||
|
||
render() { | ||
const name = this.props.component.toLowerCase(); | ||
const document = this.props.children.match(/([^]*)\n?(```[^]+```)/); | ||
const source = document[2].match(/```(.*)\n([^]+)```/); | ||
const description = marked(document[1]); | ||
const highlight = marked(document[2]); | ||
|
||
let code = source[2]; | ||
|
||
if (!/^js|javascript/i.test(source[1])) { | ||
code = `<div>${source[2]}</div>` | ||
} | ||
|
||
this.component = transform(code.replace(/this/g, 'context'), { | ||
presets: ['es2015', 'react'] | ||
}).code.replace(/React.createElement/, 'return React.createElement'); | ||
|
||
return ( | ||
<div className={`demo-block demo-box demo-${name}`}> | ||
<div className="source" ref="source"></div> | ||
<div className="meta" style={{ | ||
height: this.state.showBlock ? this.getHeight() : 0 | ||
}}> | ||
{ description && <div ref="description" className="description" dangerouslySetInnerHTML={{ __html: description }}></div> } | ||
<div ref="highlight" className="highlight" dangerouslySetInnerHTML={{ __html: highlight }}></div> | ||
</div> | ||
<div className="demo-block-control" onClick={this.blockControl.bind(this)}> | ||
{ | ||
this.state.showBlock ? ( | ||
<i className="el-icon-caret-top"></i> | ||
) : ( | ||
<i className="el-icon-caret-bottom"></i> | ||
) | ||
} | ||
</div> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
/* eslint-disable */ | ||
Canvas.propTypes = { | ||
component: PropTypes.string.isRequired, | ||
context: PropTypes.any | ||
} | ||
/* eslint-enable */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; | ||
|
||
export default class Transition extends Component { | ||
render() { | ||
return React.createElement(ReactCSSTransitionGroup, { | ||
transitionName: this.props.name, | ||
transitionEnterTimeout: Number(this.props.duration), | ||
transitionLeaveTimeout: Number(this.props.duration), | ||
component: this.props.component, | ||
className: this.props.className, | ||
style: this.props.style | ||
}, React.Children.map(this.props.children, element => { | ||
return React.cloneElement(element, { | ||
key: Math.random().toString() | ||
}) | ||
})); | ||
} | ||
} | ||
|
||
Transition.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
duration: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
component: PropTypes.string, | ||
className: PropTypes.string, | ||
style: PropTypes.object | ||
}; | ||
|
||
Transition.defaultProps = { | ||
duration: 300 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; | ||
|
||
export default class View extends Component { | ||
render() { | ||
const hidden = this.props.hasOwnProperty('show') && !this.props.show; | ||
const element = React.Children.only(this.props.children); | ||
const children = React.cloneElement(element, hidden && { | ||
key: element, | ||
style: Object.assign({}, element.props.style, { | ||
display: 'none' | ||
}) | ||
}); | ||
const style = this.props.hasOwnProperty('show') && !this.props.show && { | ||
display: 'none' | ||
}; | ||
|
||
if (this.props.transition) { | ||
return ( | ||
<ReactCSSTransitionGroup transitionName={this.props.transition} transitionEnterTimeout={500} transitionLeaveTimeout={300} key={this.props.transitionkey}> | ||
{children} | ||
</ReactCSSTransitionGroup> | ||
) | ||
if (React.Children.count(this.props.children) > 1) { | ||
return React.createElement(this.props.component, { | ||
style: Object.assign({}, this.props.style, style), | ||
className: this.props.className | ||
}, this.props.children); | ||
} else { | ||
return children; | ||
return React.cloneElement(this.props.children, { | ||
style: Object.assign({}, this.props.children.props.style, style) | ||
}); | ||
} | ||
} | ||
} | ||
|
||
/* eslint-disable */ | ||
View.propTypes = { | ||
show: PropTypes.any, | ||
transition: PropTypes.string, | ||
transitionKey: PropTypes.string | ||
component: PropTypes.string, | ||
className: PropTypes.string, | ||
style: PropTypes.object | ||
}; | ||
/* eslint-enable */ | ||
|
||
View.defaultProps = { | ||
component: 'span' | ||
} |
Oops, something went wrong.