refactor pages/emojis.category.vue to composition API

This commit is contained in:
Johann150 2022-07-31 14:04:29 +02:00
parent 1671b7123e
commit 5db59ba560
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,98 +1,46 @@
<template> <template>
<div class="driuhtrh"> <div class="driuhtrh">
<div class="query"> <div class="query">
<MkInput v-model="q" class="" :placeholder="$ts.search"> <MkInput v-model="q" class="" :placeholder="i18n.ts.search">
<template #prefix><i class="fas fa-search"></i></template> <template #prefix><i class="fas fa-search"></i></template>
</MkInput> </MkInput>
<!-- たくさんあると邪魔
<div class="tags">
<span class="tag _button" v-for="tag in tags" :class="{ active: selectedTags.has(tag) }" @click="toggleTag(tag)">{{ tag }}</span>
</div>
-->
</div> </div>
<MkFolder v-if="searchEmojis" class="emojis"> <MkFolder v-if="searchEmojis" class="emojis">
<template #header>{{ $ts.searchResult }}</template> <template #header>{{ i18n.ts.searchResult }}</template>
<div class="zuvgdzyt"> <div class="zuvgdzyt">
<XEmoji v-for="emoji in searchEmojis" :key="emoji.name" class="emoji" :emoji="emoji"/> <XEmoji v-for="emoji in searchEmojis" :key="emoji.name" class="emoji" :emoji="emoji"/>
</div> </div>
</MkFolder> </MkFolder>
<MkFolder v-for="category in customEmojiCategories" :key="category" class="emojis"> <MkFolder v-for="category in emojiCategories" :key="category" class="emojis">
<template #header>{{ category || $ts.other }}</template> <template #header>{{ category || i18n.ts.other }}</template>
<div class="zuvgdzyt"> <div class="zuvgdzyt">
<XEmoji v-for="emoji in customEmojis.filter(e => e.category === category)" :key="emoji.name" class="emoji" :emoji="emoji"/> <XEmoji v-for="emoji in instance.emojis.filter(e => e.category === category)" :key="emoji.name" class="emoji" :emoji="emoji"/>
</div> </div>
</MkFolder> </MkFolder>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, computed } from 'vue'; import { watch } from 'vue';
import MkButton from '@/components/ui/button.vue';
import MkInput from '@/components/form/input.vue'; import MkInput from '@/components/form/input.vue';
import MkSelect from '@/components/form/select.vue';
import MkFolder from '@/components/ui/folder.vue'; import MkFolder from '@/components/ui/folder.vue';
import MkTab from '@/components/tab.vue'; import { i18n } from '@/i18n';
import * as os from '@/os'; import { emojiCategories, instance } from '@/instance';
import { emojiCategories, emojiTags } from '@/instance';
import XEmoji from './emojis.emoji.vue'; import XEmoji from './emojis.emoji.vue';
export default defineComponent({ let q: string = $ref('');
components: { let searchEmojis: null | Record<string, any>[] = $ref(null);
MkButton,
MkInput,
MkSelect,
MkFolder,
MkTab,
XEmoji,
},
data() { function search() {
return { if (q === '') {
q: '', searchEmojis = null;
customEmojiCategories: emojiCategories,
customEmojis: this.$instance.emojis,
tags: emojiTags,
selectedTags: new Set(),
searchEmojis: null,
};
},
watch: {
q() { this.search(); },
selectedTags: {
handler() {
this.search();
},
deep: true
},
},
methods: {
search() {
if ((this.q === '' || this.q == null) && this.selectedTags.size === 0) {
this.searchEmojis = null;
return;
}
if (this.selectedTags.size === 0) {
this.searchEmojis = this.customEmojis.filter(emoji => emoji.name.includes(this.q) || emoji.aliases.includes(this.q));
} else { } else {
this.searchEmojis = this.customEmojis.filter(emoji => (emoji.name.includes(this.q) || emoji.aliases.includes(this.q)) && [...this.selectedTags].every(t => emoji.aliases.includes(t))); searchEmojis = instance.emojis.filter(emoji => emoji.name.includes(this.q) || emoji.aliases.includes(this.q));
} }
}, }
watch(q, search);
toggleTag(tag) {
if (this.selectedTags.has(tag)) {
this.selectedTags.delete(tag);
} else {
this.selectedTags.add(tag);
}
}
}
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>