Skip to content

Commit cae4bb0

Browse files
committed
add mentions
1 parent daf71aa commit cae4bb0

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

src/js/simplemde.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,102 @@
1+
/* eslint-disable no-useless-escape */
12
/*global require,module*/
23
"use strict";
34
var CodeMirror = require("codemirror");
45
require("codemirror/addon/edit/continuelist.js");
56
require("./codemirror/tablist");
67
require("codemirror/addon/display/fullscreen.js");
78
require("codemirror/mode/markdown/markdown.js");
8-
require("codemirror/addon/mode/overlay.js");
99
require("codemirror/addon/hint/show-hint.js");
10+
require("codemirror/addon/mode/overlay.js");
1011
require("codemirror/addon/display/placeholder.js");
1112
require("codemirror/addon/selection/mark-selection.js");
1213
require("codemirror/mode/gfm/gfm.js");
1314
require("codemirror/mode/xml/xml.js");
1415
var CodeMirrorSpellChecker = require("codemirror-spell-checker");
1516
var marked = require("marked");
1617

18+
1719
var currentEditorHelperHint;
1820

19-
CodeMirror.defineOption("autoSuggest", [], function(cm, autoSuggestOptions, old) {
21+
CodeMirror.defineOption("autoSuggest", [], function(cm, autoSuggestOptions) {
2022
cm.on("inputRead", function(cm, change) {
2123
var mode = cm.getModeAt(cm.getCursor());
2224

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) {
2455

2556
currentEditorHelperHint = autoSuggestOptions;
2657
currentEditorHelperHint.startChar = change.text[0];
2758

2859
cm.showHint({
2960
completeSingle: false,
3061
closeCharacters: /[\v()\[\]{};:>,]/,
31-
hint: function(cm, options) {
62+
className: "hints",
63+
hint: function(cm) {
3264
var cur = cm.getCursor(),
3365
token = cm.getTokenAt(cur);
3466
var start = token.start + 1,
3567
end = token.end;
3668

3769
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),
4271
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);
4473

45-
if(charBeforeStarChar != ' ' && charBeforeStarChar != '') {
74+
if(charBeforeStarChar != " " && charBeforeStarChar != "") {
4675
return false;
4776
}
4877

49-
var listCallback = currentEditorHelperHint.listCallback(stringToTest);
78+
var callbackResult = currentEditorHelperHint.triggers[currentTrigger](stringToTest);
5079

51-
if(listCallback.length == 0) {
80+
if(callbackResult.length == 0) {
5281
return false;
5382
}
5483

5584
return {
56-
list: listCallback,
85+
list: callbackResult,
5786
from: CodeMirror.Pos(cur.line, cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar)),
5887
to: CodeMirror.Pos(cur.line, end)
5988
};
6089
}
6190
});
91+
92+
forEachHint(function(hint) {
93+
hint.addEventListener("mouseenter", setHintActive);
94+
});
6295
}
6396
});
6497
});
6598

99+
66100
// Some variables
67101
var isMac = /Mac/.test(navigator.platform);
68102

@@ -1377,8 +1411,8 @@ function SimpleMDE(options) {
13771411
}
13781412

13791413
// 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) {
13821416
options.toolbar.push(options.additionalToolbarButtons[index]);
13831417
}
13841418
}
@@ -1548,7 +1582,7 @@ SimpleMDE.prototype.render = function(el) {
15481582
allowDropFileTypes: ["text/plain"],
15491583
placeholder: options.placeholder || el.getAttribute("placeholder") || "",
15501584
styleSelectedText: (options.styleSelectedText != undefined) ? options.styleSelectedText : true,
1551-
autoSuggest: (options.autoSuggest != undefined) ? options.autoSuggest : NULL
1585+
autoSuggest: (options.autoSuggest != undefined) ? options.autoSuggest : null,
15521586
});
15531587

15541588
if(options.forceSync === true) {
@@ -2079,4 +2113,4 @@ SimpleMDE.prototype.toTextArea = function() {
20792113
}
20802114
};
20812115

2082-
module.exports = SimpleMDE;
2116+
module.exports = SimpleMDE;

0 commit comments

Comments
 (0)