1
1
import PropTypes from 'prop-types' ;
2
2
import React from 'react' ;
3
3
import CodeMirror from 'codemirror' ;
4
+ import Fuse from 'fuse.js' ;
4
5
import emmet from '@emmetio/codemirror-plugin' ;
5
6
import prettier from 'prettier' ;
6
7
import babelParser from 'prettier/parser-babel' ;
@@ -27,6 +28,7 @@ import 'codemirror/addon/search/jump-to-line';
27
28
import 'codemirror/addon/edit/matchbrackets' ;
28
29
import 'codemirror/addon/edit/closebrackets' ;
29
30
import 'codemirror/addon/selection/mark-selection' ;
31
+ import 'codemirror/addon/hint/css-hint' ;
30
32
31
33
import { JSHINT } from 'jshint' ;
32
34
import { CSSLint } from 'csslint' ;
@@ -41,6 +43,8 @@ import '../../../utils/webGL-clike';
41
43
import Timer from '../components/Timer' ;
42
44
import EditorAccessibility from '../components/EditorAccessibility' ;
43
45
import { metaKey } from '../../../utils/metaKey' ;
46
+ import './EditorShowHint' ;
47
+ import * as hinter from '../../../utils/p5-hinter' ;
44
48
45
49
import '../../../utils/codemirror-search' ;
46
50
@@ -126,6 +130,10 @@ class Editor extends React.Component {
126
130
}
127
131
}
128
132
} ) ;
133
+ this . hinter = new Fuse ( hinter . p5Hinter , {
134
+ threshold : 0.2 ,
135
+ keys : [ 'text' ]
136
+ } ) ;
129
137
130
138
delete this . _cm . options . lint . options . errors ;
131
139
@@ -175,16 +183,21 @@ class Editor extends React.Component {
175
183
} ) ;
176
184
177
185
this . _cm . on ( 'keydown' , ( _cm , e ) => {
178
- // 70 === f
179
186
if (
180
187
( ( metaKey === 'Cmd' && e . metaKey ) ||
181
188
( metaKey === 'Ctrl' && e . ctrlKey ) ) &&
182
189
e . shiftKey &&
183
- e . keyCode === 70
190
+ e . key === 'f'
184
191
) {
185
192
e . preventDefault ( ) ;
186
193
this . tidyCode ( ) ;
187
194
}
195
+
196
+ // Show hint
197
+ const mode = this . _cm . getOption ( 'mode' ) ;
198
+ if ( / ^ [ a - z ] $ / i. test ( e . key ) && ( mode === 'css' || mode === 'javascript' ) ) {
199
+ this . showHint ( _cm ) ;
200
+ }
188
201
} ) ;
189
202
190
203
this . _cm . getWrapperElement ( ) . style [
@@ -317,6 +330,39 @@ class Editor extends React.Component {
317
330
this . _cm . execCommand ( 'findPersistent' ) ;
318
331
}
319
332
333
+ showHint ( _cm ) {
334
+ const hintOptions = {
335
+ completeSingle : false ,
336
+ completeOnSingleClick : false ,
337
+ closeOnUnfocus : false
338
+ // closeOnPick: false
339
+ } ;
340
+
341
+ // CSS
342
+ if ( _cm . options . mode === 'css' ) {
343
+ CodeMirror . showHint ( _cm , CodeMirror . hint . css , hintOptions ) ;
344
+ return ;
345
+ }
346
+
347
+ // JavaScript
348
+ CodeMirror . showHint (
349
+ _cm ,
350
+ ( ) => {
351
+ const c = _cm . getCursor ( ) ;
352
+ const token = _cm . getTokenAt ( c ) ;
353
+
354
+ const hints = this . hinter . search ( token . string ) ;
355
+
356
+ return {
357
+ list : hints ,
358
+ from : CodeMirror . Pos ( c . line , token . start ) ,
359
+ to : CodeMirror . Pos ( c . line , c . ch )
360
+ } ;
361
+ } ,
362
+ hintOptions
363
+ ) ;
364
+ }
365
+
320
366
showReplace ( ) {
321
367
this . _cm . execCommand ( 'replace' ) ;
322
368
}
0 commit comments