$(document).ready(function() {
  // setup html editor
  var imgUploadHtml = $('#html-editor-image-upload-tab').html();
  var handler;

  if (imgUploadHtml) {
    var options = {
        "html": true,
      customTemplates: {
        image: function(locale) {
          return imgUploadHtml;
        }
      }
    };
    handler = function() { $(this).wysihtml5(options); };
  } else {
    handler = function() { $(this).wysihtml5(); };
  }

  $(document).on('DOMNodeInserted', function(e) {
      $('textarea.html-editor', e.target).each(handler);
  })
});

$(document).ready(function() {
  // setup html editor
  var imgUploadHtml = $('#html-editor-image-upload-tab').html();
  var options = undefined;

  if (imgUploadHtml) {
    options = {
      html: true,
      customTemplates: {
        image: function(locale) {
          return imgUploadHtml;
        }
      }
    };
  }

  var handler = function() {
    var $this = $(this);
    if ($this.data('wysihtml5')) {
      // We need to reinitialise the component; otherwise the iframe is as good as dead
      // see https://github.com/xing/wysihtml5/issues/148
      $this.css('display', 'block');
      var input = $this.val();
      var $parent = $this.parent();

      $this.siblings('.wysihtml5-sandbox, .wysihtml5-toolbar, input[name="_wysihtml5_mode"]').remove();
      $('', $parent).remove();
      $this.val(input);
    }

    $(this).wysihtml5(options);
  };

  $(document).on('DOMNodeInserted', function(e) {
    $('textarea.html-editor', e.target).each(handler);
  });
});

Allow html editor components to be moved around the page.