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