MyBB Community Forums

Full Version: CodePress bug - no tab insertion/indention!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
It's a major headache working with templates in the adminCP without the ability to insert tab character. I have solved the issue and also added the possibility of code indention for FireFox 3.

Find in admin/jscripts/codepress/engines/gecko.js:

		for (var i=0; i<snippets.length; i++) {
			if(snippets[i].input == trigger) {
				var content = snippets[i].output.replace(/</g,'&lt;');
				content = content.replace(/>/g,'&gt;');
				if(content.indexOf('$0')<0) content += cc;
				else content = content.replace(/\$0/,cc);
				content = content.replace(/\n/g,'<br>');
				var pattern = new RegExp(trigger+cc,'gi');
				evt.preventDefault(); // prevent the tab key from being added
				this.syntaxHighlight('snippets',pattern,content);
			}
		}

Replace with:

		// modified to insert tab on FF
		var noInsertion = 1;
		for (var i=0; i<snippets.length; i++) {
			if(snippets[i].input == trigger) {
        noInsertion = 0;
				var content = snippets[i].output.replace(/</g,'&lt;');
				content = content.replace(/>/g,'&gt;');
				if(content.indexOf('$0')<0) content += cc;
				else content = content.replace(/\$0/,cc);
				content = content.replace(/\n/g,'<br>');
				var pattern = new RegExp(trigger+cc,'gi');
				evt.preventDefault(); // prevent the tab key from being added
				this.syntaxHighlight('snippets',pattern,content);
			}
		}

		/* Modified to insert tab on FF
		 * @author Asad Khan <asadkn [at-the-rate-of] gmail [dot] com>
		 */
    if (noInsertion == 1) {

      var range = window.getSelection().getRangeAt(0);
      
      // code indention for multiple lines...
      if (range.commonAncestorContainer.getElementsByTagName) {
        var childNodes = range.commonAncestorContainer.getElementsByTagName('*');
        var lastBr;
        
        for (var i = 0; i < childNodes.length; i++) 
        {
          srcRange = document.createRange();
          srcRange.selectNode(childNodes[i]);
          
          // in range?
          if (srcRange.compareBoundaryPoints(Range.START_TO_START, range) >= 0 && srcRange.compareBoundaryPoints(Range.END_TO_END, range) <= 0) {
            
            if (lastBr) {
              childNodes[i].innerHTML = '\t' + childNodes[i].innerHTML;
              lastBr = '';
            }

            if (childNodes[i].tagName.toLowerCase() == 'br') {
              lastBr = childNodes[i];
            }
          }
        }
        
      }
      // for the single line
      range.setEnd(range.startContainer, range.startOffset);
      
      var ele = document.createTextNode('\t');
      range.insertNode(ele);
      range.setStartAfter(ele);
      
      evt.preventDefault();
    }

This will allow you to:
  1. Indent code spanned over multiple lines by selecting the lines and pressing the tab key.
  2. Use the tab key to add a tab character instead of hopping over the next input area.


If you simply want the option #2, use the following code instead: (It's a bug that I decided to fix, so MyBB should add this code at least)
      var range = window.getSelection().getRangeAt(0);

      // for the single line
      range.setEnd(range.startContainer, range.startOffset);
      
      var ele = document.createTextNode('\t');
      range.insertNode(ele);
      range.setStartAfter(ele);
      
      evt.preventDefault();

Enjoy better editing in FF3 Smile
Thank you Asad, I´ll watch for this carefully tomorow.

I´m using FF3 too
This could be nice. Noticing at the codepress site this is not a feature yet. But rather a planned feature.
Is this actually a "bug" or a feature suggestion?
As mentioned in the #2, it's actually a bug. Smile

If you press TAB key on IE7, a tab character is insert (spacing) but the same isn't true for FF3. So the #2 fixes the bug, but #1 is a new feature only for FF3 (indent code over multiple lines).
Hmm... I think the actual answer was this isn't a bug. It's a feature suggestion, moving...
Yeah but part of it is a bug. It's a bug because it behaves differently than it's supposed to. If you press a tab key, it's supposed to add a tab character as it does in IE or older versions of FireFox, but doesn't do it in FF3.

Yeah, rest of it is an enhancement/feature.