|
| 1 | +import * as React from "react"; |
| 2 | +import {Command} from "../types"; |
| 3 | +import {ReactMde} from "../ReactMde"; |
| 4 | +import {MdePreview, MdeEditor, MdeToolbar} from "../components"; |
| 5 | +import {LayoutProps} from "../types/LayoutProps"; |
| 6 | + |
| 7 | + |
| 8 | +export const TAB_CODE: string = "TAB_CODE"; |
| 9 | +export const TAB_PREVIEW: string = "TAB_PREVIEW"; |
| 10 | + |
| 11 | +export class TabbedLayout extends React.Component<LayoutProps, {}> { |
| 12 | + state = { |
| 13 | + tab: TAB_CODE, |
| 14 | + }; |
| 15 | + |
| 16 | + editorRef: MdeEditor; |
| 17 | + previewRef: MdePreview; |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * Handler for the textArea value change |
| 23 | + * @memberOf ReactMde |
| 24 | + */ |
| 25 | + handleMdeStateChange = (value) => { |
| 26 | + const {onChange} = this.props; |
| 27 | + onChange(value); |
| 28 | + } |
| 29 | + |
| 30 | + handleCommand = (command: Command) => { |
| 31 | + const { onCommand } = this.props; |
| 32 | + onCommand(command); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Renders react-mde |
| 37 | + * @returns |
| 38 | + * @memberOf ReactMde |
| 39 | + */ |
| 40 | + render() { |
| 41 | + |
| 42 | + const { commands, mdeEditorState } = this.props; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="react-mde-tabbed-layout"> |
| 46 | + <div> |
| 47 | + <button onClick={() => this.setState({tab: TAB_CODE})}>Code</button> |
| 48 | + <button onClick={() => this.setState({tab: TAB_PREVIEW})}>Preview</button> |
| 49 | + </div> |
| 50 | + <MdeToolbar |
| 51 | + commands={commands} |
| 52 | + onCommand={this.handleCommand} |
| 53 | + /> |
| 54 | + { |
| 55 | + this.state.tab === TAB_CODE ? |
| 56 | + <MdeEditor |
| 57 | + editorRef={(c) => this.editorRef = c} |
| 58 | + onChange={this.handleMdeStateChange} |
| 59 | + editorState={mdeEditorState} |
| 60 | + /> |
| 61 | + : |
| 62 | + < MdePreview |
| 63 | + previewRef={(c) => this.previewRef = c} |
| 64 | + html={mdeEditorState ? mdeEditorState.html : ""} |
| 65 | + /> |
| 66 | + } |
| 67 | + </div> |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments