|
| 1 | +import React, { Component } from "react"; |
| 2 | +import DeleteOutlinedIcon from "@material-ui/icons/DeleteOutlined"; |
| 3 | +import CancelButton from "@material-ui/icons/ClearOutlined"; |
| 4 | +import SaveButton from "@material-ui/icons/SaveOutlined"; |
| 5 | +import "react-mde/lib/styles/css/react-mde-all.css"; |
| 6 | +import Button from "react-bootstrap/Button"; |
| 7 | +import Form from "react-bootstrap/Form"; |
| 8 | +import * as Showdown from "showdown"; |
| 9 | +import ReactMde from "react-mde"; |
| 10 | +import "./Editor.scss"; |
| 11 | + |
| 12 | +class Editor extends Component { |
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | + const { page } = this.props; |
| 16 | + this.state = { |
| 17 | + comments: "", |
| 18 | + selectedTab: "write", |
| 19 | + title: page?.title, |
| 20 | + content: page?.content, |
| 21 | + }; |
| 22 | + } |
| 23 | + |
| 24 | + handleSave = () => { |
| 25 | + const { title, content, comments } = this.state; |
| 26 | + const { page, newPage, sidebar, save } = this.props; |
| 27 | + save( |
| 28 | + { |
| 29 | + title, |
| 30 | + content, |
| 31 | + comments, |
| 32 | + history: page?.history, |
| 33 | + }, |
| 34 | + newPage, |
| 35 | + sidebar |
| 36 | + ); |
| 37 | + }; |
| 38 | + |
| 39 | + setContent = (content) => this.setState({ content }); |
| 40 | + |
| 41 | + setTitle = (evt) => this.setState({ title: evt.target.value }); |
| 42 | + |
| 43 | + setSelectedTab = (selectedTab) => this.setState({ selectedTab }); |
| 44 | + |
| 45 | + setComments = (evt) => this.setState({ comments: evt.target.value }); |
| 46 | + |
| 47 | + render() { |
| 48 | + const converter = new Showdown.Converter({ |
| 49 | + tables: true, |
| 50 | + tasklists: true, |
| 51 | + strikethrough: true, |
| 52 | + simplifiedAutoLink: true, |
| 53 | + }); |
| 54 | + const { title, content, selectedTab, comments } = this.state; |
| 55 | + const { deletePage, sidebar, newPage, page, cancel } = this.props; |
| 56 | + return ( |
| 57 | + <div className="wiki-editor"> |
| 58 | + <div className="wikis-top-controls"> |
| 59 | + <Button |
| 60 | + variant="outline-danger" |
| 61 | + onClick={deletePage} |
| 62 | + disabled={sidebar || newPage || page.title === "Home"} |
| 63 | + > |
| 64 | + <span className="vc"> |
| 65 | + <DeleteOutlinedIcon /> |
| 66 | + Delete |
| 67 | + </span> |
| 68 | + </Button> |
| 69 | + <Button onClick={cancel} variant="light"> |
| 70 | + <span className="vc"> |
| 71 | + <CancelButton /> |
| 72 | + Cancel |
| 73 | + </span> |
| 74 | + </Button> |
| 75 | + </div> |
| 76 | + <Form> |
| 77 | + <Form.Label className="field-title">Page Title</Form.Label> |
| 78 | + <Form.Control |
| 79 | + as="input" |
| 80 | + name="pageTitle" |
| 81 | + className="searchbar" |
| 82 | + value={title} |
| 83 | + onChange={this.setTitle} |
| 84 | + readOnly={!newPage} |
| 85 | + /> |
| 86 | + </Form> |
| 87 | + <ReactMde |
| 88 | + onChange={this.setContent} |
| 89 | + value={content} |
| 90 | + onTabChange={this.setSelectedTab} |
| 91 | + selectedTab={selectedTab} |
| 92 | + generateMarkdownPreview={(markdown) => |
| 93 | + Promise.resolve(converter.makeHtml(markdown)) |
| 94 | + } |
| 95 | + /> |
| 96 | + <Form> |
| 97 | + <Form.Label className="field-title">Comments</Form.Label> |
| 98 | + <Form.Control |
| 99 | + as="input" |
| 100 | + name="comments" |
| 101 | + className="searchbar" |
| 102 | + value={comments} |
| 103 | + onChange={this.setComments} |
| 104 | + /> |
| 105 | + </Form> |
| 106 | + <div className="wikis-top-controls"> |
| 107 | + <Button variant="primary" onClick={this.handleSave}> |
| 108 | + <span className="vc"> |
| 109 | + <SaveButton /> Save |
| 110 | + </span> |
| 111 | + </Button> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export default Editor; |
0 commit comments