Files
www/components/tabs/LookupTab.vue
T
2021-10-25 11:05:38 +07:00

564 lines
14 KiB
Vue

<template>
<section>
<div class="CharacterTab">
<form class="field" @submit.prevent="q = q0">
<label for="q" class="label">
Search
<b-tooltip label="How to?" position="is-right">
<a
href="https://github.com/zhquiz/zhquiz/wiki/How-to-search-or-filter"
target="_blank"
rel="noopener noreferrer"
>
<b-icon icon="info-circle"></b-icon>
</a>
</b-tooltip>
</label>
<div class="control">
<input
v-model="q0"
class="input"
type="search"
name="q"
placeholder="Type here to search."
aria-label="search"
/>
</div>
</form>
<div class="columns">
<div class="column is-6 entry-display">
<div
class="character-display clickable font-han"
@click="(evt) => openContext(evt, current, 'character')"
@contextmenu.prevent="
(evt) => openContext(evt, current, 'character')
"
>
{{ current }}
</div>
<div class="buttons has-addons">
<button
class="button"
:disabled="i < 1"
@click="i--"
@keypress="i--"
>
Previous
</button>
<button
class="button"
:disabled="i > entries.length - 2"
@click="i++"
@keypress="i++"
>
Next
</button>
</div>
<div v-if="tag.length" class="mb-4">
Tags:
<b-taglist style="display: inline-flex">
<b-tag v-for="t in tag.slice(0, 5)" :key="t" type="is-info">
{{ t }}
</b-tag>
</b-taglist>
</div>
<div v-if="level && level <= 60" class="mb-4">
<b-taglist attached>
<b-tag type="is-dark">Level</b-tag>
<b-tag type="is-primary">
{{ level }}
</b-tag>
</b-taglist>
</div>
</div>
<div class="column is-6">
<b-collapse class="card" animation="slide" :open="false">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Reading</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
{{ reading.join(' / ') }}
</div>
</b-collapse>
<b-collapse
class="card"
animation="slide"
:open="!!translation.length"
>
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Translation</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
{{ translation.join(' / ') }}
</div>
</b-collapse>
<b-collapse class="card" animation="slide" :open="!!sub.length">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Subcompositions</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
<span
v-for="h in sub"
:key="h"
class="font-han clickable"
@click="(evt) => openContext(evt, h, 'character')"
@contextmenu.prevent="(evt) => openContext(evt, h, 'character')"
>
{{ h }}
</span>
</div>
</b-collapse>
<b-collapse class="card" animation="slide" :open="!!sup.length">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Supercompositions</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
<span
v-for="h in sup"
:key="h"
class="font-han clickable"
@click="(evt) => openContext(evt, h, 'character')"
@contextmenu.prevent="(evt) => openContext(evt, h, 'character')"
>
{{ h }}
</span>
</div>
</b-collapse>
<b-collapse class="card" animation="slide" :open="!!variants.length">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Variants</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
<span
v-for="h in variants"
:key="h"
class="font-han clickable"
@click="(evt) => openContext(evt, h, 'character')"
@contextmenu.prevent="(evt) => openContext(evt, h, 'character')"
>
{{ h }}
</span>
</div>
</b-collapse>
<b-collapse class="card" animation="slide" :open="!!vocabs.length">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Vocabularies</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
<div v-for="(v, i) in vocabs" :key="i" class="long-item">
<span
class="clickable"
@click="(evt) => openContext(evt, v.entry, 'vocabulary')"
@contextmenu.prevent="
(evt) => openContext(evt, v.entry, 'vocabulary')
"
>
{{ v.entry }}
</span>
<span v-if="v.alt" class="clickable">
{{ v.alt.join(' ') }}
</span>
<span class="pinyin">[{{ v.reading.join(' / ') }}]</span>
<span>{{ v.translation.join(' / ') }}</span>
</div>
</div>
</b-collapse>
<b-collapse class="card" animation="slide" :open="!!sentences.length">
<div
slot="trigger"
slot-scope="props"
class="card-header"
role="button"
>
<h2 class="card-header-title">Sentences</h2>
<a role="button" class="card-header-icon">
<fontawesome :icon="props.open ? 'caret-down' : 'caret-up'" />
</a>
</div>
<div class="card-content">
<div v-for="(s, i) in sentences" :key="i" class="long-item">
<span
class="clickable"
@click="(evt) => openContext(evt, s.entry[0], 'sentence')"
@contextmenu.prevent="
(evt) => openContext(evt, s.entry[0], 'sentence')
"
>
{{ s.entry[0] }}
</span>
<span>{{ s.translation[0] }}</span>
</div>
</div>
</b-collapse>
</div>
</div>
</div>
<ContextMenu
ref="context"
:entry="selected.entry"
:audio="selected.audio"
:type="selected.type"
:additional="additionalContext"
/>
</section>
</template>
<script lang="ts">
import { Component, Prop, Ref, Vue, Watch } from 'nuxt-property-decorator'
import ContextMenu from '@/components/ContextMenu.vue'
import { api, IDictionaryLookup, IEntryLookup, IType } from '~/assets/api'
import { capitalize } from '~/assets/util'
type ILookupEntry = Omit<IDictionaryLookup, 'id'> & IEntryLookup
@Component<LookupTab>({
components: {
ContextMenu,
},
async created() {
this.$emit('title', this.title)
this.q = this.query.q || ''
if (this.additionalContext[0]) {
await this.additionalContext[0].handler()
}
},
})
export default class LookupTab extends Vue {
@Prop({ default: () => ({}) }) query!: {
q?: string
entry?: string
}
@Prop() type!: 'character' | 'vocabulary'
@Ref() context!: ContextMenu
title = this.type === 'character' ? 'Kanji' : capitalize(this.type)
entries: ILookupEntry[] = []
i = 0
lookup: Record<IType, IDictionaryLookup[]> = {
character: [],
vocabulary: [],
sentence: [],
}
selected: {
entry: string
type: string
} = {
entry: '',
type: '',
}
q = ''
q0 = ''
get current() {
return this.entries[this.i]?.entry || ''
}
get additionalContext() {
if (!(this.q || '').trim()) {
return [
{
name: 'Reload',
handler: async () => {
const { result } = await api.all_getRandom({ type: this.type })
this.q0 = result
},
},
]
}
return []
}
openContext(
evt: MouseEvent,
entry = this.selected.entry,
type = this.selected.type
) {
this.selected = { entry, type }
this.context.open(evt)
}
@Watch('q')
async onQChange(q: string) {
this.$emit('title', (q ? q + ' - ' : '') + this.title)
this.entries = []
if (this.type === 'character') {
if (/\p{sc=Han}/u.test(q)) {
let qs = q.split('').filter((h) => /\p{sc=Han}/u.test(h))
qs = qs.filter((h, i) => qs.indexOf(h) === i)
this.entries = qs.map(
(entry) =>
({
entry,
list: [],
type: this.type,
reading: [],
translation: [],
audio: [],
description: '',
tag: [],
sub: [],
sup: [],
variant: [],
visual: [],
amalgamation: [],
component: [],
} as ILookupEntry)
)
} else {
if (!q.trim()) {
const { result } = await api.all_getRandom({ type: this.type })
this.q0 = result
this.q = result
return
} else {
const r = await api.all_getQuery({
q,
type: this.type,
distinct: 'id',
})
this.entries = r.result.map(
({ entry }) =>
({
entry,
list: [],
type: this.type,
reading: [],
translation: [],
audio: [],
description: '',
tag: [],
sub: [],
sup: [],
variant: [],
visual: [],
amalgamation: [],
component: [],
} as ILookupEntry)
)
}
}
}
this.i = 0
}
@Watch('current')
load() {
if (this.current) {
this.loadComponents()
this.loadVocabularies()
this.loadSentences()
} else {
this.lookup = {
character: [],
vocabulary: [],
sentence: [],
}
}
}
async loadComponents() {
if (!this.current.trim()) return
const [comp, dict] = await Promise.all([
api.all_getComponent({
entry: this.current,
type: this.type,
}),
api
.all_getLookup({
entry: this.current,
type: this.type,
limit: 1,
})
.then(({ result: [r] }) => r),
])
this.$set(this.entries, this.i, {
...(dict || {
entry: this.current,
list: [],
type: this.type,
reading: [],
translation: [],
audio: [],
description: '',
tag: [],
}),
...(comp || {
sub: [],
sup: [],
variant: [],
visual: [],
amalgamation: [],
component: [],
}),
} as ILookupEntry)
}
async loadVocabularies() {
if (this.type === 'character') {
if (!/^\p{sc=Han}$/u.test(this.current)) {
this.lookup.vocabulary = []
return
}
this.lookup.vocabulary = await api
.character_getVocabulary({
entry: this.current,
distinct: 'id',
})
.then((r) => r.result)
} else {
this.lookup.vocabulary = await api
.vocabulary_getVocabulary({
entry: this.current,
distinct: 'id',
})
.then((r) => r.result)
}
}
async loadSentences() {
if (this.type === 'character') {
if (!/^\p{sc=Han}$/u.test(this.current)) {
this.lookup.sentence = []
return
}
this.lookup.sentence = await api
.character_getSentence({
entry: this.current,
distinct: 'id',
})
.then((r) => r.result)
} else {
this.lookup.sentence = await api
.vocabulary_getSentence({
entry: this.current,
distinct: 'id',
})
.then((r) => r.result)
}
}
}
</script>
<style scoped>
.entry-display {
display: flex;
flex-direction: column;
align-items: center;
}
.entry-display .clickable {
min-height: 1.5em;
display: block;
}
.card {
margin-bottom: 1rem;
}
.card-content {
max-height: 250px;
overflow: scroll;
}
.card-content .font-han {
font-size: 50px;
display: inline-block;
}
.long-item > span + span {
margin-left: 1rem;
}
.long-item > .pinyin {
min-width: 8rem;
}
</style>