|
67 | 67 |
|
68 | 68 | // Markdown Emoji |
69 | 69 | // require `atwho/atwho.min.js` and list `emojis` from `atwho/emojis.min.js` |
70 | | - var onEmoji = function() { |
| 70 | + var onEmoji = function(textarea) { |
71 | 71 | $emojis = emojis; // from `atwho/emojis.min.js` |
72 | 72 | var emojiurl = draceditor.data('base-emoji-url'); |
73 | 73 | var emoji_config = { |
|
78 | 78 | delay: 400 |
79 | 79 | } |
80 | 80 | // Triger process if inserted: https://github.com/ichord/At.js/wiki/Events#insertedatwho |
81 | | - draceditor.atwho(emoji_config).on('inserted.atwho', function(event, flag, query) { |
| 81 | + textarea.atwho(emoji_config).on('inserted.atwho', function(event, flag, query) { |
82 | 82 | //$('.markdownx').markdownx(); |
83 | 83 | }); |
84 | 84 | } |
|
114 | 114 | }); |
115 | 115 | }; |
116 | 116 |
|
117 | | - /** |
118 | | - * The state of CodeMirror at the given position. |
119 | | - * https://github.com/lepture/editor |
120 | | - */ |
121 | | - var getState = function(cm, pos) { |
122 | | - pos = pos || cm.getCursor('start'); |
123 | | - var stat = cm.getTokenAt(pos); |
124 | | - if (!stat.type) return {}; |
125 | | - |
126 | | - var types = stat.type.split(' '); |
127 | | - |
128 | | - var ret = {}, data, text; |
129 | | - for (var i = 0; i < types.length; i++) { |
130 | | - data = types[i]; |
131 | | - if (data === 'strong') { |
132 | | - ret.bold = true; |
133 | | - } else if (data === 'variable-2') { |
134 | | - text = cm.getLine(pos.line); |
135 | | - if (/^\s*\d+\.\s/.test(text)) { |
136 | | - ret['ordered-list'] = true; |
137 | | - } else { |
138 | | - ret['unordered-list'] = true; |
139 | | - } |
140 | | - } else if (data === 'atom') { |
141 | | - ret.quote = true; |
142 | | - } else if (data === 'em') { |
143 | | - ret.italic = true; |
144 | | - } |
145 | | - } |
146 | | - return ret; |
147 | | - } |
148 | | - |
149 | | - var replaceSelection = function(cm, active, start, end) { |
150 | | - var text; |
151 | | - var startPoint = cm.getCursor('start'); |
152 | | - var endPoint = cm.getCursor('end'); |
153 | | - if (active) { |
154 | | - text = cm.getLine(startPoint.line); |
155 | | - start = text.slice(0, startPoint.ch); |
156 | | - end = text.slice(startPoint.ch); |
157 | | - cm.setLine(startPoint.line, start + end); |
158 | | - } else { |
159 | | - text = cm.getSelection(); |
160 | | - cm.replaceSelection(start + text + end); |
161 | | - |
162 | | - startPoint.ch += start.length; |
163 | | - endPoint.ch += start.length; |
164 | | - } |
165 | | - cm.setSelection(startPoint, endPoint); |
166 | | - cm.focus(); |
167 | | - } |
168 | | - |
169 | 117 | var onKeyUpEvent = function(e) { |
170 | 118 | console.log(e); |
171 | 119 | onMention(); |
172 | 120 | onEmoji(); |
173 | 121 | } |
174 | 122 |
|
175 | | - var timeout; |
176 | | - var update = function(e) { |
177 | | - console.log(e); |
178 | | - onMention(); |
179 | | - console.log(e.getValue()); |
180 | | - //clearTimeout(timeout); |
181 | | - //timeout = setTimeout(getMarkdown, 1000); |
182 | | - }; |
183 | | - |
184 | | - var editor; |
185 | | - setTimeout(function(){ |
186 | | - //$('.CodeMirror').attr({'contentEditable': 'true'}); |
187 | | - editor = $('.CodeMirror')[0].CodeMirror; |
188 | | - editor.on('change', update); |
189 | | - editor.on('keyup', function(cm, e) { |
190 | | - console.log(cm, e); |
191 | | - onMention(); |
192 | | - }); |
193 | | - }, 500); |
194 | | - |
195 | 123 | var draceditor = $(this); |
196 | 124 | var dracEditor = $(this).find('.draceditor'); |
197 | 125 | dracEditor.on('keydown.draceditor', onKeyUpEvent); |
|
203 | 131 | uploadFile(selector_upload); |
204 | 132 | }); |
205 | 133 |
|
206 | | - onMention(); |
207 | | - onEmoji(); |
| 134 | + // Ace editor |
| 135 | + var editor = ace.edit('editor'); |
| 136 | + editor.setTheme('ace/theme/github'); |
| 137 | + editor.getSession().setMode('ace/mode/markdown'); |
| 138 | + editor.$blockScrolling = Infinity; //prevents ace from logging annoying warnings |
| 139 | + /* |
| 140 | + editor.getSession().on('change', function () { |
| 141 | + draceditor.val(editor.getSession().getValue()); |
| 142 | + });*/ |
| 143 | + editor.setOptions({ |
| 144 | + enableBasicAutocompletion: true, |
| 145 | + enableSnippets: true, |
| 146 | + enableLiveAutocompletion: true |
| 147 | + }); |
| 148 | + |
| 149 | + // Ace autocomplete |
| 150 | + var langTools = ace.require('ace/ext/language_tools'); |
| 151 | + |
| 152 | + var emojiWordCompleter = { |
| 153 | + getCompletions: function(editor, session, pos, prefix, callback) { |
| 154 | + var wordList = emojis; // from `atwho/emojis.min.js` |
| 155 | + var obj = editor.getSession().getTokenAt(pos.row, pos.column.count); |
| 156 | + var curTokens = obj.value.split(/\s+/); |
| 157 | + var lastToken = curTokens[curTokens.length-1]; |
| 158 | + |
| 159 | + if (lastToken[0] == ':') { |
| 160 | + console.log(lastToken); |
| 161 | + callback(null, wordList.map(function(word) { |
| 162 | + return { |
| 163 | + caption: word, |
| 164 | + value: word.replace(':', '') + ' ', |
| 165 | + meta: 'emoji' // this should return as text only. |
| 166 | + }; |
| 167 | + })); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + editor.completers = [emojiWordCompleter] |
| 172 | + |
| 173 | + // update preview based on editor content |
| 174 | + var onRender = function(){ |
| 175 | + marked.setOptions({ |
| 176 | + renderer : markedRenderer, // require from `marked-emoji.js` |
| 177 | + gfm : true, |
| 178 | + tables : true, |
| 179 | + breaks : true, |
| 180 | + pedantic : false, |
| 181 | + smartLists : true, |
| 182 | + smartypants : true |
| 183 | + }); |
| 184 | + $('#preview').html( |
| 185 | + marked(editor.getValue()) |
| 186 | + ); |
| 187 | + $('pre').each(function(i, block){ |
| 188 | + hljs.highlightBlock(block); |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + //var container_area = $(editor.container); |
| 193 | + //container_area.attr({'contenteditable': 'true'}); |
| 194 | + |
| 195 | + editor.on('change', function(e){ |
| 196 | + //onEmoji(container_area); |
| 197 | + //onMention(editor); |
| 198 | + onRender(); |
| 199 | + }); |
| 200 | + |
| 201 | + // set initial value |
| 202 | + editor.setValue(draceditor.val()); |
| 203 | + onRender(); |
208 | 204 | }; |
209 | 205 |
|
210 | 206 | $(function() { |
211 | 207 | $.fn.atwho.debug = true; |
212 | 208 | $('.draceditor').draceditor(); |
213 | 209 | }); |
214 | 210 | })(jQuery); |
215 | | - |
216 | | -// Development mode: |
217 | | -// * http://stackoverflow.com/a/27417339/6396981 |
218 | | -// * http://cgit.drupalcode.org/uikitapi/tree/uikit/js/components/htmleditor.js?id=6b5fc5f8d767b7e2b70c44e7e01b949b89870d8f |
219 | | -/* |
220 | | -$(document).ready(function(){ |
221 | | - var textArea = document.getElementById('id_description'); |
222 | | - var editor = CodeMirror.fromTextArea(textArea, { |
223 | | - onKeyEvent: function(e , s){ |
224 | | - if (s.type == "keyup") { |
225 | | - console.log("test"); // this dosn't show anything |
226 | | - } |
227 | | - } |
228 | | - }); |
229 | | - console.log(editor); // this work well. |
230 | | - editor.on("keyup", function(editor, event) { |
231 | | - console.log(editor); // this dosn't show anything |
232 | | - }); |
233 | | -}); |
234 | | -*/ |
0 commit comments