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