|
1 | | -import React, { Fragment, useEffect, useState } from "react"; |
| 1 | +import React, { useEffect, useState, useRef, useCallback } from "react"; |
2 | 2 | import PropTypes from "prop-types"; |
3 | | -import Editor from "react-simple-code-editor"; |
| 3 | +import { useEditable } from "use-editable"; |
4 | 4 | import Highlight, { Prism } from "prism-react-renderer"; |
5 | 5 | import { theme as liveTheme } from "../../constants/theme"; |
6 | 6 |
|
7 | 7 | const CodeEditor = (props) => { |
8 | | - const [state, setState] = useState({ |
9 | | - code: props.code || "", |
10 | | - }); |
| 8 | + const editorRef = useRef(null); |
| 9 | + const [code, setCode] = useState(props.code || ""); |
11 | 10 |
|
12 | | - useEffect(() => { |
13 | | - if (state.prevCodeProp && props.code !== state.prevCodeProp) { |
14 | | - setState({ code: props.code, prevCodeProp: props.code }); |
15 | | - } |
16 | | - }, [props.code]); |
| 11 | + const onEditableChange = useCallback((_code) => { |
| 12 | + setCode(_code.slice(0, -1)); |
| 13 | + }, []); |
17 | 14 |
|
18 | | - const updateContent = (code) => { |
19 | | - setState({ code }); |
20 | | - }; |
| 15 | + useEditable(editorRef, onEditableChange, { |
| 16 | + disabled: props.disabled, |
| 17 | + indentation: 2, |
| 18 | + }); |
21 | 19 |
|
22 | 20 | useEffect(() => { |
23 | 21 | if (props.onChange) { |
24 | | - props.onChange(state.code); |
| 22 | + props.onChange(code); |
25 | 23 | } |
26 | | - }, [state.code]); |
27 | | - |
28 | | - const highlightCode = (code) => ( |
29 | | - <Highlight |
30 | | - Prism={Prism} |
31 | | - code={code} |
32 | | - theme={props.theme || liveTheme} |
33 | | - language={props.language} |
34 | | - > |
35 | | - {({ tokens, getLineProps, getTokenProps }) => ( |
36 | | - <Fragment> |
37 | | - {tokens.map((line, i) => ( |
38 | | - // eslint-disable-next-line react/jsx-key |
39 | | - <div {...getLineProps({ line, key: i })}> |
40 | | - {line.map((token, key) => ( |
41 | | - // eslint-disable-next-line react/jsx-key |
42 | | - <span {...getTokenProps({ token, key })} /> |
43 | | - ))} |
44 | | - </div> |
45 | | - ))} |
46 | | - </Fragment> |
47 | | - )} |
48 | | - </Highlight> |
49 | | - ); |
50 | | - |
51 | | - // eslint-disable-next-line no-unused-vars |
52 | | - const { style, theme, onChange, ...rest } = props; |
53 | | - const { code } = state; |
54 | | - |
55 | | - const baseTheme = theme && typeof theme.plain === "object" ? theme.plain : {}; |
| 24 | + }, [code]); |
56 | 25 |
|
57 | 26 | return ( |
58 | | - <Editor |
59 | | - value={code} |
60 | | - padding={10} |
61 | | - highlight={highlightCode} |
62 | | - onValueChange={updateContent} |
63 | | - style={{ |
64 | | - whiteSpace: "pre", |
65 | | - fontFamily: "monospace", |
66 | | - ...baseTheme, |
67 | | - ...style, |
68 | | - }} |
69 | | - {...rest} |
70 | | - /> |
| 27 | + <div className={props.className}> |
| 28 | + <Highlight |
| 29 | + Prism={Prism} |
| 30 | + code={code} |
| 31 | + theme={props.theme || liveTheme} |
| 32 | + language={props.language} |
| 33 | + > |
| 34 | + {({ |
| 35 | + className: _className, |
| 36 | + tokens, |
| 37 | + getLineProps, |
| 38 | + getTokenProps, |
| 39 | + style: _style, |
| 40 | + }) => ( |
| 41 | + <pre |
| 42 | + className={_className} |
| 43 | + style={{ |
| 44 | + margin: 0, |
| 45 | + outline: "none", |
| 46 | + padding: 10, |
| 47 | + ...(!props.className || !props.theme ? {} : _style), |
| 48 | + }} |
| 49 | + ref={editorRef} |
| 50 | + > |
| 51 | + {tokens.map((line, lineIndex) => ( |
| 52 | + // eslint-disable-next-line react/jsx-key |
| 53 | + <div {...getLineProps({ line, key: `line-${lineIndex}` })}> |
| 54 | + {line |
| 55 | + .filter((token) => !token.empty) |
| 56 | + .map((token, tokenIndex) => ( |
| 57 | + // eslint-disable-next-line react/jsx-key |
| 58 | + <span |
| 59 | + {...getTokenProps({ token, key: `token-${tokenIndex}` })} |
| 60 | + /> |
| 61 | + ))} |
| 62 | + {"\n"} |
| 63 | + </div> |
| 64 | + ))} |
| 65 | + </pre> |
| 66 | + )} |
| 67 | + </Highlight> |
| 68 | + </div> |
71 | 69 | ); |
72 | 70 | }; |
73 | 71 |
|
74 | 72 | CodeEditor.propTypes = { |
| 73 | + className: PropTypes.string, |
75 | 74 | code: PropTypes.string, |
76 | 75 | disabled: PropTypes.bool, |
77 | 76 | language: PropTypes.string, |
|
0 commit comments