ubuntu-it QuickReply Enhanced Editor

Aggiunge alcune delle funzionalità dell'editor completo alla risposta rapida

2015-07-23 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        ubuntu-it QuickReply Enhanced Editor
// @description Aggiunge alcune delle funzionalità dell'editor completo alla risposta rapida
// @namespace   ubuntu-it
// @include     http://forum.ubuntu-it.org/viewtopic.php?*
// @version     201507231337
// @grant       none
// ==/UserScript==


(function($, window) {
    $.fn.selection = function() {
        var doc = window.doc;
        var element = this[0];
        var selection = {};
        if (window.getSelection) {
            /* except IE */
            selection.start = element.selectionStart;
            selection.end = element.selectionEnd;
            selection.text = element.value.slice(selection.start, selection.end);
        } else if (doc.selection) {
            /* for IE */
            element.focus();
            var range = doc.selection.createRange(),
                range2 = doc.body.createTextRange();
            selection.text = range.text;
            try {
                range2.moveToElementText(element);
                range2.setEndPoint('StartToStart', range);
            } catch (e) {
                range2 = element.createTextRange();
                range2.setEndPoint('StartToStart', range);
            }
            selection.start = element.value.length - range2.text.length;
            selection.end = selection.start + range.text.length;
        }
        return selection;
    }
})(jQuery, window);

//Pulsanti da inserire nell'editor
var buttons = [{
    code: 'b',
    name: '<strong>B</strong>'
}, {
    code: 'u',
  name: '<span style="text-decoration: underline;">u</span>'
}, {
    code: 'i',
    name: '<i>i</i>'
}, {
    code: 'code',
    name: 'Codice'
}, {
    code: 'quote',
    name: 'Cita'
}, {
    code: 'url',
    name: 'URL'
}, ];
jQuery('input[name="show_qr"]').click(function(){  
  var previewBtn = $('<input class="button2" type="submit" value="Anteprima" name="preview" tabindex="8" accesskey="a">');
  previewBtn.click(function(){
    var action = $(this).parents('form').attr('action');
    $(this).parents('form').attr('action', action+'#preview');
  });
  jQuery('.submit-buttons').append(previewBtn);
  jQuery('#message-box').each(function() {
      var $buttons = $('<div id="buttons"></div>');
      $(this).prepend($buttons);
      var $textarea = $('.inputbox', this);
      $.each(buttons, function(i, button) {
          var $btn = $('<button type="button">' + button.name + '</button>');
          $buttons.append($btn);
          $btn.click(function(e) {
              e.preventDefault();
              var textarea = $textarea[0];
              var scrollTop = textarea.scrollTop;
              var selection = $textarea.selection();

              var val = $textarea.val();
              var pre = val.substring(0, selection.start);
              var code = button.code;
              var text = selection.text || '';
              if( code === 'url' ){
                  code += '=' + prompt("Inserire l'URL");
                  if( text.length === 0 ){
                      text += prompt("Inserire il testo");
                  }
              }
              var bbcode = '[' + code + ']' + text + '[/' + button.code + ']';
              var post = val.substring(selection.end);

              var newSelectionStart = selection.start + 2 + button.code.length;
              var newSelectionStop = newSelectionStart + selection.text.length;
              $textarea.val(pre + bbcode + post);
              textarea.selectionStart = newSelectionStart;
              textarea.selectionEnd = newSelectionStop;
              textarea.focus();
              textarea.scrollTop = scrollTop;

          });
      });
  });
});