diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js index ec84150c28b63be903462bc8299dc992524fdffd..a350bac976035342195f944bf65164839c21486a 100644 --- a/src/functions/codeEditor.js +++ b/src/functions/codeEditor.js @@ -6,7 +6,7 @@ import { showCheck } from "./visualValidation"; import { handlePipeBinding, getPipesForFilter } from "./pipeBinding"; let editorValues = { - doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n // START pipe-binding\n // END pipe-binding\n\n // TODO: Code logic here\n\n\n\t});\n});\n", + doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n // START pipe-binding\n // incoming Pipes\n // outgoing Pipes\n // END pipe-binding\n\n // TODO: Code logic here\n\n\n\t});\n});\n", extensions: [basicSetup, javascript()], }; diff --git a/src/functions/pipeBinding.js b/src/functions/pipeBinding.js index 2fbd594f5bdf316c08035b2f0cd6ec0da269b555..3d53cca24925e43f883ed6d38bf60a472c1c1e90 100644 --- a/src/functions/pipeBinding.js +++ b/src/functions/pipeBinding.js @@ -35,7 +35,7 @@ export const getPipesForFilter = (instance) => { const spanToChange = document.querySelector( `#${connection.pipeId} #PipeName` ); - spanToChange.innerHTML = `"${pipeName}"`; + spanToChange.innerHTML = `${pipeName}`; showCheck(connection.pipeId); instance.repaintEverything(); defaultCount++; @@ -50,47 +50,55 @@ export const getPipesForFilter = (instance) => { export const handlePipeBinding = (pipeMapping, editor) => { // clear pipe-binindg code - const codeArray = editor.state.doc.toString(); - const start = " // START pipe-binding"; + let codeArray = editor.state.doc.toString(); + const start = " // incoming Pipes"; + const middle = " // outgoing Pipes"; const end = " // END pipe-binding"; const startIndex = codeArray.indexOf(start); - const endIndex = codeArray.indexOf(end, startIndex + start.length); + const midIndexIn = codeArray.indexOf(middle); - if (startIndex && endIndex) { - const transaction = editor.state.update({ + if (startIndex && midIndexIn) { + const transactionForIn = editor.state.update({ changes: [ { from: startIndex + start.length, + to: midIndexIn, + insert: "\n\n", + }, + ], + }); + editor.dispatch(transactionForIn); + } + codeArray = editor.state.doc.toString(); + const midIndexOut = codeArray.indexOf(middle); + const endIndex = codeArray.indexOf(end); + + if (midIndexOut && endIndex) { + const transactionForOut = editor.state.update({ + changes: [ + { + from: midIndexOut + middle.length, to: endIndex, insert: "\n", }, ], }); - editor.dispatch(transaction); + editor.dispatch(transactionForOut); } // insert pipe-binding-code - let lineNumber = 15; - pipeMapping.forEach((pipe) => { - let line = editor.state.doc.line(lineNumber); - let position = line.from; - let pipeNameUserGiven = pipe.pipeName; - let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven); - let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${ - pipe.pipeType === "Queue" ? "Queue" : "Exchange" - }(${pipeNameDeklaration}, ${ - pipe.pipeType === "Topic" ? `"topic", ` : "" - }{\n\t\t\tdurable: false\n\t\t});\n`; - let transaction = editor.state.update({ - changes: { - from: position, - insert: insertCode, - }, - }); - editor.dispatch(transaction); - lineNumber = lineNumber + 4; - }); + const incomingPipes = pipeMapping.filter( + (pipe) => pipe.pipeDirection === "in" + ); + const outgoingPipes = pipeMapping.filter( + (pipe) => pipe.pipeDirection === "out" + ); + let lineNumberIn = 16; + let lineNumberOut = 18 + incomingPipes.length * 4; + + fillEditorwithCode(editor, incomingPipes, lineNumberIn); + fillEditorwithCode(editor, outgoingPipes, lineNumberOut); }; const makeValidConstName = (str) => { @@ -164,3 +172,25 @@ const buildPipesElements = (pipeMapping) => { outgoingPipesElement.appendChild(pipeElement); }); }; + +const fillEditorwithCode = (editor, pipeMapping, lineNumber) => { + pipeMapping.forEach((pipe) => { + let line = editor.state.doc.line(lineNumber); + let position = line.from; + let pipeNameUserGiven = pipe.pipeName; + let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven); + let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${ + pipe.pipeType === "Queue" ? "Queue" : "Exchange" + }(${pipeNameDeklaration}, ${ + pipe.pipeType === "Topic" ? `"topic", ` : "" + }{\n\t\t\tdurable: false\n\t\t});\n`; + let transaction = editor.state.update({ + changes: { + from: position, + insert: insertCode, + }, + }); + editor.dispatch(transaction); + lineNumber = lineNumber + 4; + }); +};