mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: enhance tooltip functionality and improve date formatting
This commit is contained in:
@@ -7,6 +7,10 @@ export function formatBytes(bytes: number, decimals = 2): string {
|
|||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatNumber(num: number): string {
|
||||||
|
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||||
|
}
|
||||||
|
|
||||||
export function extractDateFromUUIDv7(uuid?: string): Date | null {
|
export function extractDateFromUUIDv7(uuid?: string): Date | null {
|
||||||
if (!uuid) return null;
|
if (!uuid) return null;
|
||||||
const hex = uuid.replace(/-/g, '');
|
const hex = uuid.replace(/-/g, '');
|
||||||
@@ -14,54 +18,76 @@ export function extractDateFromUUIDv7(uuid?: string): Date | null {
|
|||||||
return new Date(parseInt(timestampHex, 16));
|
return new Date(parseInt(timestampHex, 16));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dateToRelativeString(date: Date, useSuffix = true): string {
|
export function dateToRelativeString(
|
||||||
const now = new Date();
|
date: Date,
|
||||||
|
useSuffix = true,
|
||||||
|
full = false,
|
||||||
|
nowArg?: Date,
|
||||||
|
accuracy = 1
|
||||||
|
): string {
|
||||||
|
const now = nowArg ?? new Date();
|
||||||
const isFuture = date > now;
|
const isFuture = date > now;
|
||||||
const suffix = useSuffix ? (isFuture ? 'from now' : 'ago') : '';
|
const suffix = useSuffix ? (isFuture ? 'from now' : 'ago') : '';
|
||||||
const diff = Math.abs(now.getTime() - date.getTime());
|
const diff = Math.abs(now.getTime() - date.getTime());
|
||||||
|
let seconds = Math.floor(diff / 1000);
|
||||||
const seconds = Math.floor(diff / 1000);
|
const units = [
|
||||||
const minutes = Math.floor(seconds / 60);
|
{ name: 'year', value: 365 * 24 * 60 * 60 },
|
||||||
const hours = Math.floor(minutes / 60);
|
{ name: 'month', value: 30 * 24 * 60 * 60 },
|
||||||
const days = Math.floor(hours / 24);
|
{ name: 'week', value: 7 * 24 * 60 * 60 },
|
||||||
const weeks = Math.floor(days / 7);
|
{ name: 'day', value: 24 * 60 * 60 },
|
||||||
const months = Math.floor(days / 30);
|
{ name: 'hour', value: 60 * 60 },
|
||||||
const years = Math.floor(days / 365);
|
{ name: 'minute', value: 60 },
|
||||||
if (years > 0) return `${years} year${years > 1 ? 's' : ''} ${suffix}`;
|
{ name: 'second', value: 1 }
|
||||||
if (months > 0) return `${months} month${months > 1 ? 's' : ''} ${suffix}`;
|
];
|
||||||
if (weeks > 0) return `${weeks} week${weeks > 1 ? 's' : ''} ${suffix}`;
|
if (!full) {
|
||||||
if (days > 0) return `${days} day${days > 1 ? 's' : ''} ${suffix}`;
|
for (const unit of units) {
|
||||||
if (hours > 0) return `${hours} hour${hours > 1 ? 's' : ''} ${suffix}`;
|
const count = Math.floor(seconds / unit.value);
|
||||||
if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''} ${suffix}`;
|
if (count > 0) {
|
||||||
return `${seconds} second${seconds !== 1 ? 's' : ''} ${suffix}`;
|
return `${count} ${unit.name}${count > 1 ? 's' : ''} ${suffix}`.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `0 seconds ${suffix}`.trim();
|
||||||
|
}
|
||||||
|
const parts: string[] = [];
|
||||||
|
let acc = 0;
|
||||||
|
for (const unit of units) {
|
||||||
|
if (acc >= accuracy) break;
|
||||||
|
const count = Math.floor(seconds / unit.value);
|
||||||
|
if (count > 0) {
|
||||||
|
parts.push(`${count} ${unit.name}${count > 1 ? 's' : ''}`);
|
||||||
|
seconds -= count * unit.value;
|
||||||
|
acc++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (parts.length === 0) parts.push('0 seconds');
|
||||||
|
return `${parts.join(', ')} ${suffix}`.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tooltip(node: HTMLElement, text: string) {
|
export function tooltip(node: HTMLElement, text: string) {
|
||||||
if (!text || text.trim() === '') return;
|
let currentText = text ?? '';
|
||||||
let tooltipEl: HTMLDivElement | null;
|
let tooltipEl: HTMLDivElement | null = null;
|
||||||
let caretEl: HTMLDivElement | null;
|
let caretEl: HTMLDivElement | null = null;
|
||||||
let showTimeout: ReturnType<typeof setTimeout>;
|
let showTimeout: ReturnType<typeof setTimeout>;
|
||||||
let hideTimeout: ReturnType<typeof setTimeout>;
|
let hideTimeout: ReturnType<typeof setTimeout>;
|
||||||
|
|
||||||
function createTooltip() {
|
function createTooltip() {
|
||||||
|
if (!currentText || currentText.trim() === '') return;
|
||||||
const el = document.createElement('div');
|
const el = document.createElement('div');
|
||||||
el.textContent = text;
|
|
||||||
el.className =
|
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';
|
'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.maxWidth = '90%';
|
||||||
el.style.wordBreak = 'break-word';
|
el.style.wordBreak = 'break-word';
|
||||||
const lines = text.split('\n');
|
const lines = currentText.split('\n');
|
||||||
if (lines.length > 1) {
|
if (lines.length > 1) {
|
||||||
el.innerHTML = '';
|
el.innerHTML = '';
|
||||||
lines.forEach((line, index) => {
|
lines.forEach((line, index) => {
|
||||||
const lineEl = document.createElement('div');
|
const span = document.createElement('span');
|
||||||
lineEl.textContent = line;
|
span.textContent = line;
|
||||||
el.appendChild(lineEl);
|
el.appendChild(span);
|
||||||
if (index < lines.length - 1) {
|
if (index < lines.length - 1) el.appendChild(document.createElement('br'));
|
||||||
const br = document.createElement('br');
|
|
||||||
el.appendChild(br);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
el.textContent = currentText;
|
||||||
}
|
}
|
||||||
const caret = document.createElement('div');
|
const caret = document.createElement('div');
|
||||||
caret.style.position = 'absolute';
|
caret.style.position = 'absolute';
|
||||||
@@ -76,6 +102,41 @@ export function tooltip(node: HTMLElement, text: string) {
|
|||||||
tooltipEl = el;
|
tooltipEl = el;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateContent() {
|
||||||
|
if (!tooltipEl) return;
|
||||||
|
if (!currentText || currentText.trim() === '') {
|
||||||
|
tooltipEl.remove();
|
||||||
|
tooltipEl = null;
|
||||||
|
caretEl = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const lines = currentText.split('\n');
|
||||||
|
tooltipEl.innerHTML = '';
|
||||||
|
if (lines.length > 1) {
|
||||||
|
lines.forEach((line, index) => {
|
||||||
|
const span = document.createElement('span');
|
||||||
|
span.textContent = line;
|
||||||
|
tooltipEl?.appendChild(span);
|
||||||
|
if (index < lines.length - 1) tooltipEl?.appendChild(document.createElement('br'));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
tooltipEl.textContent = currentText;
|
||||||
|
}
|
||||||
|
if (!caretEl) {
|
||||||
|
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;
|
||||||
|
tooltipEl.appendChild(caretEl);
|
||||||
|
} else {
|
||||||
|
tooltipEl.appendChild(caretEl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function positionTooltip() {
|
function positionTooltip() {
|
||||||
if (!tooltipEl || !caretEl) return;
|
if (!tooltipEl || !caretEl) return;
|
||||||
const rect = node.getBoundingClientRect();
|
const rect = node.getBoundingClientRect();
|
||||||
@@ -112,7 +173,8 @@ export function tooltip(node: HTMLElement, text: string) {
|
|||||||
function mouseOver() {
|
function mouseOver() {
|
||||||
clearTimeout(hideTimeout);
|
clearTimeout(hideTimeout);
|
||||||
showTimeout = setTimeout(() => {
|
showTimeout = setTimeout(() => {
|
||||||
createTooltip();
|
if (!tooltipEl) createTooltip();
|
||||||
|
else updateContent();
|
||||||
positionTooltip();
|
positionTooltip();
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
if (tooltipEl) tooltipEl.style.opacity = '1';
|
if (tooltipEl) tooltipEl.style.opacity = '1';
|
||||||
@@ -140,6 +202,21 @@ export function tooltip(node: HTMLElement, text: string) {
|
|||||||
window.addEventListener('resize', mouseOut, true);
|
window.addEventListener('resize', mouseOut, true);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
update(newText: string) {
|
||||||
|
currentText = newText ?? '';
|
||||||
|
if (!currentText || currentText.trim() === '') {
|
||||||
|
if (tooltipEl) {
|
||||||
|
tooltipEl.remove();
|
||||||
|
tooltipEl = null;
|
||||||
|
caretEl = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (tooltipEl) {
|
||||||
|
updateContent();
|
||||||
|
positionTooltip();
|
||||||
|
}
|
||||||
|
},
|
||||||
destroy() {
|
destroy() {
|
||||||
node.removeEventListener('mouseover', mouseOver);
|
node.removeEventListener('mouseover', mouseOver);
|
||||||
node.removeEventListener('mouseout', mouseOut);
|
node.removeEventListener('mouseout', mouseOut);
|
||||||
@@ -148,4 +225,4 @@ export function tooltip(node: HTMLElement, text: string) {
|
|||||||
if (tooltipEl) tooltipEl.remove();
|
if (tooltipEl) tooltipEl.remove();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user