Community Central
Community Central
No edit summary
No edit summary
Line 16: Line 16:
 
switch (editorInstance.mode) {
 
switch (editorInstance.mode) {
 
case 'wysiwyg':
 
case 'wysiwyg':
editorInstance.getEditbox().append(sine);
+
var wysiwyg = editorInstance.getEditbox();
  +
var textOnly = wysiwyg.text().trim();
  +
if (!textOnly.length || sine == textOnly.substr(-4)) return;
  +
var lastBlock = wysiwyg.find('> *:last-child');
  +
if (lastBlock.is('p')) {
  +
if (lastBlock.text().trim().length) sine = ' -- ' + sine;
  +
var lastElement = lastBlock.find(':last-child');
  +
lastElement[lastElement.is('br') ? 'before' : 'after'](sine);
  +
} else {
  +
while (!lastBlock.is('p') && lastBlock.prev().length) lastBlock = lastBlock.prev();
  +
$('<p>' + sine + '</p>').appendTo(wysiwyg).css({
  +
marginLeft: lastBlock.css('marginLeft')
  +
});
  +
}
 
break;
 
break;
 
case 'source':
 
case 'source':
var content = editorInstance.getContent();
+
var source = editorInstance.getContent();
if (!content.trim().length) return; // if there's no content abort
+
if (!source.trim().length) return;
var lines = content.split(/\r|\n|\r\n/).reverse(); // split into lines and start at the bottom
+
var lines = source.split(/\r\n|\r|\n/).reverse();
 
var l = lines[0].trim();
 
var l = lines[0].trim();
if (l.length) { // is there content on the last line?
+
if (l.length) {
if (sine == l.substr(-4)) return; // already sined
+
if (sine == l.substr(-4)) return;
var block = /^[*#]/.test(l) || /^(={2,6})[^=]+\1$/.test(l) || /(?:----|<\/(?:blockquote|div|pre|code|nowiki)>)$/.test(l)
+
var isBlock = /^[*#]/.test(l) || /^(={2,6})[^=]+\1$/.test(l) ||
  +
/(?:----|}}|<\/(?:blockquote|div|pre|code|nowiki)>)$/.test(l);
sine = (block ? "\r\n" : " -- ") + sine; // if last line contains block element insert newline, else --
+
sine = (isBlock ? "\r\n" : " -- ") + sine;
 
} else {
 
} else {
for (var i = 1; i < lines.length; i++) { // find out about indentation
+
for (var i = 1; i < lines.length; i++) {
 
l = lines[i].trim();
 
l = lines[i].trim();
 
if (!l.length) continue;
 
if (!l.length) continue;
if (sine == l.substr(-4)) return; // already sined
+
if (sine == l.substr(-4)) return;
 
m = l.match(/^(\:+)/);
 
m = l.match(/^(\:+)/);
 
if (m) sine = m[1] + sine;
 
if (m) sine = m[1] + sine;
Line 37: Line 51:
 
}
 
}
 
}
 
}
editorInstance.setContent(content + sine);
+
editorInstance.setContent(source + sine);
 
break;
 
break;
 
}
 
}
Line 63: Line 77:
   
 
UPDATE3: made the sining for "source" editor a little smarter: when the sine would be on the same line with a block element, a newline is inserted; when the sine is on a line of its own, it will be indented if the preceding lines were indented -- [[User:Pecoes|Pecoes]] 08:50, March 10, 2012 (UTC)
 
UPDATE3: made the sining for "source" editor a little smarter: when the sine would be on the same line with a block element, a newline is inserted; when the sine is on a line of its own, it will be indented if the preceding lines were indented -- [[User:Pecoes|Pecoes]] 08:50, March 10, 2012 (UTC)
  +
  +
UPDATE4: after a serious bit of reverse engineering I think I figured out to correctly append the sine to the RTE as well. -- [[User:Pecoes|Pecoes]] 15:13, March 10, 2012 (UTC)

Revision as of 15:13, 10 March 2012

