重构数据结构

This commit is contained in:
zyronon
2023-08-29 11:53:16 +08:00
parent ce392ac56c
commit 992c7fac5a
3 changed files with 124 additions and 193 deletions

View File

@@ -23,6 +23,7 @@ import {
Delete,
} from "@icon-park/vue-next"
import {emitter, EventKey} from "@/utils/eventBus.ts"
import {cloneDeep} from "lodash"
let input = $ref('')
let wrong = $ref('')
@@ -68,65 +69,56 @@ watch(store.$state, (n) => {
function repeatWrong() {
if (store.currentDictType !== DictType.currentWrongDict) {
store.lastDictType = store.currentDictType
}
store.currentDictType = DictType.currentWrongDict
store.currentWrongDict.chapterList = [store.currentWrongDict.wordList]
store.currentWrongDict.chapterIndex = 0
store.currentWrongDict.wordIndex = 0
store.currentWrongDict.wordList = []
store.currentWrongDict.statistics = {
startDate: Date.now(),//开始日期
store.current.words = cloneDeep(store.current.wrongWords)
store.current.index = 0
store.current.wrongWords = []
store.current.statistics = {
startDate: -1,
endDate: -1,
spend: -1,
wordNumber: -1,
correctRate: -1,
wrongNumber: -1
wrongWordNumber: -1,
}
}
function next() {
if (store.currentDict.wordIndex === store.chapter.length - 1) {
if (store.currentDict.chapterIndex !== store.currentDict.chapterList.length - 1) {
console.log('这一章节完了')
if (store.currentWrongDict.wordList.length) {
repeatWrong()
}
// emitter.emit(EventKey.openStatModal)
} else {
console.log('这本书完了')
if (store.currentDictType === DictType.currentWrongDict) {
if ( store.currentWrongDict.wordList.length){
repeatWrong()
}else {
emitter.emit(EventKey.openStatModal)
}
if (store.current.wrongWords.length) {
repeatWrong()
} else {
if (store.current.index === store.chapter.length - 1) {
if (store.currentDict.chapterIndex !== store.currentDict.chapters.length - 1) {
console.log('这一章节完了')
// emitter.emit(EventKey.openStatModal)
} else {
console.log('这本书完了')
emitter.emit(EventKey.openStatModal)
}
}
} else {
store.currentDict.wordIndex++
// var msg = new SpeechSynthesisUtterance();
// // msg.text = store.word.name
// msg.text = 'Hawaii wildfires burn historic town of Lahaina to the ground'
// msg.rate = 0.8;
// msg.pitch = 1;
// msg.lang = 'en-US';
// window.speechSynthesis.speak(msg);
console.log('这个词完了')
if (store.currentDict.wordIndex) {
store.lastStatistics.wrongNumber = store.currentDict.wordIndex - store.currentWrongDict.wordList.length
store.lastStatistics.correctRate = Math.trunc(((store.currentDict.wordIndex - store.currentWrongDict.wordList.length) / (store.currentDict.wordIndex)) * 100)
} else {
store.lastStatistics.correctRate = -1
store.lastStatistics.wrongNumber = -1
store.current.index++
// var msg = new SpeechSynthesisUtterance();
// // msg.text = store.word.name
// msg.text = 'Hawaii wildfires burn historic town of Lahaina to the ground'
// msg.rate = 0.8;
// msg.pitch = 1;
// msg.lang = 'en-US';
// window.speechSynthesis.speak(msg);
console.log('这个词完了')
if (store.current.index) {
store.current.statistics.wrongWordNumber = store.current.index - store.current.wrongWords.length
store.current.statistics.correctRate = Math.trunc(((store.current.index - store.current.wrongWords.length) / (store.current.index)) * 100)
} else {
store.current.statistics.correctRate = -1
store.current.statistics.wrongWordNumber = -1
}
}
if ([DictType.custom, DictType.inner].includes(store.current.dictType) && store.skipWordNames.includes(store.word.name)) {
next()
}
}
if ([DictType.custom, DictType.inner].includes(store.currentDictType) && store.skipWordNames.includes(store.word.name)) {
next()
}
wrong = input = ''
}

View File

@@ -1,5 +1,5 @@
import {defineStore} from 'pinia'
import {Config, Dict, DictType, SaveKey, Sort, State, Statistics, Word} from "../types.ts"
import {Dict, DictType, Sort, State, Word} from "../types.ts"
import {chunk, cloneDeep} from "lodash";
import {emitter, EventKey} from "@/utils/eventBus.ts"
@@ -7,87 +7,73 @@ export const useBaseStore = defineStore('base', {
state: (): State => {
return {
newWordDict: {
type: DictType.newWordDict,
sort: Sort.normal,
name: '生词本',
description: '生词本',
category: '',
tags: [],
url: '',
length: -1,
language: 'en',
languageCategory: 'en',
wordList: [],
chapterList: [],
sort: Sort.normal,
type: DictType.newWordDict,
originWords: [],
words: [],
chapterWordNumber: 15,
chapters: [],
chapterIndex: 0,
wordIndex: 0,
dictStatistics: [],
chapterWordNumber: 15
chapterWordIndex: 0,
statistics: [],
url: '',
},
skipWordDict: {
type: DictType.skipWordDict,
sort: Sort.normal,
name: '简单词',
description: '简单词',
category: '',
tags: [],
url: '',
length: -1,
language: 'en',
languageCategory: 'en',
wordList: [],
chapterList: [],
chapterIndex: 0,
wordIndex: 0,
dictStatistics: [],
chapterWordNumber: 15
},
allWrongDict: {
type: DictType.allWrongDict,
sort: Sort.normal,
name: '错词本',
description: '错词本',
category: '',
tags: [],
url: '',
length: -1,
language: 'en',
languageCategory: 'en',
wordList: [],
chapterList: [],
type: DictType.skipWordDict,
originWords: [],
words: [],
chapterWordNumber: 15,
chapters: [],
chapterIndex: 0,
wordIndex: 0,
dictStatistics: [],
chapterWordNumber: 15
chapterWordIndex: 0,
statistics: [],
url: '',
},
wrongWordDict: {
name: '错词本',
sort: Sort.normal,
type: DictType.wrongWordDict,
originWords: [],
words: [],
chapterWordNumber: 15,
chapters: [],
chapterIndex: 0,
chapterWordIndex: 0,
statistics: [],
url: '',
},
dict: {
type: DictType.inner,
sort: Sort.normal,
name: '新概念英语-2',
description: '新概念英语第二册',
category: '青少年英语',
tags: ['新概念英语'],
url: '/dicts/NCE_2.json',
length: 858,
language: 'en',
languageCategory: 'en',
wordList: [],
chapterList: [],
chapterIndex: 0,
sort: Sort.normal,
type: DictType.inner,
originWords: [],
words: [],
chapterWordNumber: 15,
wordIndex: 0,
dictStatistics: [
{
startDate: Date.now(),
endDate: -1,
chapterWordNumber: 15,
statistics: []
}
]
chapters: [],
chapterIndex: 0,
chapterWordIndex: 0,
statistics: [],
url: '/dicts/NCE_2.json',
},
oldDicts: [],
currentDictType: DictType.inner,
lastDictType: DictType.inner,
current: {
dictType: DictType.inner,
words: [],
index: -1,
wrongWords: [],
repeatNumber: -1,
statistics: {
startDate: -1,
endDate: -1,
spend: -1,
wordNumber: -1,
correctRate: -1,
wrongWordNumber: -1,
}
},
sideIsOpen: false,
isDictation: true,
setting: {
@@ -103,21 +89,19 @@ export const useBaseStore = defineStore('base', {
},
getters: {
isWrongMode(state: State) {
return state.currentDictType === DictType.currentWrongDict
return state.current.repeatNumber > 0
},
skipWordNames: (state: State) => {
return state.skipWordDict.wordList.map(v => v.name)
return state.skipWordDict.words.map(v => v.name)
},
currentDict(state: State): Dict {
switch (state.currentDictType) {
switch (state.current.dictType) {
case DictType.newWordDict:
return state.newWordDict
case DictType.skipWordDict:
return state.skipWordDict
case DictType.allWrongDict:
return state.allWrongDict
case DictType.currentWrongDict:
return state.currentWrongDict
case DictType.wrongWordDict:
return state.wrongWordDict
case DictType.inner:
case DictType.custom:
return state.dict
@@ -127,32 +111,16 @@ export const useBaseStore = defineStore('base', {
return this.currentDict.wordIndex
},
chapter(state: State): Word[] {
return this.currentDict.chapterList[this.currentDict.chapterIndex] ?? []
return this.currentDict.chapters[this.currentDict.chapterIndex] ?? []
},
word(state: State): Word {
return this.chapter[this.currentDict.wordIndex] ?? {
return state.current.words[state.current.index] ?? {
trans: [],
name: ''
name: '',
usphone: '',
ukphone: '',
}
},
lastStatistics(state: State): Statistics {
if (state.currentDictType === DictType.currentWrongDict) {
return state.currentWrongDict.statistics
}
if (this.currentDict.dictStatistics.length) {
let stat = this.currentDict.dictStatistics[this.currentDict.dictStatistics.length - 1]
if (stat.statistics.length) {
return stat.statistics[stat.statistics.length - 1]
}
}
return {} as any
},
chapterName(state: State) {
if ([DictType.custom, DictType.inner].includes(state.currentDictType)) {
return `${state.dict.chapterIndex + 1}`
}
return ''
}
},
actions: {
setState(obj: any) {
@@ -167,50 +135,33 @@ export const useBaseStore = defineStore('base', {
// let obj: Config = JSON.parse(configStr)
// this.setState(obj)
// }
if (this.currentDictType === DictType.inner) {
if (this.current.dictType === DictType.inner) {
let r = await fetch(`/public/${this.dict.url}`)
r.json().then(v => {
this.dict.wordList = v
this.dict.chapterList = chunk(this.dict.wordList, this.dict.chapterWordNumber)
})
}
if (this.currentDictType === DictType.custom) {
let r = await fetch(`/public/${this.dict.url}`)
r.json().then(v => {
this.dict.wordList = v
this.dict.chapterList = chunk(this.dict.wordList, this.dict.chapterWordNumber)
})
}
if (this.dict.dictStatistics.length) {
this.dict.dictStatistics[this.dict.dictStatistics.length - 1].statistics.push({
startDate: Date.now(),//开始日期
endDate: -1,
correctRate: -1,
wrongNumber: -1
this.dict.originWords = cloneDeep(v)
this.dict.words = cloneDeep(v)
this.dict.chapters = chunk(this.dict.originWords, this.dict.chapterWordNumber)
})
}
this.dict.wordIndex = 0
},
async changeDict(dict: Dict, chapterIndex: number = -1, wordIndex: number = -1) {
async changeDict(dict: Dict, chapterIndex: number = -1, chapterWordIndex: number = -1) {
console.log('changeDict', dict)
emitter.emit(EventKey.resetWord)
if ([DictType.newWordDict,
DictType.skipWordDict,
DictType.allWrongDict].includes(dict.type)) {
this.currentDictType = dict.type
this[dict.type].chapterList = [this[dict.type].wordList]
DictType.wrongWordDict].includes(dict.type)) {
this.current.dictType = dict.type
this[dict.type].chapters = [this[dict.type].wordList]
this[dict.type].chapterIndex = 0
this[dict.type].wordIndex = wordIndex === -1 ? 0 : wordIndex
} else if (dict.type === DictType.currentWrongDict) {
this[dict.type].wordIndex = wordIndex === -1 ? 0 : wordIndex
this[dict.type].chapterWordIndex = chapterWordIndex === -1 ? 0 : chapterWordIndex
} else {
this.dict = cloneDeep(dict)
this.currentDictType = dict.type
if (wordIndex !== -1) this.dict.wordIndex = wordIndex
this.current.dictType = dict.type
if (chapterWordIndex !== -1) this.dict.chapterWordIndex = chapterWordIndex
if (chapterIndex !== -1) this.dict.chapterIndex = chapterIndex
}
console.log(' this.dict', this.dict)
console.log('this.dict', this.dict)
}
},
})

View File

@@ -1,17 +1,4 @@
export type Config = {
newWords: Word[],
skipWords: Word[],
skipWordNames: string[],
dict: string,
currentDict: {
wordList: Word[],
chapterList: Word[][],
name: string,
desc: string
}
chapterIndex: number,
wordIndex: number,
}
export type Word = {
"name": string,
"usphone": string,
@@ -92,8 +79,7 @@ export interface DictJson {
export enum DictType {
newWordDict = 'newWordDict',
skipWordDict = 'skipWordDict',
allWrongDict = 'allWrongDict',
currentWrongDict = 'currentWrongDict',
wrongWordDict = 'wrongWordDict',
inner = 'inner',
custom = 'custom',
}
@@ -103,12 +89,13 @@ export interface Dict {
sort: Sort,
type: DictType,
originWords: Word[],//原始单词
chapterList: Word[][],
chapterIndex: number,
words: Word[],
wordIndex: number,
chapterWordNumber: number,//章节单词数量
statistics: Statistics[]
chapters: Word[][],
chapterIndex: number,
chapterWordIndex: number,
statistics: Statistics[],
url: string,
}
export interface Statistics {
@@ -135,6 +122,7 @@ export interface State {
current: {
dictType: DictType,
words: Word[],
index: number,
wrongWords: Word[],
repeatNumber: number,
statistics: Statistics