Skip to content

Commit 14ab294

Browse files
authored
feat: add the outputEditorInstance property in panelService (#229)
* 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
1 parent 2b9002b commit 14ab294

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

src/model/workbench/panel.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,27 @@ export interface IPanel {
2727
toolbox?: IActionBarItemProps[];
2828
}
2929

30+
export interface IOutput extends IPanelItem {
31+
outputEditorInstance?: IStandaloneCodeEditor;
32+
onUpdateEditorIns?(editorInstance: IStandaloneCodeEditor): void;
33+
}
34+
3035
export function builtInOutputPanel() {
31-
return {
36+
const outputPane: IOutput = {
3237
id: PANEL_OUTPUT,
3338
name: localize(PANEL_OUTPUT, 'output'),
3439
data: '',
35-
renderPane: (item) => <Output {...item} />,
3640
};
41+
42+
function onUpdateEditorIns(editorInstance: IStandaloneCodeEditor) {
43+
outputPane.outputEditorInstance = editorInstance;
44+
}
45+
46+
outputPane.renderPane = (item) => (
47+
<Output onUpdateEditorIns={onUpdateEditorIns} {...item} />
48+
);
49+
50+
return outputPane;
3751
}
3852

3953
export function builtInPanelToolboxResize(): IActionBarItemProps {

src/services/workbench/panelService.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Component } from 'mo/react';
44
import {
55
builtInOutputPanel,
66
builtInPanelToolboxResize,
7+
IOutput,
78
IPanel,
89
IPanelItem,
910
PanelEvent,
@@ -19,6 +20,10 @@ import { localize } from 'mo/i18n/localize';
1920
import { LayoutService } from 'mo/services';
2021

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

53+
public get outputEditorInstance() {
54+
const outputPane: IOutput | undefined = this.state.data?.find(
55+
searchById(PANEL_OUTPUT)
56+
);
57+
return outputPane?.outputEditorInstance;
58+
}
59+
4860
public maximizeRestore(): void {
4961
const panelMaximized = this.layoutService.isPanelMaximized();
5062
const { toolbox = [] } = this.state;
@@ -87,16 +99,14 @@ export class PanelService extends Component<IPanel> implements IPanelService {
8799
public updateOutput(data: IPanelItem<any>): IPanelItem | undefined {
88100
return this.update(Object.assign(builtInOutputPanel(), data));
89101
}
102+
90103
public appendOutput(content: string): void {
91-
const output = this.getById(PANEL_OUTPUT);
92-
if (output) {
93-
output.data = output.data + content;
94-
this.updateOutput(output);
95-
}
104+
const outputValue = this.outputEditorInstance?.getValue();
105+
this.outputEditorInstance?.setValue(outputValue + content);
96106
}
97107

98108
public clearOutput(): void {
99-
this.updateOutput(Object.assign(builtInOutputPanel(), { data: '' }));
109+
this.outputEditorInstance?.setValue('');
100110
}
101111

102112
public add(data: IPanelItem | IPanelItem[]) {

src/style/common.scss

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ $mainBench: prefix('mainBench');
4242
$activityBar: prefix('activityBar');
4343
$notification: prefix('notification');
4444
$problems: prefix('problems');
45+
$output: prefix('output');
4546
$folderTree: prefix('folderTree');
4647
$editorTree: prefix('editorTree');
4748

src/workbench/panel/output.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import * as React from 'react';
22
import { prefixClaName } from 'mo/common/className';
3-
import { IPanelItem } from 'mo/model/workbench/panel';
3+
import { IOutput } from 'mo/model/workbench/panel';
44
import { MonacoEditor } from 'mo/components/monaco';
55

66
const defaultClassName = prefixClaName('output');
77

8-
function Output(props: IPanelItem) {
9-
const { data = '' } = props;
10-
// TODO: Output should export the editorInstance, it's used for update the editor Language, Monarch
8+
function Output(props: IOutput) {
9+
const { id, data = '', onUpdateEditorIns } = props;
1110
return (
12-
<div
13-
className={defaultClassName}
14-
style={{ width: '100%', height: '100%' }}
15-
>
11+
<div className={defaultClassName}>
1612
<MonacoEditor
17-
key={data.length}
13+
key={id}
1814
options={{
1915
value: data,
2016
readOnly: true,
@@ -25,6 +21,9 @@ function Output(props: IPanelItem) {
2521
},
2622
automaticLayout: true,
2723
}}
24+
editorInstanceRef={(editorInstance) => {
25+
onUpdateEditorIns?.(editorInstance);
26+
}}
2827
/>
2928
</div>
3029
);

src/workbench/panel/panel.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { ActionBar } from 'mo/components/actionBar';
77

88
const defaultClassName = prefixClaName('panel');
99
const panelHeaderClassName = getBEMElement(defaultClassName, 'header');
10-
1110
const panelToolbarClassName = getBEMElement(defaultClassName, 'toolbar');
12-
1311
const panelContainerClassName = getBEMElement(defaultClassName, 'container');
1412

1513
export function Panel(props: IPanel & IPanelController) {

stories/extensions/test/testPane.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export default class TestPane extends React.Component {
9393
};
9494

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

99101
const newEditor = function () {

0 commit comments

Comments
 (0)