comparison app/assets/javascripts/application.coffee @ 442:1fc5ddcde386

Some refactors
author nanaya <me@myconan.net>
date Thu, 10 Nov 2016 21:58:25 +0900
parents f0b4d6bec50e
children
comparison
equal deleted inserted replaced
441:69fe27a8b147 442:1fc5ddcde386
11 $(".js-paste-paste") 11 $(".js-paste-paste")
12 .val("") 12 .val("")
13 .focus() 13 .focus()
14 14
15 15
16 pasteText = -> 16 loadLanguageItems = ->
17 $(".js-paste-paste").val() || $(".js-paste-pl").text() 17 $languageSelectBox = $("#paste_language")
18 return unless $languageSelectBox.length
18 19
19 20 for language in hljs.listLanguages().sort()
20 setHash = (newLocation = window.location.pathname) -> 21 $languageSelectBox.append $("<option />", value: language, text: language)
21 return unless history.replaceState
22
23 history.replaceState null, "", newLocation
24 22
25 23
26 markdownfy = (plaintext) -> 24 markdownfy = (plaintext) ->
27 reader = new commonmark.Parser 25 reader = new commonmark.Parser
28 writer = new commonmark.HtmlRenderer(safe: true) 26 writer = new commonmark.HtmlRenderer(safe: true)
29 writer.render reader.parse(plaintext) 27 writer.render reader.parse(plaintext)
30 28
31 29
32 $(document).on "click", ".js-show-tab-pl", -> 30 pasteText = ->
33 setHash() 31 $(".js-paste-paste").val() || $(".js-paste-pl").text()
34 32
35 33
36 $(document).on "click", ".js-show-tab-hl", -> 34 highlightText = ->
37 setHash "#hl"
38 $hlBox = $(".js-paste-hl") 35 $hlBox = $(".js-paste-hl")
39 return if $hlBox.data("processed") 36 return if $hlBox.attr("data-processed") == "1"
40 37
41 $hlBox.text pasteText() 38 $hlBox.text pasteText()
42 39
43 highlight = -> hljs.highlightBlock($hlBox[0]) 40 highlight = -> hljs.highlightBlock($hlBox[0])
44 setTimeout highlight, 0 41 setTimeout highlight, 0
45 42
46 $hlBox.data "processed", true 43 $hlBox.attr "data-processed", "1"
47 44
48 45
49 $(document).on "click", ".js-show-tab-md", -> 46 markdownText = ->
50 setHash "#md"
51 $mdBox = $(".js-paste-md") 47 $mdBox = $(".js-paste-md")
52 return if $mdBox.data("processed") 48 return if $mdBox.attr("data-processed") == "1"
53 49
54 $mdBox.html markdownfy(pasteText()) 50 $mdBox.html markdownfy(pasteText())
55 $mdBox.find("a").attr("rel", "nofollow") 51 $mdBox.find("a").attr("rel", "nofollow")
56 $mdBox.data "processed", true 52 $mdBox.attr "data-processed", "1"
57 53
58 54
59 $(document).on "click", ".js-paste-preview-md", (e) -> 55 setHash = (e) ->
56 return unless history.replaceState
57
58 newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
59 if newLocation == "#pl"
60 newLocation = window.location.pathname
61
62 history.replaceState null, "", newLocation
63
64
65 showPreview = (e) ->
60 e.preventDefault() 66 e.preventDefault()
61 return if pasteText() == "" 67 return if pasteText() == ""
62 $(".js-paste-preview-md-box").html markdownfy(pasteText()) 68 $(".js-paste-preview-md-box").html markdownfy(pasteText())
63 $(".js-paste-preview-md-modal").modal "show" 69 $(".js-paste-preview-md-modal").modal "show"
64 70
65 71
66 $(document).on "ready", -> 72 switchToCurrentHash = ->
67 return unless $(".js-showing-paste").length 73 return unless $(".js-showing-paste").length
68 74
69 format = window.location.hash.slice(1) 75 format = window.location.hash.slice(1)
70 $(".js-show-tab-#{format}").click() 76 $(".js-show-tab[data-mode=#{format}]").click()
71 77
72 78
73 $(document).on "ready", -> 79 $ loadLanguageItems
74 $languageSelectBox = $("#paste_language") 80 $ switchToCurrentHash
75 return unless $languageSelectBox.length 81 $(document).on "click", ".js-paste-preview-md", showPreview
76 82 $(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
77 for language in hljs.listLanguages().sort() 83 $(document).on "click", ".js-show-tab[data-mode=md]", markdownText
78 $languageSelectBox.append $("<option />", value: language, text: language) 84 $(document).on "click", ".js-show-tab", setHash