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

Commit

Permalink
fix: autosave works again (#219)
Browse files Browse the repository at this point in the history
* chore: fix weird bug where old changeListener could effect different enviroments

* fix: autosave not working
  • Loading branch information
koen1711 authored Apr 11, 2024
1 parent a1b1aad commit b78b337
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/app/effects/blockly-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ export class BlocklyEditorEffects {
workspace.clearUndo();
workspace.addChangeListener(Blockly.Events.disableOrphans);
workspace.addChangeListener(async () => {
if (this.appState.currentEditor !== CodeEditorType.Beginner) return;
this.codeEditorState.code = arduino.workspaceToCode(workspace, this.appState.selectedRobotType.id);
this.blocklyState.workspaceJSON = JSON.stringify(Blockly.serialization.workspaces.save(workspace));
});

});

// When the user presses undo or redo, trigger undo or redo on the workspace
Expand Down
9 changes: 8 additions & 1 deletion src/app/effects/code-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {CodeEditorState} from "../state/code-editor.state";
import {AppState} from "../state/app.state";
import {CodeEditorType} from "../domain/code-editor.type";
import {genericRobotType} from "../domain/robots";
import {WorkspaceService} from "../services/workspace.service";


@Injectable({
Expand All @@ -15,14 +16,20 @@ export class CodeEditorEffects {
constructor(
private codeEditorState: CodeEditorState,
private appState: AppState,
private workspaceService: WorkspaceService,
) {
this.appState.codeEditor$
.subscribe(codeEditor => {
console.log('codeEditor', codeEditor);
if (codeEditor == CodeEditorType.Python) {
this.codeEditorState.code = `from leaphymicropython.utils.pins import set_pwm`;
this.workspaceService.restoreWorkspaceTemp().then(() => {});
} else if (codeEditor == CodeEditorType.CPP && this.appState.selectedRobotType == genericRobotType) {
this.codeEditorState.code = this.codeEditorState.originalProgram;
this.codeEditorState.code = this.codeEditorState.code = `void leaphyProgram() {\n\n}\n\nvoid setup() {\n leaphyProgram();\n}\n\nvoid loop() {\n\n}`;
this.workspaceService.restoreWorkspaceTemp().then(() => {});
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="monaco-editor"
[options]="editorOptions"
[ngModel]="codeEditorState.code$ | async"
(ngModelChange)="codeEditorState.code = ($event)"
(ngModelChange)="codeEditorState.code = $event"
/>

<app-button-bar></app-button-bar>
Expand Down
18 changes: 8 additions & 10 deletions src/app/services/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ export class WorkspaceService {
width: '75vw', disableClose: true,
}).afterClosed().subscribe(async (fileName) => {
if (fileName) {
const content = await this.uploaderService.runFileSystemCommand('get', fileName);
this.codeEditorState.code = content;
this.codeEditorState.code = await this.uploaderService.runFileSystemCommand('get', fileName);
this.blocklyState.projectFileHandle = new PythonFile(fileName);
}
});
Expand Down Expand Up @@ -285,6 +284,7 @@ export class WorkspaceService {
* Restore the workspace from the session storage
*/
public async restoreWorkspaceTemp() {
console.log('Restoring workspace from session storage');
const workspaceTemp = sessionStorage.getItem('workspace');
const robotType = sessionStorage.getItem('robotType');
const type = sessionStorage.getItem('type');
Expand All @@ -297,13 +297,10 @@ export class WorkspaceService {
payload: {projectFilePath: null, data: workspaceTemp, type: 'beginner', extension: robotType},
displayTimeout: 1000
})
} else if (type == 'advanced' && this.appState.currentEditor == CodeEditorType.CPP) {
try {
this.codeEditorState.code = workspaceTemp;
} catch (error) {
console.log('Error:', error.message);
}
} else if (type == 'python' && this.appState.currentEditor == CodeEditorType.Python) {
} else if (
(type == 'advanced' && this.appState.currentEditor == CodeEditorType.CPP) ||
(type == 'python' && this.appState.currentEditor == CodeEditorType.Python)
) {
try {
this.codeEditorState.code = workspaceTemp;
} catch (error) {
Expand All @@ -321,7 +318,7 @@ export class WorkspaceService {
* Restore the workspace from the session storage without checking the current editor or the selected robot type
*/
public async forceRestoreWorkspaceTemp() {
// restory workspace from session storage but don't care about if we selected the same robot type or the current editor
// restore workspace from session storage but don't care about if we selected the same robot type or the current editor
const workspaceTemp = sessionStorage.getItem('workspace');
const robotType = sessionStorage.getItem('robotType');
const type = sessionStorage.getItem('type');
Expand All @@ -341,6 +338,7 @@ export class WorkspaceService {
console.log('Error:', error.message);
}
} else if (type == 'python') {

this.appState.selectedCodeEditor = CodeEditorType.Python;
try {
this.codeEditorState.code = workspaceTemp;
Expand Down
17 changes: 2 additions & 15 deletions src/app/state/code-editor.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ import {InstalledLibrary, Library} from "src/app/domain/library-manager.types";
providedIn: 'root'
})
export class CodeEditorState {
public readonly originalProgram = `void leaphyProgram() {
}
void setup() {
leaphyProgram();
}
void loop() {
}`;

private codeSubject$: BehaviorSubject<string> = new BehaviorSubject<string>('');
public code$: Observable<string> = this.codeSubject$.asObservable();

Expand All @@ -27,11 +16,9 @@ void loop() {
constructor() { }

set code(program: string){
if (
this.codeSubject$.value !== program &&
this.codeSubject$.value !== this.originalProgram
)
if (this.codeSubject$.value !== program) {
this.saveState = false
}

this.codeSubject$.next(program);
}
Expand Down

0 comments on commit b78b337

Please sign in to comment.