index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2024-01-04 13:46:15
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2024-11-20 17:52:27
  6. * @FilePath: /CostAccountingSys/src/components/SQLEditor/index.tsx
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import React, { useEffect, useState } from 'react';
  10. import MonacoEditor from 'react-monaco-editor';
  11. import { Form, message, Modal, Input } from 'antd';
  12. import './style.less';
  13. import { copyToClipboard } from '@/utils/tooljs';
  14. interface SQLEditorProps {
  15. name: string;
  16. label: string;
  17. rules: { required: boolean; message: string }[];
  18. form: any; // 你可以根据你的表单库提供更具体的类型
  19. width?: string; // 可选的宽度属性
  20. height?: string;
  21. value?: string;
  22. }
  23. function extractSqlParameters(sqlQuery:string) {
  24. // 正则表达式匹配冒号开头的参数名,如 :param
  25. const paramRegex = /#(\w+)/g;
  26. const params = [];
  27. let match;
  28. while (match = paramRegex.exec(sqlQuery)) {
  29. params.push(match[1]); // 将匹配的参数名添加到数组中
  30. }
  31. return params;
  32. }
  33. const SQLEditor: React.FC<SQLEditorProps> = ({ name, label, rules, form, width = '100%', value = '', height = '100%' }) => {
  34. const [editorValue, setEditorValue] = useState(value);
  35. const [modalForm] = Form.useForm();
  36. const handleSQLEditorChange = (newValue: string) => {
  37. setEditorValue(newValue);
  38. form.current.setFieldValue(name, newValue);
  39. };
  40. const copyHandle = ()=>{
  41. copyToClipboard(editorValue as string);
  42. message.success('复制成功!');
  43. }
  44. const verifyHandle = ()=>{
  45. const params = extractSqlParameters(editorValue as string);
  46. Modal.confirm({
  47. title:'验证参数',
  48. icon:null,
  49. content:(
  50. <Form form={modalForm} layout="vertical">
  51. {[...new Set(params)].map(param => (
  52. <Form.Item
  53. key={param}
  54. label={`${param}`}
  55. name={param}
  56. rules={[{ required: true, message: `请输入${param}` }]}
  57. >
  58. <Input />
  59. </Form.Item>
  60. ))}
  61. </Form>
  62. ),
  63. onOk: () => {
  64. return new Promise((resolve, reject) => {
  65. modalForm.validateFields()
  66. .then(values => {
  67. let finalSql = editorValue as string;
  68. params.forEach(param => {
  69. finalSql = finalSql.replace(new RegExp(`#${param}`, 'g'), values[param]);
  70. });
  71. copyToClipboard(finalSql);
  72. message.success('替换参数值并复制成功!');
  73. resolve(true);
  74. }).catch(errorInfo => {
  75. reject(errorInfo);
  76. });
  77. });
  78. }
  79. })
  80. }
  81. const editorDidMount = (editor:any, monaco:any) => {
  82. editor.focus();
  83. };
  84. const handleEditorWillMount = (monaco: any) => {
  85. // 绑定事件确保粘贴未被拦截
  86. monaco.editor.onDidPaste(() => {
  87. console.log('Pasted content');
  88. });
  89. };
  90. useEffect(() => {
  91. setEditorValue(form.current.getFieldValue(name));
  92. }, [form, name]);
  93. return (
  94. <Form.Item
  95. name={name}
  96. label={label}
  97. rules={rules}
  98. getValueProps={() => ({ value: editorValue })}
  99. >
  100. <div className='sql_editor' style={{ border: '1px solid #CFD7E6', borderRadius: 4 }}>
  101. <MonacoEditor
  102. language="sql"
  103. theme="vs-light"
  104. value={editorValue}
  105. options={{
  106. selectOnLineNumbers: true,
  107. // autoIndent: true,
  108. automaticLayout: true,
  109. fontSize: 14,
  110. pasteAs:{
  111. enabled:true,
  112. showPasteSelector:'afterPaste'
  113. }
  114. }}
  115. width={width}
  116. height={height}
  117. onChange={handleSQLEditorChange}
  118. editorDidMount={editorDidMount}
  119. editorWillMount={handleEditorWillMount}
  120. />
  121. <div className='editerFooter'>
  122. <div className='a' onClick={()=>copyHandle()}>原文复制</div>
  123. <div className='b' onClick={()=>verifyHandle()}>替换参数值并复制</div>
  124. </div>
  125. </div>
  126. </Form.Item>
  127. );
  128. };
  129. export default SQLEditor;