1
+ /* eslint-disable no-useless-escape */
1
2
/*global require,module*/
2
3
"use strict" ;
3
4
var CodeMirror = require ( "codemirror" ) ;
4
5
require ( "codemirror/addon/edit/continuelist.js" ) ;
5
6
require ( "./codemirror/tablist" ) ;
6
7
require ( "codemirror/addon/display/fullscreen.js" ) ;
7
8
require ( "codemirror/mode/markdown/markdown.js" ) ;
8
- require ( "codemirror/addon/mode/overlay.js" ) ;
9
9
require ( "codemirror/addon/hint/show-hint.js" ) ;
10
+ require ( "codemirror/addon/mode/overlay.js" ) ;
10
11
require ( "codemirror/addon/display/placeholder.js" ) ;
11
12
require ( "codemirror/addon/selection/mark-selection.js" ) ;
12
13
require ( "codemirror/mode/gfm/gfm.js" ) ;
13
14
require ( "codemirror/mode/xml/xml.js" ) ;
14
15
var CodeMirrorSpellChecker = require ( "codemirror-spell-checker" ) ;
15
16
var marked = require ( "marked" ) ;
16
17
18
+
17
19
var currentEditorHelperHint ;
18
20
19
- CodeMirror . defineOption ( "autoSuggest" , [ ] , function ( cm , autoSuggestOptions , old ) {
21
+ CodeMirror . defineOption ( "autoSuggest" , [ ] , function ( cm , autoSuggestOptions ) {
20
22
cm . on ( "inputRead" , function ( cm , change ) {
21
23
var mode = cm . getModeAt ( cm . getCursor ( ) ) ;
22
24
23
- if ( mode . name === autoSuggestOptions . mode && autoSuggestOptions . startChars . indexOf ( change . text [ 0 ] ) != - 1 ) {
25
+ var currentTrigger = undefined ;
26
+ for ( var trigger in autoSuggestOptions . triggers ) {
27
+ if ( trigger === change . text [ 0 ] ) {
28
+ currentTrigger = trigger ;
29
+ }
30
+ }
31
+
32
+ var forEachHint = function ( action ) {
33
+ var hintsElement = document . querySelector ( ".CodeMirror-hints" ) ;
34
+ if ( hintsElement ) {
35
+ var hints = hintsElement . querySelectorAll ( ".CodeMirror-hint" ) ;
36
+ for ( var i = 0 ; i < hints . length ; i ++ ) {
37
+ var hint = hints [ i ] ;
38
+ action ( hint ) ;
39
+ }
40
+ }
41
+ } ;
42
+
43
+ var setHintActive = function ( event ) {
44
+ forEachHint ( function ( hint ) {
45
+ hint . classList . remove ( "CodeMirror-hint-active" ) ;
46
+ } ) ;
47
+ event . target . classList . add ( "CodeMirror-hint-active" ) ;
48
+ } ;
49
+
50
+ forEachHint ( function ( hint ) {
51
+ hint . removeEventListener ( "mouseenter" , setHintActive ) ;
52
+ } ) ;
53
+
54
+ if ( mode . name === autoSuggestOptions . mode && currentTrigger ) {
24
55
25
56
currentEditorHelperHint = autoSuggestOptions ;
26
57
currentEditorHelperHint . startChar = change . text [ 0 ] ;
27
58
28
59
cm . showHint ( {
29
60
completeSingle : false ,
30
61
closeCharacters : / [ \v ( ) \[ \] { } ; : > , ] / ,
31
- hint : function ( cm , options ) {
62
+ className : "hints" ,
63
+ hint : function ( cm ) {
32
64
var cur = cm . getCursor ( ) ,
33
65
token = cm . getTokenAt ( cur ) ;
34
66
var start = token . start + 1 ,
35
67
end = token . end ;
36
68
37
69
var line = cm . getCursor ( ) . line ,
38
- ch = cm . getCursor ( ) . ch ,
39
- stringToMatch = currentEditorHelperHint . startChar ,
40
- n = stringToMatch . length ,
41
- lineToStarChar = cm . getLine ( line ) . substring ( 0 , token . start + 1 ) ,
70
+ lineToStarChar = cm . getLine ( line ) . substring ( 0 , start ) ,
42
71
charBeforeStarChar = lineToStarChar . substring ( cm . getLine ( line ) . lastIndexOf ( currentEditorHelperHint . startChar ) - 1 , cm . getLine ( line ) . lastIndexOf ( currentEditorHelperHint . startChar ) ) ,
43
- stringToTest = lineToStarChar . substring ( cm . getLine ( line ) . lastIndexOf ( currentEditorHelperHint . startChar ) , token . start + 1 ) ;
72
+ stringToTest = lineToStarChar . substring ( cm . getLine ( line ) . lastIndexOf ( currentEditorHelperHint . startChar ) , start ) ;
44
73
45
- if ( charBeforeStarChar != ' ' && charBeforeStarChar != '' ) {
74
+ if ( charBeforeStarChar != " " && charBeforeStarChar != "" ) {
46
75
return false ;
47
76
}
48
77
49
- var listCallback = currentEditorHelperHint . listCallback ( stringToTest ) ;
78
+ var callbackResult = currentEditorHelperHint . triggers [ currentTrigger ] ( stringToTest ) ;
50
79
51
- if ( listCallback . length == 0 ) {
80
+ if ( callbackResult . length == 0 ) {
52
81
return false ;
53
82
}
54
83
55
84
return {
56
- list : listCallback ,
85
+ list : callbackResult ,
57
86
from : CodeMirror . Pos ( cur . line , cm . getLine ( line ) . lastIndexOf ( currentEditorHelperHint . startChar ) ) ,
58
87
to : CodeMirror . Pos ( cur . line , end )
59
88
} ;
60
89
}
61
90
} ) ;
91
+
92
+ forEachHint ( function ( hint ) {
93
+ hint . addEventListener ( "mouseenter" , setHintActive ) ;
94
+ } ) ;
62
95
}
63
96
} ) ;
64
97
} ) ;
65
98
99
+
66
100
// Some variables
67
101
var isMac = / M a c / . test ( navigator . platform ) ;
68
102
@@ -1377,8 +1411,8 @@ function SimpleMDE(options) {
1377
1411
}
1378
1412
1379
1413
// Add custom toolbar actions
1380
- if ( options . additionalToolbarButtons !== undefined ) {
1381
- for ( var index in options . additionalToolbarButtons ) {
1414
+ if ( options . additionalToolbarButtons !== undefined ) {
1415
+ for ( var index in options . additionalToolbarButtons ) {
1382
1416
options . toolbar . push ( options . additionalToolbarButtons [ index ] ) ;
1383
1417
}
1384
1418
}
@@ -1548,7 +1582,7 @@ SimpleMDE.prototype.render = function(el) {
1548
1582
allowDropFileTypes : [ "text/plain" ] ,
1549
1583
placeholder : options . placeholder || el . getAttribute ( "placeholder" ) || "" ,
1550
1584
styleSelectedText : ( options . styleSelectedText != undefined ) ? options . styleSelectedText : true ,
1551
- autoSuggest : ( options . autoSuggest != undefined ) ? options . autoSuggest : NULL
1585
+ autoSuggest : ( options . autoSuggest != undefined ) ? options . autoSuggest : null ,
1552
1586
} ) ;
1553
1587
1554
1588
if ( options . forceSync === true ) {
@@ -2079,4 +2113,4 @@ SimpleMDE.prototype.toTextArea = function() {
2079
2113
}
2080
2114
} ;
2081
2115
2082
- module . exports = SimpleMDE ;
2116
+ module . exports = SimpleMDE ;
0 commit comments