From 2a84d1fc67ff884de42c5885331702597d27a769 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Sun, 5 Jun 2016 14:05:01 -0700 Subject: [PATCH] Load editor asynchronously --- src/scenes/DocumentEdit/DocumentEdit.js | 61 ++++++++----------- src/scenes/DocumentEdit/components/Editor.js | 37 +++++++++++ .../DocumentEdit/components/EditorLoader.js | 9 +++ 3 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 src/scenes/DocumentEdit/components/Editor.js create mode 100644 src/scenes/DocumentEdit/components/EditorLoader.js diff --git a/src/scenes/DocumentEdit/DocumentEdit.js b/src/scenes/DocumentEdit/DocumentEdit.js index 507628461..ff5193647 100644 --- a/src/scenes/DocumentEdit/DocumentEdit.js +++ b/src/scenes/DocumentEdit/DocumentEdit.js @@ -1,19 +1,17 @@ import React, { Component } from 'react'; import { observer } from 'mobx-react'; -import state from './DocumentEditState'; +import store from './DocumentEditState'; import Switch from 'components/Switch'; import Layout, { Title, HeaderAction } from 'components/Layout'; import Flex from 'components/Flex'; -import MarkdownEditor from 'components/MarkdownEditor'; import AtlasPreviewLoading from 'components/AtlasPreviewLoading'; import CenteredContent from 'components/CenteredContent'; import DropdownMenu, { MenuItem } from 'components/DropdownMenu'; +import EditorLoader from './components/EditorLoader'; import SaveAction from './components/SaveAction'; -import Preview from './components/Preview'; -import EditorPane from './components/EditorPane'; import styles from './DocumentEdit.scss'; import classNames from 'classnames/bind'; @@ -22,8 +20,14 @@ const cx = classNames.bind(styles); @observer class DocumentEdit extends Component { componentDidMount = () => { - state.documentId = this.props.params.id; - state.fetchDocument(); + store.documentId = this.props.params.id; + store.fetchDocument(); + + EditorLoader() + .then(({ Editor }) => { + console.log("loaded", Editor); + this.setState({ Editor }); + }); } onSave = () => { @@ -31,10 +35,12 @@ class DocumentEdit extends Component { // alert("Please add a title before saving (hint: Write a markdown header)"); // return // } - state.updateDocument(); + store.updateDocument(); } - state = {} + state = { + scrollTop: 0, + } onScroll = (scrollTop) => { this.setState({ @@ -43,7 +49,7 @@ class DocumentEdit extends Component { } onPreviewToggle = () => { - state.togglePreview(); + store.togglePreview(); } render() { @@ -52,7 +58,7 @@ class DocumentEdit extends Component { truncate={ 60 } placeholder={ "Untitle document" } > - { state.title } + { store.title } ); const actions = ( @@ -60,49 +66,36 @@ class DocumentEdit extends Component { - Preview + Preview ); + console.log(store.isFetching, this.state) + return ( - { (state.isFetching) ? ( + { (store.isFetching || !('Editor' in this.state)) ? ( ) : ( -
- - - - { state.preview ? ( - - - - ) : null } -
+ ) }
); diff --git a/src/scenes/DocumentEdit/components/Editor.js b/src/scenes/DocumentEdit/components/Editor.js new file mode 100644 index 000000000..af90f0b0b --- /dev/null +++ b/src/scenes/DocumentEdit/components/Editor.js @@ -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 ( +
+ + + + { store.preview ? ( + + + + ) : null } +
+ ); +}); + +export default Editor; \ No newline at end of file diff --git a/src/scenes/DocumentEdit/components/EditorLoader.js b/src/scenes/DocumentEdit/components/EditorLoader.js new file mode 100644 index 000000000..9969107a8 --- /dev/null +++ b/src/scenes/DocumentEdit/components/EditorLoader.js @@ -0,0 +1,9 @@ +export default () => { + return new Promise(resolve => { + require.ensure([], () => { + resolve({ + Editor: require('./Editor').default, + }); + }); + }); +};