formatting updates

This commit is contained in:
MathMan05 2024-12-20 19:28:08 -06:00
parent ffe21e6d6c
commit d2d0f45c81
50 changed files with 7783 additions and 7432 deletions

View file

@ -1,75 +1,73 @@
import{ Contextmenu }from"./contextmenu.js";
import{ Guild }from"./guild.js";
import { emojijson } from "./jsontypes.js";
import{ Localuser }from"./localuser.js";
import { BinRead } from "./utils/binaryUtils.js";
import {Contextmenu} from "./contextmenu.js";
import {Guild} from "./guild.js";
import {emojijson} from "./jsontypes.js";
import {Localuser} from "./localuser.js";
import {BinRead} from "./utils/binaryUtils.js";
//I need to recompile the emoji format for translation
class Emoji{
class Emoji {
static emojis: {
name: string;
emojis: {
name: string;
emoji: string;
name: string;
emoji: string;
}[];
}[];
name: string;
id?: string;
emoji?:string;
emoji?: string;
animated: boolean;
owner: Guild | Localuser;
get guild(){
if(this.owner instanceof Guild){
get guild() {
if (this.owner instanceof Guild) {
return this.owner;
}
return null;
}
get localuser(){
if(this.owner instanceof Guild){
get localuser() {
if (this.owner instanceof Guild) {
return this.owner.localuser;
}else{
} else {
return this.owner;
}
}
get info(){
get info() {
return this.owner.info;
}
constructor(
json: emojijson,
owner: Guild | Localuser
){
constructor(json: emojijson, owner: Guild | Localuser) {
this.name = json.name;
this.id = json.id;
this.animated = json.animated||false;
this.animated = json.animated || false;
this.owner = owner;
this.emoji=json.emoji;
this.emoji = json.emoji;
}
getHTML(bigemoji: boolean = false){
if(this.id){
getHTML(bigemoji: boolean = false) {
if (this.id) {
const emojiElem = document.createElement("img");
emojiElem.classList.add("md-emoji");
emojiElem.classList.add(bigemoji ? "bigemoji" : "smallemoji");
emojiElem.crossOrigin = "anonymous";
emojiElem.src =this.info.cdn+"/emojis/"+this.id+"."+(this.animated ? "gif" : "png")+"?size=32";
emojiElem.src =
this.info.cdn + "/emojis/" + this.id + "." + (this.animated ? "gif" : "png") + "?size=32";
emojiElem.alt = this.name;
emojiElem.loading = "lazy";
return emojiElem;
}else if(this.emoji){
} else if (this.emoji) {
const emojiElem = document.createElement("span");
emojiElem.classList.add("md-emoji");
emojiElem.classList.add(bigemoji ? "bigemoji" : "smallemoji");
emojiElem.textContent=this.emoji;
emojiElem.textContent = this.emoji;
return emojiElem;
}else{
} else {
throw new Error("This path should *never* be gone down, this means a malformed emoji");
}
}
static decodeEmojiList(buffer: ArrayBuffer){
const reader=new BinRead(buffer)
const build: { name: string; emojis: { name: string; emoji: string }[] }[] = [];
static decodeEmojiList(buffer: ArrayBuffer) {
const reader = new BinRead(buffer);
const build: {name: string; emojis: {name: string; emoji: string}[]}[] = [];
let cats = reader.read16();
for(; cats !== 0; cats--){
for (; cats !== 0; cats--) {
const name = reader.readString16();
const emojis: {
name: string;
@ -77,7 +75,7 @@ class Emoji{
emoji: string;
}[] = [];
let emojinumber = reader.read16();
for(; emojinumber !== 0; emojinumber--){
for (; emojinumber !== 0; emojinumber--) {
//console.log(emojis);
const name = reader.readString8();
const len = reader.read8();
@ -96,22 +94,18 @@ class Emoji{
}
this.emojis = build;
}
static grabEmoji(){
static grabEmoji() {
fetch("/emoji.bin")
.then(e=>{
.then((e) => {
return e.arrayBuffer();
})
.then(e=>{
.then((e) => {
Emoji.decodeEmojiList(e);
});
}
static async emojiPicker(
x: number,
y: number,
localuser: Localuser
): Promise<Emoji | string>{
static async emojiPicker(x: number, y: number, localuser: Localuser): Promise<Emoji | string> {
let res: (r: Emoji | string) => void;
const promise: Promise<Emoji | string> = new Promise(r=>{
const promise: Promise<Emoji | string> = new Promise((r) => {
res = r;
});
const menu = document.createElement("div");
@ -130,33 +124,39 @@ class Emoji{
let isFirst = true;
localuser.guilds
.filter(guild=>guild.id != "@me" && guild.emojis.length > 0)
.forEach(guild=>{
.filter((guild) => guild.id != "@me" && guild.emojis.length > 0)
.forEach((guild) => {
const select = document.createElement("div");
select.classList.add("emojiSelect");
if(guild.properties.icon){
if (guild.properties.icon) {
const img = document.createElement("img");
img.classList.add("pfp", "servericon", "emoji-server");
img.crossOrigin = "anonymous";
img.src = localuser.info.cdn+"/icons/"+guild.properties.id+"/"+guild.properties.icon+".png?size=48";
img.src =
localuser.info.cdn +
"/icons/" +
guild.properties.id +
"/" +
guild.properties.icon +
".png?size=48";
img.alt = "Server: " + guild.properties.name;
select.appendChild(img);
}else{
} else {
const div = document.createElement("span");
div.textContent = guild.properties.name
.replace(/'s /g, " ")
.replace(/\w+/g, word=>word[0])
.replace(/\w+/g, (word) => word[0])
.replace(/\s/g, "");
select.append(div);
}
selection.append(select);
const clickEvent = ()=>{
const clickEvent = () => {
title.textContent = guild.properties.name;
body.innerHTML = "";
for(const emojit of guild.emojis){
for (const emojit of guild.emojis) {
const emojiElem = document.createElement("div");
emojiElem.classList.add("emojiSelect");
@ -166,14 +166,14 @@ class Emoji{
name: emojit.name,
animated: emojit.animated as boolean,
},
localuser
localuser,
);
emojiElem.append(emojiClass.getHTML());
body.append(emojiElem);
emojiElem.addEventListener("click", ()=>{
emojiElem.addEventListener("click", () => {
res(emojiClass);
if(Contextmenu.currentmenu !== ""){
if (Contextmenu.currentmenu !== "") {
Contextmenu.currentmenu.remove();
}
});
@ -181,14 +181,14 @@ class Emoji{
};
select.addEventListener("click", clickEvent);
if(isFirst){
if (isFirst) {
clickEvent();
isFirst = false;
}
});
setTimeout(()=>{
if(Contextmenu.currentmenu != ""){
setTimeout(() => {
if (Contextmenu.currentmenu != "") {
Contextmenu.currentmenu.remove();
}
document.body.append(menu);
@ -197,29 +197,29 @@ class Emoji{
}, 10);
let i = 0;
for(const thing of Emoji.emojis){
for (const thing of Emoji.emojis) {
const select = document.createElement("div");
select.textContent = thing.emojis[0].emoji;
select.classList.add("emojiSelect");
selection.append(select);
const clickEvent = ()=>{
const clickEvent = () => {
title.textContent = thing.name;
body.innerHTML = "";
for(const emojit of thing.emojis){
for (const emojit of thing.emojis) {
const emoji = document.createElement("div");
emoji.classList.add("emojiSelect");
emoji.textContent = emojit.emoji;
body.append(emoji);
emoji.onclick = _=>{
emoji.onclick = (_) => {
res(emojit.emoji);
if(Contextmenu.currentmenu !== ""){
if (Contextmenu.currentmenu !== "") {
Contextmenu.currentmenu.remove();
}
};
}
};
select.onclick = clickEvent;
if(i === 0){
if (i === 0) {
clickEvent();
}
i++;
@ -228,40 +228,39 @@ class Emoji{
menu.append(body);
return promise;
}
static searchEmoji(search:string,localuser:Localuser,results=50):[Emoji,number][]{
const ranked:[emojijson,number][]=[];
function similar(json:emojijson){
if(json.name.includes(search)){
ranked.push([json,search.length/json.name.length]);
static searchEmoji(search: string, localuser: Localuser, results = 50): [Emoji, number][] {
const ranked: [emojijson, number][] = [];
function similar(json: emojijson) {
if (json.name.includes(search)) {
ranked.push([json, search.length / json.name.length]);
return true;
}else if(json.name.toLowerCase().includes(search.toLowerCase())){
ranked.push([json,search.length/json.name.length/1.4]);
} else if (json.name.toLowerCase().includes(search.toLowerCase())) {
ranked.push([json, search.length / json.name.length / 1.4]);
return true;
}else{
} else {
return false;
}
}
for(const group of this.emojis){
for(const emoji of group.emojis){
similar(emoji)
for (const group of this.emojis) {
for (const emoji of group.emojis) {
similar(emoji);
}
}
const weakGuild=new WeakMap<emojijson,Guild>();
for(const guild of localuser.guilds){
if(guild.id!=="@me"&&guild.emojis.length!==0){
for(const emoji of guild.emojis){
if(similar(emoji)){
weakGuild.set(emoji,guild);
};
const weakGuild = new WeakMap<emojijson, Guild>();
for (const guild of localuser.guilds) {
if (guild.id !== "@me" && guild.emojis.length !== 0) {
for (const emoji of guild.emojis) {
if (similar(emoji)) {
weakGuild.set(emoji, guild);
}
}
}
}
ranked.sort((a,b)=>b[1]-a[1]);
return ranked.splice(0,results).map(a=>{
return [new Emoji(a[0],weakGuild.get(a[0])||localuser),a[1]];
})
ranked.sort((a, b) => b[1] - a[1]);
return ranked.splice(0, results).map((a) => {
return [new Emoji(a[0], weakGuild.get(a[0]) || localuser), a[1]];
});
}
}
Emoji.grabEmoji();
export{ Emoji };
export {Emoji};