|
1 |
| -import { ChangeEvent, createElement, ReactElement, useState } from "react"; |
| 1 | +import { createElement, ReactElement, useState, useCallback } from "react"; |
2 | 2 | import { type viewCodeConfigType } from "../../utils/formats";
|
3 | 3 | import { DialogBody, DialogContent, DialogFooter, DialogHeader, FormControl } from "./DialogContent";
|
| 4 | +import CodeMirror, { ViewUpdate } from "@uiw/react-codemirror"; |
| 5 | +import { html } from "@codemirror/lang-html"; |
| 6 | +import { githubLight } from "@uiw/codemirror-theme-github"; |
| 7 | +import { EditorView } from "codemirror"; |
| 8 | +import beautify from "js-beautify"; |
4 | 9 |
|
5 | 10 | export interface ViewCodeDialogProps {
|
6 | 11 | currentCode?: string;
|
7 | 12 | onSubmit(value: viewCodeConfigType): void;
|
8 | 13 | onClose(): void;
|
9 | 14 | }
|
10 | 15 |
|
| 16 | +const BEAUTIFY_OPTIONS: js_beautify.HTMLBeautifyOptions = { |
| 17 | + indent_size: 4, |
| 18 | + indent_char: " ", |
| 19 | + max_preserve_newlines: 5, |
| 20 | + preserve_newlines: true, |
| 21 | + indent_scripts: "normal", |
| 22 | + end_with_newline: false, |
| 23 | + wrap_line_length: 0, |
| 24 | + indent_inner_html: false, |
| 25 | + indent_empty_lines: false |
| 26 | +}; |
| 27 | + |
11 | 28 | export default function ViewCodeDialog(props: ViewCodeDialogProps): ReactElement {
|
12 | 29 | const { onSubmit, onClose, currentCode } = props;
|
13 | 30 | const [formState, setFormState] = useState({
|
14 |
| - src: currentCode || "" |
| 31 | + src: beautify.html(currentCode ?? "", BEAUTIFY_OPTIONS) || "" |
15 | 32 | });
|
16 |
| - |
17 |
| - const onInputChange = (e: ChangeEvent<HTMLTextAreaElement>): void => { |
18 |
| - setFormState({ ...formState, [e.target.name]: e.target.value }); |
19 |
| - }; |
| 33 | + const onCodeChange = useCallback((value: string, _viewUpdate: ViewUpdate) => { |
| 34 | + setFormState({ ...formState, src: value }); |
| 35 | + }, []); |
20 | 36 |
|
21 | 37 | return (
|
22 | 38 | <DialogContent className="view-code-dialog">
|
23 | 39 | <DialogHeader onClose={onClose}>View/Edit Code</DialogHeader>
|
24 | 40 | <DialogBody>
|
25 |
| - <FormControl label="Source Code"> |
26 |
| - <textarea |
27 |
| - name="src" |
| 41 | + <div> |
| 42 | + <label>Source Code</label> |
| 43 | + </div> |
| 44 | + <FormControl> |
| 45 | + <CodeMirror |
28 | 46 | className="form-control mx-textarea-input mx-textarea-noresize code-input"
|
29 |
| - onChange={onInputChange} |
30 | 47 | value={formState.src}
|
31 |
| - ></textarea> |
| 48 | + extensions={[EditorView.lineWrapping, html()]} |
| 49 | + onChange={onCodeChange} |
| 50 | + basicSetup={true} |
| 51 | + theme={githubLight} |
| 52 | + maxHeight="70vh" |
| 53 | + /> |
32 | 54 | </FormControl>
|
33 | 55 | <DialogFooter onSubmit={() => onSubmit(formState)} onClose={onClose}></DialogFooter>
|
34 | 56 | </DialogBody>
|
|
0 commit comments