Skip to content

Commit

Permalink
add mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyleu committed Jun 19, 2018
1 parent 19582c5 commit 2565375
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions src/js/simplemde.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,102 @@
/* eslint-disable no-useless-escape */
/*global require,module*/
"use strict";
var CodeMirror = require("codemirror");
require("codemirror/addon/edit/continuelist.js");
require("./codemirror/tablist");
require("codemirror/addon/display/fullscreen.js");
require("codemirror/mode/markdown/markdown.js");
require("codemirror/addon/mode/overlay.js");
require("codemirror/addon/hint/show-hint.js");
require("codemirror/addon/mode/overlay.js");
require("codemirror/addon/display/placeholder.js");
require("codemirror/addon/selection/mark-selection.js");
require("codemirror/mode/gfm/gfm.js");
require("codemirror/mode/xml/xml.js");
var CodeMirrorSpellChecker = require("codemirror-spell-checker");
var marked = require("marked");


var currentEditorHelperHint;

CodeMirror.defineOption("autoSuggest", [], function(cm, autoSuggestOptions, old) {
CodeMirror.defineOption("autoSuggest", [], function(cm, autoSuggestOptions) {
cm.on("inputRead", function(cm, change) {
var mode = cm.getModeAt(cm.getCursor());

if(mode.name === autoSuggestOptions.mode && autoSuggestOptions.startChars.indexOf(change.text[0]) != -1) {
var currentTrigger = undefined;
for(var trigger in autoSuggestOptions.triggers) {
if(trigger === change.text[0]) {
currentTrigger = trigger;
}
}

var forEachHint = function(action) {
var hintsElement = document.querySelector(".CodeMirror-hints");
if(hintsElement) {
var hints = hintsElement.querySelectorAll(".CodeMirror-hint");
for(var i = 0; i < hints.length; i++) {
var hint = hints[i];
action(hint);
}
}
};

var setHintActive = function(event) {
forEachHint(function(hint) {
hint.classList.remove("CodeMirror-hint-active");
});
event.target.classList.add("CodeMirror-hint-active");
};

forEachHint(function(hint) {
hint.removeEventListener("mouseenter", setHintActive);
});

if(mode.name === autoSuggestOptions.mode && currentTrigger) {

currentEditorHelperHint = autoSuggestOptions;
currentEditorHelperHint.startChar = change.text[0];

cm.showHint({
completeSingle: false,
closeCharacters: /[\v()\[\]{};:>,]/,
hint: function(cm, options) {
className: "hints",
hint: function(cm) {
var cur = cm.getCursor(),
token = cm.getTokenAt(cur);
var start = token.start + 1,
end = token.end;

var line = cm.getCursor().line,
ch = cm.getCursor().ch,
stringToMatch = currentEditorHelperHint.startChar,
n = stringToMatch.length,
lineToStarChar = cm.getLine(line).substring(0, token.start + 1),
lineToStarChar = cm.getLine(line).substring(0, start),
charBeforeStarChar = lineToStarChar.substring(cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar) - 1, cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar)),
stringToTest = lineToStarChar.substring(cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar), token.start + 1);
stringToTest = lineToStarChar.substring(cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar), start);

if(charBeforeStarChar != ' ' && charBeforeStarChar != '') {
if(charBeforeStarChar != " " && charBeforeStarChar != "") {
return false;
}

var listCallback = currentEditorHelperHint.listCallback(stringToTest);
var callbackResult = currentEditorHelperHint.triggers[currentTrigger](stringToTest);

if(listCallback.length == 0) {
if(callbackResult.length == 0) {
return false;
}

return {
list: listCallback,
list: callbackResult,
from: CodeMirror.Pos(cur.line, cm.getLine(line).lastIndexOf(currentEditorHelperHint.startChar)),
to: CodeMirror.Pos(cur.line, end)
};
}
});

forEachHint(function(hint) {
hint.addEventListener("mouseenter", setHintActive);
});
}
});
});


// Some variables
var isMac = /Mac/.test(navigator.platform);

Expand Down Expand Up @@ -1377,8 +1411,8 @@ function SimpleMDE(options) {
}

// Add custom toolbar actions
if (options.additionalToolbarButtons !== undefined) {
for (var index in options.additionalToolbarButtons) {
if(options.additionalToolbarButtons !== undefined) {
for(var index in options.additionalToolbarButtons) {
options.toolbar.push(options.additionalToolbarButtons[index]);
}
}
Expand Down Expand Up @@ -1548,7 +1582,7 @@ SimpleMDE.prototype.render = function(el) {
allowDropFileTypes: ["text/plain"],
placeholder: options.placeholder || el.getAttribute("placeholder") || "",
styleSelectedText: (options.styleSelectedText != undefined) ? options.styleSelectedText : true,
autoSuggest: (options.autoSuggest != undefined) ? options.autoSuggest : NULL
autoSuggest: (options.autoSuggest != undefined) ? options.autoSuggest : null,
});

if(options.forceSync === true) {
Expand Down Expand Up @@ -2079,4 +2113,4 @@ SimpleMDE.prototype.toTextArea = function() {
}
};

module.exports = SimpleMDE;
module.exports = SimpleMDE;

0 comments on commit 2565375

Please sign in to comment.