diff app/javascript/src/main.coffee @ 468:802dcd44188e

Now with webpacker
author nanaya <me@nanaya.pro>
date Sun, 23 Feb 2020 20:23:09 +0900
parents
children 68231013b01b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/javascript/src/main.coffee	Sun Feb 23 20:23:09 2020 +0900
@@ -0,0 +1,91 @@
+import 'bootstrap/js/transition'
+import 'bootstrap/js/collapse'
+import 'bootstrap/js/modal'
+import 'bootstrap/js/tab'
+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