Skip to content

Commit 8c9c882

Browse files
committed
feat(rte): add code mirror
1 parent 25e77f6 commit 8c9c882

File tree

4 files changed

+332
-39
lines changed

4 files changed

+332
-39
lines changed

packages/pluggableWidgets/rich-text-web/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@
4343
"verify": "rui-verify-package-format"
4444
},
4545
"dependencies": {
46+
"@codemirror/lang-html": "^6.4.9",
4647
"@floating-ui/react": "^0.26.27",
4748
"@melloware/coloris": "^0.24.0",
49+
"@uiw/codemirror-theme-github": "^4.21.25",
50+
"@uiw/react-codemirror": "^4.21.25",
4851
"classnames": "^2.2.6",
4952
"dompurify": "^3.2.4",
53+
"js-beautify": "^1.15.4",
5054
"katex": "^0.16.11",
5155
"linkifyjs": "^4.1.3",
5256
"lodash.merge": "^4.6.2",
@@ -68,6 +72,7 @@
6872
"@rollup/plugin-json": "^6.1.0",
6973
"@rollup/plugin-replace": "^6.0.2",
7074
"@types/dompurify": "^2.4.0",
75+
"@types/js-beautify": "^1.14.3",
7176
"@types/katex": "^0.16.7",
7277
"@types/sanitize-html": "^1.27.2",
7378
"cross-env": "^7.0.3",

packages/pluggableWidgets/rich-text-web/src/components/ModalDialog/DialogContent.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ export function DialogBody(props: PropsWithChildrenWithClass): ReactElement {
4242
}
4343

4444
export interface FormControlProps extends PropsWithChildrenWithClass {
45-
label: string;
45+
label?: string;
4646
}
4747

4848
export function FormControl(props: FormControlProps): ReactElement {
4949
const { children, className, label } = props;
5050

5151
return (
5252
<div className={classNames("form-group", className)}>
53-
<label className="control-label col-sm-3">{label}</label>
54-
<div className="col-sm-9"> {children}</div>
53+
{label && <label className="control-label col-sm-3">{label}</label>}
54+
<div className={`col-sm-${label ? "9" : "12"}`}> {children}</div>
5555
</div>
5656
);
5757
}

packages/pluggableWidgets/rich-text-web/src/components/ModalDialog/ViewCodeDialog.tsx

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
1-
import { ChangeEvent, createElement, ReactElement, useState } from "react";
1+
import { createElement, ReactElement, useState, useCallback } from "react";
22
import { type viewCodeConfigType } from "../../utils/formats";
33
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";
49

510
export interface ViewCodeDialogProps {
611
currentCode?: string;
712
onSubmit(value: viewCodeConfigType): void;
813
onClose(): void;
914
}
1015

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+
1128
export default function ViewCodeDialog(props: ViewCodeDialogProps): ReactElement {
1229
const { onSubmit, onClose, currentCode } = props;
1330
const [formState, setFormState] = useState({
14-
src: currentCode || ""
31+
src: beautify.html(currentCode ?? "", BEAUTIFY_OPTIONS) || ""
1532
});
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+
}, []);
2036

2137
return (
2238
<DialogContent className="view-code-dialog">
2339
<DialogHeader onClose={onClose}>View/Edit Code</DialogHeader>
2440
<DialogBody>
25-
<FormControl label="Source Code">
26-
<textarea
27-
name="src"
41+
<div>
42+
<label>Source Code</label>
43+
</div>
44+
<FormControl>
45+
<CodeMirror
2846
className="form-control mx-textarea-input mx-textarea-noresize code-input"
29-
onChange={onInputChange}
3047
value={formState.src}
31-
></textarea>
48+
extensions={[EditorView.lineWrapping, html()]}
49+
onChange={onCodeChange}
50+
basicSetup={true}
51+
theme={githubLight}
52+
maxHeight="70vh"
53+
/>
3254
</FormControl>
3355
<DialogFooter onSubmit={() => onSubmit(formState)} onClose={onClose}></DialogFooter>
3456
</DialogBody>

0 commit comments

Comments
 (0)