This commit is contained in:
polv committed 2021-10-25 11:05:38 +07:00
1 parent 7d5114953b
commit cbc36d19f5
7 files changed
+215 -194

No files matched your search

+137 -127
View File
@@ -267,6 +267,7 @@
<ContextMenu
ref="context"
:entry="selected.entry"
:audio="selected.audio"
:type="selected.type"
:additional="additionalContext"
/>
@@ -276,9 +277,11 @@
<script lang="ts">
import { Component, Prop, Ref, Vue, Watch } from 'nuxt-property-decorator'
import ContextMenu from '@/components/ContextMenu.vue'
import { api, IEntry, IType } from '~/assets/api'
import { api, IDictionaryLookup, IEntryLookup, IType } from '~/assets/api'
import { capitalize } from '~/assets/util'
type ILookupEntry = Omit<IDictionaryLookup, 'id'> & IEntryLookup
@Component<LookupTab>({
components: {
ContextMenu,
@@ -305,36 +308,15 @@ export default class LookupTab extends Vue {
title = this.type === 'character' ? 'Kanji' : capitalize(this.type)
entries: string[] = []
entries: ILookupEntry[] = []
i = 0
sub: string[] = []
sup: string[] = []
variant: string[] = []
visual: {
entry: string
}[] = []
amalgamation: {
entry: string
type?: IType
}[] = []
component: {
entry: string
type?: IType
}[] = []
lookup: Record<IType, IEntry[]> = {
lookup: Record<IType, IDictionaryLookup[]> = {
character: [],
vocabulary: [],
sentence: [],
}
reading: string[] = []
translation: string[] = []
tag: string[] = []
level = 0
selected: {
entry: string
type: string
@@ -347,7 +329,7 @@ export default class LookupTab extends Vue {
q0 = ''
get current() {
return this.entries[this.i] || ' '
return this.entries[this.i]?.entry || ''
}
get additionalContext() {
@@ -356,7 +338,7 @@ export default class LookupTab extends Vue {
{
name: 'Reload',
handler: async () => {
const { result } = await api.all_getRandom({ type: 'character' })
const { result } = await api.all_getRandom({ type: this.type })
this.q0 = result
},
},
@@ -378,21 +360,66 @@ export default class LookupTab extends Vue {
@Watch('q')
async onQChange(q: string) {
this.$emit('title', (q ? q + ' - ' : '') + this.title)
this.entries = []
if (/\p{sc=Han}/u.test(q)) {
const qs = q.split('').filter((h) => /\p{sc=Han}/u.test(h))
this.entries = qs.filter((h, i) => qs.indexOf(h) === i)
} else {
if (!q.trim()) {
const { result } = await api.all_getRandom({ type: 'character' })
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.q0 = result
this.q = result
return
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)
)
}
}
const r = await api.all_getQuery({ q })
this.entries = r.result.map((r) => r.entry)
}
this.i = 0
@@ -401,118 +428,101 @@ export default class LookupTab extends Vue {
@Watch('current')
load() {
if (this.current) {
this.loadCharacter()
this.loadVocab()
this.loadComponents()
this.loadVocabularies()
this.loadSentences()
} else {
this.sub = []
this.sup = []
this.variant = []
this.visual = []
this.amalgamation = []
this.component = []
this.lookup = {
character: [],
vocabulary: [],
sentence: [],
}
this.reading = []
this.translation = []
this.tag = []
this.level = 0
}
}
async loadCharacter() {
if (!/^\p{sc=Han}$/u.test(this.current)) {
this.sub = []
this.sup = []
this.variant = []
this.visual = []
async loadComponents() {
if (!this.current.trim()) return
this.translation = []
this.tag = []
this.level = 0
return
}
await Promise.all([
(async () => {
const r = await api.all_getComponent({
entry: this.current,
type: this.type,
})
this.sub = r?.sub || []
this.sup = r?.sup || []
this.variant = r?.variant || []
this.visual = r?.visual || []
this.amalgamation = r?.amalgamation || []
this.component = r?.component || []
})(),
(async () => {
const {
result: [r],
} = await api.all_getLookup({
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,
})
this.reading = r?.reading || []
this.translation = r?.translation || []
this.tag = r?.tag || []
this.level = r?.level || 0
})(),
.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 loadVocab() {
if (this.type === 'character' && !/^\p{sc=Han}$/u.test(this.current)) {
this.lookup.vocabulary = []
return
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)
}
const { result } = await api.character_getVocabulary({
entry: this.current,
distinct: 'id',
})
this.lookup.vocabulary = await Promise.all(
result
.map((r) =>
api
.all_getLookup({ entry: r.list[0]!, type: 'vocabulary', limit: 1 })
.then((r) => r.result[0]!)
)
.filter((r) => r)
)
}
async loadSentences() {
if (this.type === 'character' && !/^\p{sc=Han}$/u.test(this.current)) {
this.lookup.sentence = []
return
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)
}
const { result } = await api.character_getSentence({
entry: this.current,
distinct: 'id',
})
this.lookup.vocabulary = await Promise.all(
result
.map((r) =>
api
.all_getLookup({ entry: r.list[0]!, type: 'sentence', limit: 1 })
.then((r) => r.result[0]!)
)
.filter((r) => r)
)
}
}
</script>