Mercurial > public > sg101
comparison static/js/markitup/jquery.markitup.js @ 466:c78c6e007e61
Upgrading to markitup version 1.1.11. This fixes #226.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 24 Jul 2011 02:35:17 +0000 |
parents | 88b2b9cb8c1f |
children | d280b27fed17 |
comparison
equal
deleted
inserted
replaced
465:79d454ff2de0 | 466:c78c6e007e61 |
---|---|
1 // ---------------------------------------------------------------------------- | 1 // ---------------------------------------------------------------------------- |
2 // markItUp! Universal MarkUp Engine, JQuery plugin | 2 // markItUp! Universal MarkUp Engine, JQuery plugin |
3 // v 1.1.x | 3 // v 1.1.11 |
4 // Dual licensed under the MIT and GPL licenses. | 4 // Dual licensed under the MIT and GPL licenses. |
5 // ---------------------------------------------------------------------------- | 5 // ---------------------------------------------------------------------------- |
6 // Copyright (C) 2007-2010 Jay Salvat | 6 // Copyright (C) 2007-2011 Jay Salvat |
7 // http://markitup.jaysalvat.com/ | 7 // http://markitup.jaysalvat.com/ |
8 // ---------------------------------------------------------------------------- | 8 // ---------------------------------------------------------------------------- |
9 // Permission is hereby granted, free of charge, to any person obtaining a copy | 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 | 10 // of this software and associated documentation files (the "Software"), to deal |
11 // in the Software without restriction, including without limitation the rights | 11 // in the Software without restriction, including without limitation the rights |
34 root: '', | 34 root: '', |
35 previewInWindow: '', // 'width=800, height=600, resizable=yes, scrollbars=yes' | 35 previewInWindow: '', // 'width=800, height=600, resizable=yes, scrollbars=yes' |
36 previewAutoRefresh: true, | 36 previewAutoRefresh: true, |
37 previewPosition: 'after', | 37 previewPosition: 'after', |
38 previewTemplatePath: '~/templates/preview.html', | 38 previewTemplatePath: '~/templates/preview.html', |
39 previewParser: false, | |
39 previewParserPath: '', | 40 previewParserPath: '', |
40 previewParserVar: 'data', | 41 previewParserVar: 'data', |
41 resizeHandle: true, | 42 resizeHandle: true, |
42 beforeInsert: '', | 43 beforeInsert: '', |
43 afterInsert: '', | 44 afterInsert: '', |
160 li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>') | 161 li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>') |
161 .bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click | 162 .bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click |
162 return false; | 163 return false; |
163 }).click(function() { | 164 }).click(function() { |
164 return false; | 165 return false; |
165 }).focusin(function(){ | 166 }).bind("focusin", function(){ |
166 $$.focus(); | 167 $$.focus(); |
167 }).mousedown(function() { | 168 }).mouseup(function() { |
168 if (button.call) { | 169 if (button.call) { |
169 eval(button.call)(); | 170 eval(button.call)(); |
170 } | 171 } |
171 setTimeout(function() { markup(button) },1); | 172 setTimeout(function() { markup(button) },1); |
172 return false; | 173 return false; |
231 return magicMarkups(action); | 232 return magicMarkups(action); |
232 } | 233 } |
233 | 234 |
234 // build block to insert | 235 // build block to insert |
235 function build(string) { | 236 function build(string) { |
236 openWith = prepare(clicked.openWith); | 237 var openWith = prepare(clicked.openWith); |
237 placeHolder = prepare(clicked.placeHolder); | 238 var placeHolder = prepare(clicked.placeHolder); |
238 replaceWith = prepare(clicked.replaceWith); | 239 var replaceWith = prepare(clicked.replaceWith); |
239 closeWith = prepare(clicked.closeWith); | 240 var closeWith = prepare(clicked.closeWith); |
241 var openBlockWith = prepare(clicked.openBlockWith); | |
242 var closeBlockWith = prepare(clicked.closeBlockWith); | |
243 var multiline = clicked.multiline; | |
244 | |
240 if (replaceWith !== "") { | 245 if (replaceWith !== "") { |
241 block = openWith + replaceWith + closeWith; | 246 block = openWith + replaceWith + closeWith; |
242 } else if (selection === '' && placeHolder !== '') { | 247 } else if (selection === '' && placeHolder !== '') { |
243 block = openWith + placeHolder + closeWith; | 248 block = openWith + placeHolder + closeWith; |
244 } else { | 249 } else { |
245 block = openWith + (string||selection) + closeWith; | 250 string = string || selection; |
246 } | 251 |
252 var lines = selection.split(/\r?\n/), blocks = []; | |
253 for (var l=0; l < lines.length; l++) { | |
254 line = lines[l]; | |
255 if ($.trim(line) == '') { | |
256 continue; | |
257 } | |
258 if (line.match(/ +$/)) { | |
259 blocks.push(openWith + line.replace(/ $/, '') + closeWith + ' '); | |
260 } else { | |
261 blocks.push(openWith + line + closeWith); | |
262 } | |
263 } | |
264 | |
265 block = blocks.join("\n"); | |
266 } | |
267 | |
268 block = openBlockWith + block + closeBlockWith; | |
269 | |
247 return { block:block, | 270 return { block:block, |
248 openWith:openWith, | 271 openWith:openWith, |
249 replaceWith:replaceWith, | 272 replaceWith:replaceWith, |
250 placeHolder:placeHolder, | 273 placeHolder:placeHolder, |
251 closeWith:closeWith | 274 closeWith:closeWith |
255 // define markup to insert | 278 // define markup to insert |
256 function markup(button) { | 279 function markup(button) { |
257 var len, j, n, i; | 280 var len, j, n, i; |
258 hash = clicked = button; | 281 hash = clicked = button; |
259 get(); | 282 get(); |
260 | |
261 $.extend(hash, { line:"", | 283 $.extend(hash, { line:"", |
262 root:options.root, | 284 root:options.root, |
263 textarea:textarea, | 285 textarea:textarea, |
264 selection:(selection||''), | 286 selection:(selection||''), |
265 caretPosition:caretPosition, | 287 caretPosition:caretPosition, |
269 } | 291 } |
270 ); | 292 ); |
271 // callbacks before insertion | 293 // callbacks before insertion |
272 prepare(options.beforeInsert); | 294 prepare(options.beforeInsert); |
273 prepare(clicked.beforeInsert); | 295 prepare(clicked.beforeInsert); |
274 if (ctrlKey === true && shiftKey === true) { | 296 if ((ctrlKey === true && shiftKey === true) || button.multiline === true) { |
275 prepare(clicked.beforeMultiInsert); | 297 prepare(clicked.beforeMultiInsert); |
276 } | 298 } |
277 $.extend(hash, { line:1 }); | 299 $.extend(hash, { line:1 }); |
278 | 300 |
279 if (ctrlKey === true && shiftKey === true) { | 301 if ((ctrlKey === true && shiftKey === true)) { |
280 lines = selection.split(/\r?\n/); | 302 lines = selection.split(/\r?\n/); |
281 for (j = 0, n = lines.length, i = 0; i < n; i++) { | 303 for (j = 0, n = lines.length, i = 0; i < n; i++) { |
282 if ($.trim(lines[i]) !== '') { | 304 if ($.trim(lines[i]) !== '') { |
283 $.extend(hash, { line:++j, selection:lines[i] } ); | 305 $.extend(hash, { line:++j, selection:lines[i] } ); |
284 lines[i] = build(lines[i]).block; | 306 lines[i] = build(lines[i]).block; |
291 len = string.block.length + (($.browser.opera) ? n-1 : 0); | 313 len = string.block.length + (($.browser.opera) ? n-1 : 0); |
292 } else if (ctrlKey === true) { | 314 } else if (ctrlKey === true) { |
293 string = build(selection); | 315 string = build(selection); |
294 start = caretPosition + string.openWith.length; | 316 start = caretPosition + string.openWith.length; |
295 len = string.block.length - string.openWith.length - string.closeWith.length; | 317 len = string.block.length - string.openWith.length - string.closeWith.length; |
318 len = len - (string.block.match(/ $/) ? 1 : 0); | |
296 len -= fixIeBug(string.block); | 319 len -= fixIeBug(string.block); |
297 } else if (shiftKey === true) { | 320 } else if (shiftKey === true) { |
298 string = build(selection); | 321 string = build(selection); |
299 start = caretPosition; | 322 start = caretPosition; |
300 len = string.block.length; | 323 len = string.block.length; |
325 get(); | 348 get(); |
326 | 349 |
327 $.extend(hash, { line:'', selection:selection }); | 350 $.extend(hash, { line:'', selection:selection }); |
328 | 351 |
329 // callbacks after insertion | 352 // callbacks after insertion |
330 if (ctrlKey === true && shiftKey === true) { | 353 if ((ctrlKey === true && shiftKey === true) || button.multiline === true) { |
331 prepare(clicked.afterMultiInsert); | 354 prepare(clicked.afterMultiInsert); |
332 } | 355 } |
333 prepare(clicked.afterInsert); | 356 prepare(clicked.afterInsert); |
334 prepare(options.afterInsert); | 357 prepare(options.afterInsert); |
335 | 358 |
404 } else { // opera | 427 } else { // opera |
405 caretPosition = textarea.selectionStart; | 428 caretPosition = textarea.selectionStart; |
406 } | 429 } |
407 } else { // gecko & webkit | 430 } else { // gecko & webkit |
408 caretPosition = textarea.selectionStart; | 431 caretPosition = textarea.selectionStart; |
432 | |
409 selection = textarea.value.substring(caretPosition, textarea.selectionEnd); | 433 selection = textarea.value.substring(caretPosition, textarea.selectionEnd); |
410 } | 434 } |
411 return selection; | 435 return selection; |
412 } | 436 } |
413 | 437 |
449 renderPreview(); | 473 renderPreview(); |
450 } | 474 } |
451 | 475 |
452 function renderPreview() { | 476 function renderPreview() { |
453 var phtml; | 477 var phtml; |
454 if (options.previewParserPath !== '') { | 478 if (options.previewParser && typeof options.previewParser === 'function') { |
455 $.ajax( { | 479 var data = options.previewParser( $$.val() ); |
480 writeInPreview( localize(data, 1) ); | |
481 } else if (options.previewParserPath !== '') { | |
482 $.ajax({ | |
456 type: 'POST', | 483 type: 'POST', |
484 dataType: 'text', | |
485 global: false, | |
457 url: options.previewParserPath, | 486 url: options.previewParserPath, |
458 data: options.previewParserVar+'='+encodeURIComponent($$.val()), | 487 data: options.previewParserVar+'='+encodeURIComponent($$.val()), |
459 success: function(data) { | 488 success: function(data) { |
460 writeInPreview( localize(data, 1) ); | 489 writeInPreview( localize(data, 1) ); |
461 } | 490 } |
462 } ); | 491 }); |
463 } else { | 492 } else { |
464 if (!template) { | 493 if (!template) { |
465 $.ajax( { | 494 $.ajax({ |
466 url: options.previewTemplatePath, | 495 url: options.previewTemplatePath, |
496 dataType: 'text', | |
497 global: false, | |
467 success: function(data) { | 498 success: function(data) { |
468 writeInPreview( localize(data, 1).replace(/<!-- content -->/g, $$.val()) ); | 499 writeInPreview( localize(data, 1).replace(/<!-- content -->/g, $$.val()) ); |
469 } | 500 } |
470 } ); | 501 }); |
471 } | 502 } |
472 } | 503 } |
473 return false; | 504 return false; |
474 } | 505 } |
475 | 506 |
489 | 520 |
490 // set keys pressed | 521 // set keys pressed |
491 function keyPressed(e) { | 522 function keyPressed(e) { |
492 shiftKey = e.shiftKey; | 523 shiftKey = e.shiftKey; |
493 altKey = e.altKey; | 524 altKey = e.altKey; |
494 ctrlKey = (!(e.altKey && e.ctrlKey)) ? e.ctrlKey : false; | 525 ctrlKey = (!(e.altKey && e.ctrlKey)) ? (e.ctrlKey || e.metaKey) : false; |
495 | 526 |
496 if (e.type === 'keydown') { | 527 if (e.type === 'keydown') { |
497 if (ctrlKey === true) { | 528 if (ctrlKey === true) { |
498 li = $("a[accesskey="+String.fromCharCode(e.keyCode)+"]", header).parent('li'); | 529 li = $('a[accesskey="'+String.fromCharCode(e.keyCode)+'"]', header).parent('li'); |
499 if (li.length !== 0) { | 530 if (li.length !== 0) { |
500 ctrlKey = false; | 531 ctrlKey = false; |
501 setTimeout(function() { | 532 setTimeout(function() { |
502 li.triggerHandler('mousedown'); | 533 li.triggerHandler('mouseup'); |
503 },1); | 534 },1); |
504 return false; | 535 return false; |
505 } | 536 } |
506 } | 537 } |
507 if (e.keyCode === 13 || e.keyCode === 10) { // Enter key | 538 if (e.keyCode === 13 || e.keyCode === 10) { // Enter key |