Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/realtime/src/chat-mentions.ts
T

75 lines
3.1 KiB
TypeScript

import { CHAT_MESSAGE_MAX_MENTIONS, type ChatMention, type ChatMentionType } from "./chat-events.js";
const CHAT_MENTION_ID_PATTERN = /^[A-Za-z0-9._-]{1,500}$/u;
const CHAT_MENTION_TOKEN_PATTERN = /<@(user|project|contact|company):([A-Za-z0-9._-]{1,500})>/gu;
const EXPLICIT_MENTION_PREFIXES = {
projekt: "project",
kontakt: "contact",
firma: "company",
} as const satisfies Readonly<Record<string, ChatMentionType>>;
export type ChatMentionQuery = Readonly<{
start: number;
end: number;
query: string;
kind: ChatMentionType | null;
}>;
/** Returns the canonical Markdown token stored in a chat message body. */
export function toChatMentionToken(mention: Pick<ChatMention, "type" | "id">): string {
if (!CHAT_MENTION_ID_PATTERN.test(mention.id)) throw new Error("chat mention id is invalid");
return `<@${mention.type}:${mention.id}>`;
}
/** Extracts unique, ordered mention references without trusting client labels. */
export function extractChatMentions(body: string): ChatMention[] {
const mentions: ChatMention[] = [];
const seen = new Set<string>();
for (const match of body.matchAll(CHAT_MENTION_TOKEN_PATTERN)) {
const type = match[1] as ChatMentionType;
const id = match[2]!;
const key = `${type}\u0000${id}`;
if (seen.has(key)) continue;
seen.add(key);
mentions.push({ type, id, label: null });
if (mentions.length === CHAT_MESSAGE_MAX_MENTIONS) break;
}
return mentions;
}
/** Resolves the mention fragment immediately before the caret. */
export function findChatMentionQuery(body: string, caret: number): ChatMentionQuery | null {
if (!Number.isInteger(caret) || caret < 0 || caret > body.length) return null;
const prefix = body.slice(0, caret);
const boundary = Math.max(prefix.lastIndexOf(" "), prefix.lastIndexOf("\n"), prefix.lastIndexOf("\t"));
const start = boundary + 1;
const fragment = prefix.slice(start);
if (!fragment.startsWith("@") || fragment.startsWith("<@") || /[<>]/u.test(fragment)) return null;
const explicit = /^@(projekt|kontakt|firma):([^\s@]*)$/u.exec(fragment);
if (explicit) {
return {
start,
end: caret,
query: explicit[2]!,
kind: EXPLICIT_MENTION_PREFIXES[explicit[1] as keyof typeof EXPLICIT_MENTION_PREFIXES],
};
}
const general = /^@([^\s@:]*)$/u.exec(fragment);
if (!general) return null;
return { start, end: caret, query: general[1]!, kind: null };
}
/** Replaces the active query with one canonical token and a trailing space. */
export function insertChatMention(body: string, query: ChatMentionQuery, mention: Pick<ChatMention, "type" | "id">): { body: string; caret: number } {
if (query.start < 0 || query.end < query.start || query.end > body.length) throw new Error("chat mention query range is invalid");
const token = toChatMentionToken(mention);
const suffix = body.slice(query.end);
const separator = /^\s/u.test(suffix) ? "" : " ";
const nextBody = `${body.slice(0, query.start)}${token}${separator}${suffix}`;
const caret = query.start + token.length + (separator.length || (suffix.length > 0 ? 1 : 0));
return { body: nextBody, caret };
}