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