Skip to content

Commit 4173ab2

Browse files
fix/refactor
1 parent 5a04a13 commit 4173ab2

5 files changed

Lines changed: 35 additions & 30 deletions

File tree

‎packages/frontend/src/components/MkEmojiPicker.vue‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ SPDX-License-Identifier: AGPL-3.0-only
8686
</div>
8787
</section>
8888
</div>
89-
<div v-once class="group">
89+
<div v-memo="[customEmojiFolderRoot]" class="group">
9090
<header class="_acrylic">{{ i18n.ts.customEmojis }}</header>
9191
<XSection
9292
v-for="child in customEmojiFolderRoot.children"
@@ -189,8 +189,6 @@ const searchResultCustom = ref<Misskey.entities.EmojiSimple[]>([]);
189189
const searchResultUnicode = ref<UnicodeEmojiDef[]>([]);
190190
const tab = ref<'index' | 'custom' | 'unicode' | 'tags'>('index');
191191
192-
const customEmojiFolderRoot: CustomEmojiFolderTree = { value: '', category: '', children: [] };
193-
194192
function parseAndMergeCategories(input: string, root: CustomEmojiFolderTree): CustomEmojiFolderTree {
195193
const parts = input.split('/').map(p => p.trim());
196194
let currentNode: CustomEmojiFolderTree = root;
@@ -210,13 +208,19 @@ function parseAndMergeCategories(input: string, root: CustomEmojiFolderTree): Cu
210208
return currentNode;
211209
}
212210
213-
customEmojiCategories.value.forEach(ec => {
214-
if (ec !== null) {
215-
parseAndMergeCategories(ec, customEmojiFolderRoot);
216-
}
217-
});
211+
const customEmojiFolderRoot = computed(() => {
212+
const root: CustomEmojiFolderTree = { value: '', category: '', children: [] };
218213
219-
parseAndMergeCategories('', customEmojiFolderRoot);
214+
customEmojiCategories.value.forEach(ec => {
215+
if (ec !== null) {
216+
parseAndMergeCategories(ec, root);
217+
}
218+
});
219+
220+
parseAndMergeCategories('', root);
221+
222+
return root;
223+
});
220224
221225
watch(q, () => {
222226
if (emojisEl.value) emojisEl.value.scrollTop = 0;
@@ -257,8 +261,9 @@ watch(q, () => {
257261
}
258262
}
259263
} else {
260-
if (customEmojisMap.has(newQ)) {
261-
matches.add(customEmojisMap.get(newQ)!);
264+
const exactMatch = customEmojisMap.value.get(newQ);
265+
if (exactMatch != null) {
266+
matches.add(exactMatch);
262267
}
263268
if (matches.size >= max) return matches;
264269
@@ -405,7 +410,7 @@ function getDef(emoji: string): string | Misskey.entities.EmojiSimple | UnicodeE
405410
// カスタム絵文字が存在する場合はその情報を持つオブジェクトを返し、
406411
// サーバの管理画面から削除された等で情報が見つからない場合は名前の文字列をそのまま返しておく(undefinedを返すとエラーになるため)
407412
const name = emoji.replaceAll(':', '');
408-
return customEmojisMap.get(name) ?? emoji;
413+
return customEmojisMap.value.get(name) ?? emoji;
409414
} else {
410415
return getUnicodeEmoji(emoji);
411416
}
@@ -476,7 +481,7 @@ function done(query?: string): boolean | void {
476481
if (query == null || typeof query !== 'string') return;
477482
478483
const q2 = query.replace(/:/g, '');
479-
const exactMatchCustom = customEmojisMap.get(q2);
484+
const exactMatchCustom = customEmojisMap.value.get(q2);
480485
if (exactMatchCustom) {
481486
chosen(exactMatchCustom);
482487
return true;

‎packages/frontend/src/components/MkReactionsViewer.reaction.vue‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const emojiName = computed(() => getEmojiNameFromReaction(props.reaction));
6464
const isLocalCustomEmoji = computed(() => isLocalCustomEmojiReaction(props.reaction));
6565
6666
const canToggle = computed(() => {
67-
const emoji = isLocalCustomEmoji.value ? customEmojisMap.get(emojiName.value) : getUnicodeEmojiOrNull(props.reaction);
67+
const emoji = isLocalCustomEmoji.value ? customEmojisMap.value.get(emojiName.value) : getUnicodeEmojiOrNull(props.reaction);
6868
6969
// TODO
7070
//return $i != null && emoji != null && checkReactionPermissions($i, props.note, emoji);
@@ -107,7 +107,7 @@ async function toggleReaction() {
107107
noteId: props.noteId,
108108
reaction: props.reaction,
109109
}).then(() => {
110-
const emoji = customEmojisMap.get(emojiName.value);
110+
const emoji = customEmojisMap.value.get(emojiName.value);
111111
if (emoji == null && getUnicodeEmojiOrNull(props.reaction) == null) {
112112
return;
113113
}
@@ -141,7 +141,7 @@ async function toggleReaction() {
141141
noteId: props.noteId,
142142
reaction: props.reaction,
143143
}).then(() => {
144-
const emoji = customEmojisMap.get(emojiName.value);
144+
const emoji = customEmojisMap.value.get(emojiName.value);
145145
if (emoji == null && getUnicodeEmojiOrNull(props.reaction) == null) {
146146
return;
147147
}

‎packages/frontend/src/components/MkReactionsViewer.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function canReact(reaction: string) {
7878
if (!$i) return false;
7979
// TODO: CheckPermissions
8080
return isLocalCustomEmojiReaction(reaction)
81-
? customEmojisMap.has(getEmojiNameFromReaction(reaction))
81+
? customEmojisMap.value.has(getEmojiNameFromReaction(reaction))
8282
: isSupportedEmoji(reaction);
8383
}
8484

‎packages/frontend/src/components/global/MkCustomEmoji.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const rawUrl = computed(() => {
7979
return props.url;
8080
}
8181
if (isLocal.value) {
82-
return customEmojisMap.get(customEmojiName.value)?.url ?? null;
82+
return customEmojisMap.value.get(customEmojiName.value)?.url ?? null;
8383
}
8484
return getCustomEmojiImagePath(customEmojiName.value, props.host);
8585
});

‎packages/frontend/src/custom-emojis.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@
33
* SPDX-License-Identifier: AGPL-3.0-only
44
*/
55

6-
import { shallowRef, computed, markRaw, watch, triggerRef } from 'vue';
6+
import { shallowRef, computed, markRaw } from 'vue';
77
import * as Misskey from 'misskey-js';
88
import { misskeyApi, misskeyApiGet } from '@/utility/misskey-api.js';
99
import { get, set } from '@/utility/idb-proxy.js';
1010

1111
const storageCache = await get('emojis');
1212
export const customEmojis = shallowRef<Misskey.entities.EmojiSimple[]>(Array.isArray(storageCache) ? storageCache : []);
13-
export const customEmojiCategories = computed<[ ...string[], null ]>(() => {
13+
export const customEmojiCategories = computed<[ ...string[], null ]>((oldValue) => {
1414
const categories = new Set<string>();
1515
for (const emoji of customEmojis.value) {
1616
if (emoji.category && emoji.category !== 'null') {
1717
categories.add(emoji.category);
1818
}
1919
}
20-
return markRaw([...Array.from(categories), null]);
20+
const res: [ ...string[], null ] = [...Array.from(categories), null];
21+
// カテゴリ構成が変わっていなければ同一参照を返し、依存先の再計算を抑える
22+
if (oldValue != null && oldValue.length === res.length && oldValue.every((c, i) => c === res[i])) return oldValue;
23+
return markRaw(res);
2124
});
2225

23-
export const customEmojisMap = new Map<string, Misskey.entities.EmojiSimple>();
24-
watch(customEmojis, emojis => {
25-
customEmojisMap.clear();
26-
for (const emoji of emojis) {
27-
customEmojisMap.set(emoji.name, emoji);
26+
export const customEmojisMap = computed(() => {
27+
const map = new Map<string, Misskey.entities.EmojiSimple>();
28+
for (const emoji of customEmojis.value) {
29+
map.set(emoji.name, emoji);
2830
}
29-
}, { immediate: true });
31+
return markRaw(map);
32+
});
3033

3134
export function addCustomEmoji(emoji: Misskey.entities.EmojiSimple) {
3235
customEmojis.value = [emoji, ...customEmojis.value];
@@ -45,9 +48,6 @@ export function removeCustomEmojis(emojis: Misskey.entities.EmojiSimple[]) {
4548

4649
export async function initCustomEmojiCache() {
4750
const fetchingPromise = fetchCustomEmojis()
48-
.then(() => {
49-
triggerRef(customEmojis);
50-
})
5151
.catch(() => { /* nop */ });
5252

5353
if (customEmojis.value.length === 0) {

0 commit comments

Comments
 (0)