annotate app/assets/javascripts/application.coffee @ 464:f11862e58af4

Canonicalize 0-prefixed ids as well
author nanaya <me@nanaya.pro>
date Mon, 10 Dec 2018 02:38:45 +0900
parents 1fc5ddcde386
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
381
bfc5ad3f7dc5 Use rails' builtin jquery instead of external one.
nanaya <me@myconan.net>
parents: 379
diff changeset
1 #= require jquery
421
f0b4d6bec50e Slim out bootstrap
nanaya <me@myconan.net>
parents: 417
diff changeset
2 #= require bootstrap/transition
f0b4d6bec50e Slim out bootstrap
nanaya <me@myconan.net>
parents: 417
diff changeset
3 #= require bootstrap/collapse
f0b4d6bec50e Slim out bootstrap
nanaya <me@myconan.net>
parents: 417
diff changeset
4 #= require bootstrap/modal
f0b4d6bec50e Slim out bootstrap
nanaya <me@myconan.net>
parents: 417
diff changeset
5 #= require bootstrap/tab
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
6 #= require highlightjs
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
7 #= require commonmark
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
8
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
9 $(document).on "click", ".js-paste-clear", (e) ->
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
10 e.preventDefault()
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
11 $(".js-paste-paste")
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
12 .val("")
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
13 .focus()
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
14
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
15
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
16 loadLanguageItems = ->
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
17 $languageSelectBox = $("#paste_language")
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
18 return unless $languageSelectBox.length
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
19
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
20 for language in hljs.listLanguages().sort()
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
21 $languageSelectBox.append $("<option />", value: language, text: language)
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
22
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
23
402
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
24 markdownfy = (plaintext) ->
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
25 reader = new commonmark.Parser
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
26 writer = new commonmark.HtmlRenderer(safe: true)
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
27 writer.render reader.parse(plaintext)
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
28
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
29
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
30 pasteText = ->
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
31 $(".js-paste-paste").val() || $(".js-paste-pl").text()
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
32
382
8ceb373e32d8 Nuke out javascripts =]
nanaya <me@myconan.net>
parents: 381
diff changeset
33
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
34 highlightText = ->
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
35 $hlBox = $(".js-paste-hl")
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
36 return if $hlBox.attr("data-processed") == "1"
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
37
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
38 $hlBox.text pasteText()
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
39
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
40 highlight = -> hljs.highlightBlock($hlBox[0])
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
41 setTimeout highlight, 0
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
42
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
43 $hlBox.attr "data-processed", "1"
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
44
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
45
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
46 markdownText = ->
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
47 $mdBox = $(".js-paste-md")
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
48 return if $mdBox.attr("data-processed") == "1"
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
49
402
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
50 $mdBox.html markdownfy(pasteText())
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
51 $mdBox.find("a").attr("rel", "nofollow")
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
52 $mdBox.attr "data-processed", "1"
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
53
402
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
54
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
55 setHash = (e) ->
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
56 return unless history.replaceState
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
57
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
58 newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
59 if newLocation == "#pl"
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
60 newLocation = window.location.pathname
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
61
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
62 history.replaceState null, "", newLocation
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
63
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
64
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
65 showPreview = (e) ->
405
26c1b0b8edbe Don't add hash when clicking preview.
nanaya <me@myconan.net>
parents: 404
diff changeset
66 e.preventDefault()
402
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
67 return if pasteText() == ""
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
68 $(".js-paste-preview-md-box").html markdownfy(pasteText())
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
69 $(".js-paste-preview-md-modal").modal "show"
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
70
a18e19139f57 Markdown preview \o/
nanaya <me@myconan.net>
parents: 398
diff changeset
71
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
72 switchToCurrentHash = ->
416
c4c8214fb0b2 Simplify dom check
nanaya <me@myconan.net>
parents: 405
diff changeset
73 return unless $(".js-showing-paste").length
398
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
74
851432f21603 Nope, failing at redesigning.
nanaya <me@myconan.net>
parents: 382
diff changeset
75 format = window.location.hash.slice(1)
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
76 $(".js-show-tab[data-mode=#{format}]").click()
417
080dd141898c Add support for explicit highlighter languages
nanaya <me@myconan.net>
parents: 416
diff changeset
77
080dd141898c Add support for explicit highlighter languages
nanaya <me@myconan.net>
parents: 416
diff changeset
78
442
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
79 $ loadLanguageItems
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
80 $ switchToCurrentHash
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
81 $(document).on "click", ".js-paste-preview-md", showPreview
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
82 $(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
83 $(document).on "click", ".js-show-tab[data-mode=md]", markdownText
1fc5ddcde386 Some refactors
nanaya <me@myconan.net>
parents: 421
diff changeset
84 $(document).on "click", ".js-show-tab", setHash