This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
/
default.js
261 lines (230 loc) · 7.73 KB
/
default.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var strings = {
// Titles
"import-button-title-plain": "Import a text file from your system",
"import-button-title-textile": "Import a Textile file from your system",
"import-button-title-markdown": "Import a Markdown file from your system",
// Menus
"export-text-plain": "Text file",
"export-text-textile": "Textile file",
"export-text-markdown": "Markdown file"
}
var renderMode; // Current parser being used
var scrollPos = 0; // Persist scroll position of the preview mode
var editor;
var markdownConverter;
////////////////////////////////////////////////////////////////////////////////////////////////////
/* Parse written content and update the preview panel */
function render(val) {
switch (renderMode)
{
case "plain":
val = val.replace(/\r\n|\r|\n/g, "<br />"); // Newlines
val = val.replace(/ /g, " "); // Whitespaces
$('#paper').html("<p></p>" + val);
$('#import-button').attr("title", strings['import-button-title-plain']);
$('#export-text').text(strings['export-text-plain']);
break;
case "textile":
editor.setParser('TextileParser');
$('#paper').html(textile(val));
$('#import-button').attr("title", strings['import-button-title-textile']);
$('#export-text').text(strings['export-text-textile']);
break;
case "markdown":
editor.setParser('MarkdownParser');
$('#paper').html(markdownConverter.makeHtml(val));
$('#import-button').attr("title", strings['import-button-title-markdown']);
$('#export-text').text(strings['export-text-markdown']);
break;
case "latex":
break;
}
}
function previewOn() {
$('.editor').hide();
$('#preview').show();
render(editor.getCode());
$('#preview').css('top', '0px');
$('#preview').focus();
// Return to saved scroll position of the preview mode
$('#preview').attr({ scrollTop: scrollPos });
}
function writeOn() {
// Save scroll position of the preview mode
scrollPos = $('#preview').attr('scrollTop');
$('#preview').hide();
$('.editor').show();
$('.editor').focus();
}
/* Text file drag-and-drop */
function dropFile() {
if (typeof window.FileReader === 'undefined') {
alert('fail');
}
$("#preview").bind('dragover', function() {
$("#paper").addClass('hover');
return false;
}).bind("dragend", function() {
$("#paper").removeClass('hover');
return false;
}).bind("drop", function (e) {
$("#paper").removeClass('hover');
e.stopPropagation();
e.preventDefault();
var file = e.originalEvent.dataTransfer.files[0], reader = new FileReader();
reader.onload = function (event) {
editor.setCode(event.target.result);
render(event.target.result);
};
reader.readAsText(file);
return false;
});
}
/* Save written content */
function saveText() {
var uriContent = "data:application/octet-stream," + encodeURIComponent(editor.getCode());
document.location.href = uriContent;
}
function saveHtml() {
var attrs = [
" <style type=\"text/css\">",
" body { margin: 50px; font: 16px 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; }",
" p { text-align: justify; }",
" a { color: #261a3b; }",
" a:visited { color: #261a3b; }",
" pre, tt, code { font: 12px Menlo, Monaco, Consolas, 'Lucida Console', monospace; }",
" </style>"
];
var uriContent = "data:application/octet-stream," +
encodeURIComponent("<html>\n<head>\n"+ attrs.join("\n") +"\n<head>\n<body>\n"+ $('#paper').html() +"\n</body>\n</html>\n");
document.location.href = uriContent;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/* Tooltips (http://onehackoranother.com/projects/jquery/tipsy) */
function initTipsy() {
$('#import-button').tipsy({
opacity: 1,
trigger: 'manual'
});
$('#import-button').bind('mouseenter',
function(e) {
var allMenus = $('.opener'); // Check if there is a menu opened
if (allMenus.filter('.open').length != 0) {
// Hide all opened menus but add a class to enable linked menus
allMenus.each(function() {
hideWindow($(this));
$(this).addClass('menu-link');
});
}
$(this).tipsy("show");
});
$('#import-button').bind('mouseleave',
function(e) {
$(this).tipsy("hide");
});
}
/* Manage click events on the language menu */
function initLanguageMenu() {
$('.language').click(function() {
$('.language').each(function() {
$(this).text($(this).text().replace(' ✓', ''));
})
$(this).append(" ✓");
switch (this.id)
{
case "language-plain":
renderMode = "plain";
break;
case "language-textile":
renderMode = "textile";
break;
case "language-markdown":
renderMode = "markdown";
break;
case "language-latex":
renderMode = "latex";
break;
}
render(editor.getCode());
});
}
/* Manage click events on the export menu */
function initExportMenu() {
$('.export').click(function() {
switch (this.id)
{
case "export-text":
saveText();
break;
case "export-html":
saveHtml();
break;
case "export-pdf":
break;
}
});
}
/* Handlers to show & hide toolbar */
function initToolbar() {
$('#toolbar-area').mouseover(function() {
$('#toolbar').slideDown('fast');
});
$(editor.win.document).click(function() {
setTimeout(function() {
$('#toolbar').slideUp('fast');
},
500);
});
$('#preview').click(function() {
setTimeout(function() {
$('#toolbar').slideUp('fast');
},
500);
});
}
/* While codemirror is being loaded */
function whileLoading() {
$('.editor').hide(); // Hide editor panel
initTipsy();
markdownConverter = new Showdown.converter();
renderMode = "textile"; // Current parser being used
$('#language-textile').append(" ✓"); // Update the language menu
// jQuery uniform controls (http://pixelmatrixdesign.com/uniform)
$("select, input:checkbox, input:radio, input:file").uniform();
// Hide all opened menus when clicking anywhere
$(document).click(function(e) {
var allMenus = $('.opener');
allMenus.each(function() {
hideWindow($(this));
});
$("#paper").removeClass('hover');
});
initLanguageMenu(); // Manage click events on the language menu
initExportMenu(); // Manage click events on the export menu
dropFile(); // Allow drag-and-drop of text files
}
/* When codemirror is ready */
function init() {
render($('#code').val()); // Render the current example text
initToolbar(); // Handlers to show & hide toolbar
// Iphone switch
$('#switch').iphoneSwitch("on", previewOn, writeOn,
editor.win.document, { switch_path: 'images/switch.png' });
}
$(document).ready(function()
{
// Write panel
editor = CodeMirror.fromTextArea('code', {
parserfile: ["javascripts/modes/markdown.js", "javascripts/modes/textile.js"],
stylesheet: "stylesheets/modes/default.css",
basefiles: ["javascripts/codemirror/codemirror_base.js"],
textWrapping: true,
saveFunction: saveText,
indentUnit: 4,
parserConfig: { 'strictErrors': true },
iframeClass: "editor",
height: '100%',
initCallback: init
});
whileLoading();
});