Markdown toolbar for gitlab.com

Markdown toolbar for gitlab.com issues

  1. // ==UserScript==
  2. // @name Markdown toolbar for gitlab.com
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Markdown toolbar for gitlab.com issues
  6. // @author sharkoz
  7. // @match https://gitlab.com/*/issues/*/edit
  8. // @grant GM_addStyle
  9. //
  10. // This is a modified version of the script "Markdown toolbar for GreasyFork and UserStyles.org" ()https://greatest.deepsurf.us/en/scripts/6779-markdown-toolbar-for-greasyfork-and-userstyles-org)
  11. // Thanks a lot to wOxxOm for making that script.
  12. //
  13. // ==/UserScript==
  14. /* jshint -W097 */
  15. 'use strict';
  16.  
  17.  
  18.  
  19.  
  20. var x;
  21.  
  22. // IF IT'S A SUBMIT PAGE
  23. if (false && window.location.href.indexOf('submit') > - 1) {
  24. // THEN ADD TOOLBAR TO THE 'NEW POST' TEXTBOX
  25. x = document.querySelectorAll('div.md > textarea:nth-child(1)')[0].parentNode;
  26. addFeatures(x);
  27. }
  28. else {
  29. var textareas = document.querySelectorAll('textarea');
  30. console.log(textareas)
  31. // ADD TOOLBAR: TO EDITING YOUR POST, TO 'NEW COMMENT' FORM AND TO EDITING YOUR EXISTING COMMENT(S)
  32. for (var i = 0; i < textareas.length; i++) {
  33. console.log(i)
  34. x = document.querySelectorAll('textarea') [i].parentNode;
  35. addFeatures(x);
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. function addFeatures(n) {
  44. n.parentNode.textAreaNode = x.firstChild;
  45.  
  46. GM_addStyle('\
  47. .Button {\
  48. display: inline-block;\
  49. cursor: pointer;\
  50. margin: 0px;\
  51. font-size: 12px;\
  52. line-height: 1;\
  53. font-weight: bold;\
  54. padding: 4px 6px;\
  55. background: -moz-linear-gradient(center bottom , #CCC 0%, #FAFAFA 100%) repeat scroll 0% 0% #F8F8F8;\
  56. border: 1px solid #999;\
  57. border-radius: 2px;\
  58. white-space: nowrap;\
  59. text-shadow: 0px 1px 0px #FFF;\
  60. box-shadow: 0px 1px 0px #FFF inset, 0px -1px 2px #BBB inset;\
  61. color: #333;}');
  62.  
  63.  
  64. // add buttons
  65. btnMake(n, 'H3', 'H3', '\n### ','', true);
  66. btnMake(n, 'H2', 'H2', '\n## ','', true);
  67. btnMake(n, 'H1', 'H1', '\n# ','', true);
  68. btnMake(n, '<b>B</b>', 'Bold', '**');
  69. btnMake(n, '<i>I</i>', 'Italic', '*');
  70. // btnMake(n, '<u>U</u>', 'Underline', '<u>','</u>');
  71. // btnMake(n, '<s>S</s>', 'Strikethrough', '<s>','</s>');
  72. btnMake(n, '<s>S</s>', 'Strikethrough', '~~');
  73. btnMake(n, '^', 'Superscript', '^','', true);
  74. btnMake(n, '\\n', 'Line break', '&nbsp;\n', '', true);
  75. btnMake(n, '---', 'Horizontal line', '\n\n---\n\n', '', true);
  76. /*btnMake(n, 'URL', 'Add URL to selected text',
  77. function(e) {
  78. try {edWrapInTag('[', ']('+prompt('URL'+':')+')', edInit(e.target))}
  79. catch(e) {};
  80. });*/
  81. // btnMake(n, 'Image', 'Convert selected https://url to inline image', '!['+'image'+'](', ')');
  82. //btnMake(n, 'Table', 'Insert table template', '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n', '', true);
  83. btnMake(n, 'Code', 'Apply CODE markdown to selected text',
  84. function(e){
  85. var ed = edInit(e.target);
  86. if (ed.sel.indexOf('\n') < 0)
  87. edWrapInTag('`', '`', ed);
  88. else
  89. edWrapInTag(((ed.sel1==0) || (ed.text.charAt(ed.sel1-1) == '\n') ? '' : '\n') + '```' + (ed.sel.charAt(0) == '\n' ? '' : '\n'),
  90. (ed.sel.substr(-1) == '\n' ? '' : '\n') + '```' + (ed.text.substr(ed.sel2,1) == '\n' ? '' : '\n'),
  91. ed);
  92. });
  93. btnMake(n, 'Task', 'Task', '- [ ] ', '', true);
  94. }
  95.  
  96. function btnMake(afterNode, label, title, tag1, tag2, noWrap) {
  97. var a = document.createElement('a');
  98. a.className = 'Button';
  99. a.innerHTML = label;
  100. a.title = title;
  101. //a.style.setProperty('float','left');
  102.  
  103. a.addEventListener('click',
  104. typeof(tag1) == 'function'
  105. ? tag1
  106. : noWrap ? function(e){edInsertText(tag1, edInit(e.target))}
  107. : function(e){edWrapInTag(tag1, tag2, edInit(e.target))});
  108.  
  109. var nparent = afterNode.parentNode;
  110. a.textAreaNode = nparent.textAreaNode;
  111. nparent.insertBefore(a, nparent.firstElementChild);
  112. }
  113.  
  114.  
  115.  
  116. function edInit(btn) {
  117.  
  118. var ed = {node: btn.parentNode.textAreaNode } ;
  119.  
  120. ed.sel1 = ed.node.selectionStart;
  121. ed.sel2 = ed.node.selectionEnd,
  122. ed.text = ed.node.value;
  123. ed.sel = ed.text.substring(ed.sel1, ed.sel2);
  124. return ed;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. function edWrapInTag(tag1, tag2, ed) {
  131. ed.node.value = ed.text.substr(0, ed.sel1) + tag1 + ed.sel + (tag2?tag2:tag1) + ed.text.substr(ed.sel2);
  132. ed.node.setSelectionRange(ed.sel1 + tag1.length, ed.sel1 + tag1.length + ed.sel.length);
  133. ed.node.focus();
  134. }
  135.  
  136. function edInsertText(text, ed) {
  137. ed.node.value = ed.text.substr(0, ed.sel2) + text + ed.text.substr(ed.sel2);
  138. ed.node.setSelectionRange(ed.sel2 + text.length, ed.sel2 + text.length);
  139. ed.node.focus();
  140. }