-
Notifications
You must be signed in to change notification settings - Fork 1
/
markit.js
75 lines (73 loc) · 3.1 KB
/
markit.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
function markit(textareas) {
function bindEvent(element, type, handler) {
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
element.attachEvent('on'+type, handler);
}
}
if (textareas.length === undefined) {
textareas = [textareas];
}
var i = textareas.length;
while (i--) {
var textarea = textareas[i];
if (typeof textarea.selectionStart == 'number') {
var formatting = document.createElement('div');
formatting.className = 'markit-formats';
formatting.innerHTML = '<a class="markit-bold" href="#">Bold</a> <a class="markit-italic" href="#">Italic</a> <a class="markit-link" href="#">Link</a> <a class="markit-list" href="#">List</a>';
bindEvent(formatting, 'click', function(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) targ = targ.parentNode;
if (targ.tagName == 'A') {
var formats = [];
//formats[type] = [prepend, append];
formats['bold'] = ['**', '**'];
formats['italic'] = ['_', '_'];
formats['link'] = ['[', ']('];
formats['list'] = ['*', ''];
var type = targ.className.replace('markit-', ''),
before = formats[type][0],
after = formats[type][1],
beforeLen = before.length,
afterLen = after.length;
if (type == 'link') {
after += prompt('Enter the link URL', 'http://')+')';
}
var original = textarea.value,
start = textarea.selectionStart,
end = textarea.selectionEnd,
selected;
if (start == end) {
selected = type;
} else {
selected = original.substring(start, end);
}
if (type == 'list') {
if (start > 0 && original.charAt(start-1) != "\n") {
before = "\n"+before;
}
} else {
if (start > 0 && original.charAt(start-1) != ' ') {
before = ' '+before;
}
if (original.charAt(end) != ' ') {
after = after+' ';
}
}
textarea.value = original.substr(0, start)+before+selected+after+original.substring(end, textarea.length);
textarea.selectionStart = start+before.length;
textarea.selectionEnd = start+before.length+selected.length;
e.preventDefault();
}
});
textarea.parentNode.insertBefore(formatting, textarea);
}
}
}
if (document.querySelectorAll) {
markit(document.querySelectorAll('.markit'));
}