forked from oodavid/sparkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparkdown.js
178 lines (176 loc) · 5.25 KB
/
sparkdown.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* jQuery SPARKDOWN Plugin
*
* @author David King
* @url http://oodavid.com
* @license MIT http://www.opensource.org/licenses/mit-license.php
*/
(function($){
$.fn.sparkdown = function(tools){
// Default the tools
tools = tools || 'bold italic | h1 h2 h3 | ul ol | img url | code | hr | help | preview'
tools = tools.split(' ');
// Create a toolbar
var toolbar = $('<div class="sparkdown-toolbar" />');
// Add the tools
$.each(tools, function(k,v){
var tool = $('<span />').html(v).attr('data-sparkdown-action', v).addClass(v == '|' ? 'sparkdown-spacer' : 'sparkdown-tool');
toolbar.append(tool);
});
// Add the click event
toolbar.on("click", ".sparkdown-tool", function(e){
// What's the "action"?
var action = $(this).attr('data-sparkdown-action');
// Find the textarea
var te = $(this).closest('.sparkdown').find('textarea');
//
// Break up the selection into parts
//
var parts = {
before: te.val().substring(0, te.caret().start),
selection: te.caret().text,
after: te.val().substring(te.caret().end)
};
//
// Do something to the parts
//
var marks = false;
switch(action){
//
// Bold and italic just deal with the immediate selection
//
case 'bold':
// Bold marks
marks = '**';
case 'italic':
// Italic marks
marks = marks || '*';
// We can either wrap, or unwrap, check before and after for the marks string
var wrapbefore = parts.before.substr(-marks.length) == marks;
var wrapafter = parts.after.substr(0, marks.length) == marks;
if(wrapbefore && wrapafter){
// Unwrap
parts.before = parts.before.substr(0, (parts.before.length-marks.length));
parts.after = parts.after.substr(marks.length);
} else {
// Wrap
parts.before += marks;
parts.after = marks + parts.after;
}
// Done
break;
//
// Headings, Lists and Code deal with the immediate selection and (possibly) the line-break beforehand
//
case 'h1':
// h1 marks
marks = '# ';
case 'h2':
// h2 marks
marks = marks || '## ';
case 'h3':
// h3 marks
marks = marks || '### ';
case 'ul':
// ul marks
marks = marks || '* ';
case 'ol':
// ol marks
marks = marks || '1. ';
case 'code':
// code marks
marks = marks || ' ';
// Prefix the selection with one of: # ## ### * 1.
console.log('TODO\n\nIn the selection, replace all \\n with \\n' + marks + '\nIf the selection DOESNT begin with a \\n...\n Replace the last \\n before the selection with a \\n' + marks);
// Done
break;
//
// Images and URLs replace the immediate selection
//
case 'img':
// Grab a URL
if(url=prompt('Image URL', 'http://')){
parts.selection = '![' + parts.selection + '](' + url + ')';
}
// Done
break;
case 'url':
// Grab a URL
if(url=prompt('Link URL', 'http://')){
parts.selection = '[' + parts.selection + '](' + url + ')';
}
// Done
break;
//
// Insert a horizontal rule...
//
case 'hr':
// Simples
parts.selection = '\n\n------\n\n';
// Done
break;
//
// Help just opens the documentation page
//
case 'help':
// Simples
window.open('http://daringfireball.net/projects/markdown/syntax');
// Done
break;
//
// Preview should swap out the textarea for a preview box
//
case 'preview':
console.log('Check for showdown... if it\'s there then use it, otherwise we must alert and exit');
// Find the preview
var preview = $(this).closest('.sparkdown').find('.sparkdown-preview');
// Is it visible?
if(preview.css('display') == 'block'){
// Hide it
preview.hide();
// Re-focus the textarea
te.focus();
} else {
// Convert the markdown and inject it
converter = new Showdown.converter();
html = converter.makeHtml(te.val());
preview.html(html);
// Show it
preview.show();
}
// Really Done
return false;
}
//
// Updating the textarea
//
// Set the value
te.val(parts.before + parts.selection + parts.after);
// Re-select
var start = parts.before.length;
var end = start + parts.selection.length;
te.caret({ start: start, end: end });
return false;
/*
LOGIC
I think I should do it in the old regex style of DFACE / DCODE / DLITE...
3rd party... Not sure how much I like these methods TBH...
Bold & Italic (toggle)
See: http://code.google.com/p/pagedown/source/browse/Markdown.Editor.js
commandProto.doBold
commandProto.doItalic
commandProto.doBorI
*/
});
// Loop the textarea / elements
return this.each(function(){
// Wrap this element
$(this).wrap('<div class="sparkdown" />');
// Clone the sparkdown toolbar and add it to the element
var toolbarclone = toolbar.clone(true);
$(this).parent().prepend(toolbarclone);
// Add a preview DIV (hidden)
$(this).parent().append($('<div class="sparkdown-preview" />').css('display', 'none'));
});
};
})(jQuery);