guiedit.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)
  2. This file is part of PmWiki; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published
  4. by the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version. See pmwiki.php for full details.
  6. This file provides Javascript functions to support WYSIWYG-style
  7. editing. The concepts are borrowed from the editor used in Wikipedia,
  8. but the code has been rewritten from scratch to integrate better with
  9. PHP and PmWiki's codebase.
  10. */
  11. function insButton(mopen, mclose, mtext, mlabel, mkey) {
  12. if (mkey > '') { mkey = 'accesskey="' + mkey + '" ' }
  13. document.write("<a " + mkey + "href=\"javascript:insMarkup('"
  14. + mopen + "','"
  15. + mclose + "','"
  16. + mtext + "');\">"
  17. + mlabel + "</a>");
  18. }
  19. function insMarkup(mopen, mclose, mtext) {
  20. var tarea = document.getElementById('text');
  21. if (tarea.setSelectionRange > '') {
  22. var p0 = tarea.selectionStart;
  23. var p1 = tarea.selectionEnd;
  24. var top = tarea.scrollTop;
  25. var str = mtext;
  26. var cur0 = p0 + mopen.length;
  27. var cur1 = p0 + mopen.length + str.length;
  28. while (p1 > p0 && tarea.value.substring(p1-1, p1) == ' ') p1--;
  29. if (p1 > p0) {
  30. str = tarea.value.substring(p0, p1);
  31. cur0 = p0 + mopen.length + str.length + mclose.length;
  32. cur1 = cur0;
  33. }
  34. tarea.value = tarea.value.substring(0,p0)
  35. + mopen + str + mclose
  36. + tarea.value.substring(p1);
  37. tarea.focus();
  38. tarea.selectionStart = cur0;
  39. tarea.selectionEnd = cur1;
  40. tarea.scrollTop = top;
  41. } else if (document.selection) {
  42. var str = document.selection.createRange().text;
  43. tarea.focus();
  44. range = document.selection.createRange()
  45. if (str == '') {
  46. range.text = mopen + mtext + mclose;
  47. range.moveStart('character', -mclose.length - mtext.length );
  48. range.moveEnd('character', -mclose.length );
  49. } else {
  50. if (str.charAt(str.length - 1) == " ") {
  51. mclose = mclose + " ";
  52. str = str.substr(0, str.length - 1);
  53. }
  54. range.text = mopen + str + mclose;
  55. }
  56. range.select();
  57. } else { tarea.value += mopen + mtext + mclose; }
  58. return;
  59. }