From 7f7e276114d138789f4228a09b4bf8a9f4c1ed99 Mon Sep 17 00:00:00 2001 From: Robin Leber <rleber98@gmail.com> Date: Thu, 27 Jun 2024 09:54:54 +0200 Subject: [PATCH] ADD: extend pipe binidnig (change pipes possible) --- src/functions/codeEditor.js | 2 +- src/functions/pipeBinding.js | 67 +++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js index d175a49..cae386d 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\n\n //TODO: Code logic here\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 // 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 3d4a944..2fbd594 100644 --- a/src/functions/pipeBinding.js +++ b/src/functions/pipeBinding.js @@ -49,34 +49,47 @@ export const getPipesForFilter = (instance) => { }; export const handlePipeBinding = (pipeMapping, editor) => { - let lineNumber = 14; - let editorCodeText = editor.state.doc.toString(); - pipeMapping.forEach((pipe) => { - if ( - editorCodeText.includes( - `const ${makeValidConstName(pipe.pipeName)} = "${pipe.pipeName}"` - ) - ) { - return; - } else { - 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, + // clear pipe-binindg code + const codeArray = editor.state.doc.toString(); + const start = " // START pipe-binding"; + const end = " // END pipe-binding"; + + const startIndex = codeArray.indexOf(start); + const endIndex = codeArray.indexOf(end, startIndex + start.length); + + if (startIndex && endIndex) { + const transaction = editor.state.update({ + changes: [ + { + from: startIndex + start.length, + to: endIndex, + insert: "\n", }, - }); - editor.dispatch(transaction); - lineNumber = lineNumber + 4; - } + ], + }); + editor.dispatch(transaction); + } + + // 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; }); }; -- GitLab