Mercurial > zeropaste
annotate app/javascript/application.js @ 490:a39bc79fcb1a
Specify path to builds directory
author | nanaya <me@nanaya.net> |
---|---|
date | Tue, 17 Jan 2023 02:36:38 +0900 |
parents | 361ab9e7ffad |
children |
rev | line source |
---|---|
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
1 import 'bootstrap'; |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
2 import hljs from 'highlight.js'; |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
3 import * as commonmark from 'commonmark'; |
468 | 4 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
5 /* global $ */ |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
6 $(document).on('click', '.js-paste-clear', (e) => { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
7 e.preventDefault(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
8 $('.js-paste-paste') |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
9 .val('') |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
10 .focus(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
11 }); |
468 | 12 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
13 function loadLanguageItems () { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
14 const $languageSelectBox = $('#paste_language'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
15 if ($languageSelectBox.length === 0) return; |
468 | 16 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
17 for (const language of hljs.listLanguages().sort()) { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
18 $languageSelectBox.append($( |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
19 '<option />', |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
20 { text: language, value: language } |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
21 )); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
22 } |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
23 } |
468 | 24 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
25 function markdownfy (plaintext) { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
26 const reader = new commonmark.Parser(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
27 const writer = new commonmark.HtmlRenderer({ safe: true }); |
468 | 28 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
29 return writer.render(reader.parse(plaintext)); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
30 } |
468 | 31 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
32 function pasteText () { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
33 return $('.js-paste-paste').val() || $('.js-paste-pl').text(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
34 } |
468 | 35 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
36 function highlightText () { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
37 const $hlBox = $('.js-paste-hl'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
38 if ($hlBox.attr('data-processed') === '1') return; |
468 | 39 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
40 $hlBox.text(pasteText()); |
468 | 41 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
42 const highlight = () => hljs.highlightBlock($hlBox[0]); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
43 window.setTimeout(highlight, 0); |
468 | 44 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
45 $hlBox.attr('data-processed', '1'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
46 } |
468 | 47 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
48 function markdownText () { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
49 const $mdBox = $('.js-paste-md'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
50 if ($mdBox.attr('data-processed') === '1') return; |
468 | 51 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
52 $mdBox.html(markdownfy(pasteText())); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
53 $mdBox.find('a').attr('rel', 'nofollow'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
54 $mdBox.attr('data-processed', '1'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
55 } |
468 | 56 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
57 function setHash (e) { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
58 let newLocation = `#${e.currentTarget.getAttribute('data-mode')}`; |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
59 if (newLocation === '#pl') { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
60 newLocation = window.location.pathname; |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
61 } |
468 | 62 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
63 window.history.replaceState(null, '', newLocation); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
64 } |
468 | 65 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
66 function showPreview (e) { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
67 e.preventDefault(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
68 const text = pasteText(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
69 if (text === '') return; |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
70 $('.js-paste-preview-md-box').html(markdownfy(text)); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
71 $('.js-paste-preview-md-modal').modal('show'); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
72 } |
468 | 73 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
74 function switchToCurrentHash () { |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
75 if ($('.js-showing-paste').length === 0) return; |
478 | 76 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
77 const format = window.location.hash.slice(1); |
479 | 78 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
79 if (!['pl', 'hl', 'md'].includes(format)) return; |
468 | 80 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
81 $('.js-show-tab[data-mode=#{format}]').click(); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
82 } |
468 | 83 |
489
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
84 $(loadLanguageItems); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
85 $(switchToCurrentHash); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
86 $(document).on('click', '.js-paste-preview-md', showPreview); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
87 $(document).on('click', '.js-show-tab[data-mode=hl]', highlightText); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
88 $(document).on('click', '.js-show-tab[data-mode=md]', markdownText); |
361ab9e7ffad
Replace webpacker with esbuild and use plain js instead of coffee
nanaya <me@nanaya.net>
parents:
479
diff
changeset
|
89 $(document).on('click', '.js-show-tab', setHash); |