Bladeren bron

feat: 添加脚本模板

nobody 2 jaren geleden
bovenliggende
commit
e45f4efe33
3 gewijzigde bestanden met toevoegingen van 22 en 21 verwijderingen
  1. 20 21
      packages/yili-dag/src/ScriptEditor.tsx
  2. 2 0
      packages/yili-dag/src/ScriptNodeInfo.tsx
  3. 0 0
      untitled.dag

+ 20 - 21
packages/yili-dag/src/ScriptEditor.tsx

@@ -13,26 +13,10 @@ import 'codemirror/addon/lint/lint.js'
 import 'codemirror/addon/lint/lint.css'
 import { message } from "antd";
 
-/* async function check_python_syntax(text: any, result_cb: any) {
-  const error_list = [
-    {
-      line_no: 1,
-      column_no_start: 0,
-      column_no_stop: 3,
-      message: "invalid syntax",
-      severity: "error"
-    },
-    {
-      line_no: 2,
-      column_no_start: 1,
-      column_no_stop: 5,
-      message: "convention violation",
-      severity: "warning"
-    }
-  ]
-  console.log('text:', text);
-  result_cb(error_list)
-} */
+// const pyTemplate = "# 参考模板\ndef run(input0, input1):\n    r0 = input0\n    r1 = input1\n    return [r0, r1]\n"
+
+// const sqlTemplate = "/* 参考模板 */\nselect * from input0 where input0 = input0;\nselect * from input1 where input1 = input1;"
+
 export default class ScriptEditor extends React.Component<any, any> {
   constructor(props: any) {
     super(props)
@@ -105,6 +89,19 @@ export default class ScriptEditor extends React.Component<any, any> {
     message.success({content: '代码校验完成!', key: 'check', duration: 2})
   }
 
+  getEditorTemplate() {
+    const { inputNumber } = this.props
+    console.log('inputNumber:', inputNumber);
+    const pyTemplate = `# 参考模板
+def run(${Array.from({length: inputNumber}, (_, i) => ('input'+i)).join(', ')}):
+${Array.from({length: inputNumber}, (_, i) => ('    r'+i+' = input' + i)).join('\n')}
+    return [${Array.from({length: inputNumber}, (_, i) => ('r'+i)).join(', ')}]
+`
+    const sqlTemplate = `/* 参考模板 */
+${Array.from({length: inputNumber}, (_, i) => ('select * from input' + i + ' where r' + i + ' = p' + i + ';')).join('\n')}`
+    return this.props.type === 'sql' ? sqlTemplate : pyTemplate
+  }
+
   componentDidMount() {
     const editor = CodeMirror.fromTextArea(document.querySelector('.editor') as any, {
       lineNumbers: true,
@@ -128,7 +125,9 @@ export default class ScriptEditor extends React.Component<any, any> {
       }
     })
     editor.setSize(null, '100%')
-    editor.setValue(this.props.scriptText)
+    const template = this.getEditorTemplate()
+    const value = this.props.scriptText.trim()
+    editor.setValue(value ? value : template)
     this.setState({editor})
   }
 

+ 2 - 0
packages/yili-dag/src/ScriptNodeInfo.tsx

@@ -326,6 +326,7 @@ export default class ScriptNodeInfo extends React.Component<any, any> {
           open={this.state.scriptModalVisible}
           onOk={this.submitScriptEdit}
           className='script-modal'
+          destroyOnClose={true}
           width={'90%'}
           style={{ top: 20 }}
           onCancel={() => {this.setState({scriptModalVisible: false})}}
@@ -369,6 +370,7 @@ export default class ScriptNodeInfo extends React.Component<any, any> {
             <div className='modal-script'>
               <div className='script-header'></div>
               <ScriptEditor
+                inputNumber={this.state.inputNumber}
                 type={this.props.nodeInfo.label}
                 scriptText={this.props.nodeInfo.scriptText}
                 ref={this.editorRef as any}

File diff suppressed because it is too large
+ 0 - 0
untitled.dag


Some files were not shown because too many files changed in this diff