// @flow import 'easymde/dist/easymde.min.css'; import { FF_MAX_CHARS_DEFAULT } from 'constants/form-field'; import { openEditorMenu, stopContextMenu } from 'util/context-menu'; import { lazyImport } from 'util/lazyImport'; import Button from 'component/button'; import MarkdownPreview from 'component/common/markdown-preview'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; import SimpleMDE from 'react-simplemde-editor'; import type { ElementRef, Node } from 'react'; import Drawer from '@mui/material/Drawer'; import CommentSelectors from 'component/commentCreate/comment-selectors'; // prettier-ignore const TextareaWithSuggestions = lazyImport(() => import('component/textareaWithSuggestions' /* webpackChunkName: "suggestions" */)); type Props = { uri?: string, affixClass?: string, // class applied to prefix/postfix label autoFocus?: boolean, blockWrap: boolean, charCount?: number, children?: React$Node, defaultValue?: string | number, disabled?: boolean, error?: string | boolean, helper?: string | React$Node, hideSuggestions?: boolean, inputButton?: React$Node, isLivestream?: boolean, label?: string | Node, labelOnLeft: boolean, max?: number, min?: number, name: string, placeholder?: string | number, postfix?: string, prefix?: string, quickActionLabel?: string, range?: number, readOnly?: boolean, stretch?: boolean, textAreaMaxLength?: number, type?: string, value?: string | number, slimInput?: boolean, commentSelectorsProps?: any, showSelectors?: boolean, submitButtonRef?: any, tipModalOpen?: boolean, onSlimInputClick?: () => void, onChange?: (any) => any, setShowSelectors?: (boolean) => void, quickActionHandler?: (any) => any, render?: () => React$Node, handleTip?: (isLBC: boolean) => any, handleSubmit?: () => any, }; type State = { drawerOpen: boolean, }; export class FormField extends React.PureComponent { static defaultProps = { labelOnLeft: false, blockWrap: true }; input: { current: ElementRef }; constructor(props: Props) { super(props); this.input = React.createRef(); this.state = { drawerOpen: false, }; } componentDidMount() { const { autoFocus } = this.props; const input = this.input.current; if (input && autoFocus) input.focus(); } componentDidUpdate() { const { showSelectors, slimInput } = this.props; const input = this.input.current; // Opened selectors (emoji/sticker) -> blur input and hide keyboard if (slimInput && showSelectors && input) input.blur(); } render() { const { uri, affixClass, autoFocus, blockWrap, charCount, children, error, helper, hideSuggestions, inputButton, isLivestream, label, labelOnLeft, name, postfix, prefix, quickActionLabel, stretch, textAreaMaxLength, type, slimInput, commentSelectorsProps, showSelectors, submitButtonRef, tipModalOpen, onSlimInputClick, quickActionHandler, setShowSelectors, render, handleTip, handleSubmit, ...inputProps } = this.props; const errorMessage = typeof error === 'object' ? error.message : error; // Ideally, the character count should (and can) be appended to the // SimpleMDE's "options::status" bar. However, I couldn't figure out how // to pass the current value to it's callback, nor query the current // text length from the callback. So, we'll use our own widget. const hasCharCount = charCount !== undefined && charCount >= 0; const countInfo = hasCharCount && textAreaMaxLength !== undefined && ( {`${charCount || '0'}/${textAreaMaxLength}`} ); const Wrapper = blockWrap ? ({ children: innerChildren }) => {innerChildren} : ({ children: innerChildren }) => {innerChildren}; const quickAction = quickActionLabel && quickActionHandler ? (
) : null; const inputSimple = (type: string) => ( <> ); const inputSelect = (selectClass: string) => ( {(label || errorMessage) && ( )} ); const input = () => { switch (type) { case 'radio': return {inputSimple('radio')}; case 'checkbox': return
{inputSimple('checkbox')}
; case 'range': return
{inputSimple('range')}
; case 'select': return inputSelect(''); case 'select-tiny': return inputSelect('select--slim'); case 'markdown': const handleEvents = { contextmenu: openEditorMenu }; const getInstance = (editor) => { // SimpleMDE max char check editor.codemirror.on('beforeChange', (instance, changes) => { if (textAreaMaxLength && changes.update) { var str = changes.text.join('\n'); var delta = str.length - (instance.indexFromPos(changes.to) - instance.indexFromPos(changes.from)); if (delta <= 0) return; delta = instance.getValue().length + delta - textAreaMaxLength; if (delta > 0) { str = str.substr(0, str.length - delta); changes.update(changes.from, changes.to, str.split('\n')); } } }); // "Create Link (Ctrl-K)": highlight URL instead of label: editor.codemirror.on('changes', (instance, changes) => { try { // Grab the last change from the buffered list. I assume the // buffered one ('changes', instead of 'change') is more efficient, // and that "Create Link" will always end up last in the list. const lastChange = changes[changes.length - 1]; if (lastChange.origin === '+input') { // https://github.com/Ionaru/easy-markdown-editor/blob/8fa54c496f98621d5f45f57577ce630bee8c41ee/src/js/easymde.js#L765 const EASYMDE_URL_PLACEHOLDER = '(https://)'; // The URL placeholder is always placed last, so just look at the // last text in the array to also cover the multi-line case: const urlLineText = lastChange.text[lastChange.text.length - 1]; if (urlLineText.endsWith(EASYMDE_URL_PLACEHOLDER) && urlLineText !== '[]' + EASYMDE_URL_PLACEHOLDER) { const from = lastChange.from; const to = lastChange.to; const isSelectionMultiline = lastChange.text.length > 1; const baseIndex = isSelectionMultiline ? 0 : from.ch; // Everything works fine for the [Ctrl-K] case, but for the // [Button] case, this handler happens before the original // code, thus our change got wiped out. // Add a small delay to handle that case. setTimeout(() => { instance.setSelection( { line: to.line, ch: baseIndex + urlLineText.lastIndexOf('(') + 1 }, { line: to.line, ch: baseIndex + urlLineText.lastIndexOf(')') } ); }, 25); } } } catch (e) {} // Do nothing (revert to original behavior) }); }; return (
{quickAction}
; return ReactDOMServer.renderToString(preview); }, }} /> {countInfo}
); case 'textarea': return ( this.setState({ drawerOpen: !this.state.drawerOpen })} closeSelector={setShowSelectors ? () => setShowSelectors(false) : () => {}} commentSelectorsProps={commentSelectorsProps} showSelectors={Boolean(showSelectors)} slimInput={slimInput} tipModalOpen={tipModalOpen} onSlimInputClick={onSlimInputClick} > {(!slimInput || this.state.drawerOpen) && (label || quickAction) && (
{quickAction} {countInfo}
)} {hideSuggestions ? (