|
44 | 44 | editorVersion += '&cachebreak=' + editorCacheBreakValue;
|
45 | 45 | }
|
46 | 46 |
|
| 47 | + function logMessage(message) { |
| 48 | + console.log('TopcoderPlugin::'+ message); |
| 49 | + } |
| 50 | + |
47 | 51 | function topcoderHandles(cm, option) {
|
48 | 52 | return new Promise(function(accept) {
|
49 | 53 | setTimeout(function() {
|
50 |
| - var cursor = cm.getCursor(), line = cm.getLine(cursor.line) |
| 54 | + var cursor = cm.getCursor(), line = cm.getLine(cursor.line); |
51 | 55 | var start = cursor.ch, end = cursor.ch
|
52 | 56 | while (start && /\w/.test(line.charAt(start - 1))) --start
|
53 | 57 | while (end < line.length && /\w/.test(line.charAt(end))) ++end
|
54 | 58 | var word = line.slice(start, end).toLowerCase();
|
55 | 59 |
|
56 |
| - console.log('word' + word + ', length:' + word.length); |
| 60 | + //logMessage('word' + word + ', length:' + word.length); |
| 61 | + |
57 | 62 | if(word.length > 1) {
|
58 |
| - console.log('word' + word); |
59 | 63 | $.ajax({
|
60 | 64 | type: "GET",
|
61 | 65 | url: "/api/v2/topcoder?handle=" + word,
|
62 | 66 | cache: false,
|
63 | 67 | success: function (data) {
|
64 | 68 | var result = [];
|
65 | 69 | $.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 +")", |
67 | 71 | className: 'Username'});
|
68 | 72 | });
|
69 | 73 | return accept({
|
|
93 | 97 | }
|
94 | 98 |
|
95 | 99 | function completeAfter(cm, pred) {
|
96 |
| - if (!pred || pred()) { |
| 100 | + if (!pred || pred()) { |
97 | 101 | setTimeout(function () {
|
98 | 102 | 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 | + } |
100 | 116 | }
|
101 |
| - }, 100); |
| 117 | + }, 500); |
102 | 118 | }
|
103 | 119 | return CodeMirror.Pass;
|
104 | 120 | }
|
|
116 | 132 | var editor = new EasyMDE({
|
117 | 133 | shortcuts: {
|
118 | 134 | "mentions":"Ctrl-Space",
|
119 |
| - "mentions":"'@'" |
120 | 135 | },
|
121 | 136 | autofocus: false,
|
122 | 137 | forceSync: true, // true, force text changes made in EasyMDE to be immediately stored in original text area.
|
|
153 | 168 | // showIcons: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar.
|
154 | 169 | // sideBySideFullscreen: If set to false, allows side-by-side editing without going into fullscreen. Defaults to true.
|
155 | 170 | //theme: Override the theme. Defaults to easymde.
|
156 |
| - |
157 | 171 | });
|
158 | 172 |
|
159 | 173 | // forceSync = true, need to clear form after async requests
|
160 | 174 | $currentEditableTextarea.closest('form').on('complete', function(frm, btn) {
|
161 | 175 | editor.codemirror.setValue('');
|
162 | 176 | });
|
| 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 | + }); |
163 | 202 | }
|
164 | 203 | } //editorInit
|
165 | 204 |
|
|
0 commit comments