code-editor.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* eslint-disable react-hooks/rules-of-hooks */
  2. import { IFlowNodeParameter } from '@/types/flow';
  3. import { convertKeysToCamelCase } from '@/utils/flow';
  4. import Editor from '@monaco-editor/react';
  5. import { Button, Form, Modal } from 'antd';
  6. import { useMemo, useState } from 'react';
  7. import { useTranslation } from 'react-i18next';
  8. export const renderCodeEditor = (data: IFlowNodeParameter) => {
  9. const { t } = useTranslation();
  10. const attr = convertKeysToCamelCase(data.ui?.attr || {});
  11. const [isModalOpen, setIsModalOpen] = useState(false);
  12. const showModal = () => {
  13. setIsModalOpen(true);
  14. };
  15. const onOk = () => {
  16. setIsModalOpen(false);
  17. };
  18. const onCancel = () => {
  19. setIsModalOpen(false);
  20. };
  21. const modalWidth = useMemo(() => {
  22. if (data?.ui?.editor?.width) {
  23. return data?.ui?.editor?.width + 100;
  24. }
  25. return '80%';
  26. }, [data?.ui?.editor?.width]);
  27. return (
  28. <div className='p-2 text-sm'>
  29. <Button type='default' onClick={showModal}>
  30. {t('Open_Code_Editor')}
  31. </Button>
  32. <Modal title={t('Code_Editor')} width={modalWidth} open={isModalOpen} onOk={onOk} onCancel={onCancel}>
  33. <Form.Item name={data?.name}>
  34. <Editor
  35. {...attr}
  36. width={data?.ui?.editor?.width || '100%'}
  37. height={data?.ui?.editor?.height || 200}
  38. defaultLanguage={data?.ui?.language}
  39. theme='vs-dark'
  40. options={{
  41. minimap: {
  42. enabled: false,
  43. },
  44. wordWrap: 'on',
  45. }}
  46. />
  47. </Form.Item>
  48. </Modal>
  49. </div>
  50. );
  51. };