Reddit - side-by-side editor and RES preview

Show the edit box and preview next to each other when writing comments if you have RES installed

  1. // ==UserScript==
  2. // @name Reddit - side-by-side editor and RES preview
  3. // @description Show the edit box and preview next to each other when writing comments if you have RES installed
  4. // @author James Skinner <spiralx@gmail.com> (http://github.com/spiralx)
  5. // @namespace http://spiralx.org/
  6. // @match *://*.reddit.com/r/*/comments/*
  7. // @match *://*.reddit.com/comments/*
  8. // @match *://*.reddit.com/message/*
  9. // @version 0.1.0
  10. // @grant none
  11. // @require https://greasyfork.org/scripts/7602-mutation-observer/code/mutation-observer.js
  12. // ==/UserScript==
  13.  
  14.  
  15. jQuery.fn.contentExpand = (function() {
  16. let $hidden = jQuery('<div>')
  17. .css({
  18. display: 'none',
  19. whiteSpace: 'pre-wrap',
  20. wordWrap: 'break-word',
  21. overflowWrap: 'break-word',
  22. overflow: 'hidden'
  23. })
  24. .appendTo('body');
  25.  
  26. return function() {
  27. return this.each(function() {
  28. let $ta = jQuery(this).css({
  29. overflow: 'hidden',
  30. resize: 'none'
  31. });
  32. $ta.on('keyup', ev => {
  33. let content = $ta.val().replace(/\n/g, '<br>') + '<br style="line-height: 3px">';
  34. $hidden
  35. .css({
  36. width: $ta.width(),
  37. minHeight: $ta.css('min-height'),
  38. font: $ta.css('font'),
  39. lineHeight: $ta.css('line-height')
  40. })
  41. .html(content);
  42. $ta.css('height', $hidden.height());
  43. });
  44. });
  45. }
  46. })();
  47.  
  48.  
  49. var observer = new MutationSummary({
  50. callback: function(summaries) {
  51. //console.info('Added %d forms', summaries[0].added.length);
  52. try {
  53. jQuery(summaries[0].added).each(function() {
  54. var $form = jQuery(this),
  55. $editWrapper = $form.children('.usertext-edit'),
  56. $mdWrapper = $editWrapper.children('.markdownEditor-wrapper'),
  57. $edit = $editWrapper.children('.md'),
  58. $res = $form.find('.livePreview');
  59. //console.log($form[0].outerHTML);
  60. //console.info($mdWrapper);
  61. //console.info($edit);
  62. //console.info($res);
  63. $editWrapper.css({
  64. width: '100%',
  65. padding: '0px',
  66. marginLeft: '15px'
  67. });
  68. $res.css({
  69. margin: '0 15px 15px'
  70. });
  71.  
  72. // Move RES editor next to textarea, wrap each in divs for positioning
  73. $edit
  74. .css({
  75. maxWidth: '100%',
  76. width: '100%',
  77. clear: 'both'
  78. })
  79. .append($res)
  80. .children()
  81. .first()
  82. .css({
  83. width: '100%'
  84. })
  85. .contentExpand()
  86. .wrap('<div style="float: left; width: 50%;">')
  87. .end()
  88. .last()
  89. .wrap('<div style="display: inline-block; width: 50%;">');
  90.  
  91.  
  92. // Wrap toolbar, extras in divs for positioning side-by-side
  93. $mdWrapper
  94. .css({
  95. maxWidth: '100%',
  96. width: '100%',
  97. clear: 'both'
  98. })
  99. .children()
  100. .slice(1)
  101. .wrapAll('<div style="display: inline-block; width: 50%;">')
  102. .end()
  103. .first()
  104. .wrap('<div style="float: left; width: 50%;">');
  105.  
  106.  
  107. // Swap macro dropdown and name around, float left and right respectively
  108. let $commentAs = $mdWrapper.find('.commentingAs'),
  109. $macro = $commentAs.next();
  110. $macro
  111. .css({
  112. 'float': 'left',
  113. margin: '0 0 0 15px'
  114. });
  115. $commentAs
  116. .css({
  117. 'float': 'right',
  118. clear: 'none',
  119. margin: '0 15px 0 0'
  120. })
  121. .insertAfter($macro);
  122. });
  123. }
  124. catch (ex) {
  125. console.error(ex);
  126. }
  127. },
  128. rootNode: document.body,
  129. queries: [
  130. { element: 'form.usertext' }
  131. ]
  132. });