annotate app/javascript/src/main.coffee @ 484:84ca55a0568e

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