Skip to content

Commit a38d3f5

Browse files
authored
Merge pull request #2 from topcoder-platform/issue-303
Fixed autocomplete event and displaying '@'
2 parents 73456c1 + 4901e9f commit a38d3f5

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

js/topcodereditor.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,30 @@
4444
editorVersion += '&cachebreak=' + editorCacheBreakValue;
4545
}
4646

47+
function logMessage(message) {
48+
console.log('TopcoderPlugin::'+ message);
49+
}
50+
4751
function topcoderHandles(cm, option) {
4852
return new Promise(function(accept) {
4953
setTimeout(function() {
50-
var cursor = cm.getCursor(), line = cm.getLine(cursor.line)
54+
var cursor = cm.getCursor(), line = cm.getLine(cursor.line);
5155
var start = cursor.ch, end = cursor.ch
5256
while (start && /\w/.test(line.charAt(start - 1))) --start
5357
while (end < line.length && /\w/.test(line.charAt(end))) ++end
5458
var word = line.slice(start, end).toLowerCase();
5559

56-
console.log('word' + word + ', length:' + word.length);
60+
//logMessage('word' + word + ', length:' + word.length);
61+
5762
if(word.length > 1) {
58-
console.log('word' + word);
5963
$.ajax({
6064
type: "GET",
6165
url: "/api/v2/topcoder?handle=" + word,
6266
cache: false,
6367
success: function (data) {
6468
var result = [];
6569
$.each(data, function (i, item) {
66-
result.push({text:'@' + data[i].handle, displayText: data[i].handle+ "("+ data[i].firstName + ' ' + data[i].lastName +")",
70+
result.push({text: data[i].handle, displayText: data[i].handle+ "("+ data[i].firstName + ' ' + data[i].lastName +")",
6771
className: 'Username'});
6872
});
6973
return accept({
@@ -93,12 +97,24 @@
9397
}
9498

9599
function completeAfter(cm, pred) {
96-
if (!pred || pred()) {
100+
if (!pred || pred()) {
97101
setTimeout(function () {
98102
if (!cm.state.completionActive) {
99-
cm.showHint({ completeSingle: false, alignWithWord: true });
103+
var currentLine = cm.getCursor().line;
104+
if (cm.getCursor().ch === 0) {
105+
cm.replaceSelection("@");
106+
cm.showHint({ completeSingle: false, alignWithWord: true });
107+
} else {
108+
var from = { line: cm.getCursor().line, ch: 0};
109+
var to = cm.getCursor()
110+
var line = cm.getRange(from , to);
111+
var lastIndexOf = line.lastIndexOf(' ');
112+
var tokenIndex = lastIndexOf > -1 ? lastIndexOf+1 : 0;
113+
cm.replaceRange("@", {line: cm.getCursor().line, ch: tokenIndex});
114+
cm.showHint({ completeSingle: false, alignWithWord: true });
115+
}
100116
}
101-
}, 100);
117+
}, 500);
102118
}
103119
return CodeMirror.Pass;
104120
}
@@ -116,7 +132,6 @@
116132
var editor = new EasyMDE({
117133
shortcuts: {
118134
"mentions":"Ctrl-Space",
119-
"mentions":"'@'"
120135
},
121136
autofocus: false,
122137
forceSync: true, // true, force text changes made in EasyMDE to be immediately stored in original text area.
@@ -153,13 +168,37 @@
153168
// showIcons: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar.
154169
// sideBySideFullscreen: If set to false, allows side-by-side editing without going into fullscreen. Defaults to true.
155170
//theme: Override the theme. Defaults to easymde.
156-
157171
});
158172

159173
// forceSync = true, need to clear form after async requests
160174
$currentEditableTextarea.closest('form').on('complete', function(frm, btn) {
161175
editor.codemirror.setValue('');
162176
});
177+
178+
editor.codemirror.on('change', function (cm, changeObj){
179+
// logMessage('onChange:'+cm.getCursor().ch);
180+
});
181+
182+
editor.codemirror.on('keydown', function (cm, event){
183+
if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) {
184+
if(event.key == '@') {
185+
var currentCursorPosition = cm.getCursor();
186+
if(currentCursorPosition.ch === 0) {
187+
cm.showHint({ completeSingle: false, alignWithWord: true });
188+
return;
189+
}
190+
191+
var backwardCursorPosition = {
192+
line: currentCursorPosition.line,
193+
ch: currentCursorPosition.ch - 1
194+
};
195+
var backwardCharacter = cm.getRange(backwardCursorPosition, currentCursorPosition);
196+
if (backwardCharacter === ' ') { // space
197+
cm.showHint({ completeSingle: false, alignWithWord: true });
198+
}
199+
}
200+
}
201+
});
163202
}
164203
} //editorInit
165204

0 commit comments

Comments
 (0)