forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.tsx
More file actions
128 lines (115 loc) · 4.57 KB
/
code.tsx
File metadata and controls
128 lines (115 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
import * as React from 'react';
import { InputHistory } from '../interactive-common/inputHistory';
import { IKeyboardEvent } from '../react-common/event';
import { getLocString } from '../react-common/locReactSide';
import { IMonacoModelContentChangeEvent } from '../react-common/monacoHelpers';
import { Editor } from './editor';
import { CursorPos, IFont } from './mainState';
export interface ICodeProps {
code: string;
version: number;
codeTheme: string;
testMode: boolean;
readOnly: boolean;
history: InputHistory | undefined;
showWatermark: boolean;
monacoTheme: string | undefined;
outermostParentClass: string;
editorOptions?: monacoEditor.editor.IEditorOptions;
editorMeasureClassName?: string;
showLineNumbers?: boolean;
useQuickEdit?: boolean;
font: IFont;
hasFocus: boolean;
cursorPos: CursorPos | monacoEditor.IPosition;
disableUndoStack: boolean;
focusPending: number;
onCreated(code: string, modelId: string): void;
onChange(e: IMonacoModelContentChangeEvent): void;
openLink(uri: monacoEditor.Uri): void;
keyDown?(e: IKeyboardEvent): void;
focused?(): void;
unfocused?(): void;
}
interface ICodeState {
allowWatermark: boolean;
}
export class Code extends React.Component<ICodeProps, ICodeState> {
private editorRef: React.RefObject<Editor> = React.createRef<Editor>();
constructor(prop: ICodeProps) {
super(prop);
this.state = { allowWatermark: true };
}
public componentDidUpdate(prevProps: ICodeProps) {
if (prevProps.focusPending !== this.props.focusPending) {
this.giveFocus(CursorPos.Current);
}
}
public render() {
const readOnly = this.props.readOnly;
const waterMarkClass =
this.props.showWatermark && this.state.allowWatermark && !readOnly ? 'code-watermark' : 'hide';
const classes = readOnly ? 'code-area' : 'code-area code-area-editable';
return (
<div className={classes}>
<Editor
codeTheme={this.props.codeTheme}
readOnly={readOnly}
history={this.props.history}
onCreated={this.props.onCreated}
onChange={this.onModelChanged}
testMode={this.props.testMode}
content={this.props.code}
outermostParentClass={this.props.outermostParentClass}
monacoTheme={this.props.monacoTheme}
language="python"
editorOptions={this.props.editorOptions}
openLink={this.props.openLink}
ref={this.editorRef}
editorMeasureClassName={this.props.editorMeasureClassName}
keyDown={this.props.keyDown}
hasFocus={this.props.hasFocus}
cursorPos={this.props.cursorPos}
focused={this.props.focused}
unfocused={this.props.unfocused}
showLineNumbers={this.props.showLineNumbers}
useQuickEdit={this.props.useQuickEdit}
font={this.props.font}
disableUndoStack={this.props.disableUndoStack}
version={this.props.version}
/>
<div className={waterMarkClass} role="textbox" onClick={this.clickWatermark}>
{this.getWatermarkString()}
</div>
</div>
);
}
public getContents(): string | undefined {
if (this.editorRef.current) {
return this.editorRef.current.getContents();
}
}
private giveFocus(cursorPos: CursorPos) {
if (this.editorRef && this.editorRef.current) {
this.editorRef.current.giveFocus(cursorPos);
}
}
private clickWatermark = (ev: React.MouseEvent<HTMLDivElement>) => {
ev.stopPropagation();
// Give focus to the editor
this.giveFocus(CursorPos.Current);
};
private getWatermarkString = (): string => {
return getLocString('DataScience.inputWatermark', 'Type code here and press shift-enter to run');
};
private onModelChanged = (e: IMonacoModelContentChangeEvent) => {
if (!this.props.readOnly && e.model) {
this.setState({ allowWatermark: e.model.getValueLength() === 0 });
}
this.props.onChange(e);
};
}