diff options
author | WlodekM <[email protected]> | 2024-12-01 20:37:58 +0200 |
---|---|---|
committer | WlodekM <[email protected]> | 2024-12-01 20:37:58 +0200 |
commit | fc76e4501d0f9db81cf1e5077d2487309acd7694 (patch) | |
tree | f8d8650cdb9d170945fe241647ee9c1bfa557718 | |
parent | f84e913cfc6773fea1f79c6d99ff6253715770dc (diff) |
do stuff
-rw-r--r-- | index.html | 2 | ||||
-rw-r--r-- | lib/sd.js | 9 | ||||
-rw-r--r-- | main.js | 9 | ||||
-rw-r--r-- | pages/login/page.js | 11 | ||||
-rw-r--r-- | pages/main/page.js | 12 | ||||
-rw-r--r-- | styles.less | 2 |
6 files changed, 36 insertions, 9 deletions
diff --git a/index.html b/index.html index 0661a50..7654336 100644 --- a/index.html +++ b/index.html @@ -9,6 +9,8 @@ <script type="module" src="main.js"></script> <link rel="stylesheet/less" type="text/css" href="styles.less" /> <script src="https://cdn.jsdelivr.net/npm/less"></script> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css"> + <script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script> </head> <body> diff --git a/lib/sd.js b/lib/sd.js index ea3fba9..e66b7b3 100644 --- a/lib/sd.js +++ b/lib/sd.js @@ -9,7 +9,6 @@ export default class SoktDeer { ws; /** @type {import("node:events").EventEmitter} */ wsEvents = new EventEmitter3(); - /** @type {any[]} */ messages = []; constructor(wsUri = "wss://sokt.meltland.dev") { @@ -34,6 +33,7 @@ export default class SoktDeer { }) this.ws.onopen = () => setInterval(()=>this.ping.call(this), 5000) } + login(username, password) { return new Promise((resolve, reject) => { this.ws.send(JSON.stringify({ @@ -41,7 +41,7 @@ export default class SoktDeer { username, password })) - this.wsEvents.once('token', token => { + this.wsEvents.once('token', ({token}) => { this.token = token; resolve(token) }) @@ -54,11 +54,12 @@ export default class SoktDeer { } //TODO - implement this - loginToken(token) { + loginToken(token, username) { return new Promise((resolve, reject) => { this.ws.send(JSON.stringify({ command: "login_token", - token + token, + username })) this.wsEvents.once('error', error => { if (error.error) reject(error.code) diff --git a/main.js b/main.js index d2c863a..40b8ffa 100644 --- a/main.js +++ b/main.js @@ -21,6 +21,11 @@ window.stores.onChange = function (store, cb) { return id; } const sd = window.sd = window.stores.sdlib -sd.ws.onclose = () => pages.goToPage('login') -sd.ws.onerror = () => pages.goToPage('login') +function onclose() { + window.sd = window.stores.sdlib = new SoktDeer() + document.getElementById('topbar').classList.add('hidden') + pages.goToPage('login'); +} +sd.ws.onclose = onclose +sd.ws.onerror = onclose pages.goToPage('login') \ No newline at end of file diff --git a/pages/login/page.js b/pages/login/page.js index dcc6f20..573c382 100644 --- a/pages/login/page.js +++ b/pages/login/page.js @@ -4,14 +4,23 @@ async function fetchJSON(url, opts) { } export function onload() { + if (localStorage.getItem('token')) { + window.stores.sdlib.ws.addEventListener('open', () => { + window.stores.sdlib.loginToken(localStorage.getItem('token'), localStorage.getItem('username')); + pages.goToPage('main') + }) + } document.getElementById("loginForm").addEventListener("submit", async function (ev) { ev.preventDefault(); const username = document.getElementById('username').value; const password = document.getElementById('password').value; document.getElementById('topbar').classList.remove('hidden') try { - await stores.sdlib.login(username, password) + const token = await stores.sdlib.login(username, password) + localStorage.setItem('token', token) + localStorage.setItem('username', username) } catch (error) { + console.error(error) return document.getElementById('error').innerText = 'An error occured\n' + error } pages.goToPage("main") diff --git a/pages/main/page.js b/pages/main/page.js index f1d40f2..ebdaab2 100644 --- a/pages/main/page.js +++ b/pages/main/page.js @@ -5,7 +5,17 @@ import { openPopup } from "../../lib/popups.js"; window.html = html // debug -const md = markdwonits() +const md = markdwonits({ + highlight: function (str, lang) { + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(str, { language: lang }).value; + } catch (__) {} + } + + return ''; // use external default escaping + } +}) async function fetchJSON(url, opts) { let resp = await fetch(url, opts); diff --git a/styles.less b/styles.less index 807da2a..d825208 100644 --- a/styles.less +++ b/styles.less @@ -62,7 +62,7 @@ body { .message-header { width: 100%; } - p, h1, h2, h3, h4, h5, h6 { + p, h1, h2, h3, h4, h5, h6, pre { margin: 0; } .attachment { |