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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 EmojiBox(box) {
  27. let opened = false
  28. let emojis = ['😀','😁','😂','🤣','😃','😄','😅','😆','😉','😊','😋','😎','😍','😘','😗','😙','😚','🙂','🤗','🤔','😐','😑','😶','🙄','😏','😣','😥','😮','🤐','😯','😪','😫','😴','😌','😛','😜','😝','🤤','😒','😓','😔','😕','🙃','🤑','😲','🙁','😖','😞','😟','😤','😢','😭','😦','😧','😨','😩','😬','😰','😱','😳','😵','😡','😠','😷','🤒','🤕','🤢','🤧','😇','🤠','🤡','🤥','🤓','\n','😈','👿','👹','👺','💀','👻','👽','🤖','💩','😺','😸','😹','😻','😼','😽','🙀','😿','😾','\n','🍇','🍈','🍉','🍊','🍋','🍌','🍍','🍎','🍏','🍐','🍑','🍒','🍓','🥝','🍅','🥑','🍆','🥔','🥕','🌽','🌶','🥒','🍄','🥜','🌰','🍞','🥐','🥖','🥞','🧀','🍖','🍗','🥓','🍔','🍟','🍕','🌭','🌮','🌯','🍳','🍲','🥗','🍿','🍱','🍘','🍙','🍚','🍛','🍜','🍝','🍠','🍢','🍣','🍤','🍥','🍡','🍦','🍧','🍨','🍩','🍪','🎂','🍰','🍫','🍬','🍭','🍮','🍯','🍼','🥛','🍵','🍶','🍾','🍷','🍸','🍹','🍺','🍻','🥂','🥃','🍽','🍴','🥄','\n','🛀','🛌','💌','💣','🕳','🛍','📿','💎','🔪','🏺','🗺','💈','🛢','🛎','⌛','⏳','⌚','⏰','⏱','⏲','🕰','🌡','⛱','🎈','🎉','🎊','🎎','🎏','🎐','🎀','🎁','🔮','🕹','🖼','📯','🎙','🎚','🎛','📻','📱','📲','📞','📟','📠','🔋','🔌','💻','🖥','🖨','🖱','🖲','💽','💾','💿','📀','🎥','🎞','📽','📺','📷','📸','📹','📼','🔍','🔎','🕯','💡','🔦','🏮','📔','📕','📖','📗','📘','📙','📚','📓','📃','📜','📄','📰','🗞','📑','🔖','🏷','💰','💴','💵','💶','💷','💸','💳','📧','📨','📩','📤','📥','📦','📫','📪','📬','📭','📮','🗳','🖋','🖊','🖌','🖍','📝','📁','📂','🗂','📅','📆','🗒','🗓','📇','📈','📉','📊','📋','📌','📍','📎','🖇','📏','📐','🗃','🗄','🗑','🔒','🔓','🔏','🔐','🔑','🗝','🔨','⛏','🛠','🗡','🔫','🛡','🔧','🔩','🗜','🔗','⛓','🔬','🔭','📡','💉','💊','🚪','🛏','🛋','🚽','🚿','🛁','🚬','🗿','🚰','\n','💪','👈','👉','👆','🖕','👇','🤞','🖖','🤘','🖐','✋','👌','👍','👎','✊','👊','🤛','🤜','🤚','👋','👏','👐','🙌','🙏','🤝']
  29. let ebox = document.createElement('div')
  30. ebox.className = 'emoji_box border'
  31. for (let emoji of emojis) {
  32. if (emoji === '\n') {
  33. let br = document.createElement('br')
  34. ebox.appendChild(br)
  35. continue;
  36. }
  37. let a = document.createElement('a')
  38. a.innerHTML = emoji
  39. a.addEventListener('click', e => {
  40. wrapSelected(box, [emoji,''])
  41. e.stopPropagation()
  42. })
  43. ebox.appendChild(a)
  44. }
  45. return event => {
  46. if (!opened) {
  47. event.target.parentElement.appendChild(ebox)
  48. let f = e => {
  49. event.target.nextSibling.remove()
  50. opened = false
  51. document.removeEventListener('click', f)
  52. }
  53. window.setTimeout(_ => document.addEventListener('click', f), 1)
  54. opened = true
  55. }
  56. }
  57. }
  58. function BBEditor(box) {
  59. if (box.previousSibling && box.previousSibling.className == 'bbcode_bar') return
  60. let buttons = [
  61. {short:'B', name:'Bold', wrap:['[b]','[/b]']},
  62. {short:'I', name:'Italic', wrap:['[i]','[/i]']},
  63. {short:'U', name:'Underline', wrap:['[u]','[/u]']},
  64. {short:'S', name:'Strikethrough', wrap:['[s]','[/s]']},
  65. {short:'Left', name:'Align Left', wrap:['[align=left]','[/align]']},
  66. {short:'Center', name:'Align Center', wrap:['[align=center]','[/align]']},
  67. {short:'Right', name:'Align Right', wrap:['[align=right]','[/align]']},
  68. {short:'Pre', name:'Preformatted', wrap:['[pre]','[/pre]']},
  69. {short:'H1', name:'Subheading 1', wrap:'=='},
  70. {short:'H2', name:'Subheading 2', wrap:'==='},
  71. {short:'H3', name:'Subheading 3', wrap:'===='},
  72. {short:'Color', name:'Color', wrap:['[color=]','[/color]'], offset:7},
  73. {short:'TeX', name:'LaTeX', wrap:['[tex]','[/tex]']},
  74. {short:'Quote', name:'Quote', wrap:['[quote]','[/quote]']},
  75. {short:'List', name:'List', wrap:['[*]','']},
  76. {short:'Hide', name:'Spoiler', wrap:['[spoiler]','[/spoiler]']},
  77. {short:'Img', name:'Image', wrap:['[img]','[/img]']},
  78. {short:'Vid', name:'Video', wrap:['[embed]','[/embed]']},
  79. {short:'Link', name:'Link', wrap:['[url]','[/url]']},
  80. {short:'Torr', name:'Torrent', wrap:['[torrent]','[/torrent]']},
  81. {short:'😃', name:'Emoji', func:EmojiBox(box)}
  82. ]
  83. let bar = document.createElement('ul')
  84. bar.className = "bbcode_bar"
  85. bar.style.width = box.offsetWidth+'px'
  86. // Let the DOM update and then snap the size again (twice)
  87. setTimeout(function() {
  88. bar.style.width = box.offsetWidth+'px'
  89. bar.style.width = box.offsetWidth+'px'
  90. }, 1)
  91. for (let button of buttons) {
  92. li = document.createElement('li')
  93. b = document.createElement('a')
  94. b.setAttribute('title', button.name)
  95. b.innerHTML = button.short
  96. if (button.wrap) b.addEventListener('click', e=>wrapSelected(box, button.wrap, button.offset))
  97. else if (button.func) b.addEventListener('click', button.func)
  98. li.appendChild(b)
  99. bar.appendChild(li)
  100. }
  101. box.parentNode.insertBefore(bar, box)
  102. }
  103. $(function() {
  104. $('.bbcode_editor').each((i, el) => BBEditor(el))
  105. $(document).on('click', '.spoilerButton', e=>BBSpoiler(e.target))
  106. })