fix type of MkEmojiPicker

This commit is contained in:
Lhcfl 2024-04-11 18:00:37 +08:00
parent db37eb4ad1
commit c4a093209f
1 changed files with 33 additions and 10 deletions

View File

@ -180,6 +180,11 @@ import { i18n } from "@/i18n";
import { defaultStore } from "@/store"; import { defaultStore } from "@/store";
import icon from "@/scripts/icon"; import icon from "@/scripts/icon";
// FIXME: This variable doesn't seem to be used at all. I don't know why it was here.
const isActive = ref<boolean>();
type EmojiDef = string | entities.CustomEmoji | UnicodeEmojiDef;
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
showPinned?: boolean; showPinned?: boolean;
@ -193,7 +198,7 @@ const props = withDefaults(
); );
const emit = defineEmits<{ const emit = defineEmits<{
(ev: "chosen", v: string, ev: MouseEvent): void; chosen: [v: string, ev?: MouseEvent],
}>(); }>();
const search = ref<HTMLInputElement>(); const search = ref<HTMLInputElement>();
@ -411,12 +416,18 @@ function reset() {
} }
function getKey( function getKey(
emoji: string | entities.CustomEmoji | UnicodeEmojiDef, emoji: EmojiDef,
): string { ): string {
return typeof emoji === "string" ? emoji : emoji.emoji || `:${emoji.name}:`; if (typeof emoji === "string") {
return emoji;
}
if ("emoji" in emoji) {
return emoji.emoji;
}
return `:${emoji.name}:`;
} }
function chosen(emoji: any, ev?: MouseEvent) { function chosen(emoji: EmojiDef, ev?: MouseEvent) {
const el = const el =
ev && ((ev.currentTarget ?? ev.target) as HTMLElement | null | undefined); ev && ((ev.currentTarget ?? ev.target) as HTMLElement | null | undefined);
if (el) { if (el) {
@ -432,22 +443,33 @@ function chosen(emoji: any, ev?: MouseEvent) {
// 使 // 使
if (!pinned.value.includes(key)) { if (!pinned.value.includes(key)) {
let recents = defaultStore.state.recentlyUsedEmojis; let recents = defaultStore.state.recentlyUsedEmojis;
recents = recents.filter((emoji: any) => emoji !== key); recents = recents.filter((emoji) => emoji !== key);
recents.unshift(key); recents.unshift(key);
defaultStore.set("recentlyUsedEmojis", recents.splice(0, 32)); defaultStore.set("recentlyUsedEmojis", recents.splice(0, 32));
} }
} }
function paste(event: ClipboardEvent) { async function paste(event: ClipboardEvent) {
const paste = (event.clipboardData || window.clipboardData).getData("text"); let pasteStr: string | null = null;
if (done(paste)) { if (event.clipboardData) {
pasteStr = event.clipboardData.getData("text");
} else {
// Use native api
try {
pasteStr = await window.navigator.clipboard.readText();
} catch(_err) {
// Reading the clipboard requires permission, and the user did not give it
}
}
if (done(pasteStr)) {
event.preventDefault(); event.preventDefault();
} }
} }
function done(query?: any): boolean | void { function done(query?: string | null): boolean {
// biome-ignore lint/style/noParameterAssign: assign it intentially
if (query == null) query = q.value; if (query == null) query = q.value;
if (query == null || typeof query !== "string") return; if (query == null || typeof query !== "string") return false;
const q2 = query.replaceAll(":", ""); const q2 = query.replaceAll(":", "");
const exactMatchCustom = customEmojis.find((emoji) => emoji.name === q2); const exactMatchCustom = customEmojis.find((emoji) => emoji.name === q2);
@ -470,6 +492,7 @@ function done(query?: any): boolean | void {
chosen(searchResultUnicode.value[0]); chosen(searchResultUnicode.value[0]);
return true; return true;
} }
return false;
} }
onMounted(() => { onMounted(() => {