view app/javascript/src/main.coffee @ 469:68231013b01b bootstrap-4

Now with more bootstrap than ever
author nanaya <me@nanaya.pro>
date Sun, 23 Feb 2020 22:25:50 +0900
parents 802dcd44188e
children 46ed68bf9918
line wrap: on
line source

import 'bootstrap'
import hljs from 'highlight.js'
import * as commonmark from 'commonmark'

#= require jquery
#= require bootstrap/transition
#= require bootstrap/collapse
#= require bootstrap/modal
#= require bootstrap/tab
#= require highlightjs
#= require commonmark

$(document).on "click", ".js-paste-clear", (e) ->
  e.preventDefault()
  $(".js-paste-paste")
    .val("")
    .focus()


loadLanguageItems = ->
  $languageSelectBox = $("#paste_language")
  return unless $languageSelectBox.length

  for language in hljs.listLanguages().sort()
    $languageSelectBox.append $("<option />", value: language, text: language)


markdownfy = (plaintext) ->
  reader = new commonmark.Parser
  writer = new commonmark.HtmlRenderer(safe: true)
  writer.render reader.parse(plaintext)


pasteText = ->
  $(".js-paste-paste").val() || $(".js-paste-pl").text()


highlightText = ->
  $hlBox = $(".js-paste-hl")
  return if $hlBox.attr("data-processed") == "1"

  $hlBox.text pasteText()

  highlight = -> hljs.highlightBlock($hlBox[0])
  setTimeout highlight, 0

  $hlBox.attr "data-processed", "1"


markdownText = ->
  $mdBox = $(".js-paste-md")
  return if $mdBox.attr("data-processed") == "1"

  $mdBox.html markdownfy(pasteText())
  $mdBox.find("a").attr("rel", "nofollow")
  $mdBox.attr "data-processed", "1"


setHash = (e) ->
  return unless history.replaceState

  newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
  if newLocation == "#pl"
    newLocation = window.location.pathname

  history.replaceState null, "", newLocation


showPreview = (e) ->
  e.preventDefault()
  return if pasteText() == ""
  $(".js-paste-preview-md-box").html markdownfy(pasteText())
  $(".js-paste-preview-md-modal").modal "show"


switchToCurrentHash = ->
  return unless $(".js-showing-paste").length

  format = window.location.hash.slice(1)
  $(".js-show-tab[data-mode=#{format}]").click()


$ loadLanguageItems
$ switchToCurrentHash
$(document).on "click", ".js-paste-preview-md", showPreview
$(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
$(document).on "click", ".js-show-tab[data-mode=md]", markdownText
$(document).on "click", ".js-show-tab", setHash