@@ -44,29 +44,71 @@ export class Editor extends Component<EditorProps> {
44
44
this . editor = editor ;
45
45
this . visibleCursorDisplayStyle = this . editor . renderer . $cursorLayer . element . style . display ;
46
46
47
+ editor . commands . on ( 'afterExec' , ( e : any ) => {
48
+ if (
49
+ // Only suggest autocomplete if autocompleter was set
50
+ this . autocompleter &&
51
+ e . command . name === 'insertstring' &&
52
+ / ^ [ \w . ] $ / . test ( e . args )
53
+ ) {
54
+ this . editor . execCommand ( 'startAutocomplete' ) ;
55
+ }
56
+ } ) ;
57
+ } ;
58
+
59
+ private autocompleter : AceAutocompleterAdapter | null = null ;
60
+
61
+ private editorId : number = Date . now ( ) ;
62
+
63
+ constructor ( props : EditorProps ) {
64
+ super ( props ) ;
47
65
if ( this . props . autocompleter ) {
48
- editor . commands . on ( 'afterExec' , function ( e : any ) {
49
- if ( e . command . name === 'insertstring' && / ^ [ \w . ] $ / . test ( e . args ) ) {
50
- editor . execCommand ( 'startAutocomplete' ) ;
51
- }
52
- } ) ;
66
+ this . autocompleter = new AceAutocompleterAdapter (
67
+ this . props . autocompleter
68
+ ) ;
69
+ }
70
+ }
53
71
54
- tools . setCompleters ( [ new AceAutocompleterAdapter ( this . props . autocompleter ) ] ) ;
72
+ private onFocus = ( ) => {
73
+ if ( this . autocompleter ) {
74
+ tools . setCompleters ( [ this . autocompleter ] ) ;
75
+ } else {
76
+ tools . setCompleters ( [ ] ) ;
55
77
}
56
78
} ;
57
79
58
- componentDidUpdate ( ) : void {
59
- if ( this . props . operationInProgress ) {
60
- this . hideCursor ( ) ;
61
- } else {
62
- this . showCursor ( ) ;
80
+ componentDidUpdate ( prevProps : EditorProps ) : void {
81
+ if ( prevProps . operationInProgress !== this . props . operationInProgress ) {
82
+ if ( this . props . operationInProgress ) {
83
+ this . hideCursor ( ) ;
84
+ } else {
85
+ this . showCursor ( ) ;
86
+ }
87
+ }
88
+
89
+ if ( prevProps . moveCursorToTheEndOfInput !== this . props . moveCursorToTheEndOfInput ) {
90
+ if ( this . props . moveCursorToTheEndOfInput ) {
91
+ this . moveCursorToTheEndOfInput ( ) ;
92
+ }
63
93
}
64
94
65
- if ( this . props . moveCursorToTheEndOfInput ) {
66
- this . moveCursorToTheEndOfInput ( ) ;
95
+ if ( prevProps . autocompleter !== this . props . autocompleter ) {
96
+ if ( this . props . autocompleter ) {
97
+ this . autocompleter = new AceAutocompleterAdapter (
98
+ this . props . autocompleter
99
+ ) ;
100
+ tools . setCompleters ( [ this . autocompleter ] ) ;
101
+ } else {
102
+ this . autocompleter = null ;
103
+ tools . setCompleters ( [ ] ) ;
104
+ }
67
105
}
68
106
}
69
107
108
+ componentWillUnmount ( ) : void {
109
+ this . autocompleter = null ;
110
+ }
111
+
70
112
private hideCursor ( ) : void {
71
113
this . editor . renderer . $cursorLayer . element . style . display = 'none' ;
72
114
}
@@ -95,16 +137,17 @@ export class Editor extends Component<EditorProps> {
95
137
tabSize : 2 ,
96
138
useWorker : false
97
139
} }
98
- name = { `mongosh-ace-${ Date . now ( ) } ` }
140
+ name = { `mongosh-ace-${ this . editorId } ` }
99
141
mode = "javascript"
100
142
ref = { ( ref : AceEditor | null ) : void => {
101
143
if ( this . props . setInputRef && ref !== null ) {
102
144
this . props . setInputRef ( ref as { editor ?: IAceEditor } ) ;
103
145
}
104
146
} }
105
147
theme = "mongosh"
106
- onChange = { this . props . onChange }
107
148
onLoad = { this . onEditorLoad }
149
+ onFocus = { this . onFocus }
150
+ onChange = { this . props . onChange }
108
151
commands = { [
109
152
{
110
153
name : 'return' ,
0 commit comments