amis2/examples/components/App.tsx

484 lines
12 KiB
TypeScript
Raw Normal View History

2019-06-11 17:00:02 +08:00
import React from 'react';
2019-04-30 11:11:25 +08:00
import NotFound from '../../src/components/404';
import Layout from '../../src/components/Layout';
import AsideNav from '../../src/components/AsideNav';
2020-10-22 20:04:52 +08:00
import {
AlertComponent,
Drawer,
ToastComponent
} from '../../src/components/index';
2019-09-09 00:53:39 +08:00
import {mapTree} from '../../src/utils/helper';
import {Icon} from '../../src/components/icons';
2020-06-02 20:41:51 +08:00
import '../../src/locale/en';
2019-11-07 10:41:14 +08:00
import {
Router,
Route,
IndexRoute,
browserHistory,
hashHistory,
Link,
Redirect,
withRouter
} from 'react-router';
2019-04-30 11:11:25 +08:00
import Select from '../../src/components/Select';
2019-06-04 16:24:09 +08:00
import DocSearch from './DocSearch';
2020-07-24 17:20:08 +08:00
import Doc, {docs} from './Doc';
import Example, {examples} from './Example';
2019-04-30 11:11:25 +08:00
2020-07-30 22:21:03 +08:00
let ExamplePathPrefix = '/examples';
let DocPathPrefix = '/docs';
2019-06-04 20:13:40 +08:00
let ContextPath = '';
2019-04-30 11:11:25 +08:00
2019-05-09 19:40:59 +08:00
if (process.env.NODE_ENV === 'production') {
2020-07-30 22:21:03 +08:00
ExamplePathPrefix = '';
DocPathPrefix = '';
2019-11-07 10:41:14 +08:00
ContextPath = '/amis';
2019-05-09 19:40:59 +08:00
}
2019-04-30 11:11:25 +08:00
const themes = [
2019-11-07 10:41:14 +08:00
{
label: '默认主题',
ns: 'a-',
value: 'default'
},
{
label: '百度云舍',
ns: 'cxd-',
value: 'cxd'
},
{
label: 'Dark',
ns: 'dark-',
value: 'dark'
}
2019-04-30 11:11:25 +08:00
];
2020-06-02 20:41:51 +08:00
const locales = [
{
2020-07-23 20:31:51 +08:00
label: '中文',
2020-06-02 20:41:51 +08:00
value: 'zh-cn'
},
{
label: 'English',
value: 'en'
}
];
const viewModes = [
{
label: '桌面端',
value: 'pc'
},
{
label: '移动端',
value: 'mobile'
}
];
2020-07-30 22:21:03 +08:00
function getPath(path) {
return path
? path[0] === '/'
? ContextPath + path
: `${ContextPath}/${path}`
: '';
}
2020-10-23 17:17:19 +08:00
class BackTop extends React.PureComponent {
state = {
show: false
};
componentDidMount() {
document.addEventListener('scroll', this.handleScroll.bind(this));
}
componentWillUnmount() {
document.removeEventListener('scroll', this.handleScroll.bind(this));
}
handleScroll(e) {
this.setState({
show: e.target.scrollingElement?.scrollTop > 350
2020-10-23 17:17:19 +08:00
});
}
render() {
return (
<div
className={`Backtop ${this.state.show ? 'visible' : ''}`}
onClick={() => scrollTo({top: 0})}
>
<i className="fa fa-rocket"></i>
</div>
);
}
}
2019-05-20 12:03:25 +08:00
@withRouter
2019-04-30 11:11:25 +08:00
export class App extends React.PureComponent {
2019-11-07 10:41:14 +08:00
state = {
viewMode: localStorage.getItem('viewMode') || 'pc',
2019-11-07 10:41:14 +08:00
offScreen: false,
headerVisible: true,
themeIndex: 0,
themes: themes,
2020-06-02 20:41:51 +08:00
theme: themes[localStorage.getItem('themeIndex') || 0],
2020-07-23 20:31:51 +08:00
locale: localStorage.getItem('locale') || '',
2020-10-23 17:17:19 +08:00
navigations: []
2019-11-07 10:41:14 +08:00
};
constructor(props) {
super(props);
2020-07-24 17:20:08 +08:00
this.setNavigations = this.setNavigations.bind(this);
2019-11-07 10:41:14 +08:00
}
componentDidMount() {
if (this.state.theme.value !== 'default') {
document.querySelectorAll('link[title]').forEach(item => {
item.disabled = true;
});
2020-10-23 17:17:19 +08:00
2020-11-27 17:29:49 +08:00
document
.querySelectorAll(`link[title=${this.state.theme.value}]`)
.forEach(item => {
item.disabled = false;
});
2020-07-29 20:25:04 +08:00
if (this.state.theme.value === 'dark') {
document.querySelector('body').classList.add('dark');
}
2019-11-07 10:41:14 +08:00
}
}
componentDidUpdate(preProps, preState) {
const props = this.props;
if (preState.theme.value !== this.state.theme.value) {
2020-11-27 17:29:49 +08:00
document
.querySelectorAll(`link[title=${preState.theme.value}]`)
.forEach(item => {
item.disabled = true;
});
document
.querySelectorAll(`link[title=${this.state.theme.value}]`)
.forEach(item => {
item.disabled = false;
});
2019-09-09 00:53:39 +08:00
}
2019-04-30 11:11:25 +08:00
2019-11-07 10:41:14 +08:00
if (props.location.pathname !== preProps.location.pathname) {
this.setState(
{
offScreen: false
},
() => window.scrollTo(0, 0)
);
2020-10-23 17:17:19 +08:00
_hmt && _hmt.push(['_trackPageview', props.location.pathname]);
2019-09-09 00:53:39 +08:00
}
2019-11-07 10:41:14 +08:00
}
2019-04-30 11:11:25 +08:00
2020-07-24 17:20:08 +08:00
setNavigations(items) {
this.setState({
navigations: items
});
}
2019-11-07 10:41:14 +08:00
renderHeader() {
const location = this.props.location;
const theme = this.state.theme;
if (location.pathname === '/edit') {
return (
<div id="headerBar" className="box-shadow bg-dark">
<div className={`${theme.ns}Layout-brand`}>AMis </div>
</div>
);
2019-09-09 00:53:39 +08:00
}
2019-11-07 10:41:14 +08:00
return (
2020-07-23 20:31:51 +08:00
<>
2019-11-07 10:41:14 +08:00
<div className={`${theme.ns}Layout-brandBar`}>
2020-10-22 20:04:52 +08:00
<div
2019-11-07 10:41:14 +08:00
onClick={() => this.setState({offScreen: !this.state.offScreen})}
2020-10-22 20:04:52 +08:00
className={`${theme.ns}Layout-offScreen-btn pull-left visible-xs`}
2019-11-07 10:41:14 +08:00
>
2020-10-22 20:04:52 +08:00
<i className="bui-icon iconfont icon-collapse"></i>
</div>
2020-07-23 20:31:51 +08:00
2019-11-07 10:41:14 +08:00
<div className={`${theme.ns}Layout-brand`}>
2020-07-31 12:40:04 +08:00
<Link to={`${ContextPath}/docs`}>
2020-08-05 13:22:14 +08:00
<div className="logo"></div>
2020-07-31 12:40:04 +08:00
</Link>
2019-11-07 10:41:14 +08:00
</div>
</div>
2019-09-09 00:53:39 +08:00
2020-07-23 20:31:51 +08:00
<div className={`${theme.ns}Layout-headerBar`}>
<ul className={`${theme.ns}Layout-headerBar-links pull-left`}>
2020-07-30 22:21:03 +08:00
<Link to={`${ContextPath}/docs`} activeClassName="is-active">
2020-07-24 17:20:08 +08:00
</Link>
2020-07-30 22:21:03 +08:00
<Link to={`${ContextPath}/examples`} activeClassName="is-active">
2020-07-24 17:20:08 +08:00
</Link>
2020-10-22 15:28:24 +08:00
<a
href="https://github.com/fex-team/amis-editor-demo"
target="_blank"
>
2020-07-31 12:40:04 +08:00
</a>
2020-07-31 13:02:48 +08:00
{/* <a href="https://suda.bce.baidu.com" target="_blank">
2020-07-31 12:40:04 +08:00
2020-07-31 13:02:48 +08:00
</a> */}
2020-07-23 20:31:51 +08:00
</ul>
<div className="hidden-xs p-t pull-right m-l-sm">
<Select
clearable={false}
theme={this.state.theme.value}
value={this.state.locale || 'zh-cn'}
options={locales}
onChange={locale => {
this.setState({locale: locale.value});
localStorage.setItem('locale', locale.value);
window.location.reload();
2020-07-23 20:31:51 +08:00
}}
/>
2020-06-02 20:41:51 +08:00
</div>
<div className="hidden-xs p-t pull-right m-l-sm">
2020-07-23 20:31:51 +08:00
<Select
clearable={false}
theme={this.state.theme.value}
value={this.state.theme}
options={this.state.themes}
onChange={theme => {
this.setState({theme});
localStorage.setItem(
'themeIndex',
this.state.themes.indexOf(theme)
);
2020-07-29 20:25:04 +08:00
document
.querySelector('body')
.classList[theme.value === 'dark' ? 'add' : 'remove']('dark');
2020-07-23 20:31:51 +08:00
}}
/>
2019-11-07 10:41:14 +08:00
</div>
<div className="hidden-xs p-t pull-right">
<Select
clearable={false}
theme={this.state.theme.value}
value={this.state.viewMode || 'pc'}
options={viewModes}
onChange={viewMode => {
this.setState({viewMode: viewMode.value});
localStorage.setItem('viewMode', viewMode.value);
window.location.reload();
}}
/>
</div>
2020-08-21 17:33:31 +08:00
</div>
2020-08-21 12:54:47 +08:00
2020-08-21 17:33:31 +08:00
<div className={`${theme.ns}Layout-searchBar hidden-xs hidden-sm`}>
<DocSearch theme={theme} />
2019-11-07 10:41:14 +08:00
</div>
2020-07-30 14:21:21 +08:00
<a
className="gh-icon"
href="https://github.com/baidu/amis"
target="_blank"
>
<i className="fa fa-github" />
</a>
2020-07-23 20:31:51 +08:00
</>
2019-11-07 10:41:14 +08:00
);
}
2019-04-30 11:11:25 +08:00
2020-10-22 20:04:52 +08:00
renderNavigation() {
return (
<div className="Doc-navigation">
<AsideNav
navigations={this.state.navigations.map(item => ({
...item,
children: item.children
? item.children.map(item => ({
...item,
className: 'is-top'
}))
: []
}))}
renderLink={({
link,
active,
toggleExpand,
classnames: cx,
depth
}: any) => {
let children = [];
if (link.children && link.children.length) {
children.push(
<span
key="expand-toggle"
className={cx('AsideNav-itemArrow')}
onClick={e => toggleExpand(link, e)}
></span>
2020-07-28 14:55:49 +08:00
);
2020-10-22 20:04:52 +08:00
}
children.push(
<span className={cx('AsideNav-itemLabel')} key="label">
{link.label}
</span>
);
return link.path ? (
/^https?\:/.test(link.path) ? (
<a target="_blank" href={link.path}>
{children}
</a>
) : (
<Link
to={
2020-10-22 20:36:01 +08:00
getPath(link.path) ||
2020-10-22 20:04:52 +08:00
(link.children && getPath(link.children[0].path))
}
>
{children}
</Link>
)
) : (
<a onClick={link.children ? () => toggleExpand(link) : undefined}>
{children}
</a>
);
}}
isActive={(link: any) => isActive(link, location)}
/>
</div>
);
2020-07-23 20:31:51 +08:00
}
2019-11-07 10:41:14 +08:00
render() {
const theme = this.state.theme;
2020-07-23 20:31:51 +08:00
2019-11-07 10:41:14 +08:00
return (
<Layout
theme={theme.value}
2020-07-23 20:31:51 +08:00
boxed={true}
2019-11-07 10:41:14 +08:00
offScreen={this.state.offScreen}
header={this.state.headerVisible ? this.renderHeader() : null}
>
2020-06-02 20:41:51 +08:00
<ToastComponent theme={theme.value} locale={this.state.locale} />
<AlertComponent theme={theme.value} locale={this.state.locale} />
2020-07-23 20:31:51 +08:00
<div className="Doc">
2020-08-05 13:22:14 +08:00
<div className="Doc-nav hidden-xs hidden-sm">
2020-10-22 20:04:52 +08:00
{this.renderNavigation()}
2020-07-23 20:31:51 +08:00
</div>
2020-10-22 20:04:52 +08:00
<Drawer
size="xs"
className="Doc-navDrawer"
overlay
closeOnOutside
onHide={() => this.setState({offScreen: false})}
show={this.state.offScreen}
position="left"
>
{this.renderNavigation()}
</Drawer>
2020-10-23 17:17:19 +08:00
<BackTop />
2020-07-28 18:23:02 +08:00
2020-07-23 20:31:51 +08:00
{React.cloneElement(this.props.children, {
2020-08-01 00:26:55 +08:00
key: theme.value,
2020-07-23 20:31:51 +08:00
...this.props.children.props,
2020-07-24 17:20:08 +08:00
setNavigations: this.setNavigations,
2020-07-23 20:31:51 +08:00
theme: theme.value,
classPrefix: theme.ns,
viewMode: this.state.viewMode,
2020-07-31 13:18:44 +08:00
locale: this.state.locale,
2020-10-22 20:04:52 +08:00
offScreen: this.state.offScreen,
2020-07-31 13:18:44 +08:00
ContextPath
2020-07-23 20:31:51 +08:00
})}
</div>
2019-11-07 10:41:14 +08:00
</Layout>
);
}
2019-09-09 00:53:39 +08:00
}
2019-04-30 11:11:25 +08:00
2020-10-22 20:04:52 +08:00
function isActive(link: any, location: any) {
2020-10-22 20:36:01 +08:00
return !!(link.path && getPath(link.path) === location.pathname);
2020-10-22 20:04:52 +08:00
}
2020-07-30 22:21:03 +08:00
function navigations2route(pathPrefix = DocPathPrefix, navigations) {
2019-11-07 10:41:14 +08:00
let routes = [];
navigations.forEach(root => {
root.children &&
mapTree(root.children, item => {
if (item.path && item.component) {
routes.push(
<Route
key={routes.length + 1}
path={
item.path[0] === '/'
? ContextPath + item.path
2020-07-30 22:21:03 +08:00
: `${ContextPath}/${item.path}`
2019-11-07 10:41:14 +08:00
}
component={item.component}
/>
);
} else if (item.path && item.getComponent) {
routes.push(
<Route
key={routes.length + 1}
path={
item.path[0] === '/'
? ContextPath + item.path
2020-07-30 22:21:03 +08:00
: `${ContextPath}/${item.path}`
2019-11-07 10:41:14 +08:00
}
getComponent={item.getComponent}
/>
);
}
});
});
2019-04-30 11:11:25 +08:00
2019-11-07 10:41:14 +08:00
return routes;
2019-09-09 00:53:39 +08:00
}
2019-04-30 11:11:25 +08:00
2019-09-09 00:53:39 +08:00
export default function entry({pathPrefix}) {
2020-07-30 22:21:03 +08:00
// PathPrefix = pathPrefix || DocPathPrefix;
2019-11-07 10:41:14 +08:00
return (
2020-07-24 17:20:08 +08:00
<Router history={browserHistory}>
2019-11-07 10:41:14 +08:00
<Route component={App}>
2020-07-29 15:33:25 +08:00
<Redirect from={`${ContextPath}/`} to={`${ContextPath}/docs/index`} />
2020-07-30 22:21:03 +08:00
<Redirect
from={`${ContextPath}/docs`}
to={`${ContextPath}/docs/index`}
/>
<Redirect
from={`${ContextPath}/examples`}
to={`${ContextPath}/examples/pages/simple`}
/>
<Route path={`${ContextPath}/docs`} component={Doc}>
{navigations2route(DocPathPrefix, docs)}
2020-07-24 17:20:08 +08:00
</Route>
2020-07-30 22:21:03 +08:00
<Route path={`${ContextPath}/examples`} component={Example}>
{navigations2route(ExamplePathPrefix, examples)}
2020-07-24 17:20:08 +08:00
</Route>
2019-11-07 10:41:14 +08:00
</Route>
2020-07-24 17:20:08 +08:00
<Route path="*" component={NotFound} />
2019-11-07 10:41:14 +08:00
</Router>
);
2019-09-09 00:53:39 +08:00
}