Skip to content

Commit

Permalink
Migrate apps/meteor/app/emoji-custom/client/lib/emojiCustom.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Oct 18, 2024
1 parent 52f5582 commit 2cec073
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IEmoji } from '@rocket.chat/core-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
Expand All @@ -6,52 +7,61 @@ import { emoji, updateRecent } from '../../../emoji/client';
import { CachedCollectionManager } from '../../../ui-cached-collection/client';
import { getURL } from '../../../utils/client';
import { sdk } from '../../../utils/client/lib/SDKClient';
import { isSetNotNull } from './function-isSet';

export const getEmojiUrlFromName = function (name, extension) {
const isSetNotNull = (fn: () => unknown) => {
let value;
try {
value = fn();
} catch (e) {
value = null;
}
return value !== null && value !== undefined;
};

const getEmojiUrlFromName = (name: string, extension: string) => {
if (name == null) {
return;
}

const key = `emoji_random_${name}`;
const key = `emoji_random_${name}` as const;

const random = isSetNotNull(() => Session.keys[key]) ? Session.keys[key] : 0;
const random = (Session as unknown as { keys: Record<string, any> }).keys[key] ?? 0;

return getURL(`/emoji-custom/${encodeURIComponent(name)}.${extension}?_dc=${random}`);
};

export const deleteEmojiCustom = function (emojiData) {
export const deleteEmojiCustom = (emojiData: IEmoji) => {
delete emoji.list[`:${emojiData.name}:`];
const arrayIndex = emoji.packages.emojiCustom.emojisByCategory.rocket.indexOf(emojiData.name);
if (arrayIndex !== -1) {
emoji.packages.emojiCustom.emojisByCategory.rocket.splice(arrayIndex, 1);
}
const arrayIndexList = emoji.packages.emojiCustom.list.indexOf(`:${emojiData.name}:`);
const arrayIndexList = emoji.packages.emojiCustom.list?.indexOf(`:${emojiData.name}:`) ?? -1;
if (arrayIndexList !== -1) {
emoji.packages.emojiCustom.list.splice(arrayIndexList, 1);
emoji.packages.emojiCustom.list?.splice(arrayIndexList, 1);
}
if (isSetNotNull(() => emojiData.aliases)) {
if (emojiData.aliases) {
for (const alias of emojiData.aliases) {
delete emoji.list[`:${alias}:`];
const aliasIndex = emoji.packages.emojiCustom.list.indexOf(`:${alias}:`);
const aliasIndex = emoji.packages.emojiCustom.list?.indexOf(`:${alias}:`) ?? -1;
if (aliasIndex !== -1) {
emoji.packages.emojiCustom.list.splice(aliasIndex, 1);
emoji.packages.emojiCustom.list?.splice(aliasIndex, 1);
}
}
}
updateRecent('rocket');
updateRecent(['rocket']);
};

export const updateEmojiCustom = function (emojiData) {
export const updateEmojiCustom = (emojiData: IEmoji) => {
const previousExists = isSetNotNull(() => emojiData.previousName);
const currentAliases = isSetNotNull(() => emojiData.aliases);

if (previousExists && isSetNotNull(() => emoji.list[`:${emojiData.previousName}:`].aliases)) {
for (const alias of emoji.list[`:${emojiData.previousName}:`].aliases) {
for (const alias of emoji.list[`:${emojiData.previousName}:`].aliases ?? []) {
delete emoji.list[`:${alias}:`];
const aliasIndex = emoji.packages.emojiCustom.list.indexOf(`:${alias}:`);
const aliasIndex = emoji.packages.emojiCustom.list?.indexOf(`:${alias}:`) ?? -1;
if (aliasIndex !== -1) {
emoji.packages.emojiCustom.list.splice(aliasIndex, 1);
emoji.packages.emojiCustom.list?.splice(aliasIndex, 1);
}
}
}
Expand All @@ -61,33 +71,34 @@ export const updateEmojiCustom = function (emojiData) {
if (arrayIndex !== -1) {
emoji.packages.emojiCustom.emojisByCategory.rocket.splice(arrayIndex, 1);
}
const arrayIndexList = emoji.packages.emojiCustom.list.indexOf(`:${emojiData.previousName}:`);
const arrayIndexList = emoji.packages.emojiCustom.list?.indexOf(`:${emojiData.previousName}:`) ?? -1;
if (arrayIndexList !== -1) {
emoji.packages.emojiCustom.list.splice(arrayIndexList, 1);
emoji.packages.emojiCustom.list?.splice(arrayIndexList, 1);
}
delete emoji.list[`:${emojiData.previousName}:`];
}

const categoryIndex = emoji.packages.emojiCustom.emojisByCategory.rocket.indexOf(`${emojiData.name}`);
if (categoryIndex === -1) {
emoji.packages.emojiCustom.emojisByCategory.rocket.push(`${emojiData.name}`);
emoji.packages.emojiCustom.list.push(`:${emojiData.name}:`);
emoji.packages.emojiCustom.list?.push(`:${emojiData.name}:`);
}
emoji.list[`:${emojiData.name}:`] = Object.assign({ emojiPackage: 'emojiCustom' }, emoji.list[`:${emojiData.name}:`], emojiData);
if (currentAliases) {
for (const alias of emojiData.aliases) {
emoji.packages.emojiCustom.list.push(`:${alias}:`);
emoji.list[`:${alias}:`] = {};
emoji.list[`:${alias}:`].emojiPackage = 'emojiCustom';
emoji.list[`:${alias}:`].aliasOf = emojiData.name;
emoji.packages.emojiCustom.list?.push(`:${alias}:`);
emoji.list[`:${alias}:`] = {
emojiPackage: 'emojiCustom',
aliasOf: emojiData.name,
};
}
}

updateRecent('rocket');
updateRecent(['rocket']);
};

const customRender = (html) => {
const emojisMatchGroup = emoji.packages.emojiCustom.list.map(escapeRegExp).join('|');
const customRender = (html: string) => {
const emojisMatchGroup = emoji.packages.emojiCustom.list?.map(escapeRegExp).join('|');
if (emojisMatchGroup !== emoji.packages.emojiCustom._regexpSignature) {
emoji.packages.emojiCustom._regexpSignature = emojisMatchGroup;
emoji.packages.emojiCustom._regexp = new RegExp(
Expand All @@ -96,22 +107,22 @@ const customRender = (html) => {
);
}

html = html.replace(emoji.packages.emojiCustom._regexp, (shortname) => {
if (typeof shortname === 'undefined' || shortname === '' || emoji.packages.emojiCustom.list.indexOf(shortname) === -1) {
html = html.replace(emoji.packages.emojiCustom._regexp!, (shortname) => {
if (typeof shortname === 'undefined' || shortname === '' || (emoji.packages.emojiCustom.list?.indexOf(shortname) ?? -1) === -1) {
return shortname;
}

let emojiAlias = shortname.replace(/:/g, '');

let dataCheck = emoji.list[shortname];
if (dataCheck.hasOwnProperty('aliasOf')) {
if (dataCheck.aliasOf) {
emojiAlias = dataCheck.aliasOf;
dataCheck = emoji.list[`:${emojiAlias}:`];
}

return `<span class="emoji" style="background-image:url(${getEmojiUrlFromName(
emojiAlias,
dataCheck.extension,
dataCheck.extension!,
)});" data-emoji="${emojiAlias}" title="${shortname}">${shortname}</span>`;
});

Expand All @@ -125,7 +136,7 @@ emoji.packages.emojiCustom = {
list: [],
_regexpSignature: null,
_regexp: null,

emojisByCategory: {},
render: customRender,
renderPicker: customRender,
};
Expand All @@ -135,16 +146,15 @@ Meteor.startup(() =>
try {
const {
emojis: { update: emojis },
} = await sdk.rest.get('/v1/emoji-custom.list');
} = await sdk.rest.get('/v1/emoji-custom.list', { query: '' });

emoji.packages.emojiCustom.emojisByCategory = { rocket: [] };
for (const currentEmoji of emojis) {
emoji.packages.emojiCustom.emojisByCategory.rocket.push(currentEmoji.name);
emoji.packages.emojiCustom.list.push(`:${currentEmoji.name}:`);
emoji.list[`:${currentEmoji.name}:`] = currentEmoji;
emoji.list[`:${currentEmoji.name}:`].emojiPackage = 'emojiCustom';
emoji.packages.emojiCustom.list?.push(`:${currentEmoji.name}:`);
emoji.list[`:${currentEmoji.name}:`] = { ...currentEmoji, emojiPackage: 'emojiCustom' } as any;
for (const alias of currentEmoji.aliases) {
emoji.packages.emojiCustom.list.push(`:${alias}:`);
emoji.packages.emojiCustom.list?.push(`:${alias}:`);
emoji.list[`:${alias}:`] = {
emojiPackage: 'emojiCustom',
aliasOf: currentEmoji.name,
Expand Down
9 changes: 0 additions & 9 deletions apps/meteor/app/emoji-custom/client/lib/function-isSet.js

This file was deleted.

32 changes: 23 additions & 9 deletions apps/meteor/app/emoji/lib/rocketchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,35 @@ export type EmojiPackage = {
renderPicker: (emojiToRender: string) => string | undefined;
ascii?: boolean;
sprites?: unknown;
list?: string[];
_regexpSignature?: string | null;
_regexp?: RegExp | null;
};

export type EmojiPackages = {
packages: {
[key: string]: EmojiPackage;
};
list: {
[key: keyof NonNullable<EmojiPackages['packages']>]: {
category: string;
emojiPackage: string;
shortnames: string[];
uc_base: string;
uc_greedy: string;
uc_match: string;
uc_output: string;
};
[key: keyof NonNullable<EmojiPackages['packages']>]:
| {
category: string;
emojiPackage: string;
shortnames: string[];
uc_base: string;
uc_greedy: string;
uc_match: string;
uc_output: string;
aliases?: string[];
aliasOf?: undefined;
extension?: string;
}
| {
emojiPackage: string;
aliasOf: string;
extension?: undefined;
aliases?: undefined;
shortnames?: undefined;
};
};
};

0 comments on commit 2cec073

Please sign in to comment.