inhaus-plugin-chat/resources/js/lib/format.js
Friederich Loheide 2844452002 Nachrichten-Plugin 1.0.0: Direktnachrichten, Gruppen, Anhänge, GIFs, Emojis
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 16:34:53 +00:00

62 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const time = new Intl.DateTimeFormat('de-DE', { hour: '2-digit', minute: '2-digit' });
const weekday = new Intl.DateTimeFormat('de-DE', { weekday: 'short' });
const short = new Intl.DateTimeFormat('de-DE', { day: '2-digit', month: '2-digit', year: '2-digit' });
const long = new Intl.DateTimeFormat('de-DE', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' });
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
/** Zeit in der Gesprächsliste: heute Uhrzeit, diese Woche Wochentag, sonst Datum */
export function listTime(iso) {
if (!iso) return '';
const date = new Date(iso);
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
if (days === 0) return time.format(date);
if (days === 1) return 'Gestern';
if (days < 7) return weekday.format(date);
return short.format(date);
}
export const messageTime = (iso) => time.format(new Date(iso));
/** Trenner zwischen Tagen im Verlauf */
export function dayLabel(iso) {
const date = new Date(iso);
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
if (days === 0) return 'Heute';
if (days === 1) return 'Gestern';
return long.format(date);
}
export const sameDay = (a, b) => startOfDay(new Date(a)) === startOfDay(new Date(b));
export function fileSize(bytes) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
return `${(bytes / 1024 / 1024).toFixed(1).replace('.', ',')} MB`;
}
/** Text in Stücke zerlegen, Links erkennen – ohne v-html */
export function linkify(text) {
const parts = [];
const pattern = /(https?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)\]])/g;
let last = 0;
for (const match of text.matchAll(pattern)) {
if (match.index > last) parts.push({ text: text.slice(last, match.index) });
parts.push({ text: match[0], href: match[0] });
last = match.index + match[0].length;
}
if (last < text.length) parts.push({ text: text.slice(last) });
return parts;
}
/** Nur Emojis (bis zu 3)? Dann groß darstellen */
export const onlyEmoji = (text) => !!text && /^(\p{Extended_Pictographic}|\p{Emoji_Component}|‍|️|\s){1,12}$/u.test(text) && [...text.replace(/\s/g, '')].length <= 8;