forked from Q42/meteor-autoform-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.js
72 lines (60 loc) · 2.01 KB
/
markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
marked.setOptions({
breaks: true
});
Template.registerHelper('renderMarkdown', function renderMarkdown(value) {
if(value)
return marked(value);
});
Template.afMarkdown.created = function() {
this.markdownInput = new ReactiveVar('');
};
Template.afMarkdown.rendered = function() {
var textArea = Template.instance().$('.markdownInput');
Template.instance().markdownInput.set(textArea.val());
};
Template.afMarkdown.helpers({
markdownText: function() {
return marked(Template.instance().markdownInput.get());
}
});
Template.afMarkdown.events({
'keyup .markdownInput': function(evt, instance) {
instance.markdownInput.set(evt.currentTarget.value);
},
'click .select': function(evt, instance) {
evt.preventDefault();
var markdown = evt.currentTarget.className.split(' ')[0];
var textArea = Template.instance().$('.markdownInput');
var selectedText = textArea.selection();
switch (markdown) {
case 'markdownBold':
textArea.selection('replace', { text: '**' + selectedText + '**' });
break;
case 'markdownItalic':
textArea.selection('replace', { text: '*' + selectedText + '*' });
break;
case 'markdownList':
var listitems = selectedText.split('\n');
var markdownList = "";
listitems.forEach(function (item) {
markdownList = markdownList + "- " + item + "\n";
});
textArea.selection('replace', {text: markdownList + "\n "});
break;
default:
break;
}
instance.markdownInput.set(textArea.val());
},
'change .markdownHeader': function(evt, instance) {
var header = $(evt.currentTarget).find('option:selected').val();
var textArea = Template.instance().$('.markdownInput');
var selectedText = textArea.selection();
textArea.selection('replace', { text: header + selectedText });
$(evt.currentTarget).prop('selectedIndex',0);
instance.markdownInput.set(textArea.val());
}
});
AutoForm.addInputType("markdown", {
template: "afMarkdown",
});