Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from leaphy-robotics/development
Browse files Browse the repository at this point in the history
Add Language switch sutff and add restoring temporary saved workspaces
  • Loading branch information
koen1711 authored Aug 10, 2023
2 parents d70ab32 + 5730626 commit 47c5f05
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/app/effects/app.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ export class AppEffects {
})
});
}
}
}
20 changes: 19 additions & 1 deletion src/app/effects/backend.wired.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare var Blockly: any;
// Defines the effects on the Electron environment that different state changes have
export class BackendWiredEffects {

constructor(private router: Router, private backEndState: BackEndState, private appState: AppState, private blocklyEditorState: BlocklyEditorState, private robotWiredState: RobotWiredState, private dialogState: DialogState, private zone: NgZone, private dialog: MatDialog) {
constructor(private blocklyState: BlocklyEditorState, private router: Router, private backEndState: BackEndState, private appState: AppState, private blocklyEditorState: BlocklyEditorState, private robotWiredState: RobotWiredState, private dialogState: DialogState, private zone: NgZone, private dialog: MatDialog) {
// Only set up these effects when we're in Desktop mode
this.appState.isDesktop$
.pipe(filter(isDesktop => !!isDesktop))
Expand Down Expand Up @@ -273,6 +273,24 @@ export class BackendWiredEffects {
case 'open-browser-page':
const url = args[0];
window.open(url, '_blank').focus();
break;
case 'save-workspace-temp':
const data = args[0].data;
sessionStorage.setItem('workspace', data);
break;
case 'restore-workspace-temp':
console.log('restoring workspace');
const workspaceTemp = sessionStorage.getItem('workspace');
console.log(workspaceTemp);
try {
this.blocklyState.setWorkspaceXml(workspaceTemp);
this.blocklyState.setProjectFilePath('');
this.blocklyState.setWorkspaceStatus(WorkspaceStatus.Restoring);
} catch (error) {
console.log('Error:', error.message);
}
console.log('restored workspace');
break;
default:
console.log(channel);
break;
Expand Down
21 changes: 13 additions & 8 deletions src/app/effects/blockly-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ export class BlocklyEditorEffects {
this.appState.currentLanguage$
.pipe(filter(language => !!language))
.subscribe(async language => {
console.log('Loading Blockly translations for language: ' + language.code);
const translations = await import(`node_modules/leaphy-blockly/msg/${language.code}.js`);
Object.keys(translations.default).forEach(function (tk) {
Blockly.Msg[tk] = translations[tk];
});
Blockly.setLocale(translations);
});

// When the language is changed, save the workspace temporarily
Expand Down Expand Up @@ -139,11 +138,17 @@ export class BlocklyEditorEffects {
this.blocklyState.workspaceStatus$
.pipe(filter(status => status === WorkspaceStatus.Restoring))
.pipe(withLatestFrom(this.blocklyState.workspaceXml$, this.blocklyState.workspace$))
.subscribe(([, workspaceXml, workspace]) => {
workspace.clear();
const xml = Blockly.Xml.textToDom(workspaceXml);
Blockly.Xml.domToWorkspace(xml, workspace);
this.blocklyState.setWorkspaceStatus(WorkspaceStatus.Clean);
.subscribe(async ([, workspaceXml, workspace]) => {
console.log('Restoring workspace');
while (this.blocklyState.workspace == null) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
workspace = this.blocklyState.workspace;
console.log('Workspace loaded');
workspace.clear();
const xml = Blockly.Xml.textToDom(workspaceXml);
Blockly.Xml.domToWorkspace(xml, workspace);
this.blocklyState.setWorkspaceStatus(WorkspaceStatus.Clean);
});

// When the user presses undo or redo, trigger undo or redo on the workspace
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/blockly-editor/blockly-editor.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<app-leaphy-blockly></app-leaphy-blockly>
</mat-sidenav-content>
<mat-sidenav id="sidenav" mode="side" [opened]="blocklyState.isSideNavOpen$ | async" position="end">
<app-code-view></app-code-view>
<app-code-view></app-code-view>
</mat-sidenav>
</mat-sidenav-container>
<app-button-bar [ngStyle]="(blocklyState.isSideNavOpen$ | async) && {'right': '20%'}"></app-button-bar>
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/blockly-editor/blockly-editor.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import {Component} from '@angular/core';
import { BlocklyEditorState } from 'src/app/state/blockly-editor.state';
import { DialogState } from 'src/app/state/dialog.state';

Expand All @@ -8,9 +8,9 @@ import { DialogState } from 'src/app/state/dialog.state';
styleUrls: ['./blockly-editor.page.scss']
})

export class BlocklyEditorPage {
export class BlocklyEditorPage {
constructor(
public blocklyState: BlocklyEditorState,
) {
}
public dialogState: DialogState,
) {}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div #blockContent id="blockly-container"></div>
<div #blockContent id="blockly-container"></div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import {
Component,
ViewChild,
ElementRef,
AfterViewInit,
} from '@angular/core';
import { BlocklyEditorState } from 'src/app/state/blockly-editor.state';
import { BackEndState } from 'src/app/state/backend.state';

@Component({
selector: 'app-leaphy-blockly',
Expand All @@ -13,9 +17,7 @@ export class LeaphyBlocklyComponent implements AfterViewInit {

constructor(
public blocklyState: BlocklyEditorState,
public backEndState: BackEndState
) {
}
) {}

ngAfterViewInit() {
this.blocklyState.setBlocklyElement(this.blockContent.nativeElement);
Expand Down
1 change: 1 addition & 0 deletions src/app/modules/core/components/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class HeaderComponent {

public onLanguageChanged(language: Language) {
this.appState.setChangedLanguage(language);
window.location.reload();
}

public onBackToBlocks() {
Expand Down
4 changes: 4 additions & 0 deletions src/app/state/blockly-editor.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@ export class BlocklyEditorState {
get workspaceXml(): string {
return this.workspaceXmlSubject$.getValue();
}

get workspace(): any {
return this.workspaceSubject$.getValue();
}
}

0 comments on commit 47c5f05

Please sign in to comment.