import { Box, Button, Field, Flex } from '@rocket.chat/fuselage'; import { useToggle } from '@rocket.chat/fuselage-hooks'; import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from '../../../../contexts/TranslationContext'; import { ResetSettingButton } from '../ResetSettingButton'; function CodeMirror({ lineNumbers = true, lineWrapping = true, mode = 'javascript', gutters = ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'], foldGutter = true, matchBrackets = true, autoCloseBrackets = true, matchTags = true, showTrailingSpace = true, highlightSelectionMatches = true, readOnly, value: valueProp, defaultValue, onChange, ...props }) { const [editor, setEditor] = useState(); const [value, setValue] = useState(valueProp || defaultValue); const ref = useRef(); useEffect(() => { let editor; const setupCodeMirror = async () => { const CodeMirror = await import('codemirror/lib/codemirror.js'); await import('../../../../../app/ui/client/lib/codeMirror/codeMirror'); await import('codemirror/lib/codemirror.css'); const { current: textarea } = ref; if (!textarea) { return; } editor = CodeMirror.fromTextArea(textarea, { lineNumbers, lineWrapping, mode, gutters, foldGutter, matchBrackets, autoCloseBrackets, matchTags, showTrailingSpace, highlightSelectionMatches, readOnly, }); editor.on('change', (doc) => { const value = doc.getValue(); setValue(value); onChange(value); }); setEditor(editor); }; setupCodeMirror(); return () => { if (!editor) { return; } editor.toTextArea(); }; }, [ref]); useEffect(() => { setValue(valueProp); }, [valueProp]); useEffect(() => { if (!editor) { return; } if (value !== editor.getValue()) { editor.setValue(value); } }, [editor, ref, value]); return