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
100 lines (90 loc) · 3.71 KB
/
code.tsx
File metadata and controls
100 lines (90 loc) · 3.71 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
// 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 { Editor } from './editor';
export interface ICodeProps {
autoFocus: boolean;
code : string;
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;
onCreated(code: string, modelId: string): void;
onChange(changes: monacoEditor.editor.IModelContentChange[], modelId: string): 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 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}
focused={this.props.focused}
unfocused={this.props.unfocused}
showLineNumbers={this.props.showLineNumbers}
useQuickEdit={this.props.useQuickEdit}
/>
<div className={waterMarkClass} role='textbox' onClick={this.clickWatermark}>{this.getWatermarkString()}</div>
</div>
);
}
public giveFocus() {
if (this.editorRef && this.editorRef.current) {
this.editorRef.current.giveFocus();
}
}
private clickWatermark = (ev: React.MouseEvent<HTMLDivElement>) => {
ev.stopPropagation();
// Give focus to the editor
this.giveFocus();
}
private getWatermarkString = () : string => {
return getLocString('DataScience.inputWatermark', 'Type code here and press shift-enter to run');
}
private onModelChanged = (changes: monacoEditor.editor.IModelContentChange[], model: monacoEditor.editor.ITextModel) => {
if (!this.props.readOnly && model) {
this.setState({allowWatermark: model.getValueLength() === 0});
}
this.props.onChange(changes, model.id);
}
}