mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: enhance user experience with tooltips; implement return URL handling on login and registration; add user roles; improve paste visibility checks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { user } from '$lib/stores/user';
|
||||
import { resolve } from '$app/paths';
|
||||
import { page } from '$app/stores';
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem('token');
|
||||
@@ -8,6 +9,9 @@
|
||||
user.set(null);
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
$: currentPath = $page.url.pathname;
|
||||
$: returnParam = currentPath !== '/' ? `?return=${encodeURIComponent($page.url.pathname + $page.url.search)}` : '';
|
||||
</script>
|
||||
|
||||
<div class="fixed top-0 left-0 h-15 w-full bg-neutral-900">
|
||||
@@ -18,15 +22,15 @@
|
||||
<span class="text-neutral-400">{$user.username}</span>
|
||||
<button
|
||||
on:click={logout}
|
||||
class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800">Logout</button
|
||||
class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800 cursor-pointer">Logout</button
|
||||
>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="ml-auto flex items-center space-x-4">
|
||||
<a href={resolve('/login')} class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800"
|
||||
<a href={resolve('/login') + returnParam} class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800"
|
||||
>Login</a
|
||||
>
|
||||
<a href={resolve('/register')} class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800"
|
||||
<a href={resolve('/register') + returnParam} class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800"
|
||||
>Register</a
|
||||
>
|
||||
</div>
|
||||
|
||||
@@ -72,3 +72,10 @@ export const displayNames: Record<string, string> = {
|
||||
};
|
||||
// only include langs that need special display names
|
||||
// e.g. cpp -> C++ or toml -> TOML, langs like java are automatically capitalized
|
||||
|
||||
export const roles = {
|
||||
0: 'User',
|
||||
1: 'Moderator',
|
||||
255: 'Administrator',
|
||||
403: 'Banned'
|
||||
}
|
||||
@@ -45,7 +45,11 @@ export async function refreshTokenIfNeeded() {
|
||||
const data = await res.json();
|
||||
if (data.token && data.refreshToken) {
|
||||
setTokens(data.token, data.refreshToken);
|
||||
} else {
|
||||
clearTokens();
|
||||
}
|
||||
} else {
|
||||
clearTokens();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,3 +35,116 @@ export function dateToRelativeString(date: Date, useSuffix = true): string {
|
||||
if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''} ${suffix}`;
|
||||
return `${seconds} second${seconds !== 1 ? 's' : ''} ${suffix}`;
|
||||
}
|
||||
|
||||
export function tooltip(node: HTMLElement, text: string) {
|
||||
let tooltipEl: HTMLDivElement | null;
|
||||
let caretEl: HTMLDivElement | null;
|
||||
let showTimeout: ReturnType<typeof setTimeout>;
|
||||
let hideTimeout: ReturnType<typeof setTimeout>;
|
||||
|
||||
function createTooltip() {
|
||||
const el = document.createElement('div');
|
||||
el.textContent = text;
|
||||
el.className =
|
||||
'fixed z-50 rounded bg-neutral-800 px-2 py-1 text-sm text-white shadow-lg opacity-0 pointer-events-none transition-opacity duration-150';
|
||||
el.style.maxWidth = '90%';
|
||||
el.style.wordBreak = 'break-word';
|
||||
const lines = text.split('\n');
|
||||
if (lines.length > 1) {
|
||||
el.innerHTML = '';
|
||||
lines.forEach((line, index) => {
|
||||
const lineEl = document.createElement('div');
|
||||
lineEl.textContent = line;
|
||||
el.appendChild(lineEl);
|
||||
if (index < lines.length - 1) {
|
||||
const br = document.createElement('br');
|
||||
el.appendChild(br);
|
||||
}
|
||||
});
|
||||
}
|
||||
const caret = document.createElement('div');
|
||||
caret.style.position = 'absolute';
|
||||
caret.style.width = '0';
|
||||
caret.style.height = '0';
|
||||
caret.style.left = '50%';
|
||||
caret.style.transform = 'translateX(-50%)';
|
||||
caret.style.pointerEvents = 'none';
|
||||
caretEl = caret;
|
||||
el.appendChild(caret);
|
||||
document.body.appendChild(el);
|
||||
tooltipEl = el;
|
||||
}
|
||||
|
||||
function positionTooltip() {
|
||||
if (!tooltipEl || !caretEl) return;
|
||||
const rect = node.getBoundingClientRect();
|
||||
const tooltipRect = tooltipEl.getBoundingClientRect();
|
||||
let top = rect.top - tooltipRect.height - 8 + window.scrollY;
|
||||
let left = rect.left + rect.width / 2 - tooltipRect.width / 2 + window.scrollX;
|
||||
let caretOnTop = false;
|
||||
|
||||
if (top < window.scrollY) {
|
||||
top = rect.bottom + 8 + window.scrollY;
|
||||
caretOnTop = true;
|
||||
}
|
||||
if (left < 0) left = 8;
|
||||
if (left + tooltipRect.width > window.innerWidth)
|
||||
left = window.innerWidth - tooltipRect.width - 8;
|
||||
|
||||
tooltipEl.style.top = `${top}px`;
|
||||
tooltipEl.style.left = `${left}px`;
|
||||
tooltipEl.style.position = 'absolute';
|
||||
|
||||
caretEl.style.top = caretOnTop ? '-7px' : '';
|
||||
caretEl.style.bottom = caretOnTop ? '' : '-7px';
|
||||
caretEl.style.borderLeft = '7px solid transparent';
|
||||
caretEl.style.borderRight = '7px solid transparent';
|
||||
if (caretOnTop) {
|
||||
caretEl.style.borderBottom = '7px solid #27272a';
|
||||
caretEl.style.borderTop = '';
|
||||
} else {
|
||||
caretEl.style.borderTop = '7px solid #27272a';
|
||||
caretEl.style.borderBottom = '';
|
||||
}
|
||||
}
|
||||
|
||||
function mouseOver() {
|
||||
clearTimeout(hideTimeout);
|
||||
showTimeout = setTimeout(() => {
|
||||
createTooltip();
|
||||
positionTooltip();
|
||||
requestAnimationFrame(() => {
|
||||
if (tooltipEl) tooltipEl.style.opacity = '1';
|
||||
});
|
||||
}, 200);
|
||||
}
|
||||
|
||||
function mouseOut() {
|
||||
clearTimeout(showTimeout);
|
||||
if (tooltipEl) {
|
||||
tooltipEl.style.opacity = '0';
|
||||
hideTimeout = setTimeout(() => {
|
||||
if (tooltipEl) {
|
||||
tooltipEl.remove();
|
||||
tooltipEl = null;
|
||||
caretEl = null;
|
||||
}
|
||||
}, 150);
|
||||
}
|
||||
}
|
||||
|
||||
node.addEventListener('mouseover', mouseOver);
|
||||
node.addEventListener('mouseout', mouseOut);
|
||||
window.addEventListener('scroll', mouseOut, true);
|
||||
window.addEventListener('resize', mouseOut, true);
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
node.removeEventListener('mouseover', mouseOver);
|
||||
node.removeEventListener('mouseout', mouseOut);
|
||||
window.removeEventListener('scroll', mouseOut, true);
|
||||
window.removeEventListener('resize', mouseOut, true);
|
||||
if (tooltipEl) tooltipEl.remove();
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user