annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
468
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
1 import 'bootstrap/js/transition'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
2 import 'bootstrap/js/collapse'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
3 import 'bootstrap/js/modal'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
4 import 'bootstrap/js/tab'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
5 import hljs from 'highlight.js'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
6 import * as commonmark from 'commonmark'
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
7
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
8 #= require jquery
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
9 #= require bootstrap/transition
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
10 #= require bootstrap/collapse
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
11 #= require bootstrap/modal
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
12 #= require bootstrap/tab
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
13 #= require highlightjs
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
14 #= require commonmark
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
15
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
16 $(document).on "click", ".js-paste-clear", (e) ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
17 e.preventDefault()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
18 $(".js-paste-paste")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
19 .val("")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
20 .focus()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
21
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
22
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
23 loadLanguageItems = ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
24 $languageSelectBox = $("#paste_language")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
25 return unless $languageSelectBox.length
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
26
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
27 for language in hljs.listLanguages().sort()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
28 $languageSelectBox.append $("<option />", value: language, text: language)
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
29
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
30
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
31 markdownfy = (plaintext) ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
32 reader = new commonmark.Parser
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
33 writer = new commonmark.HtmlRenderer(safe: true)
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
34 writer.render reader.parse(plaintext)
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
35
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
36
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
37 pasteText = ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
38 $(".js-paste-paste").val() || $(".js-paste-pl").text()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
39
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
40
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
41 highlightText = ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
42 $hlBox = $(".js-paste-hl")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
43 return if $hlBox.attr("data-processed") == "1"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
44
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
45 $hlBox.text pasteText()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
46
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
47 highlight = -> hljs.highlightBlock($hlBox[0])
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
48 setTimeout highlight, 0
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
49
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
50 $hlBox.attr "data-processed", "1"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
51
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
52
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
53 markdownText = ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
54 $mdBox = $(".js-paste-md")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
55 return if $mdBox.attr("data-processed") == "1"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
56
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
57 $mdBox.html markdownfy(pasteText())
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
58 $mdBox.find("a").attr("rel", "nofollow")
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
59 $mdBox.attr "data-processed", "1"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
60
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
61
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
62 setHash = (e) ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
63 return unless history.replaceState
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
64
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
65 newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
66 if newLocation == "#pl"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
67 newLocation = window.location.pathname
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
68
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
69 history.replaceState null, "", newLocation
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
70
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
71
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
72 showPreview = (e) ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
73 e.preventDefault()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
74 return if pasteText() == ""
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
75 $(".js-paste-preview-md-box").html markdownfy(pasteText())
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
76 $(".js-paste-preview-md-modal").modal "show"
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
77
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
78
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
79 switchToCurrentHash = ->
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
80 return unless $(".js-showing-paste").length
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
81
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
82 format = window.location.hash.slice(1)
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
83 $(".js-show-tab[data-mode=#{format}]").click()
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
84
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
85
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
86 $ loadLanguageItems
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
87 $ switchToCurrentHash
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
88 $(document).on "click", ".js-paste-preview-md", showPreview
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
89 $(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
90 $(document).on "click", ".js-show-tab[data-mode=md]", markdownText
802dcd44188e Now with webpacker
nanaya <me@nanaya.pro>
parents:
diff changeset
91 $(document).on "click", ".js-show-tab", setHash