Skip to content

Commit 56542fc

Browse files
committed
changed markdown editor to ace editor
1 parent bbd48c6 commit 56542fc

22 files changed

+2144
-2656
lines changed
Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
1-
form .uk-htmleditor-navbar-nav,
2-
form .aligned ul.uk-htmleditor-navbar-nav {
3-
margin: 0;
4-
padding: 0;
5-
list-style: none;
6-
float: left;
1+
.main-draceditor {
2+
max-width: 100%;
3+
position: relative;
4+
margin: 10px 0;
5+
max-height: 700px;
6+
}
7+
.draceditor {
8+
height: 500px;
9+
max-height: 500px;
10+
}
11+
#editor {
12+
margin: 0;
13+
position: absolute;
14+
top: 0;
15+
bottom: 0;
16+
left: 0;
17+
right: 50%;
18+
}
19+
20+
#preview {
21+
margin: 0;
22+
padding: 0.5rem;
23+
position: absolute;
24+
top: 0;
25+
bottom: 0;
26+
left: 50%;
27+
right: 0;
28+
overflow: auto;
29+
background: #f0f0f0
30+
}
31+
32+
@media print {
33+
#editor {
34+
display: none;
35+
}
36+
37+
#preview {
38+
left: 0;
39+
}
40+
}
41+
42+
.marked-emoji {
43+
max-width: 20px;
744
}

draceditor/static/js/draceditor.js

Lines changed: 72 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
// Markdown Emoji
6969
// require `atwho/atwho.min.js` and list `emojis` from `atwho/emojis.min.js`
70-
var onEmoji = function() {
70+
var onEmoji = function(textarea) {
7171
$emojis = emojis; // from `atwho/emojis.min.js`
7272
var emojiurl = draceditor.data('base-emoji-url');
7373
var emoji_config = {
@@ -78,7 +78,7 @@
7878
delay: 400
7979
}
8080
// 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) {
8282
//$('.markdownx').markdownx();
8383
});
8484
}
@@ -114,84 +114,12 @@
114114
});
115115
};
116116

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-
169117
var onKeyUpEvent = function(e) {
170118
console.log(e);
171119
onMention();
172120
onEmoji();
173121
}
174122

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-
195123
var draceditor = $(this);
196124
var dracEditor = $(this).find('.draceditor');
197125
dracEditor.on('keydown.draceditor', onKeyUpEvent);
@@ -203,32 +131,80 @@
203131
uploadFile(selector_upload);
204132
});
205133

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();
208204
};
209205

210206
$(function() {
211207
$.fn.atwho.debug = true;
212208
$('.draceditor').draceditor();
213209
});
214210
})(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-
*/

draceditor/static/plugins/css/ace.min.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)