diff --git a/example/mock/db.json b/example/mock/db.json index 551cad4..55a2a0f 100644 --- a/example/mock/db.json +++ b/example/mock/db.json @@ -464,7 +464,7 @@ "configSchema": "{\n \n}", "configData": {}, "dependencies": "{\n \"lodash.get\": \"4.4.2\"\n}", - "code": "import fpget from 'lodash.get';\n\nexport default async function(ctx) {\n const obj = {a: {b: 'hello imove~'}};\n console.log(fpget(obj, 'a.b'));\n}" + "code": "import fpget from 'lodash.get';\n\nexport default async function(ctx) {\n const obj = {a: {b: 'hello imove~'}};\n console.log(fpget(obj, 'a.b'));\n return ctx.getPipe();\n}" }, "ports": { "groups": { diff --git a/packages/compile-code/src/compileForOnline.ts b/packages/compile-code/src/compileForOnline.ts index 5af5984..ee9d5d9 100644 --- a/packages/compile-code/src/compileForOnline.ts +++ b/packages/compile-code/src/compileForOnline.ts @@ -1,5 +1,5 @@ import {Cell} from '@antv/x6'; -import tpl from './template/runOnline'; +import makeCode from './template/runOnline'; import simplifyDSL from './simplifyDSL'; interface DSL { @@ -69,6 +69,17 @@ const findStartNode = (dsl: DSL): Cell.Properties => { return startNode; }; +const getNextNode = (curNode: Cell.Properties, dsl: DSL) => { + + const nodes = dsl.cells.filter(cell => cell.shape !== 'edge'); + const edges = dsl.cells.filter(cell => cell.shape === 'edge'); + + const foundEdge = edges.find(edge => edge.source.cell === curNode.id); + if(foundEdge) { + return nodes.find(node => node.id === foundEdge.target.cell); + } +}; + const compileSimplifiedDSL = (dsl: DSL): string => { const simplyfiedDSL = JSON.stringify(simplifyDSL(dsl), null, 2); return `const dsl = ${simplyfiedDSL};`; @@ -96,9 +107,10 @@ const compileNodeFnsMap = (dsl: DSL): string => { return `const nodeFns = {\n ${kvs.join(',\n ')}\n}`; }; -const compile = (dsl: DSL): string => { +const compile = (dsl: DSL, mockInput: any): string => { const startNode = findStartNode(dsl); - return tpl + const mockNode = getNextNode(startNode, dsl); + return makeCode(mockNode, mockInput) .replace(INSERT_DSL_COMMENT, compileSimplifiedDSL(dsl)) .replace(INSERT_NODE_FNS_COMMENT, compileNodeFnsMap(dsl)) .replace('$TRIGGER$', startNode.data.trigger); diff --git a/packages/compile-code/src/template/runOnline.ts b/packages/compile-code/src/template/runOnline.ts index bc59ae4..f2091ec 100644 --- a/packages/compile-code/src/template/runOnline.ts +++ b/packages/compile-code/src/template/runOnline.ts @@ -1,7 +1,7 @@ import logic from './logic'; import context from './context'; -export default ` +const makeCode = (mockNode: any, mockInput: any) => ` (async function run() { // Context ${context.replace(/export\s+default/, '')} @@ -24,8 +24,37 @@ export default ` // nodeFns map // define nodeFns here + // imove plugin + const mockPlugin = () => { + const mockNode = ${JSON.stringify(mockNode)}; + const mockInput = ${JSON.stringify(mockInput)}; + const mockKeys = [ + ['config', 'getConfig'], + ['pipe', 'getPipe'], + ['payload', 'getPayload'], + ['context', 'getContext'] + ]; + return { + ctxCreated(ctx) { + if(mockNode && mockInput) { + mockKeys.forEach(([inputType, method]) => { + const _method = ctx[method]; + ctx[method] = () => { + if(ctx.curNode.id === mockNode.id) { + return mockInput[inputType]; + } else { + return _method(); + } + } + }); + } + }, + }; + }; + // instantiation and invoke const logic = new Logic({ dsl }); + logic.use(mockPlugin); logic.invoke('$TRIGGER$', {}, (pipe) => { const ctx = logic._getUnsafeCtx(); const context = ctx.getContext(); @@ -33,3 +62,5 @@ export default ` }); })(); `; + +export default makeCode; diff --git a/packages/core/src/components/codeRun/index.tsx b/packages/core/src/components/codeRun/index.tsx index 5717415..e124283 100644 --- a/packages/core/src/components/codeRun/index.tsx +++ b/packages/core/src/components/codeRun/index.tsx @@ -69,9 +69,9 @@ const CodeRun: React.FC = (props) => { const onClickRun = useCallback(() => { const selectedCelssJson = toSelectedCellsJSON(flowChart); - const compiledCode = compileForOnline(selectedCelssJson); + const compiledCode = compileForOnline(selectedCelssJson, input); executeScript(compiledCode); - }, [flowChart]); + }, [flowChart, input]); const onChangeInput = useCallback((val: any) => { setInput(val);