1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix: focus to the editor on page load / dock just opened

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-10-27 12:23:26 +03:00
parent f276207860
commit aaa5939867

View File

@ -30,36 +30,47 @@ import { MonacoEditor, MonacoEditorProps } from "../monaco-editor";
import type { DockTabContentProps } from "./dock.views-manager"; import type { DockTabContentProps } from "./dock.views-manager";
export interface EditorPanelProps extends DockTabContentProps { export interface EditorPanelProps extends DockTabContentProps {
autoFocus?: boolean; // default: true
value: string; value: string;
onChange: MonacoEditorProps["onChange"]; onChange: MonacoEditorProps["onChange"];
onError?: MonacoEditorProps["onError"]; onError?: MonacoEditorProps["onError"];
} }
const defaultProps: Partial<EditorPanelProps> = {
autoFocus: true,
};
@observer @observer
export class EditorPanel extends React.Component<EditorPanelProps> { export class EditorPanel extends React.Component<EditorPanelProps> {
@observable.ref editor: MonacoEditor; static defaultProps = defaultProps as object;
@observable.ref editor?: MonacoEditor;
constructor(props: EditorPanelProps) { constructor(props: EditorPanelProps) {
super(props); super(props);
makeObservable(this); makeObservable(this);
}
componentDidMount() {
disposeOnUnmount(this, [ disposeOnUnmount(this, [
// keep focus on editor's area when <Dock/> just opened // keep focus on editor's area when <Dock/> just opened
reaction(() => dockStore.isOpen, isOpen => isOpen && this.editor.focus()), reaction(() => dockStore.isOpen, isOpen => isOpen && this.editor?.focus(), {
fireImmediately: true,
}),
// focus to editor on dock's resize or turning into fullscreen mode // focus to editor on dock's resize or turning into fullscreen mode
dockStore.onResize(throttle(() => this.editor.focus(), 250)), dockStore.onResize(throttle(() => this.editor?.focus(), 250)),
]); ]);
} }
render() { render() {
const { className, tabId, value, onChange, onError } = this.props; const { className, autoFocus, tabId, value, onChange, onError } = this.props;
if (!tabId) return null; if (!tabId) return null;
return ( return (
<MonacoEditor <MonacoEditor
autoFocus autoFocus={autoFocus}
id={tabId} id={tabId}
value={value} value={value}
className={cssNames(styles.EditorPanel, className)} className={cssNames(styles.EditorPanel, className)}