mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
monaco fine-tuning / final steps
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
40f0dc989d
commit
883317b5d0
@ -25,14 +25,15 @@ import yaml from "js-yaml";
|
|||||||
import { computed, makeObservable } from "mobx";
|
import { computed, makeObservable } from "mobx";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { createResourceStore } from "./create-resource.store";
|
import { createResourceStore } from "./create-resource.store";
|
||||||
import { InfoPanel, InfoPanelProps } from "./info-panel";
|
import { InfoPanel } from "./info-panel";
|
||||||
import * as resourceApplierApi from "../../../common/k8s-api/endpoints/resource-applier.api";
|
import * as resourceApplierApi from "../../../common/k8s-api/endpoints/resource-applier.api";
|
||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { TabKind } from "./dock.store";
|
import { TabKind } from "./dock.store";
|
||||||
import { dockViewsManager } from "./dock.views-manager";
|
import { dockViewsManager } from "./dock.views-manager";
|
||||||
import logger from "../../../common/logger";
|
import logger from "../../../common/logger";
|
||||||
|
import { DockTabContent, DockTabContentProps } from "./dock-tab-content";
|
||||||
|
|
||||||
interface Props extends InfoPanelProps {
|
interface Props extends DockTabContentProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SelectOptionTemplate = SelectOption<SelectOptionTemplateValue>;
|
type SelectOptionTemplate = SelectOption<SelectOptionTemplateValue>;
|
||||||
@ -44,7 +45,7 @@ interface SelectOptionTemplateValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class CreateResourceInfoPanel extends React.Component<Props> {
|
export class CreateResource extends React.Component<Props> {
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
super(props);
|
super(props);
|
||||||
makeObservable(this);
|
makeObservable(this);
|
||||||
@ -133,26 +134,27 @@ export class CreateResourceInfoPanel extends React.Component<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const { tabId } = this;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<DockTabContent
|
||||||
|
tabId={tabId}
|
||||||
|
withEditor
|
||||||
|
editorValue={createResourceStore.getData(tabId)}
|
||||||
|
editorOnChange={value => createResourceStore.setData(tabId, value)}
|
||||||
|
>
|
||||||
<InfoPanel
|
<InfoPanel
|
||||||
{...this.props}
|
tabId={tabId}
|
||||||
controls={this.renderControls()}
|
controls={this.renderControls()}
|
||||||
submit={this.create}
|
submit={this.create}
|
||||||
submitLabel="Create"
|
submitLabel="Create"
|
||||||
showNotifications={false}
|
showNotifications={false}
|
||||||
/>
|
/>
|
||||||
|
</DockTabContent>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dockViewsManager.register(TabKind.CREATE_RESOURCE, {
|
dockViewsManager.register(TabKind.CREATE_RESOURCE, {
|
||||||
InfoPanel: CreateResourceInfoPanel,
|
Content: CreateResource,
|
||||||
editor: {
|
|
||||||
getValue(tabId) {
|
|
||||||
return createResourceStore.getData(tabId);
|
|
||||||
},
|
|
||||||
setValue(tabId, value) {
|
|
||||||
createResourceStore.setData(tabId, value);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,4 +25,8 @@
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
|
.editor {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,107 +22,58 @@
|
|||||||
import styles from "./dock-tab-content.module.css";
|
import styles from "./dock-tab-content.module.css";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { disposeOnUnmount, observer } from "mobx-react";
|
import { disposeOnUnmount, observer } from "mobx-react";
|
||||||
import { action, computed, makeObservable, observable, reaction } from "mobx";
|
import { makeObservable, observable, reaction } from "mobx";
|
||||||
import type { DockTab, TabId } from "./dock.store";
|
import { dockStore, TabId } from "./dock.store";
|
||||||
import { dockStore } from "./dock.store";
|
|
||||||
import { cssNames } from "../../utils";
|
import { cssNames } from "../../utils";
|
||||||
import { DockTabComponents, dockViewsManager } from "./dock.views-manager";
|
|
||||||
import { MonacoEditor } from "../monaco-editor";
|
import { MonacoEditor } from "../monaco-editor";
|
||||||
import throttle from "lodash/throttle";
|
import throttle from "lodash/throttle";
|
||||||
|
|
||||||
export interface DockTabContentProps extends React.HTMLAttributes<any> {
|
export interface DockTabContentProps extends React.HTMLAttributes<any> {
|
||||||
|
tabId: TabId;
|
||||||
className?: string;
|
className?: string;
|
||||||
tab?: DockTab;
|
withEditor?: boolean;
|
||||||
bindContainerRef?(elem: HTMLElement): void;
|
editorValue?: string;
|
||||||
|
editorOnChange?(value: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class DockTabContent extends React.Component<DockTabContentProps> {
|
export class DockTabContent extends React.Component<DockTabContentProps> {
|
||||||
@observable.ref editor: MonacoEditor;
|
@observable.ref editor?: MonacoEditor;
|
||||||
|
@observable error = "";
|
||||||
|
|
||||||
constructor(props: DockTabContentProps) {
|
constructor(props: DockTabContentProps) {
|
||||||
super(props);
|
super(props);
|
||||||
makeObservable(this);
|
makeObservable(this);
|
||||||
|
|
||||||
disposeOnUnmount(this, [
|
disposeOnUnmount(this, [
|
||||||
reaction(() => this.tabId, this.onTabChange, { delay: 100 }),
|
|
||||||
|
|
||||||
// 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.focusEditor()),
|
reaction(() => dockStore.isOpen, isOpen => isOpen && this.editor?.focus()),
|
||||||
|
|
||||||
// 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.focusEditor(), 250)),
|
dockStore.onResize(throttle(() => this.editor?.focus(), 250)),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@computed get tabId(): TabId | undefined {
|
|
||||||
return this.props.tab?.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@computed get tabComponents(): DockTabComponents | undefined {
|
|
||||||
return dockViewsManager.get(this.props.tab?.kind);
|
|
||||||
}
|
|
||||||
|
|
||||||
@observable error = "";
|
|
||||||
|
|
||||||
@computed get editorIsVisible() {
|
|
||||||
return Boolean(this.tabComponents?.editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
focusEditor() {
|
|
||||||
if (!this.editorIsVisible) return;
|
|
||||||
|
|
||||||
this.editor.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
@action
|
|
||||||
onTabChange = () => {
|
|
||||||
this.error = ""; // reset any errors from another tab
|
|
||||||
this.focusEditor(); // focus to editor if available via registered tab views
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Always keep editor in DOM while (while <Dock/> is open/rendered).
|
|
||||||
* This allows to restore editor's model-view state (cursor pos, selection, etc.)
|
|
||||||
* while switching between different tab.kind-s (e.g. "terminal" tab doesn't have editor)
|
|
||||||
*/
|
|
||||||
renderEditor() {
|
|
||||||
const { tabId, tabComponents: { editor } } = this;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<MonacoEditor
|
|
||||||
id={tabId}
|
|
||||||
autoFocus={true}
|
|
||||||
className={cssNames({ hidden: !this.editorIsVisible })}
|
|
||||||
value={editor?.getValue(tabId) ?? ""}
|
|
||||||
onChange={value => {
|
|
||||||
this.error = "";
|
|
||||||
editor?.setValue(tabId, value);
|
|
||||||
}}
|
|
||||||
onError={error => {
|
|
||||||
this.error = this.editorIsVisible ? error : "";
|
|
||||||
}}
|
|
||||||
ref={editor => this.editor = editor}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { bindContainerRef, className, tab } = this.props;
|
const { className, tabId, withEditor, editorValue, editorOnChange } = this.props;
|
||||||
|
|
||||||
if (!tab) return null;
|
if (!tabId) return null;
|
||||||
const { InfoPanel, Content } = this.tabComponents;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div className={cssNames(styles.DockTabContent, className)}>
|
||||||
data-test-component="dock-tab-content"
|
|
||||||
className={cssNames(styles.DockTabContent, className)}
|
|
||||||
ref={bindContainerRef}
|
|
||||||
>
|
|
||||||
{InfoPanel && <InfoPanel tabId={this.tabId} error={this.error}/>}
|
|
||||||
{Content && <Content {...this.props}/>}
|
|
||||||
{this.renderEditor()}
|
|
||||||
{this.props.children}
|
{this.props.children}
|
||||||
|
|
||||||
|
{withEditor && (
|
||||||
|
<MonacoEditor
|
||||||
|
autoFocus
|
||||||
|
id={tabId}
|
||||||
|
className={styles.editor}
|
||||||
|
value={editorValue ?? ""}
|
||||||
|
onChange={editorOnChange}
|
||||||
|
onError={error => this.error = error}
|
||||||
|
ref={monaco => this.editor = monaco}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ import { createResourceTab } from "./create-resource.store";
|
|||||||
import { DockTabs } from "./dock-tabs";
|
import { DockTabs } from "./dock-tabs";
|
||||||
import { dockStore, DockTab } from "./dock.store";
|
import { dockStore, DockTab } from "./dock.store";
|
||||||
import { createTerminalTab } from "./terminal.store";
|
import { createTerminalTab } from "./terminal.store";
|
||||||
import { DockTabContent } from "./dock-tab-content";
|
import { dockViewsManager } from "./dock.views-manager";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string;
|
className?: string;
|
||||||
@ -78,6 +78,7 @@ export class Dock extends React.Component<Props> {
|
|||||||
render() {
|
render() {
|
||||||
const { className } = this.props;
|
const { className } = this.props;
|
||||||
const { isOpen, toggle, tabs, toggleFillSize, selectedTab, hasTabs, fullSize, height } = dockStore;
|
const { isOpen, toggle, tabs, toggleFillSize, selectedTab, hasTabs, fullSize, height } = dockStore;
|
||||||
|
const { Content: DockTabContent } = dockViewsManager.get(selectedTab?.kind) ?? {};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -133,7 +134,7 @@ export class Dock extends React.Component<Props> {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="tab-content" style={{ flexBasis: isOpen ? height : 0 }}>
|
<div className="tab-content" style={{ flexBasis: isOpen ? height : 0 }}>
|
||||||
<DockTabContent tab={selectedTab}/>
|
{selectedTab && <DockTabContent tabId={selectedTab.id}/>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -21,17 +21,11 @@
|
|||||||
|
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { observable } from "mobx";
|
import { observable } from "mobx";
|
||||||
import type { TabId, TabKind } from "./dock.store";
|
import type { TabKind } from "./dock.store";
|
||||||
import type { DockTabContentProps } from "./dock-tab-content";
|
import type { DockTabContentProps } from "./dock-tab-content";
|
||||||
import type { InfoPanelProps } from "./info-panel";
|
|
||||||
|
|
||||||
export interface DockTabComponents {
|
export interface DockTabComponents {
|
||||||
InfoPanel?: React.ComponentType<InfoPanelProps>;
|
|
||||||
Content?: React.ComponentType<DockTabContentProps>;
|
Content?: React.ComponentType<DockTabContentProps>;
|
||||||
editor?: {
|
|
||||||
getValue(tabId: TabId): string | undefined;
|
|
||||||
setValue(tabId: TabId, value: string): void;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const dockViews = observable.map<TabKind, DockTabComponents>([], {
|
const dockViews = observable.map<TabKind, DockTabComponents>([], {
|
||||||
|
|||||||
@ -24,18 +24,19 @@ import yaml from "js-yaml";
|
|||||||
import { makeObservable, observable, reaction } from "mobx";
|
import { makeObservable, observable, reaction } from "mobx";
|
||||||
import { disposeOnUnmount, observer } from "mobx-react";
|
import { disposeOnUnmount, observer } from "mobx-react";
|
||||||
import { editResourceStore } from "./edit-resource.store";
|
import { editResourceStore } from "./edit-resource.store";
|
||||||
import { InfoPanel, InfoPanelProps } from "./info-panel";
|
import { InfoPanel } from "./info-panel";
|
||||||
import { Badge } from "../badge";
|
import { Badge } from "../badge";
|
||||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||||
import { TabKind } from "./dock.store";
|
import { TabKind } from "./dock.store";
|
||||||
import { dockViewsManager } from "./dock.views-manager";
|
import { dockViewsManager } from "./dock.views-manager";
|
||||||
import { createPatch } from "rfc6902";
|
import { createPatch } from "rfc6902";
|
||||||
|
import { DockTabContent, DockTabContentProps } from "./dock-tab-content";
|
||||||
|
|
||||||
interface Props extends InfoPanelProps {
|
interface Props extends DockTabContentProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class EditResourceInfoPanel extends React.Component<Props> {
|
export class EditResource extends React.Component<Props> {
|
||||||
@observable.ref resource?: KubeObject;
|
@observable.ref resource?: KubeObject;
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
@ -104,13 +105,20 @@ export class EditResourceInfoPanel extends React.Component<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { resource } = this;
|
const { resource, tabId } = this;
|
||||||
|
const tabData = editResourceStore.getData(tabId);
|
||||||
|
|
||||||
if (!resource) return null;
|
if (!resource) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<DockTabContent
|
||||||
|
tabId={tabId}
|
||||||
|
withEditor
|
||||||
|
editorValue={tabData?.draft}
|
||||||
|
editorOnChange={v => tabData.draft = v}
|
||||||
|
>
|
||||||
<InfoPanel
|
<InfoPanel
|
||||||
{...this.props}
|
tabId={tabId}
|
||||||
submit={this.save}
|
submit={this.save}
|
||||||
submitLabel="Save"
|
submitLabel="Save"
|
||||||
submittingMessage="Applying.."
|
submittingMessage="Applying.."
|
||||||
@ -122,18 +130,11 @@ export class EditResourceInfoPanel extends React.Component<Props> {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
</DockTabContent>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dockViewsManager.register(TabKind.EDIT_RESOURCE, {
|
dockViewsManager.register(TabKind.EDIT_RESOURCE, {
|
||||||
InfoPanel: EditResourceInfoPanel,
|
Content: EditResource,
|
||||||
editor: {
|
|
||||||
getValue(tabId) {
|
|
||||||
return editResourceStore.getData(tabId)?.draft ?? "";
|
|
||||||
},
|
|
||||||
setValue(tabId, value) {
|
|
||||||
editResourceStore.getData(tabId).draft = value;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import React, { Component } from "react";
|
|||||||
import { computed, makeObservable, observable } from "mobx";
|
import { computed, makeObservable, observable } from "mobx";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { dockStore, TabKind } from "./dock.store";
|
import { dockStore, TabKind } from "./dock.store";
|
||||||
import { InfoPanel, InfoPanelProps } from "./info-panel";
|
import { InfoPanel } from "./info-panel";
|
||||||
import { Badge } from "../badge";
|
import { Badge } from "../badge";
|
||||||
import { NamespaceSelect } from "../+namespaces/namespace-select";
|
import { NamespaceSelect } from "../+namespaces/namespace-select";
|
||||||
import { prevDefault } from "../../utils";
|
import { prevDefault } from "../../utils";
|
||||||
@ -40,12 +40,13 @@ import { Input } from "../input";
|
|||||||
import { navigate } from "../../navigation";
|
import { navigate } from "../../navigation";
|
||||||
import { releaseURL } from "../../../common/routes";
|
import { releaseURL } from "../../../common/routes";
|
||||||
import { dockViewsManager } from "./dock.views-manager";
|
import { dockViewsManager } from "./dock.views-manager";
|
||||||
|
import { DockTabContent, DockTabContentProps } from "./dock-tab-content";
|
||||||
|
|
||||||
interface Props extends InfoPanelProps {
|
interface Props extends DockTabContentProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class InstallChartInfoPanel extends Component<Props> {
|
export class InstallChart extends Component<Props> {
|
||||||
@observable showNotes = false;
|
@observable showNotes = false;
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
@ -159,7 +160,7 @@ export class InstallChartInfoPanel extends Component<Props> {
|
|||||||
return this.renderReleaseDetails();
|
return this.renderReleaseDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { chartData, install, versions } = this;
|
const { chartData, install, versions, tabId } = this;
|
||||||
const { repo, name, version, namespace, releaseName } = chartData;
|
const { repo, name, version, namespace, releaseName } = chartData;
|
||||||
|
|
||||||
const panelControls = (
|
const panelControls = (
|
||||||
@ -194,8 +195,14 @@ export class InstallChartInfoPanel extends Component<Props> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<DockTabContent
|
||||||
|
tabId={tabId}
|
||||||
|
withEditor
|
||||||
|
editorValue={installChartStore.getData(tabId).values}
|
||||||
|
editorOnChange={v => installChartStore.getData(tabId).values = v}
|
||||||
|
>
|
||||||
<InfoPanel
|
<InfoPanel
|
||||||
{...this.props}
|
tabId={tabId}
|
||||||
className="InstallChartInfoPanel"
|
className="InstallChartInfoPanel"
|
||||||
controls={panelControls}
|
controls={panelControls}
|
||||||
submit={install}
|
submit={install}
|
||||||
@ -203,18 +210,11 @@ export class InstallChartInfoPanel extends Component<Props> {
|
|||||||
submittingMessage="Installing..."
|
submittingMessage="Installing..."
|
||||||
showSubmitClose={false}
|
showSubmitClose={false}
|
||||||
/>
|
/>
|
||||||
|
</DockTabContent>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dockViewsManager.register(TabKind.INSTALL_CHART, {
|
dockViewsManager.register(TabKind.INSTALL_CHART, {
|
||||||
InfoPanel: InstallChartInfoPanel,
|
Content: InstallChart,
|
||||||
editor: {
|
|
||||||
getValue(tabId) {
|
|
||||||
return installChartStore.getData(tabId).values;
|
|
||||||
},
|
|
||||||
setValue(tabId, value) {
|
|
||||||
installChartStore.getData(tabId).values = value;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -52,12 +52,12 @@ export class Logs extends React.Component<Props> {
|
|||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
disposeOnUnmount(this,
|
disposeOnUnmount(this,
|
||||||
reaction(() => this.props.tab.id, this.reload, { fireImmediately: true }),
|
reaction(() => this.props.tabId, this.reload, { fireImmediately: true }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
get tabId() {
|
get tabId() {
|
||||||
return this.props.tab.id;
|
return this.props.tabId;
|
||||||
}
|
}
|
||||||
|
|
||||||
load = async () => {
|
load = async () => {
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { action, computed, makeObservable, observable } from "mobx";
|
import { action, computed, makeObservable, observable } from "mobx";
|
||||||
import { disposeOnUnmount, observer } from "mobx-react";
|
import { disposeOnUnmount, observer } from "mobx-react";
|
||||||
import { InfoPanel, InfoPanelProps } from "./info-panel";
|
import { InfoPanel } from "./info-panel";
|
||||||
import { Badge } from "../badge";
|
import { Badge } from "../badge";
|
||||||
import { Select, SelectOption } from "../select";
|
import { Select, SelectOption } from "../select";
|
||||||
import { dockStore, TabId, TabKind } from "./dock.store";
|
import { dockStore, TabId, TabKind } from "./dock.store";
|
||||||
@ -30,12 +30,14 @@ import type { IChartVersion } from "../+apps-helm-charts/helm-chart.store";
|
|||||||
import type { HelmRelease } from "../../../common/k8s-api/endpoints/helm-releases.api";
|
import type { HelmRelease } from "../../../common/k8s-api/endpoints/helm-releases.api";
|
||||||
import { upgradeChartStore } from "./upgrade-chart.store";
|
import { upgradeChartStore } from "./upgrade-chart.store";
|
||||||
import { dockViewsManager } from "./dock.views-manager";
|
import { dockViewsManager } from "./dock.views-manager";
|
||||||
|
import { DockTabContent, DockTabContentProps } from "./dock-tab-content";
|
||||||
|
import { Spinner } from "../spinner";
|
||||||
|
|
||||||
interface Props extends InfoPanelProps {
|
interface Props extends DockTabContentProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class UpgradeChartInfoPanel extends React.Component<Props> {
|
export class UpgradeChart extends React.Component<Props> {
|
||||||
@observable selectedVersion: IChartVersion;
|
@observable selectedVersion: IChartVersion;
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
@ -88,14 +90,20 @@ export class UpgradeChartInfoPanel extends React.Component<Props> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (!upgradeChartStore.isReady(this.tabId)) {
|
if (!upgradeChartStore.isReady(this.tabId)) {
|
||||||
return null;
|
return <Spinner center/>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { release, selectedVersion, chartVersions } = this;
|
const { release, selectedVersion, chartVersions, tabId } = this;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<DockTabContent
|
||||||
|
tabId={tabId}
|
||||||
|
withEditor
|
||||||
|
editorValue={upgradeChartStore.values.get(tabId)}
|
||||||
|
editorOnChange={v => upgradeChartStore.values.set(tabId, v)}
|
||||||
|
>
|
||||||
<InfoPanel
|
<InfoPanel
|
||||||
{...this.props}
|
tabId={this.tabId}
|
||||||
submit={this.upgrade}
|
submit={this.upgrade}
|
||||||
submitLabel="Upgrade"
|
submitLabel="Upgrade"
|
||||||
submittingMessage="Updating.."
|
submittingMessage="Updating.."
|
||||||
@ -116,18 +124,11 @@ export class UpgradeChartInfoPanel extends React.Component<Props> {
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
</DockTabContent>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dockViewsManager.register(TabKind.UPGRADE_CHART, {
|
dockViewsManager.register(TabKind.UPGRADE_CHART, {
|
||||||
InfoPanel: UpgradeChartInfoPanel,
|
Content: UpgradeChart,
|
||||||
editor: {
|
|
||||||
getValue(tabId): string {
|
|
||||||
return upgradeChartStore.values.get(tabId);
|
|
||||||
},
|
|
||||||
setValue(tabId, value) {
|
|
||||||
upgradeChartStore.values.set(tabId, value);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -33,8 +33,10 @@ import debounce from "lodash/debounce";
|
|||||||
|
|
||||||
registerCustomThemes(); // setup
|
registerCustomThemes(); // setup
|
||||||
|
|
||||||
|
export type MonacoEditorId = string;
|
||||||
|
|
||||||
export interface MonacoEditorProps {
|
export interface MonacoEditorProps {
|
||||||
id?: string; // associating editor's ID with created model.uri
|
id?: MonacoEditorId; // associating editor's ID with created model.uri
|
||||||
value?: string;
|
value?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
autoFocus?: boolean;
|
autoFocus?: boolean;
|
||||||
@ -59,7 +61,11 @@ export const defaultEditorProps: Partial<MonacoEditorProps> = {
|
|||||||
@observer
|
@observer
|
||||||
export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
||||||
static defaultProps = defaultEditorProps as object;
|
static defaultProps = defaultEditorProps as object;
|
||||||
static viewStates = new WeakMap<editor.ITextModel, editor.ICodeEditorViewState>();
|
static viewStates = new WeakMap<Uri, editor.ICodeEditorViewState>();
|
||||||
|
|
||||||
|
static createUri(id: MonacoEditorId): Uri {
|
||||||
|
return Uri.file(`/monaco-editor/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
public staticId = `editor-id#${Math.round(1e7 * Math.random())}`;
|
public staticId = `editor-id#${Math.round(1e7 * Math.random())}`;
|
||||||
public dispose = disposer();
|
public dispose = disposer();
|
||||||
@ -68,13 +74,24 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
@observable.ref editor: editor.IStandaloneCodeEditor;
|
@observable.ref editor: editor.IStandaloneCodeEditor;
|
||||||
@observable dimensions: { width?: number, height?: number } = {};
|
@observable dimensions: { width?: number, height?: number } = {};
|
||||||
@observable unmounting = false;
|
@observable unmounting = false;
|
||||||
validationErrors = observable.array<string>();
|
|
||||||
|
|
||||||
constructor(props: MonacoEditorProps) {
|
constructor(props: MonacoEditorProps) {
|
||||||
super(props);
|
super(props);
|
||||||
makeObservable(this);
|
makeObservable(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@computed get model(): editor.ITextModel {
|
||||||
|
const uri = MonacoEditor.createUri(this.props.id ?? this.staticId);
|
||||||
|
const model = editor.getModels().find(model => model.uri.toString() == uri.toString());
|
||||||
|
|
||||||
|
if (model) {
|
||||||
|
return model; // already exists
|
||||||
|
}
|
||||||
|
|
||||||
|
const { language, value } = this.props;
|
||||||
|
return editor.createModel(value, language, uri);
|
||||||
|
}
|
||||||
|
|
||||||
@computed get options(): editor.IStandaloneEditorConstructionOptions {
|
@computed get options(): editor.IStandaloneEditorConstructionOptions {
|
||||||
return toJS({
|
return toJS({
|
||||||
...UserStore.getInstance().editorConfiguration,
|
...UserStore.getInstance().editorConfiguration,
|
||||||
@ -105,20 +122,31 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
onModelChange = (model: editor.ITextModel, oldModel?: editor.ITextModel) => {
|
onModelChange = (model: editor.ITextModel, oldModel?: editor.ITextModel) => {
|
||||||
console.info("[MONACO]: model change", { model, oldModel });
|
console.info("[MONACO]: model change", { model, oldModel });
|
||||||
|
|
||||||
this.saveViewState(oldModel); // save current view-model state in the editor
|
this.saveViewState(oldModel);
|
||||||
this.editor.setModel(model);
|
this.editor.setModel(model);
|
||||||
this.editor.restoreViewState(this.getViewState(model)); // restore cursor position, selection, etc.
|
this.restoreViewState(model);
|
||||||
this.editor.layout();
|
this.editor.layout();
|
||||||
this.editor.focus(); // keep focus in editor, e.g. when clicking between dock-tabs
|
this.editor.focus(); // keep focus in editor, e.g. when clicking between dock-tabs
|
||||||
this.validateLazy();
|
this.validateLazy();
|
||||||
};
|
};
|
||||||
|
|
||||||
@computed get editorId(): string {
|
/**
|
||||||
return this.props.id ?? this.staticId;
|
* Save current view-model state in the editor.
|
||||||
|
* This will allow restore cursor position, selected text, etc.
|
||||||
|
* @param {editor.ITextModel} model
|
||||||
|
*/
|
||||||
|
saveViewState(model = this.model) {
|
||||||
|
if (!model) return;
|
||||||
|
|
||||||
|
MonacoEditor.viewStates.set(model.uri, this.editor.saveViewState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@computed get model(): editor.ITextModel {
|
restoreViewState(model = this.model) {
|
||||||
return this.getModelById(this.editorId);
|
if (!model) return;
|
||||||
|
|
||||||
|
const viewState = MonacoEditor.viewStates.get(model.uri);
|
||||||
|
|
||||||
|
this.editor.restoreViewState(viewState);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
@ -132,6 +160,7 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.unmounting = true;
|
this.unmounting = true;
|
||||||
|
this.saveViewState();
|
||||||
this.destroy();
|
this.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,8 +179,10 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
readOnly,
|
readOnly,
|
||||||
...this.options,
|
...this.options,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.info(`[MONACO]: editor created for language=${language}, theme=${theme}`);
|
console.info(`[MONACO]: editor created for language=${language}, theme=${theme}`);
|
||||||
this.validateLazy(); // validate initial value
|
this.validateLazy(); // validate initial value
|
||||||
|
this.restoreViewState(); // restore previous state if any
|
||||||
|
|
||||||
if (this.props.autoFocus) {
|
if (this.props.autoFocus) {
|
||||||
this.editor.focus();
|
this.editor.focus();
|
||||||
@ -204,33 +235,6 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
this.editor?.layout({ width, height });
|
this.editor?.layout({ width, height });
|
||||||
}
|
}
|
||||||
|
|
||||||
createUri(id: string): Uri {
|
|
||||||
return Uri.file(`/monaco-editor/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
getViewState(model = this.model): editor.ICodeEditorViewState {
|
|
||||||
return MonacoEditor.viewStates.get(model) ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
saveViewState(model = this.model) {
|
|
||||||
if (!model) return;
|
|
||||||
MonacoEditor.viewStates.set(model, this.editor.saveViewState());
|
|
||||||
}
|
|
||||||
|
|
||||||
getModelById(id: string): editor.ITextModel | null {
|
|
||||||
const uri = this.createUri(id);
|
|
||||||
const model = editor.getModels().find(model => String(model.uri) === String(uri));
|
|
||||||
|
|
||||||
if (model) {
|
|
||||||
return model; // model with corresponding props.id exists
|
|
||||||
}
|
|
||||||
|
|
||||||
// creating new temporary model if not exists regarding to props.ID
|
|
||||||
const { language, value } = this.props;
|
|
||||||
|
|
||||||
return editor.createModel(value, language, uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
setValue(value = ""): void {
|
setValue(value = ""): void {
|
||||||
if (value == this.getValue()) return;
|
if (value == this.getValue()) return;
|
||||||
|
|
||||||
@ -256,15 +260,11 @@ export class MonacoEditor extends React.Component<MonacoEditorProps> {
|
|||||||
validators.push(syntaxValidator);
|
validators.push(syntaxValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.info('[MONACO]: validating', { value, validators });
|
|
||||||
this.validationErrors.clear(); // reset first
|
|
||||||
|
|
||||||
for (const validate of validators) {
|
for (const validate of validators) {
|
||||||
try {
|
try {
|
||||||
await validate(value);
|
await validate(value);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error = String(error);
|
error = String(error);
|
||||||
this.validationErrors.push(error);
|
|
||||||
this.props.onError?.(error); // emit error outside via callback
|
this.props.onError?.(error); // emit error outside via callback
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user