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 select all action always select the editor content #415

Merged
merged 5 commits into from
Sep 9, 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
13 changes: 11 additions & 2 deletions src/controller/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import {
MenuBarService,
LayoutService,
} from 'mo/services';
import { ID_SIDE_BAR } from 'mo/common/id';
import { ID_APP, ID_SIDE_BAR } from 'mo/common/id';
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
import { CommandQuickSideBarViewAction } from 'mo/monaco/quickToggleSideBarAction';
import { QuickTogglePanelAction } from 'mo/monaco/quickTogglePanelAction';

export interface IMenuBarController {
onSelect?: (key: string, item?: IActivityBarItem) => void;
onClick: (event: React.MouseEvent<any, any>, item: IMenuBarItem) => void;
updateFocusinEle?: (ele: HTMLElement | null) => void;
updateStatusBar?: () => void;
updateMenuBar?: () => void;
updateActivityBar?: () => void;
Expand All @@ -45,6 +46,8 @@ export class MenuBarController
private readonly menuBarService: IMenuBarService;
private readonly layoutService: ILayoutService;
private readonly monacoService: IMonacoService;
private focusinEle: HTMLElement | null = null;

private automation = {
[ACTION_QUICK_CREATE_FILE]: () => this.createFile(),
[ACTION_QUICK_UNDO]: () => this.undo(),
Expand All @@ -66,6 +69,11 @@ export class MenuBarController
this.monacoService = container.resolve(MonacoService);
}

public updateFocusinEle = (ele: HTMLElement | null) => {
if (ele?.id == ID_APP) return;
this.focusinEle = ele;
};

public readonly onClick = (event: React.MouseEvent, item: IMenuBarItem) => {
const menuId = item.id || '';

Expand Down Expand Up @@ -105,7 +113,8 @@ export class MenuBarController

public selectAll = () => {
this.monacoService.commandService.executeCommand(
ACTION_QUICK_SELECT_ALL
ACTION_QUICK_SELECT_ALL,
this.focusinEle
);
};

Expand Down
33 changes: 28 additions & 5 deletions src/monaco/quickSelectAllAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,33 @@ export class QuickSelectAllAction extends Action2 {
this.editorService = container.resolve(EditorService);
}

run(): void {
this.editorService.editorInstance!.focus();
this.editorService.editorInstance!.setSelection(
this.editorService.editorInstance!.getModel()!.getFullModelRange()
);
selectEditorAll() {
const editor = this.editorService.editorInstance;
if (editor) {
editor.focus();
editor.setSelection(editor.getModel()!.getFullModelRange());
}
}

isTextdom(ele: Element): ele is HTMLInputElement {
return typeof (ele as HTMLInputElement).selectionStart === 'number';
}

run(accessor, ...args): void {
const focusinEle = args[0];
// execute the action via shortcut if focusinEle is undefined
const currentFocusinEle: Element | null =
focusinEle || document.activeElement;
if (
currentFocusinEle &&
this.isTextdom(currentFocusinEle) &&
!currentFocusinEle.className.includes('monaco')
) {
// native element can select by the native method
currentFocusinEle.select();
} else {
// monaco component should use the method from instance
this.selectEditorAll();
}
}
}
23 changes: 22 additions & 1 deletion src/workbench/menuBar/__tests__/menubar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { fireEvent, render } from '@testing-library/react';
import { cleanup, fireEvent, render } from '@testing-library/react';
import '@testing-library/jest-dom';

import MenuBar, { actionClassName } from '../menuBar';
Expand All @@ -25,6 +25,8 @@ const menuData = [
const TEST_FN: jest.Mock<any, any> = jest.fn();

describe('Test MenuBar Component', () => {
afterEach(cleanup);

test('Match the MenuBar snapshot', () => {
const component = renderer.create(
<MenuBar data={menuData} onClick={TEST_FN} />
Expand All @@ -48,4 +50,23 @@ describe('Test MenuBar Component', () => {
const menuBar = getByRole('menu').firstElementChild as HTMLLIElement;
expect(menuBar).toBeInTheDocument();
});

test('Should support to get the focus element', () => {
const mockFn = jest.fn();
const {} = render(
<MenuBar
data={menuData}
onClick={TEST_FN}
updateFocusinEle={mockFn}
/>
);

const input = document.createElement('input');
document.body.append(input);

input.focus();

expect(mockFn).toBeCalled();
expect(mockFn.mock.calls[0][0]).toEqual(input);
});
});
16 changes: 14 additions & 2 deletions src/workbench/menuBar/menuBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import { getBEMElement, prefixClaName } from 'mo/common/className';
import { IMenuBar, IMenuBarItem } from 'mo/model/workbench/menuBar';
import { IMenuBarController } from 'mo/controller/menuBar';
Expand All @@ -11,7 +11,7 @@ export const defaultClassName = prefixClaName('menuBar');
export const actionClassName = getBEMElement(defaultClassName, 'action');

export function MenuBar(props: IMenuBar & IMenuBarController) {
const { data, onClick } = props;
const { data, onClick, updateFocusinEle } = props;
const childRef = useRef<DropDownRef>(null);

const addKeybindingForData = (
Expand Down Expand Up @@ -50,6 +50,18 @@ export function MenuBar(props: IMenuBar & IMenuBarController) {
data={addKeybindingForData(data)}
/>
);

const handleSaveFocusinEle = useCallback((e: FocusEvent) => {
updateFocusinEle?.(e.target as HTMLElement | null);
}, []);

useEffect(() => {
document.body.addEventListener('focusin', handleSaveFocusinEle);
return () => {
document.body.removeEventListener('focusin', handleSaveFocusinEle);
};
}, []);

return (
<div className={defaultClassName}>
<DropDown
Expand Down