From cd1956b971592e904f652ab6f7e1ec4527120483 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 18 Nov 2018 18:28:22 -0800 Subject: [PATCH] Split editor --- app/components/Editor.js | 88 ++--------------------- app/scenes/Document/Document.js | 3 +- app/scenes/Document/components/Editor.js | 91 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 app/scenes/Document/components/Editor.js diff --git a/app/components/Editor.js b/app/components/Editor.js index 26b2002aa..adf0e72b3 100644 --- a/app/components/Editor.js +++ b/app/components/Editor.js @@ -1,9 +1,6 @@ // @flow import * as React from 'react'; -import styled from 'styled-components'; -import { Text } from 'slate'; -import RichMarkdownEditor, { Placeholder } from 'rich-markdown-editor'; -import ClickablePadding from 'components/ClickablePadding'; +import RichMarkdownEditor from 'rich-markdown-editor'; import { uploadFile } from 'utils/uploadFile'; import isInternalUrl from 'utils/isInternalUrl'; @@ -18,26 +15,6 @@ type Props = { }; class Editor extends React.Component { - editor: *; - - componentDidMount() { - if (!this.props.defaultValue) { - this.focusAtStart(); - } - } - - setEditorRef = (ref: RichMarkdownEditor) => { - this.editor = ref; - }; - - focusAtStart = () => { - if (this.editor) this.editor.focusAtStart(); - }; - - focusAtEnd = () => { - if (this.editor) this.editor.focusAtEnd(); - }; - onUploadImage = async (file: File) => { const result = await uploadFile(file); return result.url; @@ -74,67 +51,16 @@ class Editor extends React.Component { this.props.ui.showToast(message, 'success'); }; - renderPlaceholder = (props: *) => { - const { editor, node } = props; - - if (editor.state.isComposing) return; - if (node.object !== 'block') return; - if (!Text.isTextList(node.nodes)) return; - if (node.text !== '') return; - - const index = editor.value.document.getBlocks().indexOf(node); - if (index > 1) return; - - const text = - index === 0 ? this.props.titlePlaceholder : this.props.bodyPlaceholder; - - return {editor.props.readOnly ? '' : text}; - }; - render() { - const { readOnly, expandToFit } = this.props; - return ( - - - {expandToFit && ( - - )} - + ); } } -// additional styles account for placeholder nodes not always re-rendering -const StyledEditor = styled(RichMarkdownEditor)` - display: flex; - flex: 0; - - ${Placeholder} { - visibility: hidden; - } - - h1:first-of-type { - ${Placeholder} { - visibility: visible; - } - } - - p:nth-child(2):last-child { - ${Placeholder} { - visibility: visible; - } - } -`; - export default Editor; diff --git a/app/scenes/Document/Document.js b/app/scenes/Document/Document.js index 786b46580..46bffb4fb 100644 --- a/app/scenes/Document/Document.js +++ b/app/scenes/Document/Document.js @@ -172,7 +172,7 @@ class DocumentScene extends React.Component { }; loadEditor = async () => { - const EditorImport = await import('components/Editor'); + const EditorImport = await import('./components/Editor'); this.editorComponent = EditorImport.default; }; @@ -360,7 +360,6 @@ class DocumentScene extends React.Component { history={this.props.history} ui={this.props.ui} schema={schema} - expandToFit /> diff --git a/app/scenes/Document/components/Editor.js b/app/scenes/Document/components/Editor.js new file mode 100644 index 000000000..c5cc85b24 --- /dev/null +++ b/app/scenes/Document/components/Editor.js @@ -0,0 +1,91 @@ +// @flow +import * as React from 'react'; +import styled from 'styled-components'; +import { Text } from 'slate'; +import { Placeholder } from 'rich-markdown-editor'; +import Editor from 'components/Editor'; +import ClickablePadding from 'components/ClickablePadding'; + +type Props = { + titlePlaceholder?: string, + bodyPlaceholder?: string, + defaultValue?: string, + readOnly?: boolean, +}; + +class DocumentEditor extends React.Component { + editor: *; + + componentDidMount() { + if (!this.props.defaultValue) { + this.focusAtStart(); + } + } + + focusAtStart = () => { + if (this.editor) this.editor.focusAtStart(); + }; + + focusAtEnd = () => { + if (this.editor) this.editor.focusAtEnd(); + }; + + renderPlaceholder = (props: *) => { + const { editor, node } = props; + + if (editor.state.isComposing) return; + if (node.object !== 'block') return; + if (!Text.isTextList(node.nodes)) return; + if (node.text !== '') return; + + const index = editor.value.document.getBlocks().indexOf(node); + if (index > 1) return; + + const text = + index === 0 ? this.props.titlePlaceholder : this.props.bodyPlaceholder; + + return {editor.props.readOnly ? '' : text}; + }; + + render() { + const { readOnly } = this.props; + + return ( + + (this.editor = ref)} + renderPlaceholder={this.renderPlaceholder} + {...this.props} + /> + + + ); + } +} + +// additional styles account for placeholder nodes not always re-rendering +const StyledEditor = styled(Editor)` + display: flex; + flex: 0; + + ${Placeholder} { + visibility: hidden; + } + + h1:first-of-type { + ${Placeholder} { + visibility: visible; + } + } + + p:nth-child(2):last-child { + ${Placeholder} { + visibility: visible; + } + } +`; + +export default DocumentEditor;