Mercurial > public > sg101
comparison media/js/markitup/jquery.markitup.js @ 244:a61b1c598001
Updated to markItUp 1.1.8. Ticket #94.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 17 Sep 2010 02:27:40 +0000 |
parents | 217119c87a06 |
children |
comparison
equal
deleted
inserted
replaced
243:7ddd60164245 | 244:a61b1c598001 |
---|---|
1 // ---------------------------------------------------------------------------- | 1 // ---------------------------------------------------------------------------- |
2 // markItUp! Universal MarkUp Engine, JQuery plugin | 2 // markItUp! Universal MarkUp Engine, JQuery plugin |
3 // v 1.1.7 | 3 // v 1.1.x |
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-2010 Jay Salvat |
7 // http://markitup.jaysalvat.com/ | 7 // http://markitup.jaysalvat.com/ |
8 // ---------------------------------------------------------------------------- | 8 // ---------------------------------------------------------------------------- |
26 // ---------------------------------------------------------------------------- | 26 // ---------------------------------------------------------------------------- |
27 (function($) { | 27 (function($) { |
28 $.fn.markItUp = function(settings, extraSettings) { | 28 $.fn.markItUp = function(settings, extraSettings) { |
29 var options, ctrlKey, shiftKey, altKey; | 29 var options, ctrlKey, shiftKey, altKey; |
30 ctrlKey = shiftKey = altKey = false; | 30 ctrlKey = shiftKey = altKey = false; |
31 | 31 |
32 options = { id: '', | 32 options = { id: '', |
33 nameSpace: '', | 33 nameSpace: '', |
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, |
160 li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>') | 160 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 | 161 .bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click |
162 return false; | 162 return false; |
163 }).click(function() { | 163 }).click(function() { |
164 return false; | 164 return false; |
165 }).focusin(function(){ | |
166 $$.focus(); | |
165 }).mousedown(function() { | 167 }).mousedown(function() { |
166 if (button.call) { | 168 if (button.call) { |
167 eval(button.call)(); | 169 eval(button.call)(); |
168 } | 170 } |
169 setTimeout(function() { markup(button) },1); | 171 setTimeout(function() { markup(button) },1); |
284 lines[i] = ""; | 286 lines[i] = ""; |
285 } | 287 } |
286 } | 288 } |
287 string = { block:lines.join('\n')}; | 289 string = { block:lines.join('\n')}; |
288 start = caretPosition; | 290 start = caretPosition; |
289 len = string.block.length + (($.browser.opera) ? n : 0); | 291 len = string.block.length + (($.browser.opera) ? n-1 : 0); |
290 } else if (ctrlKey === true) { | 292 } else if (ctrlKey === true) { |
291 string = build(selection); | 293 string = build(selection); |
292 start = caretPosition + string.openWith.length; | 294 start = caretPosition + string.openWith.length; |
293 len = string.block.length - string.openWith.length - string.closeWith.length; | 295 len = string.block.length - string.openWith.length - string.closeWith.length; |
294 len -= fixIeBug(string.block); | 296 len -= fixIeBug(string.block); |
359 function insert(block) { | 361 function insert(block) { |
360 if (document.selection) { | 362 if (document.selection) { |
361 var newSelection = document.selection.createRange(); | 363 var newSelection = document.selection.createRange(); |
362 newSelection.text = block; | 364 newSelection.text = block; |
363 } else { | 365 } else { |
364 $$.val($$.val().substring(0, caretPosition) + block + $$.val().substring(caretPosition + selection.length, $$.val().length)); | 366 textarea.value = textarea.value.substring(0, caretPosition) + block + textarea.value.substring(caretPosition + selection.length, textarea.value.length); |
365 } | 367 } |
366 } | 368 } |
367 | 369 |
368 // set a selection | 370 // set a selection |
369 function set(start, len) { | 371 function set(start, len) { |
393 selection = document.selection.createRange().text; | 395 selection = document.selection.createRange().text; |
394 if ($.browser.msie) { // ie | 396 if ($.browser.msie) { // ie |
395 var range = document.selection.createRange(), rangeCopy = range.duplicate(); | 397 var range = document.selection.createRange(), rangeCopy = range.duplicate(); |
396 rangeCopy.moveToElementText(textarea); | 398 rangeCopy.moveToElementText(textarea); |
397 caretPosition = -1; | 399 caretPosition = -1; |
398 while(rangeCopy.inRange(range)) { // fix most of the ie bugs with linefeeds... | 400 while(rangeCopy.inRange(range)) { |
399 rangeCopy.moveStart('character'); | 401 rangeCopy.moveStart('character'); |
400 caretPosition ++; | 402 caretPosition ++; |
401 } | 403 } |
402 } else { // opera | 404 } else { // opera |
403 caretPosition = textarea.selectionStart; | 405 caretPosition = textarea.selectionStart; |
404 } | 406 } |
405 } else { // gecko & webkit | 407 } else { // gecko & webkit |
406 caretPosition = textarea.selectionStart; | 408 caretPosition = textarea.selectionStart; |
407 selection = $$.val().substring(caretPosition, textarea.selectionEnd); | 409 selection = textarea.value.substring(caretPosition, textarea.selectionEnd); |
408 } | 410 } |
409 return selection; | 411 return selection; |
410 } | 412 } |
411 | 413 |
412 // open preview window | 414 // open preview window |
413 function preview() { | 415 function preview() { |
414 if (!previewWindow || previewWindow.closed) { | 416 if (!previewWindow || previewWindow.closed) { |
415 if (options.previewInWindow) { | 417 if (options.previewInWindow) { |
416 previewWindow = window.open('', 'preview', options.previewInWindow); | 418 previewWindow = window.open('', 'preview', options.previewInWindow); |
419 $(window).unload(function() { | |
420 previewWindow.close(); | |
421 }); | |
417 } else { | 422 } else { |
418 iFrame = $('<iframe class="markItUpPreviewFrame"></iframe>'); | 423 iFrame = $('<iframe class="markItUpPreviewFrame"></iframe>'); |
419 if (options.previewPosition == 'after') { | 424 if (options.previewPosition == 'after') { |
420 iFrame.insertAfter(footer); | 425 iFrame.insertAfter(footer); |
421 } else { | 426 } else { |
422 iFrame.insertBefore(header); | 427 iFrame.insertBefore(header); |
423 } | 428 } |
424 previewWindow = iFrame[iFrame.length - 1].contentWindow || frame[iFrame.length - 1]; | 429 previewWindow = iFrame[iFrame.length - 1].contentWindow || frame[iFrame.length - 1]; |
425 } | 430 } |
426 } else if (altKey === true) { | 431 } else if (altKey === true) { |
427 // Thx Stephen M. Redd for the IE8 fix | |
428 if (iFrame) { | 432 if (iFrame) { |
429 iFrame.remove(); | 433 iFrame.remove(); |
430 } else { | 434 } else { |
431 previewWindow.close(); | 435 previewWindow.close(); |
432 } | 436 } |
433 previewWindow = iFrame = false; | 437 previewWindow = iFrame = false; |
434 } | 438 } |
435 if (!options.previewAutoRefresh) { | 439 if (!options.previewAutoRefresh) { |
436 refreshPreview(); | 440 refreshPreview(); |
441 } | |
442 if (options.previewInWindow) { | |
443 previewWindow.focus(); | |
437 } | 444 } |
438 } | 445 } |
439 | 446 |
440 // refresh Preview window | 447 // refresh Preview window |
441 function refreshPreview() { | 448 function refreshPreview() { |
476 previewWindow.document.open(); | 483 previewWindow.document.open(); |
477 previewWindow.document.write(data); | 484 previewWindow.document.write(data); |
478 previewWindow.document.close(); | 485 previewWindow.document.close(); |
479 previewWindow.document.documentElement.scrollTop = sp; | 486 previewWindow.document.documentElement.scrollTop = sp; |
480 } | 487 } |
481 if (options.previewInWindow) { | |
482 previewWindow.focus(); | |
483 } | |
484 } | 488 } |
485 | 489 |
486 // set keys pressed | 490 // set keys pressed |
487 function keyPressed(e) { | 491 function keyPressed(e) { |
488 shiftKey = e.shiftKey; | 492 shiftKey = e.shiftKey; |
513 markup(options.onEnter); | 517 markup(options.onEnter); |
514 return options.onEnter.keepDefault; | 518 return options.onEnter.keepDefault; |
515 } | 519 } |
516 } | 520 } |
517 if (e.keyCode === 9) { // Tab key | 521 if (e.keyCode === 9) { // Tab key |
518 if (shiftKey == true || ctrlKey == true || altKey == true) { // Thx Dr Floob. | 522 if (shiftKey == true || ctrlKey == true || altKey == true) { |
519 return false; | 523 return false; |
520 } | 524 } |
521 if (caretOffset !== -1) { | 525 if (caretOffset !== -1) { |
522 get(); | 526 get(); |
523 caretOffset = $$.val().length - caretOffset; | 527 caretOffset = $$.val().length - caretOffset; |