diff --git a/ArduinoFrontend/src/app/Libs/Workspace.ts b/ArduinoFrontend/src/app/Libs/Workspace.ts index 34870b34..440770c2 100644 --- a/ArduinoFrontend/src/app/Libs/Workspace.ts +++ b/ArduinoFrontend/src/app/Libs/Workspace.ts @@ -577,6 +577,12 @@ export class Workspace { window['scope'][classString].push(obj); // Push dump to Undo stack & Reset UndoUtils.pushChangeToUndoAndReset({ keyName: obj.keyName, event: 'add', element: obj.save() }); + // **Trigger Reinit Only for Arduino Uno** + if (classString === 'ArduinoUno') { + setTimeout(() => { + window['reinitCodeEditor'] = true; // Set global reinit flag + }, 0); + } } /** Function updates the position of wires */ static updateWires() { @@ -798,14 +804,6 @@ export class Workspace { // Save Dump of current Workspace // Check if component is selected if (window['Selected']) { - // is selected component is an arduini uno then show confirm message - if (window['Selected'] instanceof ArduinoUno) { - const ans = confirm('The Respective code will also be lost!'); - if (!ans) { - return; - } - } - // get the component id const uid = window.Selected.id; const key = window.Selected.keyName; diff --git a/ArduinoFrontend/src/app/code-editor/code-editor.component.html b/ArduinoFrontend/src/app/code-editor/code-editor.component.html index 78cdd5f2..8c29b65f 100644 --- a/ArduinoFrontend/src/app/code-editor/code-editor.component.html +++ b/ArduinoFrontend/src/app/code-editor/code-editor.component.html @@ -1,11 +1,5 @@
- -
-

No Programmable Component in this Circuit

-
- -
+
diff --git a/ArduinoFrontend/src/app/code-editor/code-editor.component.ts b/ArduinoFrontend/src/app/code-editor/code-editor.component.ts index 3c3341e0..a84bff9f 100644 --- a/ArduinoFrontend/src/app/code-editor/code-editor.component.ts +++ b/ArduinoFrontend/src/app/code-editor/code-editor.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, Input } from '@angular/core'; +import { Component, OnInit, Input, DoCheck } from '@angular/core'; import { ArduinoUno } from '../Libs/outputs/Arduino'; import { Download } from '../Libs/Download'; @@ -16,7 +16,7 @@ declare var window; templateUrl: './code-editor.component.html', styleUrls: ['./code-editor.component.css'] }) -export class CodeEditorComponent { +export class CodeEditorComponent implements DoCheck { // TODO: Fetch records and Suggestion from api /** @@ -122,6 +122,8 @@ export class CodeEditorComponent { window['isCodeEditorOpened'] = value; if (value) { + const DEFAULT_CODE = 'void setup(){\n\t\n}\n\nvoid loop(){\n\t\n}'; + const previousCode = this.code || ''; // Clear names and instances this.names = []; this.arduinos = []; @@ -132,31 +134,41 @@ export class CodeEditorComponent { this.arduinos.push(window['ArduinoUno_name'][key]); } } - // reset selected index - if (this.selectedIndex >= this.arduinos.length) { - this.selectedIndex = 0; - } - // select the code of respective arduino + // If an Uno is present, ensure it gets the existing code if (this.arduinos.length > 0) { - this.code = this.arduinos[this.selectedIndex].code; + // Assign the code to all newly added Uno boards + this.arduinos.forEach((arduino, index) => { + if (arduino.code === DEFAULT_CODE && previousCode !== '') { + arduino.code = previousCode; + } + }); + this.code = this.arduinos[this.selectedIndex].code || ''; } - // show loading animation if code editor is nor initialized + + // Show loading animation if the code editor is not initialized if (this.names.length !== 0 && !this.init) { window['showLoading'](); } } } + ngDoCheck() { + if (window['reinitCodeEditor']) { + // console.log('Reinitializing the code editor'); + window['reinitCodeEditor'] = false; // Reset flag + this.reinit = true; + } + } /** * Increase the size of the font in the editor */ - IncreaseFont(fontSize: number) { + IncreaseFont(): void { this.size = this.size + 1; this.editorOptions = {...this.editorOptions, fontSize: this.size}; } /** * Decrease the size of the font in the editor */ - DecreaseFont(fontSize: number) { + DecreaseFont(): void { this.size = this.size - 1; this.editorOptions = {...this.editorOptions, fontSize: this.size}; } @@ -1216,7 +1228,11 @@ export class CodeEditorComponent { * On code Change update the code in arduino */ codeChanged() { - this.arduinos[this.selectedIndex].code = this.code; + if (this.arduinos && this.selectedIndex >= 0 && this.selectedIndex < this.arduinos.length) { + this.arduinos[this.selectedIndex].code = this.code; + } else { + window['savedCode'] = this.code; + } } /** * Select the code for respective arduino. Event handler for Choosing arduino