diff --git a/ArduinoFrontend/src/app/Libs/Workspace.ts b/ArduinoFrontend/src/app/Libs/Workspace.ts index d50c93e59..32da14cdb 100644 --- a/ArduinoFrontend/src/app/Libs/Workspace.ts +++ b/ArduinoFrontend/src/app/Libs/Workspace.ts @@ -477,8 +477,8 @@ export class Workspace { if (window.isCodeEditorOpened) { return; } - // console.log([event.ctrlKey, event.key]); - if (event.key === 'Delete' || event.key === 'Backspace') { + if ((event.key === 'Delete' || event.key === 'Backspace') + && !(event['target']['localName'] === 'input' || event['target']['localName'] === 'textarea')) { // Backspace or Delete Workspace.DeleteComponent(); } @@ -1050,4 +1050,76 @@ export class Workspace { // Hide Loading animation window.hideLoading(); } + + + /** + * Function generates a JSON object containing all details of the workspace and downloads it + * @param name string + * @param description string + */ + static SaveJson(name: string = '', description: string = '') { + + const id = Date.now(); + + // Default Save object + const saveObj = { + id, + canvas: { + x: Workspace.translateX, + y: Workspace.translateY, + scale: Workspace.scale + }, + project: { + name, + description, + created_at: Date.now(), + } + }; + + // For each item in the scope + for (const key in window.scope) { + // if atleast one component is present + if (window.scope[key] && window.scope[key].length > 0) { + saveObj[key] = []; + // Add the component to the save object + for (const item of window.scope[key]) { + if (item.save) { + saveObj[key].push(item.save()); + } + } + } + } + + // Export JSON File & Download it + const filename = `${name}.json`; + const jsonStr = JSON.stringify(saveObj); + + const element = document.createElement('a'); + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(jsonStr)); + element.setAttribute('download', filename); + + element.style.display = 'none'; + document.body.appendChild(element); + + element.click(); + + document.body.removeChild(element); + + return true; + + } + + /** + * Function to return if workspace is empty or not + * @returns 'False' if workspace is not empty & 'True' if workspace is empty + */ + static checkIfWorkspaceEmpty() { + for (const key in window.scope) { + if (window.scope[key].length > 0) { + return false; + } + } + return true; + } + } diff --git a/ArduinoFrontend/src/app/app.module.ts b/ArduinoFrontend/src/app/app.module.ts index 740eadc26..059ca67ce 100644 --- a/ArduinoFrontend/src/app/app.module.ts +++ b/ArduinoFrontend/src/app/app.module.ts @@ -35,6 +35,8 @@ import { HeaderComponent } from './header/header.component'; import { ViewProjectComponent } from './view-project/view-project.component'; import { AlertModalComponent } from './alert/alert-modal/alert-modal.component'; import { ConfirmModalComponent } from './alert/confirm-modal/confirm-modal.component'; +import { ExportJSONDialogComponent } from './export-jsondialog/export-jsondialog.component'; +import { ExitConfirmDialogComponent } from './exit-confirm-dialog/exit-confirm-dialog.component'; /** * Monaco OnLoad Function @@ -67,6 +69,8 @@ const monacoConfig: NgxMonacoEditorConfig = { HeaderComponent, AlertModalComponent, ConfirmModalComponent, + ExportJSONDialogComponent, + ExitConfirmDialogComponent, ], imports: [ BrowserModule, @@ -89,7 +93,15 @@ const monacoConfig: NgxMonacoEditorConfig = { // providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}], providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }], bootstrap: [AppComponent], - entryComponents: [ViewComponentInfoComponent, ExportfileComponent, ComponentlistComponent, AlertModalComponent, ConfirmModalComponent], + entryComponents: [ + ViewComponentInfoComponent, + ExportfileComponent, + ComponentlistComponent, + AlertModalComponent, + ConfirmModalComponent, + ExportJSONDialogComponent, + ExitConfirmDialogComponent, + ], schemas: [ CUSTOM_ELEMENTS_SCHEMA ], diff --git a/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.css b/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.css new file mode 100644 index 000000000..49c838514 --- /dev/null +++ b/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.css @@ -0,0 +1,4 @@ +.action-div{ + display: flex; + justify-content: space-around; +} diff --git a/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.html b/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.html new file mode 100644 index 000000000..8892ea25f --- /dev/null +++ b/ArduinoFrontend/src/app/exit-confirm-dialog/exit-confirm-dialog.component.html @@ -0,0 +1,5 @@ +