Load editor asynchronously
This commit is contained in:
@@ -1,19 +1,17 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { observer } from 'mobx-react';
|
import { observer } from 'mobx-react';
|
||||||
|
|
||||||
import state from './DocumentEditState';
|
import store from './DocumentEditState';
|
||||||
|
|
||||||
import Switch from 'components/Switch';
|
import Switch from 'components/Switch';
|
||||||
import Layout, { Title, HeaderAction } from 'components/Layout';
|
import Layout, { Title, HeaderAction } from 'components/Layout';
|
||||||
import Flex from 'components/Flex';
|
import Flex from 'components/Flex';
|
||||||
import MarkdownEditor from 'components/MarkdownEditor';
|
|
||||||
import AtlasPreviewLoading from 'components/AtlasPreviewLoading';
|
import AtlasPreviewLoading from 'components/AtlasPreviewLoading';
|
||||||
import CenteredContent from 'components/CenteredContent';
|
import CenteredContent from 'components/CenteredContent';
|
||||||
import DropdownMenu, { MenuItem } from 'components/DropdownMenu';
|
import DropdownMenu, { MenuItem } from 'components/DropdownMenu';
|
||||||
|
|
||||||
|
import EditorLoader from './components/EditorLoader';
|
||||||
import SaveAction from './components/SaveAction';
|
import SaveAction from './components/SaveAction';
|
||||||
import Preview from './components/Preview';
|
|
||||||
import EditorPane from './components/EditorPane';
|
|
||||||
|
|
||||||
import styles from './DocumentEdit.scss';
|
import styles from './DocumentEdit.scss';
|
||||||
import classNames from 'classnames/bind';
|
import classNames from 'classnames/bind';
|
||||||
@@ -22,8 +20,14 @@ const cx = classNames.bind(styles);
|
|||||||
@observer
|
@observer
|
||||||
class DocumentEdit extends Component {
|
class DocumentEdit extends Component {
|
||||||
componentDidMount = () => {
|
componentDidMount = () => {
|
||||||
state.documentId = this.props.params.id;
|
store.documentId = this.props.params.id;
|
||||||
state.fetchDocument();
|
store.fetchDocument();
|
||||||
|
|
||||||
|
EditorLoader()
|
||||||
|
.then(({ Editor }) => {
|
||||||
|
console.log("loaded", Editor);
|
||||||
|
this.setState({ Editor });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onSave = () => {
|
onSave = () => {
|
||||||
@@ -31,10 +35,12 @@ class DocumentEdit extends Component {
|
|||||||
// alert("Please add a title before saving (hint: Write a markdown header)");
|
// alert("Please add a title before saving (hint: Write a markdown header)");
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
state.updateDocument();
|
store.updateDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
state = {}
|
state = {
|
||||||
|
scrollTop: 0,
|
||||||
|
}
|
||||||
|
|
||||||
onScroll = (scrollTop) => {
|
onScroll = (scrollTop) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
@@ -43,7 +49,7 @@ class DocumentEdit extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onPreviewToggle = () => {
|
onPreviewToggle = () => {
|
||||||
state.togglePreview();
|
store.togglePreview();
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -52,7 +58,7 @@ class DocumentEdit extends Component {
|
|||||||
truncate={ 60 }
|
truncate={ 60 }
|
||||||
placeholder={ "Untitle document" }
|
placeholder={ "Untitle document" }
|
||||||
>
|
>
|
||||||
{ state.title }
|
{ store.title }
|
||||||
</Title>
|
</Title>
|
||||||
);
|
);
|
||||||
const actions = (
|
const actions = (
|
||||||
@@ -60,49 +66,36 @@ class DocumentEdit extends Component {
|
|||||||
<HeaderAction>
|
<HeaderAction>
|
||||||
<SaveAction
|
<SaveAction
|
||||||
onClick={ this.onSave }
|
onClick={ this.onSave }
|
||||||
disabled={ state.isSaving }
|
disabled={ store.isSaving }
|
||||||
/>
|
/>
|
||||||
</HeaderAction>
|
</HeaderAction>
|
||||||
<DropdownMenu label="More">
|
<DropdownMenu label="More">
|
||||||
<MenuItem onClick={ this.onPreviewToggle }>
|
<MenuItem onClick={ this.onPreviewToggle }>
|
||||||
Preview <Switch checked={ state.preview } />
|
Preview <Switch checked={ store.preview } />
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</Flex>
|
</Flex>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(store.isFetching, this.state)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout
|
<Layout
|
||||||
actions={ actions }
|
actions={ actions }
|
||||||
title={ title }
|
title={ title }
|
||||||
fixed={ true }
|
fixed={ true }
|
||||||
loading={ state.isSaving }
|
loading={ store.isSaving }
|
||||||
>
|
>
|
||||||
{ (state.isFetching) ? (
|
{ (store.isFetching || !('Editor' in this.state)) ? (
|
||||||
<CenteredContent>
|
<CenteredContent>
|
||||||
<AtlasPreviewLoading />
|
<AtlasPreviewLoading />
|
||||||
</CenteredContent>
|
</CenteredContent>
|
||||||
) : (
|
) : (
|
||||||
<div className={ styles.container }>
|
<this.state.Editor
|
||||||
<EditorPane
|
store={ store }
|
||||||
fullWidth={ !state.preview }
|
scrollTop={ this.state.scrollTop }
|
||||||
onScroll={ this.onScroll }
|
onScroll={ this.onScroll }
|
||||||
>
|
/>
|
||||||
<MarkdownEditor
|
|
||||||
onChange={ state.updateText }
|
|
||||||
text={ state.text }
|
|
||||||
replaceText={ state.replaceText }
|
|
||||||
preview={ state.preview }
|
|
||||||
/>
|
|
||||||
</EditorPane>
|
|
||||||
{ state.preview ? (
|
|
||||||
<EditorPane
|
|
||||||
scrollTop={ this.state.scrollTop }
|
|
||||||
>
|
|
||||||
<Preview html={ state.htmlPreview } />
|
|
||||||
</EditorPane>
|
|
||||||
) : null }
|
|
||||||
</div>
|
|
||||||
) }
|
) }
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
|
|||||||
37
src/scenes/DocumentEdit/components/Editor.js
Normal file
37
src/scenes/DocumentEdit/components/Editor.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { observer } from 'mobx-react';
|
||||||
|
|
||||||
|
import MarkdownEditor from 'components/MarkdownEditor';
|
||||||
|
import Preview from './Preview';
|
||||||
|
import EditorPane from './EditorPane';
|
||||||
|
|
||||||
|
import styles from '../DocumentEdit.scss';
|
||||||
|
|
||||||
|
const Editor = observer((props) => {
|
||||||
|
const store = props.store;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={ styles.container }>
|
||||||
|
<EditorPane
|
||||||
|
fullWidth={ !store.preview }
|
||||||
|
onScroll={ props.onScroll }
|
||||||
|
>
|
||||||
|
<MarkdownEditor
|
||||||
|
onChange={ store.updateText }
|
||||||
|
text={ store.text }
|
||||||
|
replaceText={ store.replaceText }
|
||||||
|
preview={ store.preview }
|
||||||
|
/>
|
||||||
|
</EditorPane>
|
||||||
|
{ store.preview ? (
|
||||||
|
<EditorPane
|
||||||
|
scrollTop={ props.scrollTop }
|
||||||
|
>
|
||||||
|
<Preview html={ store.htmlPreview } />
|
||||||
|
</EditorPane>
|
||||||
|
) : null }
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Editor;
|
||||||
9
src/scenes/DocumentEdit/components/EditorLoader.js
Normal file
9
src/scenes/DocumentEdit/components/EditorLoader.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export default () => {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
require.ensure([], () => {
|
||||||
|
resolve({
|
||||||
|
Editor: require('./Editor').default,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user