diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js index a8b483224d4f2bb7c0a054019a2fe288cc7f35a7..e66ff7a45f196d7f31a5101fda0968e709c27cee 100644 --- a/src/functions/codeEditor.js +++ b/src/functions/codeEditor.js @@ -3,7 +3,11 @@ import { EditorState } from "@codemirror/state"; import { javascript } from "@codemirror/lang-javascript"; import { createCustomCode } from "./api"; import { showCheck } from "./visualValidation"; -import { handlePipeBinding, getPipesForFilter } from "./pipeBinding"; +import { + handlePipeBinding, + getPipesForFilter, + deleteWrittenCode, +} from "./codeEditorHandler"; let editorValues = { doc: "// Please don't remove comments!\nconst 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", @@ -140,7 +144,12 @@ export const codeEditorElement = (instance) => { }; const handleCancel = (node) => { - node.style.visibility = "hidden"; + if (confirm("No changes will be saved!")) { + node.style.visibility = "hidden"; + deleteWrittenCode(node.editor); + } else { + return; + } }; const handleSubmit = (node, code) => { const requestBody = { diff --git a/src/functions/pipeBinding.js b/src/functions/codeEditorHandler.js similarity index 94% rename from src/functions/pipeBinding.js rename to src/functions/codeEditorHandler.js index 4f82826f48b6115b04768307bbbf8873b90dc5df..7cd563f7a831cd9d4252858427155c69290289a5 100644 --- a/src/functions/pipeBinding.js +++ b/src/functions/codeEditorHandler.js @@ -235,3 +235,19 @@ export const renamePipeNamesInCode = (editor, oldName, newName) => { }); editor.dispatch(transaction); }; + +export const deleteWrittenCode = (editor) => { + const codeString = editor.state.doc.toString(); + const from = " // TODO: Code logic here"; + const fromIndex = codeString.indexOf(from); + const transaction = editor.state.update({ + changes: [ + { + from: fromIndex + from.length, + to: editor.state.doc.length, + insert: "\n\n\n\t});\n});\n", + }, + ], + }); + editor.dispatch(transaction); +}; diff --git a/src/functions/naming.js b/src/functions/naming.js index b11d7322dacd7d01812a6c35fe411bbee84f65e6..d9ae8999942c444cd024e8c3109710df942cf57c 100644 --- a/src/functions/naming.js +++ b/src/functions/naming.js @@ -1,6 +1,6 @@ import { showCheck } from "./visualValidation"; import { appState } from "./state"; -import { renamePipeNamesInCode } from "./pipeBinding"; +import { renamePipeNamesInCode } from "./codeEditorHandler"; export const namePipe = (instance) => { let newPipeName;