Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix undo and redo actions #440

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/controller/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ export class MenuBarController
};

public undo = () => {
this.monacoService.commandService.executeCommand(ACTION_QUICK_UNDO);
this.monacoService.commandService.executeCommand(
ACTION_QUICK_UNDO,
this.focusinEle
);
};

public redo = () => {
this.monacoService.commandService.executeCommand(ACTION_QUICK_REDO);
this.monacoService.commandService.executeCommand(
ACTION_QUICK_REDO,
this.focusinEle
);
};

public gotoQuickCommand = () => {
Expand Down
38 changes: 32 additions & 6 deletions src/monaco/quickRedo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { localize } from 'mo/i18n/localize';
import { KeyMod, KeyCode } from 'mo/monaco';
import { KeyMod, KeyCode, Uri, editor as MonacoEditor } from 'mo/monaco';
import { EditorService, IEditorService } from 'mo/services';
import { container } from 'tsyringe';
import { Action2, KeybindingWeight } from './common';
Expand Down Expand Up @@ -32,11 +32,37 @@ export class QuickRedo extends Action2 {
this.editorService = container.resolve(EditorService);
}

run() {
const editorInstance = this.editorService.editorInstance;
editorInstance!.focus();
if (!document.execCommand(QuickRedo.DESC)) {
editorInstance?.getModel()?.[QuickRedo.DESC]?.();
isTextdom(ele: Element): ele is HTMLInputElement {
return typeof (ele as HTMLInputElement).selectionStart === 'number';
}

run(accessor, ...args) {
const focusinEle = args[0];
const currentFocusinEle: Element | null =
focusinEle || document.activeElement;
if (
currentFocusinEle &&
this.isTextdom(currentFocusinEle) &&
!currentFocusinEle.className.includes('monaco')
) {
// native dom use the native methods
document.execCommand('redo');
} else {
// monaco component should use the method from instance
const editorInstance = this.editorService.editorInstance;
if (editorInstance) {
const currentActiveGroup = this.editorService.getState()
.current;
if (currentActiveGroup) {
const tab = this.editorService.getTabById(
currentActiveGroup.activeTab!,
currentActiveGroup
);
editorInstance?.focus();
const model = MonacoEditor.getModel(Uri.parse(tab!.id!))!;
(model as any).redo();
}
}
}
}
}
38 changes: 32 additions & 6 deletions src/monaco/quickUndo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { localize } from 'mo/i18n/localize';
import { KeyMod, KeyCode } from 'mo/monaco';
import { KeyMod, KeyCode, Uri, editor as MonacoEditor } from 'mo/monaco';
import { EditorService, IEditorService } from 'mo/services';
import { container } from 'tsyringe';
import { Action2, KeybindingWeight } from './common';
Expand Down Expand Up @@ -32,11 +32,37 @@ export class QuickUndo extends Action2 {
this.editorService = container.resolve(EditorService);
}

run() {
const editorInstance = this.editorService.editorInstance;
editorInstance!.focus();
if (!document.execCommand(QuickUndo.DESC)) {
editorInstance?.getModel()?.[QuickUndo.DESC]?.();
isTextdom(ele: Element): ele is HTMLInputElement {
return typeof (ele as HTMLInputElement).selectionStart === 'number';
}

run(accessor, ...args) {
const focusinEle = args[0];
const currentFocusinEle: Element | null =
focusinEle || document.activeElement;
if (
currentFocusinEle &&
this.isTextdom(currentFocusinEle) &&
!currentFocusinEle.className.includes('monaco')
) {
// native dom use the native methods
document.execCommand('undo');
} else {
// monaco component should use the method from instance
const editorInstance = this.editorService.editorInstance;
if (editorInstance) {
const currentActiveGroup = this.editorService.getState()
.current;
if (currentActiveGroup) {
const tab = this.editorService.getTabById(
currentActiveGroup.activeTab!,
currentActiveGroup
);
editorInstance?.focus();
const model = MonacoEditor.getModel(Uri.parse(tab!.id!))!;
(model as any).undo();
}
}
}
}
}