Contributing back some bug fixes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bbcode.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function BBSpoiler(link) {
  2. if ($(link.nextSibling).has_class('hidden')) {
  3. $(link.nextSibling).gshow();
  4. $(link).html('Hide');
  5. if ($(link).attr("value")) {
  6. $(link).attr("value", "Hide" + $(link).attr("value").substring(4))
  7. }
  8. } else {
  9. $(link.nextSibling).ghide();
  10. $(link).html('Show');
  11. if ($(link).attr("value")) {
  12. $(link).attr("value", "Show" + $(link).attr("value").substring(4))
  13. }
  14. }
  15. }
  16. function wrapSelected(box, wrap, offset) {
  17. if (!Array.isArray(wrap)) wrap = [wrap, wrap]
  18. if (wrap.length < 2) wrap[1] = wrap[0]
  19. var s = box.selectionStart
  20. var e = box.selectionEnd
  21. var v = box.value
  22. box.value = v.slice(0,s)+wrap[0]+v.slice(s,e)+wrap[1]+v.slice(e)
  23. box.focus()
  24. box.selectionEnd = (offset!==undefined?s+offset:e+wrap[0].length)
  25. }
  26. function BBEditor(box) {
  27. if (box.previousSibling && box.previousSibling.className == 'bbcode_bar') return
  28. let buttons = [
  29. {short:'B', name:'Bold', wrap:['[b]','[/b]']},
  30. {short:'I', name:'Italic', wrap:['[i]','[/i]']},
  31. {short:'U', name:'Underline', wrap:['[u]','[/u]']},
  32. {short:'S', name:'Strikethrough', wrap:['[s]','[/s]']},
  33. {short:'Left', name:'Align Left', wrap:['[align=left]','[/align]']},
  34. {short:'Center', name:'Align Center', wrap:['[align=center]','[/align]']},
  35. {short:'Right', name:'Align Right', wrap:['[align=right]','[/align]']},
  36. {short:'Pre', name:'Preformatted', wrap:['[pre]','[/pre]']},
  37. {short:'H1', name:'Subheading 1', wrap:'=='},
  38. {short:'H2', name:'Subheading 2', wrap:'==='},
  39. {short:'H3', name:'Subheading 3', wrap:'===='},
  40. {short:'Color', name:'Color', wrap:['[color=]','[/color]'], offset:7},
  41. {short:'TeX', name:'LaTeX', wrap:['[tex]','[/tex]']},
  42. {short:'Quote', name:'Quote', wrap:['[quote]','[/quote]']},
  43. {short:'List', name:'List', wrap:['[*]','']},
  44. {short:'Hide', name:'Spoiler', wrap:['[spoiler]','[/spoiler]']},
  45. {short:'Img', name:'Image', wrap:['[img]','[/img]']},
  46. {short:'Vid', name:'Video', wrap:['[embed]','[/embed]']},
  47. {short:'Link', name:'Link', wrap:['[url]','[/url]']},
  48. {short:'Torr', name:'Torrent', wrap:['[torrent]','[/torrent]']}
  49. ]
  50. let bar = document.createElement('ul')
  51. bar.className = "bbcode_bar"
  52. bar.style.width = box.offsetWidth+'px'
  53. // Let the DOM update and then snap the size again (twice)
  54. setTimeout(function() {
  55. bar.style.width = box.offsetWidth+'px'
  56. bar.style.width = box.offsetWidth+'px'
  57. }, 1)
  58. for (let button of buttons) {
  59. li = document.createElement('li')
  60. b = document.createElement('a')
  61. b.setAttribute('title', button.name)
  62. b.innerHTML = button.short
  63. b.addEventListener('click', e=>wrapSelected(box, button.wrap, button.offset))
  64. li.appendChild(b)
  65. bar.appendChild(li)
  66. }
  67. box.parentNode.insertBefore(bar, box)
  68. }
  69. $(function() {
  70. $('.bbcode_editor').each(function(i, el) { BBEditor(el) })
  71. $('.spoilerButton').each(function(i, el) {
  72. el.addEventListener("click", ()=>BBSpoiler(el))
  73. })
  74. })