Skip to content

Commit

Permalink
feat: add the outputEditorInstance property in panelService (#229)
Browse files Browse the repository at this point in the history
* feat: add the outputEditorInstance property in panelService

* refactor: add css style for the Output component

* refactor: set builtIn output ID to PANEL_OUTPUT

* refactor: remove useless css style
  • Loading branch information
wewoor authored Jul 9, 2021
1 parent 2b9002b commit 14ab294
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
18 changes: 16 additions & 2 deletions src/model/workbench/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ export interface IPanel {
toolbox?: IActionBarItemProps[];
}

export interface IOutput extends IPanelItem {
outputEditorInstance?: IStandaloneCodeEditor;
onUpdateEditorIns?(editorInstance: IStandaloneCodeEditor): void;
}

export function builtInOutputPanel() {
return {
const outputPane: IOutput = {
id: PANEL_OUTPUT,
name: localize(PANEL_OUTPUT, 'output'),
data: '',
renderPane: (item) => <Output {...item} />,
};

function onUpdateEditorIns(editorInstance: IStandaloneCodeEditor) {
outputPane.outputEditorInstance = editorInstance;
}

outputPane.renderPane = (item) => (
<Output onUpdateEditorIns={onUpdateEditorIns} {...item} />
);

return outputPane;
}

export function builtInPanelToolboxResize(): IActionBarItemProps {
Expand Down
22 changes: 16 additions & 6 deletions src/services/workbench/panelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Component } from 'mo/react';
import {
builtInOutputPanel,
builtInPanelToolboxResize,
IOutput,
IPanel,
IPanelItem,
PanelEvent,
Expand All @@ -19,6 +20,10 @@ import { localize } from 'mo/i18n/localize';
import { LayoutService } from 'mo/services';

export interface IPanelService extends Component<IPanel> {
/**
* The editorInstance of Output
*/
readonly outputEditorInstance: IStandaloneCodeEditor | undefined;
open(data: IPanelItem): void;
getById(id: string): IPanelItem | undefined;
add(data: IPanelItem | IPanelItem[]): void;
Expand All @@ -45,6 +50,13 @@ export class PanelService extends Component<IPanel> implements IPanelService {
this.layoutService = container.resolve(LayoutService);
}

public get outputEditorInstance() {
const outputPane: IOutput | undefined = this.state.data?.find(
searchById(PANEL_OUTPUT)
);
return outputPane?.outputEditorInstance;
}

public maximizeRestore(): void {
const panelMaximized = this.layoutService.isPanelMaximized();
const { toolbox = [] } = this.state;
Expand Down Expand Up @@ -87,16 +99,14 @@ export class PanelService extends Component<IPanel> implements IPanelService {
public updateOutput(data: IPanelItem<any>): IPanelItem | undefined {
return this.update(Object.assign(builtInOutputPanel(), data));
}

public appendOutput(content: string): void {
const output = this.getById(PANEL_OUTPUT);
if (output) {
output.data = output.data + content;
this.updateOutput(output);
}
const outputValue = this.outputEditorInstance?.getValue();
this.outputEditorInstance?.setValue(outputValue + content);
}

public clearOutput(): void {
this.updateOutput(Object.assign(builtInOutputPanel(), { data: '' }));
this.outputEditorInstance?.setValue('');
}

public add(data: IPanelItem | IPanelItem[]) {
Expand Down
1 change: 1 addition & 0 deletions src/style/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $mainBench: prefix('mainBench');
$activityBar: prefix('activityBar');
$notification: prefix('notification');
$problems: prefix('problems');
$output: prefix('output');
$folderTree: prefix('folderTree');
$editorTree: prefix('editorTree');

Expand Down
17 changes: 8 additions & 9 deletions src/workbench/panel/output.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import * as React from 'react';
import { prefixClaName } from 'mo/common/className';
import { IPanelItem } from 'mo/model/workbench/panel';
import { IOutput } from 'mo/model/workbench/panel';
import { MonacoEditor } from 'mo/components/monaco';

const defaultClassName = prefixClaName('output');

function Output(props: IPanelItem) {
const { data = '' } = props;
// TODO: Output should export the editorInstance, it's used for update the editor Language, Monarch
function Output(props: IOutput) {
const { id, data = '', onUpdateEditorIns } = props;
return (
<div
className={defaultClassName}
style={{ width: '100%', height: '100%' }}
>
<div className={defaultClassName}>
<MonacoEditor
key={data.length}
key={id}
options={{
value: data,
readOnly: true,
Expand All @@ -25,6 +21,9 @@ function Output(props: IPanelItem) {
},
automaticLayout: true,
}}
editorInstanceRef={(editorInstance) => {
onUpdateEditorIns?.(editorInstance);
}}
/>
</div>
);
Expand Down
2 changes: 0 additions & 2 deletions src/workbench/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { ActionBar } from 'mo/components/actionBar';

const defaultClassName = prefixClaName('panel');
const panelHeaderClassName = getBEMElement(defaultClassName, 'header');

const panelToolbarClassName = getBEMElement(defaultClassName, 'toolbar');

const panelContainerClassName = getBEMElement(defaultClassName, 'container');

export function Panel(props: IPanel & IPanelController) {
Expand Down
4 changes: 3 additions & 1 deletion stories/extensions/test/testPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export default class TestPane extends React.Component {
};

const updateOutput = () => {
molecule.panel.appendOutput('Number: ' + Math.random() * 10);
const editorIns = molecule.panel.outputEditorInstance;
console.log('outputEditorInstance:', editorIns);
molecule.panel.appendOutput('Number: ' + Math.random() * 10 + '\n');
};

const newEditor = function () {
Expand Down

0 comments on commit 14ab294

Please sign in to comment.