|
1 | 1 | window.addEventListener('load', function() { |
2 | | - var editors = document.getElementsByClassName('markdown-editor'); |
3 | | - for (let editor of editors) { |
4 | | - var sourceLink = editor.getElementsByClassName('markdown-editor-source-link')[0]; |
5 | | - var previewLink = editor.getElementsByClassName('markdown-editor-preview-link')[0]; |
6 | | - var editorTextarea = editor.getElementsByClassName('markdown-editor-source')[0]; |
7 | | - var preview = editor.getElementsByClassName('markdown-editor-preview')[0]; |
8 | | - var fileUploader = editor.getElementsByClassName('file-uploader')[0]; |
9 | | - if (fileUploader === undefined) { |
10 | | - continue; |
11 | | - } |
12 | | - var fileUploaderInput = fileUploader.getElementsByClassName('file-uploader-input')[0]; |
13 | | - var fileUploaderLink = fileUploader.getElementsByClassName('file-uploader-link')[0]; |
14 | | - if ( |
15 | | - sourceLink === undefined || |
16 | | - previewLink === undefined || |
17 | | - editorTextarea === undefined || |
18 | | - preview === undefined || |
19 | | - fileUploaderInput === undefined || |
20 | | - fileUploaderLink === undefined |
21 | | - ) { |
22 | | - continue; |
23 | | - } |
| 2 | + var editors = document.getElementsByClassName('markdown-editor'); |
| 3 | + for (let editor of editors) { |
| 4 | + var sourceLink = editor.getElementsByClassName('markdown-editor-source-link')[0]; |
| 5 | + var previewLink = editor.getElementsByClassName('markdown-editor-preview-link')[0]; |
| 6 | + var editorTextarea = editor.getElementsByClassName('markdown-editor-source')[0]; |
| 7 | + var preview = editor.getElementsByClassName('markdown-editor-preview')[0]; |
| 8 | + var fileUploader = editor.getElementsByClassName('file-uploader')[0]; |
| 9 | + if (fileUploader === undefined) { |
| 10 | + continue; |
| 11 | + } |
| 12 | + var fileUploaderInput = fileUploader.getElementsByClassName('file-uploader-input')[0]; |
| 13 | + var fileUploaderLink = fileUploader.getElementsByClassName('file-uploader-link')[0]; |
| 14 | + if ( |
| 15 | + sourceLink === undefined || |
| 16 | + previewLink === undefined || |
| 17 | + editorTextarea === undefined || |
| 18 | + preview === undefined || |
| 19 | + fileUploaderInput === undefined || |
| 20 | + fileUploaderLink === undefined |
| 21 | + ) { |
| 22 | + continue; |
| 23 | + } |
24 | 24 |
|
25 | | - sourceLink.onclick = function() { |
26 | | - if (editorTextarea.classList.contains('d-none')) { |
27 | | - editorTextarea.classList.remove('d-none'); |
28 | | - preview.classList.add('d-none'); |
29 | | - sourceLink.classList.add('active'); |
30 | | - previewLink.classList.remove('active'); |
31 | | - fileUploader.classList.remove('d-none'); |
32 | | - } |
33 | | - } |
| 25 | + sourceLink.onclick = function() { |
| 26 | + if (editorTextarea.classList.contains('d-none')) { |
| 27 | + editorTextarea.classList.remove('d-none'); |
| 28 | + preview.classList.add('d-none'); |
| 29 | + sourceLink.classList.add('active'); |
| 30 | + previewLink.classList.remove('active'); |
| 31 | + fileUploader.classList.remove('d-none'); |
| 32 | + } |
| 33 | + } |
34 | 34 |
|
35 | | - previewLink.onclick = function() { |
36 | | - if (preview.classList.contains('d-none')) { |
37 | | - var xhr = new XMLHttpRequest(); |
38 | | - xhr.onreadystatechange = function() { |
39 | | - if (xhr.readyState == 4 && xhr.status == 200) { |
40 | | - preview.innerHTML = xhr.responseText; |
41 | | - preview.querySelectorAll('pre code').forEach(function(block) { |
42 | | - hljs.highlightBlock(block); |
43 | | - }); |
44 | | - MathJax.Hub.Queue(["Typeset", MathJax.Hub, preview]); |
45 | | - preview.classList.remove('d-none'); |
46 | | - editorTextarea.classList.add('d-none'); |
47 | | - previewLink.classList.add('active'); |
48 | | - sourceLink.classList.remove('active'); |
49 | | - fileUploader.classList.add('d-none'); |
50 | | - } |
| 35 | + previewLink.onclick = function() { |
| 36 | + if (preview.classList.contains('d-none')) { |
| 37 | + var xhr = new XMLHttpRequest(); |
| 38 | + xhr.onreadystatechange = function() { |
| 39 | + if (xhr.readyState == 4 && xhr.status == 200) { |
| 40 | + preview.innerHTML = xhr.responseText; |
| 41 | + preview.querySelectorAll('pre code').forEach(function(block) { |
| 42 | + hljs.highlightBlock(block); |
| 43 | + }); |
| 44 | + MathJax.Hub.Queue(["Typeset", MathJax.Hub, preview]); |
| 45 | + preview.classList.remove('d-none'); |
| 46 | + editorTextarea.classList.add('d-none'); |
| 47 | + previewLink.classList.add('active'); |
| 48 | + sourceLink.classList.remove('active'); |
| 49 | + fileUploader.classList.add('d-none'); |
| 50 | + } |
| 51 | + } |
| 52 | + xhr.open('POST', '/md', true); |
| 53 | + xhr.send(editorTextarea.value); |
| 54 | + } |
51 | 55 | } |
52 | | - xhr.open('POST', '/md', true); |
53 | | - xhr.send(editorTextarea.value); |
54 | | - } |
55 | | - } |
56 | 56 |
|
57 | | - fileUploaderLink.onclick = function() { |
58 | | - fileUploaderInput.click(); |
59 | | - } |
| 57 | + fileUploaderLink.onclick = function() { |
| 58 | + fileUploaderInput.click(); |
| 59 | + } |
60 | 60 |
|
61 | | - fileUploaderInput.onchange = function() { |
62 | | - var file = fileUploaderInput.files[0]; |
63 | | - if (file === undefined) { |
64 | | - return; |
65 | | - } |
| 61 | + fileUploaderInput.onchange = function() { |
| 62 | + var file = fileUploaderInput.files[0]; |
| 63 | + if (file === undefined) { |
| 64 | + return; |
| 65 | + } |
66 | 66 |
|
67 | | - var formData = new FormData(); |
68 | | - formData.append('file', file); |
| 67 | + var formData = new FormData(); |
| 68 | + formData.append('file', file); |
69 | 69 |
|
70 | | - var xhr = new XMLHttpRequest(); |
71 | | - xhr.onreadystatechange = function() { |
72 | | - if (xhr.readyState == 4 && xhr.status == 200) { |
73 | | - var fileLink = '[' + file.name + '](' + xhr.responseText + ')'; |
74 | | - if (file.type.startsWith('image/')) { |
75 | | - fileLink = '!' + fileLink; |
76 | | - } |
77 | | - if (editorTextarea.value.slice(-1) != '\n') { |
78 | | - editorTextarea.value += '\n'; |
79 | | - } |
80 | | - editorTextarea.value += fileLink; |
| 70 | + var xhr = new XMLHttpRequest(); |
| 71 | + xhr.onreadystatechange = function() { |
| 72 | + if (xhr.readyState == 4 && xhr.status == 200) { |
| 73 | + var fileLink = '[' + file.name + '](' + xhr.responseText + ')'; |
| 74 | + if (file.type.startsWith('image/')) { |
| 75 | + fileLink = '!' + fileLink; |
| 76 | + } |
| 77 | + if (editorTextarea.value.slice(-1) != '\n') { |
| 78 | + editorTextarea.value += '\n'; |
| 79 | + } |
| 80 | + editorTextarea.value += fileLink; |
| 81 | + } |
| 82 | + }; |
| 83 | + xhr.open('POST', '/upload', true); |
| 84 | + xhr.send(formData); |
81 | 85 | } |
82 | | - }; |
83 | | - xhr.open('POST', '/upload', true); |
84 | | - xhr.send(formData); |
85 | 86 | } |
86 | | - } |
87 | 87 | }); |
0 commit comments