Forums: Admin Central Index Watercooler Testers needed for automated signature!
Central's forums are a place for the community to help other members.
To contact staff directly or to report bugs, please use Special:Contact.
Note: This topic has been unedited for 4423 days. It is considered archived - the discussion is over. Do not add to unless it really needs a response.



I'm the sort of person who constantly forgets to sign his posts. But I'm also a programmer and as such I fail to see why I should do what a script could do in my stead. This is this script:

// Sine:
$(function () {
    talkNameSpaces = [1,3,5,7,9,11,13,15,110,111,114,401,500,501,502,503];
    if (-1 < $.inArray(wgNamespaceNumber, talkNameSpaces) && 'edit' == $.getUrlVar('action')) {
        appendSine = function () {
            if ($('input#wpMinoredit').is(':checked')) return;
            var sine = '~~' + String.fromCharCode(126) + '~';
            switch (editorInstance.mode) {
                case 'wysiwyg':
                    var wysiwyg = editorInstance.getEditbox();
                    var textOnly = wysiwyg.text().trim();
                    if (!textOnly.length || sine == textOnly.substr(-4)) return;
                    var lastBlock = wysiwyg.find('> *:last-child');
                    if (lastBlock.is('p')) {
                        if (lastBlock.text().trim().length) sine = ' -- ' + sine;
                        var lastElement = lastBlock.find(':last-child');
                        lastElement[lastElement.is('br') ? 'before' : 'after'](sine);
                    } else {
                        while (!lastBlock.is('p') && lastBlock.prev().length) lastBlock = lastBlock.prev();
                        $('<p>' + sine + '</p>').appendTo(wysiwyg).css({
                            marginLeft: lastBlock.css('marginLeft')
                        });
                    }
                break;
                case 'source':
                    var source = editorInstance.getContent();
                    if (!source.trim().length) return;
                    var lines = source.split(/\r\n|\r|\n/).reverse();
                    var l = lines[0].trim();
                    if (l.length) {
                        if (sine == l.substr(-4)) return;
                        var isBlock = /^[*#]/.test(l) || /^(={2,6})[^=]+\1$/.test(l) ||
                            /(?:----|}}|<\/(?:blockquote|div|pre|code|nowiki)>)$/.test(l);
                        sine = (isBlock ? "\r\n" : " -- ") + sine;
                    } else {
                        for (var i = 1; i < lines.length; i++) {
                            l = lines[i].trim();
                            if (!l.length) continue;
                            if (sine == l.substr(-4)) return;
                            m = l.match(/^(\:+)/);
                            if (m) sine = m[1] + sine;
                            break;
                        }
                    }
                    editorInstance.setContent(source + sine);
                break;
            }
        }
        $('input#wpSave').click(appendSine);
        $('a#publish').click(appendSine);
    }
});

Or at least I hope it is. I did test it and it does seem to work. But it would be helpful if a few others would test it as well before I publish it at the dev wiki.

To install the script go to your global.js and copypaste the code from here to there. You will need to refresh both global.js and the test page. You may also have to wait a while for the script to take effect or purge manually. I'm not certain about the latter.

Note: If you don't want to have your post signed, check "Minor edit".

Let me know whether it does or does not work! Suggestions and critique are welcome too!

-- Pecoes 06:29, March 9, 2012 (UTC)


UPDATE1: added check for input#wpMinoredit. -- Pecoes 07:03, March 9, 2012 (UTC)

UPDATE2: replaced call to editorInstance.setContent() with call to editorInstance.getEditbox().append() for WYSIWYG editor -- Pecoes 09:24, March 9, 2012 (UTC)

UPDATE3: made the sining for "source" editor a little smarter: when the sine would be on the same line with a block element, a newline is inserted; when the sine is on a line of its own, it will be indented if the preceding lines were indented -- Pecoes 08:50, March 10, 2012 (UTC)

UPDATE4: after a serious bit of reverse engineering I think I figured out to correctly append the sine to the RTE as well. -- Pecoes 15:13, March 10, 2012 (UTC)