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
|
//==============================================================================
// Elements which contains the location of the previous and next site
//==============================================================================
var prev = document.getElementById("prev-site");
var next = document.getElementById("next-site");
//==============================================================================
// Handle arrow keys and change the location to the desired direction
//==============================================================================
document.addEventListener("keyup", function(event) {
if(!event.ctrlKey && !event.shiftKey) {
(event.keyCode === 37 && prev) && (window.location.href = prev.getAttribute("href"));
(event.keyCode === 39 && next) && (window.location.href = next.getAttribute("href"));
}
}, false);
//==============================================================================
// Markdown tags to replace
//==============================================================================
var markdownTags = {
"bold": ["**", "**"],
"italic": ["*", "*"],
"heading": ["## ", "\n"],
"link": ["[", "](href)"],
"image": ["![", "](href)"],
"code": ["\n~~~\n", "\n~~~\n"],
"quote": ["\n> ", ""],
"list_ul": ["* ", ""],
"list_ol": ["1. ", ""]
};
//==============================================================================
// Set caret position in editor
//==============================================================================
function setCaretPosition(position) {
window.setTimeout(function() {
document.getElementById("content-editor").focus();
document.getElementById("content-editor").setSelectionRange(position, position);
}, 50);
}
//==============================================================================
// Insert markdown around text in editor
//==============================================================================
function markdownReplace(tagname) {
var element = document.activeElement;
if(element.nodeName === 'TEXTAREA') {
var selectionStart = element.selectionStart;
var selectionEnd = element.selectionEnd;
var selectedText = element.value.substring(selectionStart, selectionEnd);
var content = element.value;
element.value = content.slice(0, selectionStart) + markdownTags[tagname][0] + selectedText + markdownTags[tagname][1] + content.slice(selectionEnd);
setCaretPosition(selectionStart + markdownTags[tagname][0].length + selectedText.length + markdownTags[tagname][1].length);
}
}
//==============================================================================
// Insert emoticon after cursor in editor
//==============================================================================
function emoticonReplace(emoticon) {
var element = document.activeElement;
if(element.nodeName === 'TEXTAREA') {
var selectionStart = element.selectionStart;
var selectionEnd = element.selectionEnd;
var content = element.value;
element.value = content.slice(0, selectionStart) + emoticon + content.slice(selectionEnd);
setCaretPosition(selectionStart + emoticon.length);
}
}
//==============================================================================
// Keep server-side session active if the user is writing a long text
//==============================================================================
addEventListener("DOMContentLoaded", function() {
setInterval(function() {
var Request = new XMLHttpRequest();
Request.open("HEAD", "", true);
Request.send();
}, 300000);
}, false);
//==============================================================================
// Insert tab indent into editor if <tab> is pressed
//==============================================================================
addEventListener("DOMContentLoaded", function() {
if(document.getElementById("content-editor")) {
var element = document.getElementById("content-editor");
element.addEventListener('keydown', function(e) {
if(e.keyCode === 9 && !e.ctrlKey && !e.shiftKey) {
var selectionStart = element.selectionStart;
var selectionEnd = element.selectionEnd;
var content = element.value;
element.value = content.substring(0, selectionStart) + "\t" + content.substring(selectionEnd);
setCaretPosition(selectionStart + 1);
e.preventDefault();
}
}, false);
}
}, false);
//==============================================================================
// Confirmation message for delete buttons
//==============================================================================
addEventListener("DOMContentLoaded", function() {
if(document.getElementById("delete-button")) {
document.getElementById("delete-button").onclick = function(e) {
return confirm(e.target.getAttribute('data-text'));
};
}
}, false);
//==============================================================================
// Emoticon listener
//==============================================================================
addEventListener("DOMContentLoaded", function() {
if(document.getElementById("emoticon-list")) {
var list = document.getElementById("emoticon-list");
var items = list.getElementsByTagName("li");
for(var i = 0; i < items.length; ++i) {
items[i].onmousedown = function(e) {
emoticonReplace(e.target.getAttribute('data-emoticon'));
};
}
}
}, false);
//==============================================================================
// Markdown listener
//==============================================================================
addEventListener("DOMContentLoaded", function() {
if(document.getElementById("markdown-list")) {
var list = document.getElementById("markdown-list");
var items = list.getElementsByTagName("li");
for(var i = 0; i < items.length; ++i) {
items[i].onmousedown = function(e) {
markdownReplace(e.target.getAttribute('data-markdown'));
};
}
}
}, false);
|