import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'; import refractor from 'refractor'; import { processHtml, htmlEncode } from './utils'; import shortcuts from './shortcuts'; import * as styles from './styles'; import './style/index.less'; export * from './SelectionText'; export interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes { prefixCls?: string; /** * Set what programming language the code belongs to. */ language?: string; /** * Optional padding for code. Default: `10`. */ padding?: number; /** * The minimum height of the editor. Default: `16`. */ minHeight?: number; /** * A refractor syntax (Prism language definition) to be registered by refractor. Default: `undefined`. */ syntax?: refractor.RefractorSyntax; } export default React.forwardRef((props, ref) => { const { prefixCls = 'w-tc-editor', value: _, padding = 10, minHeight = 16, syntax = undefined, placeholder, language, className, style, onChange, ...other } = props; const [value, setValue] = useState(props.value || ''); useEffect(() => setValue(props.value || ''), [props.value]); const textRef = useRef(null); useImperativeHandle(ref, () => textRef.current!); const contentStyle = { paddingTop: padding, paddingRight: padding, paddingBottom: padding, paddingLeft: padding, }; const htmlStr = useMemo( () => processHtml( ``, syntax, ), [value, language], ); const preView = useMemo( () => (
), // eslint-disable-next-line react-hooks/exhaustive-deps [prefixCls, language, htmlStr], ); return (