// Copyright (c) 2019 Shellyl_N and Authors // license: ISC // https://github.com/shellyln import React from 'react'; import { connect } from 'react-redux'; import { RouteComponentProps } from 'react-router-dom'; import { makeStyles, useTheme } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; import CheckIcon from '@material-ui/icons/Check'; import DeleteIcon from '@material-ui/icons/Delete'; import Typography from '@material-ui/core/Typography'; import Fab from '@material-ui/core/Fab'; import clsx from 'clsx'; import { UnControlled as CodeMirror } from 'react-codemirror2'; import 'codemirror/lib/codemirror.css'; import 'codemirror/theme/material.css'; import 'codemirror/mode/yaml/yaml'; import jsYaml from 'js-yaml'; import { KanbanBoardState } from '../types'; import { mapDispatchToProps, mapStateToProps, KanbanBoardActions } from '../dispatchers/KanbanBoardDispatcher'; import ConfirmDialog from '../components/ConfirmDialog'; import { pickEditableBoardProps, validateBoardProps } from '../lib/validation'; import './EditorView.css'; type EditorViewProps = KanbanBoardState & KanbanBoardActions & RouteComponentProps<{id: string}> & { }; const useStyles = makeStyles(theme => ({ root: { flexGrow: 1, color: 'var(--main-text-color)', backgroundColor: 'var(--main-bg-color)', width: '100%', height: '100%', }, header: { height: '45px', position: 'relative', }, codemirror: { display: 'grid', width: 'calc(100% - 20px)', height: 'calc(100vh - 65px)', }, fabSave: { position: 'absolute', margin: theme.spacing(1), left: theme.spacing(1), top: theme.spacing(1) / 10, }, fabDelete: { position: 'absolute', margin: theme.spacing(1), right: theme.spacing(1), top: theme.spacing(1) / 10, }, })); const EditorView: React.FC = (props) => { const classes = useStyles(); const theme = useTheme(); const [confirmDeletingOpen, setConfirmDeletingOpen] = React.useState(false); const [editorValue, setEditorValue] = React.useState(''); const [editCount, setEditCount] = React.useState(0); // NOTE: codeMirror.current.props.value is NOT updated after onChange event. // const codeMirror = useRef((null as any) as CodeMirror); function handleSaveClick() { try { const data = jsYaml.safeLoad(editorValue); if (data) { validateBoardProps(data); props.editBoardAndStickys(Object.assign({}, data, { _id: props.activeBoard._id })); } else { // } } catch (e) { props.showAlertDialog({ open: true, title: 'Error', message: e.message || String(e), singleButton: true, colorIsSecondary: true, onClose: () => props.closeAlertDialog(), }); } } function handleConfirmDeleting(apply: boolean) { setConfirmDeletingOpen(false); if (apply) { props.deleteBoard(props.activeBoard._id); } } function handleEditorChange(editor: any, data: any, value: string) { setEditorValue(value); setEditCount(editCount + 1); } if (props.match.params.id) { if (props.activeBoard._id !== props.match.params.id) { const index = props.boards.findIndex(x => x._id === props.match.params.id); props.changeActiveBoard(props.match.params.id); // NOTE: dirty hack setTimeout(() => { setEditCount(0); }, 30); return (
{index < 0 ? <> No boards found. {props.history.push('/')}} > Click here to show main board. : <> }
); } } return (
{props.activeBoard.name} {props.boards.length > 1 ? : <> }
{confirmDeletingOpen ? } onClose={handleConfirmDeleting} /> : <> }
); } export default connect(mapStateToProps, mapDispatchToProps)(EditorView);