|
@@ -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})
|
|
|
}
|
|
|
|