|
| 1 | +import { ButtonGroup, Icon, NumericInput, Position } from '@blueprintjs/core' |
| 2 | +import { IconNames } from '@blueprintjs/icons' |
| 3 | +import * as React from 'react' |
| 4 | +import ReactMde, { ReactMdeTypes } from 'react-mde' |
| 5 | +import * as Showdown from 'showdown' |
| 6 | +import { controlButton } from '../../commons' |
| 7 | + |
| 8 | +type GradingEditorProps = DispatchProps & OwnProps & StateProps |
| 9 | + |
| 10 | +export type DispatchProps = { |
| 11 | + handleGradingCommentsChange: (s: string) => void |
| 12 | + handleGradingXPChange: (i: number | undefined) => void |
| 13 | + handleGradingInputSave: (s: string, i: number | undefined) => void |
| 14 | +} |
| 15 | + |
| 16 | +export type OwnProps = { |
| 17 | + maximumXP: number |
| 18 | +} |
| 19 | + |
| 20 | +export type StateProps = { |
| 21 | + gradingCommentsValue: string |
| 22 | + gradingXP: number | undefined |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Keeps track of the current editor state, |
| 27 | + * as well as the XP in the numeric input. |
| 28 | + * |
| 29 | + * XP can be undefined to show the hint text. |
| 30 | + */ |
| 31 | +type State = { |
| 32 | + mdeState: ReactMdeTypes.MdeState |
| 33 | + XPInput: number | undefined |
| 34 | +} |
| 35 | + |
| 36 | +class GradingEditor extends React.Component<GradingEditorProps, State> { |
| 37 | + private converter: Showdown.Converter |
| 38 | + |
| 39 | + constructor(props: GradingEditorProps) { |
| 40 | + super(props) |
| 41 | + this.state = { |
| 42 | + mdeState: { |
| 43 | + markdown: this.props.gradingCommentsValue |
| 44 | + }, |
| 45 | + XPInput: this.props.gradingXP |
| 46 | + } |
| 47 | + /** |
| 48 | + * The markdown-to-html converter for the editor. |
| 49 | + */ |
| 50 | + this.converter = new Showdown.Converter({ |
| 51 | + tables: true, |
| 52 | + simplifiedAutoLink: true, |
| 53 | + strikethrough: true, |
| 54 | + tasklists: true, |
| 55 | + openLinksInNewWindow: true |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Update the redux state's grading comments value, using the latest |
| 61 | + * value in the local state. |
| 62 | + */ |
| 63 | + public componentWillUnmount() { |
| 64 | + this.props.handleGradingCommentsChange(this.state.mdeState.markdown!) |
| 65 | + this.props.handleGradingXPChange(this.state.XPInput) |
| 66 | + } |
| 67 | + |
| 68 | + public render() { |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <div className="grading-editor-input-parent"> |
| 72 | + <ButtonGroup fill={true}> |
| 73 | + <NumericInput |
| 74 | + onValueChange={this.onXPInputChange} |
| 75 | + value={this.state.XPInput} |
| 76 | + buttonPosition={Position.LEFT} |
| 77 | + placeholder="XP here" |
| 78 | + min={0} |
| 79 | + max={this.props.maximumXP} |
| 80 | + /> |
| 81 | + {controlButton('Save', IconNames.FLOPPY_DISK, this.onClickSaveButton)} |
| 82 | + </ButtonGroup> |
| 83 | + </div> |
| 84 | + <div className="react-mde-parent"> |
| 85 | + <ReactMde |
| 86 | + buttonContentOptions={{ |
| 87 | + iconProvider: this.blueprintIconProvider |
| 88 | + }} |
| 89 | + layout={'vertical'} |
| 90 | + onChange={this.handleValueChange} |
| 91 | + editorState={this.state.mdeState} |
| 92 | + generateMarkdownPreview={this.generateMarkdownPreview} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + </> |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * A custom icons provider. It uses a bulky mapping function |
| 101 | + * defined below. |
| 102 | + * |
| 103 | + * See {@link https://github.com/andrerpena/react-mde} |
| 104 | + */ |
| 105 | + private blueprintIconProvider(name: string) { |
| 106 | + return <Icon icon={faToBlueprintIconMapping(name)} /> |
| 107 | + } |
| 108 | + |
| 109 | + private onClickSaveButton = () => { |
| 110 | + this.props.handleGradingInputSave(this.state.mdeState.markdown!, this.state.XPInput) |
| 111 | + } |
| 112 | + |
| 113 | + private onXPInputChange = (newValue: number) => { |
| 114 | + this.setState({ |
| 115 | + ...this.state, |
| 116 | + XPInput: newValue |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + private handleValueChange = (mdeState: ReactMdeTypes.MdeState) => { |
| 121 | + this.setState({ mdeState }) |
| 122 | + } |
| 123 | + |
| 124 | + private generateMarkdownPreview = (markdown: string) => |
| 125 | + Promise.resolve(this.converter.makeHtml(markdown)) |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Maps FontAwesome5 icon names to blueprintjs counterparts. |
| 130 | + * This is to reduce the number of dependencies on icons, and |
| 131 | + * keep a more consistent look. |
| 132 | + */ |
| 133 | +const faToBlueprintIconMapping = (name: string) => { |
| 134 | + switch (name) { |
| 135 | + case 'heading': |
| 136 | + return IconNames.HEADER |
| 137 | + case 'bold': |
| 138 | + return IconNames.BOLD |
| 139 | + case 'italic': |
| 140 | + return IconNames.ITALIC |
| 141 | + case 'strikethrough': |
| 142 | + return IconNames.STRIKETHROUGH |
| 143 | + case 'link': |
| 144 | + return IconNames.LINK |
| 145 | + case 'quote-right': |
| 146 | + return IconNames.CITATION |
| 147 | + case 'code': |
| 148 | + return IconNames.CODE |
| 149 | + case 'image': |
| 150 | + return IconNames.MEDIA |
| 151 | + case 'list-ul': |
| 152 | + return IconNames.PROPERTIES |
| 153 | + case 'list-ol': |
| 154 | + return IconNames.NUMBERED_LIST |
| 155 | + case 'tasks': |
| 156 | + return IconNames.TICK |
| 157 | + default: |
| 158 | + return IconNames.HELP |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export default GradingEditor |
0 commit comments