Skip to content

Commit

Permalink
fix: keep sync the macro editor and macro doc (#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb authored Aug 15, 2022
1 parent fdd52d8 commit 5866e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export class MacroCommandEditorComponent implements AfterViewInit, ControlValueA
editor.onDidFocusEditorText(()=> {
this.isFocused = true;
this.gotFocus.emit();
// To be sure the macro command doc got the latest edited macro command
this.smartMacroDocService.updateCommand(this.value);
});

this.lineHeight = this.editor.getOption(MONACO_EDITOR_LINE_HEIGHT_OPTION) as any;
Expand All @@ -189,7 +191,13 @@ export class MacroCommandEditorComponent implements AfterViewInit, ControlValueA
}).pipe(
debounceTime(MACRO_CHANGE_DEBOUNCE_TIME),
distinctUntilChanged()
).subscribe(this.onChanged);
).subscribe(data => {
// If the user modify the macro without saving then we update the macro text
if (this.isFocused) {
this.smartMacroDocService.updateCommand(data);
}
this.onChanged(data);
});
}
this.changeObserver$.next(value);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/uhk-web/src/app/services/smart-macro-doc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class SmartMacroDocService implements OnDestroy {
this.dispatchMacroEditorFocusEvent();
}

updateCommand(command: string): void {
this.dispatchMacroEditorFocusEvent(command);
}

private dispatchStoreAction(action: Action) {
this.logService.misc(`[SmartMacroDocService] dispatch action: ${action.type}`);
this.zone.run(() => this.store.dispatch(action));
Expand Down Expand Up @@ -102,7 +106,7 @@ export class SmartMacroDocService implements OnDestroy {
});
}

private dispatchMacroEditorFocusEvent(): void {
private dispatchMacroEditorFocusEvent(command = ''): void {
if (!this.iframe?.contentWindow) {
return;
}
Expand All @@ -114,9 +118,17 @@ export class SmartMacroDocService implements OnDestroy {
version: '1.0.0'
};

if (this.selectedMacroAction?.type === TabName.Command) {
if (command) {
message.action = 'agent-message-editor-got-focus';
message.command = command;
}
// it should be the 2nd condition otherwise the unchanged command will dispatch
else if (this.selectedMacroAction?.type === TabName.Command) {
message.action = 'agent-message-editor-got-focus';
message.command = (this.selectedMacroAction.macroAction as CommandMacroAction).command;
} else if (this.selectedMacroAction?.id === 'new') {
message.action = 'agent-message-editor-got-focus';
message.command = '';
}

this.iframe.contentWindow.postMessage(message, '*');
Expand Down

0 comments on commit 5866e4c

Please sign in to comment.