[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/assets/javascript/ -> editor.js (source)

   1  /**
   2  * bbCode control by subBlue design [ www.subBlue.com ]
   3  * Includes unixsafe colour palette selector by SHS`
   4  */
   5  
   6  // Startup variables
   7  var imageTag = false;
   8  var theSelection = false;
   9  var bbcodeEnabled = true;
  10  
  11  // Check for Browser & Platform for PC & IE specific bits
  12  // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
  13  var clientPC = navigator.userAgent.toLowerCase(); // Get client info
  14  var clientVer = parseInt(navigator.appVersion, 10); // Get browser version
  15  
  16  var is_ie = ((clientPC.indexOf('msie') !== -1) && (clientPC.indexOf('opera') === -1));
  17  var is_win = ((clientPC.indexOf('win') !== -1) || (clientPC.indexOf('16bit') !== -1));
  18  var baseHeight;
  19  
  20  /**
  21  * Fix a bug involving the TextRange object. From
  22  * http://www.frostjedi.com/terra/scripts/demo/caretBug.html
  23  */
  24  function initInsertions() {
  25      var doc;
  26  
  27      if (document.forms[form_name]) {
  28          doc = document;
  29      } else {
  30          doc = opener.document;
  31      }
  32  
  33      var textarea = doc.forms[form_name].elements[text_name];
  34  
  35      if (is_ie && typeof(baseHeight) !== 'number') {
  36          textarea.focus();
  37          baseHeight = doc.selection.createRange().duplicate().boundingHeight;
  38  
  39          if (!document.forms[form_name]) {
  40              document.body.focus();
  41          }
  42      }
  43  }
  44  
  45  /**
  46  * bbstyle
  47  */
  48  function bbstyle(bbnumber) {
  49      if (bbnumber !== -1) {
  50          bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
  51      } else {
  52          insert_text('[*]');
  53          document.forms[form_name].elements[text_name].focus();
  54      }
  55  }
  56  
  57  /**
  58  * Apply bbcodes
  59  */
  60  function bbfontstyle(bbopen, bbclose) {
  61      theSelection = false;
  62  
  63      var textarea = document.forms[form_name].elements[text_name];
  64  
  65      textarea.focus();
  66  
  67      if ((clientVer >= 4) && is_ie && is_win) {
  68          // Get text selection
  69          theSelection = document.selection.createRange().text;
  70  
  71          if (theSelection) {
  72              // Add tags around selection
  73              document.selection.createRange().text = bbopen + theSelection + bbclose;
  74              textarea.focus();
  75              theSelection = '';
  76              return;
  77          }
  78      } else if (textarea.selectionEnd && (textarea.selectionEnd - textarea.selectionStart > 0)) {
  79          mozWrap(textarea, bbopen, bbclose);
  80          textarea.focus();
  81          theSelection = '';
  82          return;
  83      }
  84  
  85      //The new position for the cursor after adding the bbcode
  86      var caret_pos = getCaretPosition(textarea).start;
  87      var new_pos = caret_pos + bbopen.length;
  88  
  89      // Open tag
  90      insert_text(bbopen + bbclose);
  91  
  92      // Center the cursor when we don't have a selection
  93      // Gecko and proper browsers
  94      if (!isNaN(textarea.selectionStart)) {
  95          textarea.selectionStart = new_pos;
  96          textarea.selectionEnd = new_pos;
  97      }
  98      // IE
  99      else if (document.selection) {
 100          var range = textarea.createTextRange();
 101          range.move("character", new_pos);
 102          range.select();
 103          storeCaret(textarea);
 104      }
 105  
 106      textarea.focus();
 107  }
 108  
 109  /**
 110  * Insert text at position
 111  */
 112  function insert_text(text, spaces, popup) {
 113      var textarea;
 114  
 115      if (!popup) {
 116          textarea = document.forms[form_name].elements[text_name];
 117      } else {
 118          textarea = opener.document.forms[form_name].elements[text_name];
 119      }
 120  
 121      if (spaces) {
 122          text = ' ' + text + ' ';
 123      }
 124  
 125      // Since IE9, IE also has textarea.selectionStart, but it still needs to be treated the old way.
 126      // Therefore we simply add a !is_ie here until IE fixes the text-selection completely.
 127      if (!isNaN(textarea.selectionStart) && !is_ie) {
 128          var sel_start = textarea.selectionStart;
 129          var sel_end = textarea.selectionEnd;
 130  
 131          mozWrap(textarea, text, '');
 132          textarea.selectionStart = sel_start + text.length;
 133          textarea.selectionEnd = sel_end + text.length;
 134      } else if (textarea.createTextRange && textarea.caretPos) {
 135          if (baseHeight !== textarea.caretPos.boundingHeight) {
 136              textarea.focus();
 137              storeCaret(textarea);
 138          }
 139  
 140          var caret_pos = textarea.caretPos;
 141          caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) === ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
 142      } else {
 143          textarea.value = textarea.value + text;
 144      }
 145  
 146      if (!popup) {
 147          textarea.focus();
 148      }
 149  }
 150  
 151  /**
 152  * Add inline attachment at position
 153  */
 154  function attachInline(index, filename) {
 155      insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
 156      document.forms[form_name].elements[text_name].focus();
 157  }
 158  
 159  /**
 160  * Add quote text to message
 161  */
 162  function addquote(post_id, username, l_wrote, attributes) {
 163      var message_name = 'message_' + post_id;
 164      var theSelection = '';
 165      var divarea = false;
 166      var i;
 167  
 168      if (l_wrote === undefined) {
 169          // Backwards compatibility
 170          l_wrote = 'wrote';
 171      }
 172      if (typeof attributes !== 'object') {
 173          attributes = {};
 174      }
 175  
 176      if (document.all) {
 177          divarea = document.all[message_name];
 178      } else {
 179          divarea = document.getElementById(message_name);
 180      }
 181  
 182      // Get text selection - not only the post content :(
 183      // IE9 must use the document.selection method but has the *.getSelection so we just force no IE
 184      if (window.getSelection && !is_ie && !window.opera) {
 185          theSelection = window.getSelection().toString();
 186      } else if (document.getSelection && !is_ie) {
 187          theSelection = document.getSelection();
 188      } else if (document.selection) {
 189          theSelection = document.selection.createRange().text;
 190      }
 191  
 192      if (theSelection === '' || typeof theSelection === 'undefined' || theSelection === null) {
 193          if (divarea.innerHTML) {
 194              theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
 195              theSelection = theSelection.replace(/<br\/>/ig, '\n');
 196              theSelection = theSelection.replace(/&lt\;/ig, '<');
 197              theSelection = theSelection.replace(/&gt\;/ig, '>');
 198              theSelection = theSelection.replace(/&amp\;/ig, '&');
 199              theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
 200          } else if (document.all) {
 201              theSelection = divarea.innerText;
 202          } else if (divarea.textContent) {
 203              theSelection = divarea.textContent;
 204          } else if (divarea.firstChild.nodeValue) {
 205              theSelection = divarea.firstChild.nodeValue;
 206          }
 207      }
 208  
 209      if (theSelection) {
 210          if (bbcodeEnabled) {
 211              attributes.author = username;
 212              insert_text(generateQuote(theSelection, attributes));
 213          } else {
 214              insert_text(username + ' ' + l_wrote + ':' + '\n');
 215              var lines = split_lines(theSelection);
 216              for (i = 0; i < lines.length; i++) {
 217                  insert_text('> ' + lines[i] + '\n');
 218              }
 219          }
 220      }
 221  }
 222  
 223  /**
 224  * Create a quote block for given text
 225  *
 226  * Possible attributes:
 227  *   - author:  author's name (usually a username)
 228  *   - post_id: post_id of the post being quoted
 229  *   - user_id: user_id of the user being quoted
 230  *   - time:    timestamp of the original message
 231  *
 232  * @param  {!string} text       Quote's text
 233  * @param  {!Object} attributes Quote's attributes
 234  * @return {!string}            Quote block to be used in a new post/text
 235  */
 236  function generateQuote(text, attributes) {
 237      text = text.replace(/^\s+/, '').replace(/\s+$/, '');
 238      var quote = '[quote';
 239      if (attributes.author) {
 240          // Add the author as the BBCode's default attribute
 241          quote += '=' + formatAttributeValue(attributes.author);
 242          delete attributes.author;
 243      }
 244      for (var name in attributes) {
 245          if (attributes.hasOwnProperty(name)) {
 246              var value = attributes[name];
 247              quote += ' ' + name + '=' + formatAttributeValue(value.toString());
 248          }
 249      }
 250      quote += ']';
 251      var newline = ((quote + text + '[/quote]').length > 80 || text.indexOf('\n') > -1) ? '\n' : '';
 252      quote += newline + text + newline + '[/quote]';
 253  
 254      return quote;
 255  }
 256  
 257  /**
 258  * Format given string to be used as an attribute value
 259  *
 260  * Will return the string as-is if it can be used in a BBCode without quotes. Otherwise,
 261  * it will use either single- or double- quotes depending on whichever requires less escaping.
 262  * Quotes and backslashes are escaped with backslashes where necessary
 263  *
 264  * @param  {!string} str Original string
 265  * @return {!string}     Same string if possible, escaped string within quotes otherwise
 266  */
 267  function formatAttributeValue(str) {
 268      if (!/[ "'\\\]]/.test(str)) {
 269          // Return as-is if it contains none of: space, ' " \ or ]
 270          return str;
 271      }
 272      var singleQuoted = "'" + str.replace(/[\\']/g, '\\$&') + "'",
 273          doubleQuoted = '"' + str.replace(/[\\"]/g, '\\$&') + '"';
 274  
 275      return (singleQuoted.length < doubleQuoted.length) ? singleQuoted : doubleQuoted;
 276  }
 277  
 278  function split_lines(text) {
 279      var lines = text.split('\n');
 280      var splitLines = new Array();
 281      var j = 0;
 282      var i;
 283  
 284      for(i = 0; i < lines.length; i++) {
 285          if (lines[i].length <= 80) {
 286              splitLines[j] = lines[i];
 287              j++;
 288          } else {
 289              var line = lines[i];
 290              var splitAt;
 291              do {
 292                  splitAt = line.indexOf(' ', 80);
 293  
 294                  if (splitAt === -1) {
 295                      splitLines[j] = line;
 296                      j++;
 297                  } else {
 298                      splitLines[j] = line.substring(0, splitAt);
 299                      line = line.substring(splitAt);
 300                      j++;
 301                  }
 302              }
 303              while(splitAt !== -1);
 304          }
 305      }
 306      return splitLines;
 307  }
 308  
 309  /**
 310  * From http://www.massless.org/mozedit/
 311  */
 312  function mozWrap(txtarea, open, close) {
 313      var selLength = (typeof(txtarea.textLength) === 'undefined') ? txtarea.value.length : txtarea.textLength;
 314      var selStart = txtarea.selectionStart;
 315      var selEnd = txtarea.selectionEnd;
 316      var scrollTop = txtarea.scrollTop;
 317  
 318      var s1 = (txtarea.value).substring(0,selStart);
 319      var s2 = (txtarea.value).substring(selStart, selEnd);
 320      var s3 = (txtarea.value).substring(selEnd, selLength);
 321  
 322      txtarea.value = s1 + open + s2 + close + s3;
 323      txtarea.selectionStart = selStart + open.length;
 324      txtarea.selectionEnd = selEnd + open.length;
 325      txtarea.focus();
 326      txtarea.scrollTop = scrollTop;
 327  
 328      return;
 329  }
 330  
 331  /**
 332  * Insert at Caret position. Code from
 333  * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
 334  */
 335  function storeCaret(textEl) {
 336      if (textEl.createTextRange && document.selection) {
 337          textEl.caretPos = document.selection.createRange().duplicate();
 338      }
 339  }
 340  
 341  /**
 342  * Caret Position object
 343  */
 344  function caretPosition() {
 345      var start = null;
 346      var end = null;
 347  }
 348  
 349  /**
 350  * Get the caret position in an textarea
 351  */
 352  function getCaretPosition(txtarea) {
 353      var caretPos = new caretPosition();
 354  
 355      // simple Gecko/Opera way
 356      if (txtarea.selectionStart || txtarea.selectionStart === 0) {
 357          caretPos.start = txtarea.selectionStart;
 358          caretPos.end = txtarea.selectionEnd;
 359      }
 360      // dirty and slow IE way
 361      else if (document.selection) {
 362          // get current selection
 363          var range = document.selection.createRange();
 364  
 365          // a new selection of the whole textarea
 366          var range_all = document.body.createTextRange();
 367          range_all.moveToElementText(txtarea);
 368  
 369          // calculate selection start point by moving beginning of range_all to beginning of range
 370          var sel_start;
 371          for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++) {
 372              range_all.moveStart('character', 1);
 373          }
 374  
 375          txtarea.sel_start = sel_start;
 376  
 377          // we ignore the end value for IE, this is already dirty enough and we don't need it
 378          caretPos.start = txtarea.sel_start;
 379          caretPos.end = txtarea.sel_start;
 380      }
 381  
 382      return caretPos;
 383  }
 384  
 385  /**
 386  * Allow to use tab character when typing code
 387  * Keep indentation of last line of code when typing code
 388  */
 389  (function($) {
 390      $(document).ready(function() {
 391          var doc, textarea;
 392  
 393          // find textarea, make sure browser supports necessary functions
 394          if (document.forms[form_name]) {
 395              doc = document;
 396          } else {
 397              doc = opener.document;
 398          }
 399  
 400          if (!doc.forms[form_name]) {
 401              return;
 402          }
 403  
 404          textarea = doc.forms[form_name].elements[text_name];
 405  
 406          phpbb.applyCodeEditor(textarea);
 407          if ($('#attach-panel').length) {
 408              phpbb.showDragNDrop(textarea);
 409          }
 410  
 411          $('textarea').on('keydown', function (e) {
 412              if (e.which === 13 && (e.metaKey || e.ctrlKey)) {
 413                  $(this).closest('form').find(':submit').click();
 414              }
 415          });
 416      });
 417  })(jQuery);
 418  


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1