From f1abdd3b501ea734891bc585ebd313ee4192531d Mon Sep 17 00:00:00 2001 From: nekobit Date: Fri, 24 Jun 2022 23:56:54 +0000 Subject: [PATCH] Fetch emoji picker document instead for JS - Before we were generating our own but this was pretty slow for JS FossilOrigin-Name: 97ab71ad7f73882b8f980ce0daaad90636e9d2214e4fab4169f975b64f5eb309 --- dist/js/emoji.js | 99 ++--------------------------------------- src/emoji.c | 30 ++++++++++--- src/emoji.h | 1 + src/main.c | 2 + static/emoji_plain.tmpl | 1 + 5 files changed, 33 insertions(+), 100 deletions(-) create mode 100644 static/emoji_plain.tmpl diff --git a/dist/js/emoji.js b/dist/js/emoji.js index 020db66..cf98852 100644 --- a/dist/js/emoji.js +++ b/dist/js/emoji.js @@ -1,10 +1,8 @@ -let emojo_json = null; - // Get emojis from file -function get_emojo_json(callback) +function get_emojo_picker(callback) { let xhr = new XMLHttpRequest(); - xhr.open("GET", "/emoji.json"); + xhr.open("GET", "/emoji_picker"); xhr.onreadystatechange = function() { if (this.readyState === XMLHttpRequest.DONE) callback(this.response); @@ -12,95 +10,6 @@ function get_emojo_json(callback) xhr.send(); } -function construct_emojo_picker(e) -{ - let index = 0; - let emoji_picker = document.createElement("div"); - emoji_picker.className = "emoji-picker"; - emoji_picker.innerHTML = ` - - - - - - - - - - -
- - - - - - - - - - - - - - - -
- -
- - ${construct_emojis("Smileys & Emotion").outerHTML} - - ${construct_emojis("Animals & Nature", index).outerHTML} - - ${construct_emojis("Food & Drink", index).outerHTML} - - ${construct_emojis("Travel & Places", index).outerHTML} - - ${construct_emojis("Activities", index).outerHTML} - - ${construct_emojis("Objects", index).outerHTML} - - ${construct_emojis("Symbols", index).outerHTML} - - ${construct_emojis("Flags", index).outerHTML} -
- -`; - - return emoji_picker; -} - -function construct_emojis(category, start_index = 0) -{ - let emoji_picker_div = document.createElement("div"); - emoji_picker_div.className = "emoji-picker-emojos"; - - for (let i = 0; i < emojo_json.length; ++i) - { - if (emojo_json[i].group === category) - emoji_picker_div.innerHTML += `${emojo_json[i].char}`; - } - - return emoji_picker_div; -} - -get_emojo_json((emojo_json_str) => { - emojo_json = JSON.parse(emojo_json_str); - document.body.appendChild(construct_emojo_picker(emojo_json)); +get_emojo_picker((emojo_picker_str) => { + document.body.innerHTML += emojo_picker_str; }); diff --git a/src/emoji.c b/src/emoji.c index c4c16c7..970d4e5 100644 --- a/src/emoji.c +++ b/src/emoji.c @@ -18,6 +18,7 @@ #include #include +#include "base_page.h" #include "string.h" #include "emoji.h" #include "easprintf.h" @@ -25,6 +26,7 @@ // Pages #include "../static/emoji.ctmpl" +#include "../static/emoji_plain.ctmpl" #include "../static/emoji_picker.ctmpl" enum emoji_categories @@ -83,11 +85,20 @@ char* construct_emoji(struct emoji_info* emoji, char* status_id, size_t* size) if (!emoji) return NULL; - struct emoji_template data = { - .status_id = status_id, - .emoji = emoji->codes - }; - return tmpl_gen_emoji(&data, size); + if (status_id) + { + struct emoji_template data = { + .status_id = status_id, + .emoji = emoji->codes + }; + return tmpl_gen_emoji(&data, size); + } + else { + struct emoji_plain_template data = { + .emoji = emoji->codes + }; + return tmpl_gen_emoji_plain(&data, size); + } } static char* construct_emoji_voidwrap(void* passed, size_t index, size_t* res) @@ -99,6 +110,15 @@ static char* construct_emoji_voidwrap(void* passed, size_t index, size_t* res) #define EMOJI_PICKER_ARGS(this_index) { .status_id = status_id, .index = this_index } +void content_emoji_picker(struct session* ssn, mastodont_t* api, char** data) +{ + char* picker = construct_emoji_picker(NULL, NULL); + + send_result(NULL, NULL, picker, 0); + + free(picker); +} + char* construct_emoji_picker(char* status_id, size_t* size) { diff --git a/src/emoji.h b/src/emoji.h index 1b5d98c..db77084 100644 --- a/src/emoji.h +++ b/src/emoji.h @@ -33,6 +33,7 @@ enum emoji_picker_cat char* emojify(char* content, struct mstdnt_emoji* emos, size_t emos_len); char* construct_emoji(struct emoji_info* emoji, char* status_id, size_t* size); +void content_emoji_picker(struct session* ssn, mastodont_t* api, char** data); char* construct_emoji_picker(char* status_id, size_t* size); #endif // EMOJI_H diff --git a/src/main.c b/src/main.c index d59d94b..c7560de 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,7 @@ #include "page_config.h" #include "path.h" #include "account.h" +#include "emoji.h" #include "login.h" #include "local_config.h" #include "cookie.h" @@ -89,6 +90,7 @@ int main(void) { "/search/accounts", content_search_accounts }, { "/search/hashtags", content_search_hashtags }, { "/search", content_search_all }, + { "/emoji_picker", content_emoji_picker }, { "/lists/edit/:", list_edit }, { "/lists/for/:", content_tl_list }, { "/lists", content_lists }, diff --git a/static/emoji_plain.tmpl b/static/emoji_plain.tmpl new file mode 100644 index 0000000..5fc698f --- /dev/null +++ b/static/emoji_plain.tmpl @@ -0,0 +1 @@ +{{%s:emoji}}