huge refactor. addresses #21 w/ inifinite scrolling notifications

This commit is contained in:
ari melody 2024-07-03 22:00:32 +01:00
parent f883b61659
commit 2e64f63caa
Signed by: ari
GPG key ID: CF99829C92678188
29 changed files with 887 additions and 1030 deletions

View file

@ -1,52 +1,27 @@
import { client } from './client/client.js';
import { get } from 'svelte/store';
export const EMOJI_REGEX = /:[\w\-.]{0,32}:/g;
export const EMOJI_REGEX = /:[\w\-.]{0,32}@[\w\-.]{0,32}:/g;
export const EMOJI_NAME_REGEX = /:[\w\-.]{0,32}:/g;
export default class Emoji {
name;
url;
constructor(id, name, host, url) {
this.id = id;
this.name = name;
this.host = host;
this.url = url;
}
get html() {
if (this.url)
return `<img src="${this.url}" class="emoji" height="20" title="${this.name}" alt="${this.name}">`;
else
return `${this.name}`;
}
export function parseEmoji(shortcode, url) {
let emoji = { shortcode, url };
if (emoji.shortcode == '❤') emoji.shortcode = '❤️'; // stupid heart unicode
emoji.html = `<img src="${emoji.url}" class="emoji" height="20" title="${emoji.shortcode}" alt="${emoji.shortcode}"/>`;
return emoji;
}
export function parseText(text, host) {
export function renderEmoji(text, emoji_list) {
if (!text) return text;
let index = text.search(EMOJI_NAME_REGEX);
let index = text.search(EMOJI_REGEX);
if (index === -1) return text;
// find the emoji name
// find the closing comma
let length = text.substring(index + 1).search(':');
if (length <= 0) return text;
let emoji_name = text.substring(index + 1, index + length + 1);
let emoji = get(client).getEmoji(emoji_name + '@' + host);
if (emoji) {
return text.substring(0, index) + emoji.html +
parseText(text.substring(index + length + 2), host);
}
return text.substring(0, index + length + 1) +
parseText(text.substring(index + length + 1), host);
}
// see if emoji is valid
let shortcode = text.substring(index + 1, index + length + 1);
let emoji = emoji_list[shortcode];
let replace = emoji ? emoji.html : shortcode;
export function parseOne(emoji_id) {
if (emoji_id == '❤') return '❤️'; // stupid heart unicode
if (EMOJI_REGEX.exec(':' + emoji_id + ':')) return emoji_id;
let cached_emoji = get(client).getEmoji(emoji_id);
if (!cached_emoji) return emoji_id;
return cached_emoji.html;
return text.substring(0, index) + replace + renderEmoji(text.substring(index + length + 2), emoji_list);
}