Current time: 05-21-2012, 09:25 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodePress bug - no tab insertion/indention!
08-03-2008, 03:05 PM (This post was last modified: 08-03-2008 03:42 PM by Asad_Niazi.)
Post: #1
CodePress bug - no tab insertion/indention!
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:

Code:
        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:

Code:
        // 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)
Code:
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

Linux Server overloading explained
Move MySQL to a second drive
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 03:40 PM
Post: #2
RE: CodePress bug - no tab insertion/indention!
Thank you Asad, I´ll watch for this carefully tomorow.

I´m using FF3 too

[]s, Claudio

Tutorial: Jquery (by google API) with mybb (Prototype)
Distinction between Paid/Free Plugins Threads
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 03:42 PM
Post: #3
RE: CodePress bug - no tab insertion/indention!
This could be nice. Noticing at the codepress site this is not a feature yet. But rather a planned feature.

Are you an avatar movie fan?
[Image: avatarzd.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 03:56 PM
Post: #4
RE: CodePress bug - no tab insertion/indention!
Is this actually a "bug" or a feature suggestion?
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 03:57 PM
Post: #5
RE: CodePress bug - no tab insertion/indention!
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).

Linux Server overloading explained
Move MySQL to a second drive
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 03:59 PM
Post: #6
RE: CodePress bug - no tab insertion/indention!
Hmm... I think the actual answer was this isn't a bug. It's a feature suggestion, moving...
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2008, 04:02 PM
Post: #7
RE: CodePress bug - no tab insertion/indention!
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.

Linux Server overloading explained
Move MySQL to a second drive
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | MyBB | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication