diff --git a/src/components/list/DictItem.vue b/src/components/list/DictItem.vue
index 3a09a0ae..ec2f0226 100644
--- a/src/components/list/DictItem.vue
+++ b/src/components/list/DictItem.vue
@@ -23,7 +23,7 @@ const emit = defineEmits<{
{{ dict.name }}
{{ dict.description }}
- {{ dict.length }}词
+ {{ dict.length ?? dict.originWords.length }}词
diff --git a/src/hooks/dict.ts b/src/hooks/dict.ts
index 1a0a0122..e94c0b58 100644
--- a/src/hooks/dict.ts
+++ b/src/hooks/dict.ts
@@ -1,64 +1,117 @@
-import {Word} from "@/types.ts";
+import {Dict, DictType, Word} from "@/types.ts";
import {useBaseStore} from "@/stores/base.ts";
+import {useRuntimeStore} from "@/stores/runtime.ts";
+import {chunk, cloneDeep} from "lodash-es";
+import {v4 as uuidv4} from "uuid";
export function useWordOptions() {
- const store = useBaseStore()
+ const store = useBaseStore()
- function isWordCollect(val: Word) {
- return !!store.collect.originWords.find(v => v.name.toLowerCase() === val.name.toLowerCase())
- }
-
- function toggleWordCollect(val: Word) {
- let rIndex = store.collect.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.collect.originWords.splice(rIndex, 1)
- } else {
- let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.simple.originWords.splice(rIndex, 1)
- }
- store.collect.originWords.push(val)
+ function isWordCollect(val: Word) {
+ return !!store.collect.originWords.find(v => v.name.toLowerCase() === val.name.toLowerCase())
}
- }
- function isWordSimple(val: Word) {
- return !!store.simple.originWords.find(v => v.name.toLowerCase() === val.name.toLowerCase())
- }
-
- function toggleWordSimple(val: Word) {
- let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.simple.originWords.splice(rIndex, 1)
- } else {
- let rIndex = store.collect.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.collect.originWords.splice(rIndex, 1)
- }
- store.simple.originWords.push(val)
+ function toggleWordCollect(val: Word) {
+ let rIndex = store.collect.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.collect.originWords.splice(rIndex, 1)
+ } else {
+ let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.simple.originWords.splice(rIndex, 1)
+ }
+ store.collect.originWords.push(val)
+ }
}
- }
- function delWrongWord(val: Word) {
- let rIndex = store.wrong.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.wrong.originWords.splice(rIndex, 1)
+ function isWordSimple(val: Word) {
+ return !!store.simple.originWords.find(v => v.name.toLowerCase() === val.name.toLowerCase())
}
- }
- function delSimpleWord(val: Word) {
- let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
- if (rIndex > -1) {
- store.simple.originWords.splice(rIndex, 1)
+ function toggleWordSimple(val: Word) {
+ let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.simple.originWords.splice(rIndex, 1)
+ } else {
+ let rIndex = store.collect.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.collect.originWords.splice(rIndex, 1)
+ }
+ store.simple.originWords.push(val)
+ }
}
- }
- return {
- isWordCollect,
- toggleWordCollect,
- isWordSimple,
- toggleWordSimple,
- delWrongWord,
- delSimpleWord
- }
+ function delWrongWord(val: Word) {
+ let rIndex = store.wrong.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.wrong.originWords.splice(rIndex, 1)
+ }
+ }
+
+ function delSimpleWord(val: Word) {
+ let rIndex = store.simple.originWords.findIndex(v => v.name.toLowerCase() === val.name.toLowerCase())
+ if (rIndex > -1) {
+ store.simple.originWords.splice(rIndex, 1)
+ }
+ }
+
+ return {
+ isWordCollect,
+ toggleWordCollect,
+ isWordSimple,
+ toggleWordSimple,
+ delWrongWord,
+ delSimpleWord
+ }
+}
+
+export async function checkDictHasTranslate(dict: Dict) {
+ let dictResourceUrl = `./dicts/${dict.language}/${dict.type}/${dict.translateLanguage}/${dict.url}`;
+ if ([
+ DictType.word,
+ DictType.customWord,
+ ].includes(dict.type)) {
+ if (!dict.originWords.length) {
+ let r = await fetch(dictResourceUrl)
+ // let r = await fetch(`.${dict.url}`)
+ let v = await r.json()
+ if (dict.translateLanguage === 'common') {
+ const runtimeStore = useRuntimeStore()
+ let r2 = await fetch('./translate/en2zh_CN-min.json')
+ // fetch('http://sc.ttentau.top/en2zh_CN-min.json').then(r2 => {
+ let list: Word[] = await r2.json()
+
+ runtimeStore.translateWordList = list
+
+ dict.originWords = cloneDeep(v)
+ dict.words = cloneDeep(v)
+ dict.chapterWords = chunk(dict.words, dict.chapterWordNumber)
+ dict.chapterWords[dict.chapterIndex].map((w: Word) => {
+ let res = list.find(a => a.name === w.name)
+ if (res) w = Object.assign(w, res)
+ })
+ } else {
+ dict.originWords = cloneDeep(v)
+ dict.words = cloneDeep(v)
+ dict.chapterWords = chunk(dict.words, dict.chapterWordNumber)
+ }
+ }
+ }
+
+ if ([
+ DictType.article,
+ DictType.customArticle,
+ ].includes(dict.type)) {
+ if (!dict.articles.length) {
+ let r = await fetch(dictResourceUrl)
+ let s: any[] = await r.json()
+ dict.articles = cloneDeep(s.map(v => {
+ v.id = uuidv4()
+ return v
+ }))
+ }
+ }
+
+
}
\ No newline at end of file
diff --git a/src/stores/base.ts b/src/stores/base.ts
index 5a90d541..271961f2 100644
--- a/src/stores/base.ts
+++ b/src/stores/base.ts
@@ -1,17 +1,16 @@
import {defineStore} from 'pinia'
-import {DefaultDict, DefaultWord, Dict, DictType, DisplayStatistics, SaveDict, Word} from "../types.ts"
-import {chunk, cloneDeep} from "lodash-es";
+import {DefaultDict, Dict, DictType, DisplayStatistics, SaveDict, Word} from "../types.ts"
+import {chunk, cloneDeep, merge} from "lodash-es";
import {emitter, EventKey} from "@/utils/eventBus.ts"
import {v4 as uuidv4} from 'uuid';
import {useRuntimeStore} from "@/stores/runtime.ts";
import * as localforage from "localforage";
-import {sizeofByte} from "@/utils";
+import {checkDictHasTranslate} from "@/hooks/dict.ts";
export interface BaseState {
myDictList: Dict[],
current: {
index: number,
- editIndex: number,
practiceType: DictType,//练习类型,目前仅词典为collect时判断是练单词还是文章使用
},
simpleWords: string[],
@@ -116,7 +115,6 @@ export const useBaseStore = defineStore('base', {
],
current: {
index: 3,
- editIndex: 0,
// dictType: DictType.article,
// index: 0,
practiceType: DictType.word,
@@ -134,7 +132,7 @@ export const useBaseStore = defineStore('base', {
},
getters: {
collect() {
- return this.myDictList[0]
+ return this.myDictList[0] ?? {}
},
simple(): Dict {
return this.myDictList[1]
@@ -158,21 +156,9 @@ export const useBaseStore = defineStore('base', {
DictType.customArticle
].includes(this.currentDict.type)
},
- editDict(state: BaseState) {
- if (state.current.editIndex === -1) {
- return cloneDeep(DefaultDict)
- }
- return state.myDictList.filter(v => [DictType.customWord, DictType.customArticle].includes(v.type))[state.current.editIndex - 3]
- },
currentDict(): Dict {
return this.myDictList[this.current.index]
},
- currentEditDict(): Dict {
- return this.myDictList[this.current.editIndex]
- },
- wordIndex(state: BaseState): number {
- return this.currentDict.wordIndex
- },
chapter(state: BaseState): Word[] {
return this.currentDict.chapterWords[this.currentDict.chapterIndex] ?? []
},
@@ -184,9 +170,10 @@ export const useBaseStore = defineStore('base', {
let title = ''
switch (this.currentDict.type) {
case DictType.collect:
- if (state.current.practiceType === DictType.word) {
+ if (state.current.practiceType === DictType.article || state.current.practiceType === DictType.customArticle) {
return `第${this.currentDict.chapterIndex + 1}章`
}
+ return ''
case DictType.word:
case DictType.customWord:
return `第${this.currentDict.chapterIndex + 1}章`
@@ -196,17 +183,16 @@ export const useBaseStore = defineStore('base', {
},
actions: {
setState(obj: any) {
- for (const [key, value] of Object.entries(obj)) {
- this[key] = value
- }
- // console.log('this/', this)
+ //这样不会丢失watch的值的引用
+ merge(this, obj)
},
async init() {
return new Promise(async resolve => {
try {
let configStr: string = await localforage.getItem(SaveDict.key)
- // console.log('s', configStr)
+ console.log(configStr)
console.log('s', new Blob([configStr]).size)
+ configStr = ''
if (configStr) {
let data = JSON.parse(configStr)
let state: BaseState = data.val
@@ -219,7 +205,7 @@ export const useBaseStore = defineStore('base', {
}
}
} catch (e) {
- console.error('读取本地dict数据失败',e)
+ console.error('读取本地dict数据失败', e)
}
if (this.current.index < 3) {
@@ -239,21 +225,13 @@ export const useBaseStore = defineStore('base', {
let r2 = await fetch('./translate/en2zh_CN-min.json')
// fetch('http://sc.ttentau.top/en2zh_CN-min.json').then(r2 => {
let list: Word[] = await r2.json()
-
- runtimeStore.translateWordList = list
-
- this.currentDict.originWords = cloneDeep(v)
- this.currentDict.words = cloneDeep(v)
- this.currentDict.chapterWords = chunk(this.currentDict.words, this.currentDict.chapterWordNumber)
- this.currentDict.chapterWords[this.currentDict.chapterIndex].map((w: Word) => {
- let res = list.find(a => a.name === w.name)
- if (res) w = Object.assign(w, res)
- })
- } else {
- this.currentDict.originWords = cloneDeep(v)
- this.currentDict.words = cloneDeep(v)
- this.currentDict.chapterWords = chunk(this.currentDict.words, this.currentDict.chapterWordNumber)
+ if (list && list.length) {
+ runtimeStore.translateWordList = list
+ }
}
+ this.currentDict.originWords = cloneDeep(v)
+ this.currentDict.words = cloneDeep(v)
+ this.currentDict.chapterWords = chunk(this.currentDict.words, this.currentDict.chapterWordNumber)
}
}
@@ -280,38 +258,45 @@ export const useBaseStore = defineStore('base', {
this.currentDict.statistics.push(statistics)
}
},
- async changeDict(dict: Dict, chapterIndex: number = dict.chapterIndex, chapterWordIndex: number = dict.chapterWordNumber, practiceType: DictType) {
+ async changeDict(dict: Dict, chapterIndex: number = dict.chapterIndex, wordIndex: number = dict.wordIndex, practiceType: DictType) {
//TODO 保存统计
// this.saveStatistics()
- console.log('changeDict', cloneDeep(dict), chapterIndex, chapterWordIndex)
+ console.log('changeDict', cloneDeep(dict), chapterIndex, wordIndex)
this.currentDict.type = dict.type
this.current.practiceType = practiceType
if ([DictType.collect,
DictType.simple,
DictType.wrong].includes(dict.type)) {
- this[dict.type].chapterIndex = 0
- this[dict.type].chapterWordIndex = chapterWordIndex
- this[dict.type].chapterWords = [this[dict.type].words]
+ dict.chapterIndex = 0
+ dict.wordIndex = wordIndex
+ dict.chapterWordNumber = dict.words.length
+ dict.chapterWords = [dict.words]
} else {
if (dict.type === DictType.article || dict.type === DictType.customArticle) {
if (chapterIndex > dict.articles.length) {
dict.chapterIndex = 0
+ dict.wordIndex = 0
}
} else {
if (chapterIndex > dict.chapterWords.length) {
dict.chapterIndex = 0
+ dict.wordIndex = 0
}
}
- let rIndex = this.myDictList.findIndex((v: Dict) => v.name === dict.name)
- if (rIndex > -1) {
- this.myDictList[rIndex] = dict
- this.current.index = rIndex
- } else {
- this.myDictList.push(cloneDeep(dict))
- this.current.index = this.myDictList.length - 1
- }
+ }
+
+ // await checkDictHasTranslate(dict)
+
+ let rIndex = this.myDictList.findIndex((v: Dict) => v.id === dict.id)
+ if (rIndex > -1) {
+ this.myDictList[rIndex] = dict
+ this.current.index = rIndex
+ } else {
+ this.myDictList.push(cloneDeep(dict))
+ this.current.index = this.myDictList.length - 1
}
emitter.emit(EventKey.resetWord)
+ emitter.emit(EventKey.changeDict)
}
},
})
\ No newline at end of file
diff --git a/src/utils/eventBus.ts b/src/utils/eventBus.ts
index 5f44b2a4..ba954ad8 100644
--- a/src/utils/eventBus.ts
+++ b/src/utils/eventBus.ts
@@ -3,8 +3,10 @@ import mitt from 'mitt'
export const emitter = mitt()
export const EventKey = {
resetWord: 'resetWord',
+ changeDict: 'changeDict',
openStatModal: 'openStatModal',
openWordListModal: 'openWordListModal',
+ openDictModal: 'openDictModal',
openArticleListModal: 'openArticleListModal',
closeOther: 'closeOther',
keydown: 'keydown',
diff --git a/src/utils/index.ts b/src/utils/index.ts
index 74ae72be..227a5154 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -1,42 +1,3 @@
export function getRandom(a: number, b: number): number {
return Math.random() * (b - a) + a;
}
-
-export function sizeofByte(str, charset = 'utf-16') {
- let total = 0
- let charCode
-
- charset = charset.toLowerCase()
-
- if (charset === 'utf-8' || charset === 'utf8') {
- for (let i = 0, len = str.length; i < len; i++) {
- charCode = str.codePointAt(i)
-
- if (charCode <= 0x007f) {
- total += 1
- } else if (charCode <= 0x07ff) {
- total += 2
- } else if (charCode <= 0xffff) {
- total += 3
- } else {
- total += 4
- i++
- }
- }
- } else if (charset === 'utf-16' || charset === 'utf16') {
- for (let i = 0, len = str.length; i < len; i++) {
- charCode = str.codePointAt(i)
-
- if (charCode <= 0xffff) {
- total += 2
- } else {
- total += 4
- i++
- }
- }
- } else {
- total = str.replace(/[^\x00-\xff]/g, 'aa').length
- }
-
- return total
-}
\ No newline at end of file