Oppaitime's version of Gazelle
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 3.0KB

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