|
@@ -0,0 +1,673 @@
|
|
1
|
+// ----------------------------------------------------------------------------
|
|
2
|
+// markItUp! Universal MarkUp Engine, JQuery plugin
|
|
3
|
+// v 1.1.x
|
|
4
|
+// Dual licensed under the MIT and GPL licenses.
|
|
5
|
+// ----------------------------------------------------------------------------
|
|
6
|
+// Copyright (C) 2007-2012 Jay Salvat
|
|
7
|
+// http://markitup.jaysalvat.com/
|
|
8
|
+// ----------------------------------------------------------------------------
|
|
9
|
+// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+// of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+// in the Software without restriction, including without limitation the rights
|
|
12
|
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+// copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+// furnished to do so, subject to the following conditions:
|
|
15
|
+//
|
|
16
|
+// The above copyright notice and this permission notice shall be included in
|
|
17
|
+// all copies or substantial portions of the Software.
|
|
18
|
+//
|
|
19
|
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
|
+// THE SOFTWARE.
|
|
26
|
+// ----------------------------------------------------------------------------
|
|
27
|
+(function($) {
|
|
28
|
+ $.fn.markItUp = function(settings, extraSettings) {
|
|
29
|
+ var method, params, options, ctrlKey, shiftKey, altKey; ctrlKey = shiftKey = altKey = false;
|
|
30
|
+
|
|
31
|
+ if (typeof settings == 'string') {
|
|
32
|
+ method = settings;
|
|
33
|
+ params = extraSettings;
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ options = { id: '',
|
|
37
|
+ nameSpace: '',
|
|
38
|
+ root: '',
|
|
39
|
+ previewHandler: false,
|
|
40
|
+ previewInWindow: '', // 'width=800, height=600, resizable=yes, scrollbars=yes'
|
|
41
|
+ previewInElement: '',
|
|
42
|
+ previewAutoRefresh: true,
|
|
43
|
+ previewPosition: 'after',
|
|
44
|
+ previewTemplatePath: '~/templates/preview.html',
|
|
45
|
+ previewParser: false,
|
|
46
|
+ previewParserPath: '',
|
|
47
|
+ previewParserVar: 'data',
|
|
48
|
+ previewParserAjaxType: 'POST',
|
|
49
|
+ resizeHandle: true,
|
|
50
|
+ beforeInsert: '',
|
|
51
|
+ afterInsert: '',
|
|
52
|
+ onEnter: {},
|
|
53
|
+ onShiftEnter: {},
|
|
54
|
+ onCtrlEnter: {},
|
|
55
|
+ onTab: {},
|
|
56
|
+ markupSet: [ { /* set */ } ]
|
|
57
|
+ };
|
|
58
|
+ $.extend(options, settings, extraSettings);
|
|
59
|
+
|
|
60
|
+ // compute markItUp! path
|
|
61
|
+ if (!options.root) {
|
|
62
|
+ $('script').each(function(a, tag) {
|
|
63
|
+ var miuScript = $(tag).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/);
|
|
64
|
+ if (miuScript !== null) {
|
|
65
|
+ options.root = miuScript[1];
|
|
66
|
+ }
|
|
67
|
+ });
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ // Quick patch to keep compatibility with jQuery 1.9
|
|
71
|
+ var uaMatch = function(ua) {
|
|
72
|
+ ua = ua.toLowerCase();
|
|
73
|
+
|
|
74
|
+ var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
|
|
75
|
+ /(webkit)[ \/]([\w.]+)/.exec(ua) ||
|
|
76
|
+ /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
|
|
77
|
+ /(msie) ([\w.]+)/.exec(ua) ||
|
|
78
|
+ ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
|
|
79
|
+ [];
|
|
80
|
+
|
|
81
|
+ return {
|
|
82
|
+ browser: match[ 1 ] || "",
|
|
83
|
+ version: match[ 2 ] || "0"
|
|
84
|
+ };
|
|
85
|
+ };
|
|
86
|
+ var matched = uaMatch( navigator.userAgent );
|
|
87
|
+ var browser = {};
|
|
88
|
+
|
|
89
|
+ if (matched.browser) {
|
|
90
|
+ browser[matched.browser] = true;
|
|
91
|
+ browser.version = matched.version;
|
|
92
|
+ }
|
|
93
|
+ if (browser.chrome) {
|
|
94
|
+ browser.webkit = true;
|
|
95
|
+ } else if (browser.webkit) {
|
|
96
|
+ browser.safari = true;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ return this.each(function() {
|
|
100
|
+ var $$, textarea, levels, scrollPosition, caretPosition, caretOffset,
|
|
101
|
+ clicked, hash, header, footer, previewWindow, template, iFrame, abort;
|
|
102
|
+ $$ = $(this);
|
|
103
|
+ textarea = this;
|
|
104
|
+ levels = [];
|
|
105
|
+ abort = false;
|
|
106
|
+ scrollPosition = caretPosition = 0;
|
|
107
|
+ caretOffset = -1;
|
|
108
|
+
|
|
109
|
+ options.previewParserPath = localize(options.previewParserPath);
|
|
110
|
+ options.previewTemplatePath = localize(options.previewTemplatePath);
|
|
111
|
+
|
|
112
|
+ if (method) {
|
|
113
|
+ switch(method) {
|
|
114
|
+ case 'remove':
|
|
115
|
+ remove();
|
|
116
|
+ break;
|
|
117
|
+ case 'insert':
|
|
118
|
+ markup(params);
|
|
119
|
+ break;
|
|
120
|
+ default:
|
|
121
|
+ $.error('Method ' + method + ' does not exist on jQuery.markItUp');
|
|
122
|
+ }
|
|
123
|
+ return;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ // apply the computed path to ~/
|
|
127
|
+ function localize(data, inText) {
|
|
128
|
+ if (inText) {
|
|
129
|
+ return data.replace(/("|')~\//g, "$1"+options.root);
|
|
130
|
+ }
|
|
131
|
+ return data.replace(/^~\//, options.root);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ // init and build editor
|
|
135
|
+ function init() {
|
|
136
|
+ id = ''; nameSpace = '';
|
|
137
|
+ if (options.id) {
|
|
138
|
+ id = 'id="'+options.id+'"';
|
|
139
|
+ } else if ($$.attr("id")) {
|
|
140
|
+ id = 'id="markItUp'+($$.attr("id").substr(0, 1).toUpperCase())+($$.attr("id").substr(1))+'"';
|
|
141
|
+
|
|
142
|
+ }
|
|
143
|
+ if (options.nameSpace) {
|
|
144
|
+ nameSpace = 'class="'+options.nameSpace+'"';
|
|
145
|
+ }
|
|
146
|
+ $$.wrap('<div '+nameSpace+'></div>');
|
|
147
|
+ $$.wrap('<div '+id+' class="markItUp"></div>');
|
|
148
|
+ $$.wrap('<div class="markItUpContainer"></div>');
|
|
149
|
+ $$.addClass("markItUpEditor");
|
|
150
|
+
|
|
151
|
+ // add the header before the textarea
|
|
152
|
+ header = $('<div class="markItUpHeader"></div>').insertBefore($$);
|
|
153
|
+ $(dropMenus(options.markupSet)).appendTo(header);
|
|
154
|
+
|
|
155
|
+ // add the footer after the textarea
|
|
156
|
+ footer = $('<div class="markItUpFooter"></div>').insertAfter($$);
|
|
157
|
+
|
|
158
|
+ // add the resize handle after textarea
|
|
159
|
+ if (options.resizeHandle === true && browser.safari !== true) {
|
|
160
|
+ resizeHandle = $('<div class="markItUpResizeHandle"></div>')
|
|
161
|
+ .insertAfter($$)
|
|
162
|
+ .bind("mousedown.markItUp", function(e) {
|
|
163
|
+ var h = $$.height(), y = e.clientY, mouseMove, mouseUp;
|
|
164
|
+ mouseMove = function(e) {
|
|
165
|
+ $$.css("height", Math.max(20, e.clientY+h-y)+"px");
|
|
166
|
+ return false;
|
|
167
|
+ };
|
|
168
|
+ mouseUp = function(e) {
|
|
169
|
+ $("html").unbind("mousemove.markItUp", mouseMove).unbind("mouseup.markItUp", mouseUp);
|
|
170
|
+ return false;
|
|
171
|
+ };
|
|
172
|
+ $("html").bind("mousemove.markItUp", mouseMove).bind("mouseup.markItUp", mouseUp);
|
|
173
|
+ });
|
|
174
|
+ footer.append(resizeHandle);
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ // listen key events
|
|
178
|
+ $$.bind('keydown.markItUp', keyPressed).bind('keyup', keyPressed);
|
|
179
|
+
|
|
180
|
+ // bind an event to catch external calls
|
|
181
|
+ $$.bind("insertion.markItUp", function(e, settings) {
|
|
182
|
+ if (settings.target !== false) {
|
|
183
|
+ get();
|
|
184
|
+ }
|
|
185
|
+ if (textarea === $.markItUp.focused) {
|
|
186
|
+ markup(settings);
|
|
187
|
+ }
|
|
188
|
+ });
|
|
189
|
+
|
|
190
|
+ // remember the last focus
|
|
191
|
+ $$.bind('focus.markItUp', function() {
|
|
192
|
+ $.markItUp.focused = this;
|
|
193
|
+ });
|
|
194
|
+
|
|
195
|
+ if (options.previewInElement) {
|
|
196
|
+ refreshPreview();
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ // recursively build header with dropMenus from markupset
|
|
201
|
+ function dropMenus(markupSet) {
|
|
202
|
+ var ul = $('<ul></ul>'), i = 0;
|
|
203
|
+ $('li:hover > ul', ul).css('display', 'block');
|
|
204
|
+ $.each(markupSet, function() {
|
|
205
|
+ var button = this, t = '', title, li, j;
|
|
206
|
+ button.title ? title = (button.key) ? (button.title||'')+' [Ctrl+'+button.key+']' : (button.title||'') : title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
|
|
207
|
+ key = (button.key) ? 'accesskey="'+button.key+'"' : '';
|
|
208
|
+ if (button.separator) {
|
|
209
|
+ li = $('<li class="markItUpSeparator">'+(button.separator||'')+'</li>').appendTo(ul);
|
|
210
|
+ } else {
|
|
211
|
+ i++;
|
|
212
|
+ for (j = levels.length -1; j >= 0; j--) {
|
|
213
|
+ t += levels[j]+"-";
|
|
214
|
+ }
|
|
215
|
+ li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="#" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
|
|
216
|
+ .bind("contextmenu.markItUp", function() { // prevent contextmenu on mac and allow ctrl+click
|
|
217
|
+ return false;
|
|
218
|
+ }).bind('click.markItUp', function(e) {
|
|
219
|
+ e.preventDefault();
|
|
220
|
+ }).bind("focusin.markItUp", function(){
|
|
221
|
+ $$.focus();
|
|
222
|
+ }).bind('mouseup', function(e) {
|
|
223
|
+ if (button.call) {
|
|
224
|
+ eval(button.call)(e); // Pass the mouseup event to custom delegate
|
|
225
|
+ }
|
|
226
|
+ setTimeout(function() { markup(button) },1);
|
|
227
|
+ return false;
|
|
228
|
+ }).bind('mouseenter.markItUp', function() {
|
|
229
|
+ $('> ul', this).show();
|
|
230
|
+ $(document).one('click', function() { // close dropmenu if click outside
|
|
231
|
+ $('ul ul', header).hide();
|
|
232
|
+ }
|
|
233
|
+ );
|
|
234
|
+ }).bind('mouseleave.markItUp', function() {
|
|
235
|
+ $('> ul', this).hide();
|
|
236
|
+ }).appendTo(ul);
|
|
237
|
+ if (button.dropMenu) {
|
|
238
|
+ levels.push(i);
|
|
239
|
+ $(li).addClass('markItUpDropMenu').append(dropMenus(button.dropMenu));
|
|
240
|
+ }
|
|
241
|
+ }
|
|
242
|
+ });
|
|
243
|
+ levels.pop();
|
|
244
|
+ return ul;
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ // markItUp! markups
|
|
248
|
+ function magicMarkups(string) {
|
|
249
|
+ if (string) {
|
|
250
|
+ string = string.toString();
|
|
251
|
+ string = string.replace(/\(\!\(([\s\S]*?)\)\!\)/g,
|
|
252
|
+ function(x, a) {
|
|
253
|
+ var b = a.split('|!|');
|
|
254
|
+ if (altKey === true) {
|
|
255
|
+ return (b[1] !== undefined) ? b[1] : b[0];
|
|
256
|
+ } else {
|
|
257
|
+ return (b[1] === undefined) ? "" : b[0];
|
|
258
|
+ }
|
|
259
|
+ }
|
|
260
|
+ );
|
|
261
|
+ // [![prompt]!], [![prompt:!:value]!]
|
|
262
|
+ string = string.replace(/\[\!\[([\s\S]*?)\]\!\]/g,
|
|
263
|
+ function(x, a) {
|
|
264
|
+ var b = a.split(':!:');
|
|
265
|
+ if (abort === true) {
|
|
266
|
+ return false;
|
|
267
|
+ }
|
|
268
|
+ value = prompt(b[0], (b[1]) ? b[1] : '');
|
|
269
|
+ if (value === null) {
|
|
270
|
+ abort = true;
|
|
271
|
+ }
|
|
272
|
+ return value;
|
|
273
|
+ }
|
|
274
|
+ );
|
|
275
|
+ return string;
|
|
276
|
+ }
|
|
277
|
+ return "";
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ // prepare action
|
|
281
|
+ function prepare(action) {
|
|
282
|
+ if ($.isFunction(action)) {
|
|
283
|
+ action = action(hash);
|
|
284
|
+ }
|
|
285
|
+ return magicMarkups(action);
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ // build block to insert
|
|
289
|
+ function build(string) {
|
|
290
|
+ var openWith = prepare(clicked.openWith);
|
|
291
|
+ var placeHolder = prepare(clicked.placeHolder);
|
|
292
|
+ var replaceWith = prepare(clicked.replaceWith);
|
|
293
|
+ var closeWith = prepare(clicked.closeWith);
|
|
294
|
+ var openBlockWith = prepare(clicked.openBlockWith);
|
|
295
|
+ var closeBlockWith = prepare(clicked.closeBlockWith);
|
|
296
|
+ var multiline = clicked.multiline;
|
|
297
|
+
|
|
298
|
+ if (replaceWith !== "") {
|
|
299
|
+ block = openWith + replaceWith + closeWith;
|
|
300
|
+ } else if (selection === '' && placeHolder !== '') {
|
|
301
|
+ block = openWith + placeHolder + closeWith;
|
|
302
|
+ } else {
|
|
303
|
+ string = string || selection;
|
|
304
|
+
|
|
305
|
+ var lines = [string], blocks = [];
|
|
306
|
+
|
|
307
|
+ if (multiline === true) {
|
|
308
|
+ lines = string.split(/\r?\n/);
|
|
309
|
+ }
|
|
310
|
+
|
|
311
|
+ for (var l = 0; l < lines.length; l++) {
|
|
312
|
+ line = lines[l];
|
|
313
|
+ var trailingSpaces;
|
|
314
|
+ if (trailingSpaces = line.match(/ *$/)) {
|
|
315
|
+ blocks.push(openWith + line.replace(/ *$/g, '') + closeWith + trailingSpaces);
|
|
316
|
+ } else {
|
|
317
|
+ blocks.push(openWith + line + closeWith);
|
|
318
|
+ }
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ block = blocks.join("\n");
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ block = openBlockWith + block + closeBlockWith;
|
|
325
|
+
|
|
326
|
+ return { block:block,
|
|
327
|
+ openBlockWith:openBlockWith,
|
|
328
|
+ openWith:openWith,
|
|
329
|
+ replaceWith:replaceWith,
|
|
330
|
+ placeHolder:placeHolder,
|
|
331
|
+ closeWith:closeWith,
|
|
332
|
+ closeBlockWith:closeBlockWith
|
|
333
|
+ };
|
|
334
|
+ }
|
|
335
|
+
|
|
336
|
+ // define markup to insert
|
|
337
|
+ function markup(button) {
|
|
338
|
+ var len, j, n, i;
|
|
339
|
+ hash = clicked = button;
|
|
340
|
+ get();
|
|
341
|
+ $.extend(hash, { line:"",
|
|
342
|
+ root:options.root,
|
|
343
|
+ textarea:textarea,
|
|
344
|
+ selection:(selection||''),
|
|
345
|
+ caretPosition:caretPosition,
|
|
346
|
+ ctrlKey:ctrlKey,
|
|
347
|
+ shiftKey:shiftKey,
|
|
348
|
+ altKey:altKey
|
|
349
|
+ }
|
|
350
|
+ );
|
|
351
|
+ // callbacks before insertion
|
|
352
|
+ prepare(options.beforeInsert);
|
|
353
|
+ prepare(clicked.beforeInsert);
|
|
354
|
+ if ((ctrlKey === true && shiftKey === true) || button.multiline === true) {
|
|
355
|
+ prepare(clicked.beforeMultiInsert);
|
|
356
|
+ }
|
|
357
|
+ $.extend(hash, { line:1 });
|
|
358
|
+
|
|
359
|
+ if ((ctrlKey === true && shiftKey === true)) {
|
|
360
|
+ lines = selection.split(/\r?\n/);
|
|
361
|
+ for (j = 0, n = lines.length, i = 0; i < n; i++) {
|
|
362
|
+ if ($.trim(lines[i]) !== '') {
|
|
363
|
+ $.extend(hash, { line:++j, selection:lines[i] } );
|
|
364
|
+ lines[i] = build(lines[i]).block;
|
|
365
|
+ } else {
|
|
366
|
+ lines[i] = "";
|
|
367
|
+ }
|
|
368
|
+ }
|
|
369
|
+
|
|
370
|
+ string = { block:lines.join('\n')};
|
|
371
|
+ start = caretPosition;
|
|
372
|
+ len = string.block.length + ((browser.opera) ? n-1 : 0);
|
|
373
|
+ } else if (ctrlKey === true) {
|
|
374
|
+ string = build(selection);
|
|
375
|
+ start = caretPosition + string.openWith.length;
|
|
376
|
+ len = string.block.length - string.openWith.length - string.closeWith.length;
|
|
377
|
+ len = len - (string.block.match(/ $/) ? 1 : 0);
|
|
378
|
+ len -= fixIeBug(string.block);
|
|
379
|
+ } else if (shiftKey === true) {
|
|
380
|
+ string = build(selection);
|
|
381
|
+ start = caretPosition;
|
|
382
|
+ len = string.block.length;
|
|
383
|
+ len -= fixIeBug(string.block);
|
|
384
|
+ } else {
|
|
385
|
+ string = build(selection);
|
|
386
|
+ start = caretPosition + string.block.length ;
|
|
387
|
+ len = 0;
|
|
388
|
+ start -= fixIeBug(string.block);
|
|
389
|
+ }
|
|
390
|
+ if ((selection === '' && string.replaceWith === '')) {
|
|
391
|
+ caretOffset += fixOperaBug(string.block);
|
|
392
|
+
|
|
393
|
+ start = caretPosition + string.openBlockWith.length + string.openWith.length;
|
|
394
|
+ len = string.block.length - string.openBlockWith.length - string.openWith.length - string.closeWith.length - string.closeBlockWith.length;
|
|
395
|
+
|
|
396
|
+ caretOffset = $$.val().substring(caretPosition, $$.val().length).length;
|
|
397
|
+ caretOffset -= fixOperaBug($$.val().substring(0, caretPosition));
|
|
398
|
+ }
|
|
399
|
+ $.extend(hash, { caretPosition:caretPosition, scrollPosition:scrollPosition } );
|
|
400
|
+
|
|
401
|
+ if (string.block !== selection && abort === false) {
|
|
402
|
+ insert(string.block);
|
|
403
|
+ set(start, len);
|
|
404
|
+ } else {
|
|
405
|
+ caretOffset = -1;
|
|
406
|
+ }
|
|
407
|
+ get();
|
|
408
|
+
|
|
409
|
+ $.extend(hash, { line:'', selection:selection });
|
|
410
|
+
|
|
411
|
+ // callbacks after insertion
|
|
412
|
+ if ((ctrlKey === true && shiftKey === true) || button.multiline === true) {
|
|
413
|
+ prepare(clicked.afterMultiInsert);
|
|
414
|
+ }
|
|
415
|
+ prepare(clicked.afterInsert);
|
|
416
|
+ prepare(options.afterInsert);
|
|
417
|
+
|
|
418
|
+ // refresh preview if opened
|
|
419
|
+ if (previewWindow && options.previewAutoRefresh) {
|
|
420
|
+ refreshPreview();
|
|
421
|
+ }
|
|
422
|
+
|
|
423
|
+ // reinit keyevent
|
|
424
|
+ shiftKey = altKey = ctrlKey = abort = false;
|
|
425
|
+ }
|
|
426
|
+
|
|
427
|
+ // Substract linefeed in Opera
|
|
428
|
+ function fixOperaBug(string) {
|
|
429
|
+ if (browser.opera) {
|
|
430
|
+ return string.length - string.replace(/\n*/g, '').length;
|
|
431
|
+ }
|
|
432
|
+ return 0;
|
|
433
|
+ }
|
|
434
|
+ // Substract linefeed in IE
|
|
435
|
+ function fixIeBug(string) {
|
|
436
|
+ if (browser.msie) {
|
|
437
|
+ return string.length - string.replace(/\r*/g, '').length;
|
|
438
|
+ }
|
|
439
|
+ return 0;
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ // add markup
|
|
443
|
+ function insert(block) {
|
|
444
|
+ if (document.selection) {
|
|
445
|
+ var newSelection = document.selection.createRange();
|
|
446
|
+ newSelection.text = block;
|
|
447
|
+ } else {
|
|
448
|
+ textarea.value = textarea.value.substring(0, caretPosition) + block + textarea.value.substring(caretPosition + selection.length, textarea.value.length);
|
|
449
|
+ }
|
|
450
|
+ }
|
|
451
|
+
|
|
452
|
+ // set a selection
|
|
453
|
+ function set(start, len) {
|
|
454
|
+ if (textarea.createTextRange){
|
|
455
|
+ // quick fix to make it work on Opera 9.5
|
|
456
|
+ if (browser.opera && browser.version >= 9.5 && len == 0) {
|
|
457
|
+ return false;
|
|
458
|
+ }
|
|
459
|
+ range = textarea.createTextRange();
|
|
460
|
+ range.collapse(true);
|
|
461
|
+ range.moveStart('character', start);
|
|
462
|
+ range.moveEnd('character', len);
|
|
463
|
+ range.select();
|
|
464
|
+ } else if (textarea.setSelectionRange ){
|
|
465
|
+ textarea.setSelectionRange(start, start + len);
|
|
466
|
+ }
|
|
467
|
+ textarea.scrollTop = scrollPosition;
|
|
468
|
+ textarea.focus();
|
|
469
|
+ }
|
|
470
|
+
|
|
471
|
+ // get the selection
|
|
472
|
+ function get() {
|
|
473
|
+ textarea.focus();
|
|
474
|
+
|
|
475
|
+ scrollPosition = textarea.scrollTop;
|
|
476
|
+ if (document.selection) {
|
|
477
|
+ selection = document.selection.createRange().text;
|
|
478
|
+ if (browser.msie) { // ie
|
|
479
|
+ var range = document.selection.createRange(), rangeCopy = range.duplicate();
|
|
480
|
+ rangeCopy.moveToElementText(textarea);
|
|
481
|
+ caretPosition = -1;
|
|
482
|
+ while(rangeCopy.inRange(range)) {
|
|
483
|
+ rangeCopy.moveStart('character');
|
|
484
|
+ caretPosition ++;
|
|
485
|
+ }
|
|
486
|
+ } else { // opera
|
|
487
|
+ caretPosition = textarea.selectionStart;
|
|
488
|
+ }
|
|
489
|
+ } else { // gecko & webkit
|
|
490
|
+ caretPosition = textarea.selectionStart;
|
|
491
|
+
|
|
492
|
+ selection = textarea.value.substring(caretPosition, textarea.selectionEnd);
|
|
493
|
+ }
|
|
494
|
+ return selection;
|
|
495
|
+ }
|
|
496
|
+
|
|
497
|
+ // open preview window
|
|
498
|
+ function preview() {
|
|
499
|
+ if (typeof options.previewHandler === 'function') {
|
|
500
|
+ previewWindow = true;
|
|
501
|
+ } else if (options.previewInElement) {
|
|
502
|
+ previewWindow = $(options.previewInElement);
|
|
503
|
+ } else if (!previewWindow || previewWindow.closed) {
|
|
504
|
+ if (options.previewInWindow) {
|
|
505
|
+ previewWindow = window.open('', 'preview', options.previewInWindow);
|
|
506
|
+ $(window).unload(function() {
|
|
507
|
+ previewWindow.close();
|
|
508
|
+ });
|
|
509
|
+ } else {
|
|
510
|
+ iFrame = $('<iframe class="markItUpPreviewFrame"></iframe>');
|
|
511
|
+ if (options.previewPosition == 'after') {
|
|
512
|
+ iFrame.insertAfter(footer);
|
|
513
|
+ } else {
|
|
514
|
+ iFrame.insertBefore(header);
|
|
515
|
+ }
|
|
516
|
+ previewWindow = iFrame[iFrame.length - 1].contentWindow || frame[iFrame.length - 1];
|
|
517
|
+ }
|
|
518
|
+ } else if (altKey === true) {
|
|
519
|
+ if (iFrame) {
|
|
520
|
+ iFrame.remove();
|
|
521
|
+ } else {
|
|
522
|
+ previewWindow.close();
|
|
523
|
+ }
|
|
524
|
+ previewWindow = iFrame = false;
|
|
525
|
+ }
|
|
526
|
+ if (!options.previewAutoRefresh) {
|
|
527
|
+ refreshPreview();
|
|
528
|
+ }
|
|
529
|
+ if (options.previewInWindow) {
|
|
530
|
+ previewWindow.focus();
|
|
531
|
+ }
|
|
532
|
+ }
|
|
533
|
+
|
|
534
|
+ // refresh Preview window
|
|
535
|
+ function refreshPreview() {
|
|
536
|
+ renderPreview();
|
|
537
|
+ }
|
|
538
|
+
|
|
539
|
+ function renderPreview() {
|
|
540
|
+ var phtml;
|
|
541
|
+ var parsedData = $$.val();
|
|
542
|
+ if (options.previewParser && typeof options.previewParser === 'function') {
|
|
543
|
+ parsedData = options.previewParser(parsedData);
|
|
544
|
+ }
|
|
545
|
+ if (options.previewHandler && typeof options.previewHandler === 'function') {
|
|
546
|
+ options.previewHandler(parsedData);
|
|
547
|
+ } else if (options.previewParserPath !== '') {
|
|
548
|
+ $.ajax({
|
|
549
|
+ type: options.previewParserAjaxType,
|
|
550
|
+ dataType: 'text',
|
|
551
|
+ global: false,
|
|
552
|
+ url: options.previewParserPath,
|
|
553
|
+ data: options.previewParserVar+'='+encodeURIComponent(parsedData),
|
|
554
|
+ success: function(data) {
|
|
555
|
+ writeInPreview( localize(data, 1) );
|
|
556
|
+ }
|
|
557
|
+ });
|
|
558
|
+ } else {
|
|
559
|
+ if (!template) {
|
|
560
|
+ $.ajax({
|
|
561
|
+ url: options.previewTemplatePath,
|
|
562
|
+ dataType: 'text',
|
|
563
|
+ global: false,
|
|
564
|
+ success: function(data) {
|
|
565
|
+ writeInPreview( localize(data, 1).replace(/<!-- content -->/g, parsedData) );
|
|
566
|
+ }
|
|
567
|
+ });
|
|
568
|
+ }
|
|
569
|
+ }
|
|
570
|
+ return false;
|
|
571
|
+ }
|
|
572
|
+
|
|
573
|
+ function writeInPreview(data) {
|
|
574
|
+ if (options.previewInElement) {
|
|
575
|
+ $(options.previewInElement).html(data);
|
|
576
|
+ } else if (previewWindow && previewWindow.document) {
|
|
577
|
+ try {
|
|
578
|
+ sp = previewWindow.document.documentElement.scrollTop
|
|
579
|
+ } catch(e) {
|
|
580
|
+ sp = 0;
|
|
581
|
+ }
|
|
582
|
+ previewWindow.document.open();
|
|
583
|
+ previewWindow.document.write(data);
|
|
584
|
+ previewWindow.document.close();
|
|
585
|
+ previewWindow.document.documentElement.scrollTop = sp;
|
|
586
|
+ }
|
|
587
|
+ }
|
|
588
|
+
|
|
589
|
+ // set keys pressed
|
|
590
|
+ function keyPressed(e) {
|
|
591
|
+ shiftKey = e.shiftKey;
|
|
592
|
+ altKey = e.altKey;
|
|
593
|
+ ctrlKey = (!(e.altKey && e.ctrlKey)) ? (e.ctrlKey || e.metaKey) : false;
|
|
594
|
+
|
|
595
|
+ if (e.type === 'keydown') {
|
|
596
|
+ if (ctrlKey === true) {
|
|
597
|
+ li = $('a[accesskey="'+((e.keyCode == 13) ? '\\n' : String.fromCharCode(e.keyCode))+'"]', header).parent('li');
|
|
598
|
+ if (li.length !== 0) {
|
|
599
|
+ ctrlKey = false;
|
|
600
|
+ setTimeout(function() {
|
|
601
|
+ li.triggerHandler('mouseup');
|
|
602
|
+ },1);
|
|
603
|
+ return false;
|
|
604
|
+ }
|
|
605
|
+ }
|
|
606
|
+ if (e.keyCode === 13 || e.keyCode === 10) { // Enter key
|
|
607
|
+ if (ctrlKey === true) { // Enter + Ctrl
|
|
608
|
+ ctrlKey = false;
|
|
609
|
+ markup(options.onCtrlEnter);
|
|
610
|
+ return options.onCtrlEnter.keepDefault;
|
|
611
|
+ } else if (shiftKey === true) { // Enter + Shift
|
|
612
|
+ shiftKey = false;
|
|
613
|
+ markup(options.onShiftEnter);
|
|
614
|
+ return options.onShiftEnter.keepDefault;
|
|
615
|
+ } else { // only Enter
|
|
616
|
+ markup(options.onEnter);
|
|
617
|
+ return options.onEnter.keepDefault;
|
|
618
|
+ }
|
|
619
|
+ }
|
|
620
|
+ if (e.keyCode === 9) { // Tab key
|
|
621
|
+ if (shiftKey == true || ctrlKey == true || altKey == true) {
|
|
622
|
+ return false;
|
|
623
|
+ }
|
|
624
|
+ if (caretOffset !== -1) {
|
|
625
|
+ get();
|
|
626
|
+ caretOffset = $$.val().length - caretOffset;
|
|
627
|
+ set(caretOffset, 0);
|
|
628
|
+ caretOffset = -1;
|
|
629
|
+ return false;
|
|
630
|
+ } else {
|
|
631
|
+ markup(options.onTab);
|
|
632
|
+ return options.onTab.keepDefault;
|
|
633
|
+ }
|
|
634
|
+ }
|
|
635
|
+ }
|
|
636
|
+ }
|
|
637
|
+
|
|
638
|
+ function remove() {
|
|
639
|
+ $$.unbind(".markItUp").removeClass('markItUpEditor');
|
|
640
|
+ $$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
|
|
641
|
+
|
|
642
|
+ var relativeRef = $$.parent('div').parent('div.markItUp').parent('div');
|
|
643
|
+ if (relativeRef.length) {
|
|
644
|
+ relativeRef.replaceWith($$);
|
|
645
|
+ }
|
|
646
|
+
|
|
647
|
+ $$.data('markItUp', null);
|
|
648
|
+ }
|
|
649
|
+
|
|
650
|
+ init();
|
|
651
|
+ });
|
|
652
|
+ };
|
|
653
|
+
|
|
654
|
+ $.fn.markItUpRemove = function() {
|
|
655
|
+ return this.each(function() {
|
|
656
|
+ $(this).markItUp('remove');
|
|
657
|
+ }
|
|
658
|
+ );
|
|
659
|
+ };
|
|
660
|
+
|
|
661
|
+ $.markItUp = function(settings) {
|
|
662
|
+ var options = { target:false };
|
|
663
|
+ $.extend(options, settings);
|
|
664
|
+ if (options.target) {
|
|
665
|
+ return $(options.target).each(function() {
|
|
666
|
+ $(this).focus();
|
|
667
|
+ $(this).trigger('insertion', [options]);
|
|
668
|
+ });
|
|
669
|
+ } else {
|
|
670
|
+ $('textarea').trigger('insertion', [options]);
|
|
671
|
+ }
|
|
672
|
+ };
|
|
673
|
+})(jQuery);
|