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

Remove usage of shared global state from a component

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-07-18 15:02:52 +03:00
parent 700c756bf3
commit 1fed6c4be0
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 48 additions and 2 deletions

View File

@ -0,0 +1,43 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { editor } from "monaco-editor";
import React from "react";
import type { MonacoEditorProps, MonacoEditorRef } from "../monaco-editor";
import { monacoValidators } from "../monaco-validators";
class FakeMonacoEditor extends React.Component<MonacoEditorProps> {
render() {
const { id, value, onChange, onError, language = "yaml" } = this.props;
return (
<input
data-testid={`monaco-editor-for-${id}`}
onChange={(event) => {
const newValue = event.target.value;
onChange?.(
newValue,
{} as editor.IModelContentChangedEvent,
);
const validator = monacoValidators[language];
try {
validator(newValue);
} catch(e) {
onError?.(e);
}
}}
value={value}
/>
);
}
}
export const MonacoEditor = React.forwardRef<
MonacoEditorRef,
MonacoEditorProps
>((props, ref) => <FakeMonacoEditor innerRef={ref} {...props} />);

View File

@ -12,10 +12,11 @@ import type { MonacoTheme } from "./monaco-themes";
import { type MonacoValidator, monacoValidators } from "./monaco-validators";
import { debounce, merge } from "lodash";
import { autoBind, cssNames, disposer } from "../../utils";
import { UserStore } from "../../../common/user-store";
import type { UserStore } from "../../../common/user-store";
import type { ThemeStore } from "../../themes/store";
import { withInjectables } from "@ogre-tools/injectable-react";
import themeStoreInjectable from "../../themes/store.injectable";
import userStoreInjectable from "../../../common/user-store/user-store.injectable";
export type MonacoEditorId = string;
@ -39,6 +40,7 @@ export interface MonacoEditorProps {
interface Dependencies {
themeStore: ThemeStore;
userStore: UserStore;
}
export function createMonacoUri(id: MonacoEditorId): Uri {
@ -99,7 +101,7 @@ class NonInjectedMonacoEditor extends React.Component<MonacoEditorProps & Depend
@computed get options(): editor.IStandaloneEditorConstructionOptions {
return merge({},
UserStore.getInstance().editorConfiguration,
this.props.userStore.editorConfiguration,
this.props.options,
);
}
@ -305,6 +307,7 @@ export const MonacoEditor = withInjectables<Dependencies, MonacoEditorProps, Mon
getProps: (di, props) => ({
...props,
themeStore: di.inject(themeStoreInjectable),
userStore: di.inject(userStoreInjectable),
}),
},
);