1 |
|
2 |
|
3 |
|
4 |
|
5 | (function () {
|
6 | const deck = document.querySelector('markdown-deck')
|
7 |
|
8 | if (!deck) return
|
9 |
|
10 | window.addEventListener('keydown', ev => {
|
11 | if (ev.ctrlKey || ev.metaKey) {
|
12 | if (ev.code === 'KeyS') {
|
13 | ev.preventDefault()
|
14 | deck.markdown && save(deck.markdown)
|
15 | }
|
16 | }
|
17 | })
|
18 |
|
19 | async function save (markdown) {
|
20 | return fetch('/api/save', {
|
21 | method: 'POST',
|
22 | headers: { 'Content-Type': 'application/json' },
|
23 | body: JSON.stringify({ markdown })
|
24 | }).then(async res => {
|
25 | const msg = await res.text()
|
26 | if (res.status === 200) {
|
27 | console.info(msg)
|
28 | toast(msg, 'success')
|
29 | } else {
|
30 | console.error(msg)
|
31 | toast(msg, 'error')
|
32 | }
|
33 | }).catch((err) => {
|
34 | toast('Eloc server is closed', 'error')
|
35 | console.error(err)
|
36 | })
|
37 | }
|
38 |
|
39 | function toast (msg, type = 'info') {
|
40 | const toastElem = document.createElement('div')
|
41 | toastElem.className = `toast ${type}`
|
42 | toastElem.innerText = msg
|
43 | toastElem.onanimationend = () => document.body.removeChild(toastElem)
|
44 |
|
45 | document.body.insertAdjacentElement('beforeend', toastElem)
|
46 | }
|
47 | })()
|