修改数据结构

This commit is contained in:
zyronon
2023-09-09 16:23:30 +08:00
parent 03deaac774
commit f920d191ec
9 changed files with 352 additions and 310 deletions

View File

@@ -70,6 +70,7 @@ let input = $ref('')
let wrong = $ref('')
let isSpace = $ref(false)
let isDictation = $ref(true)
let isTranslate = $ref(false)
let showFullWord = $ref(false)
let hoverIndex = $ref({
sectionIndex: 0,
@@ -93,33 +94,34 @@ let article = reactive<Article>({
onMounted(() => {
let sections = useSplitArticle(article.article)
practiceStore.total = 0
sections.map(v => {
v.map(w => {
w.words.map(s => {
if (!store.skipWordNamesWithSimpleWords.includes(s.toLowerCase())) {
practiceStore.total++
}
})
})
})
practiceStore.startDate = Date.now()
let temp = useSplitArticle(article.articleTranslate, 'cn', CnKeyboardMap)
temp.map((v, i) => {
v.map((w, j) => {
article.translate.push({
sentence: w.sentence,
location: i + '-' + j
})
})
})
article.sections = sections
console.log(cloneDeep(article))
calcTranslateLocation()
// practiceStore.total = 0
// sections.map(v => {
// v.map(w => {
// w.words.map(s => {
// if (!store.skipWordNamesWithSimpleWords.includes(s.toLowerCase())) {
// practiceStore.total++
// }
// })
// })
// })
// practiceStore.startDate = Date.now()
//
// let temp = useSplitArticle(article.articleTranslate, 'cn', CnKeyboardMap)
// temp.map((v, i) => {
// v.map((w, j) => {
// article.translate.push({
// sentence: w.sentence,
// location: i + '-' + j
// })
// })
// })
// article.sections = sections
console.log(cloneDeep(sections))
// calcTranslateLocation()
})
function calcTranslateLocation() {
if (!isTranslate) return
nextTick(() => {
setTimeout(() => {
let articleRect = articleWrapperRef.getBoundingClientRect()

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import {computed, onMounted, onUnmounted, provide, watch} from "vue"
import {computed, onMounted, onUnmounted, provide, watch, watchEffect} from "vue"
import 快速打字的机械键盘声音Mp3 from '../../assets/sound/key-sounds/快速打字的机械键盘声音.mp3'
import 键盘快速打字的声音Mp3 from '../../assets/sound/key-sounds/键盘快速打字的声音.mp3'
import 电话打字的声音Mp3 from '../../assets/sound/key-sounds/电话打字的声音.mp3'
@@ -44,26 +44,6 @@ let data = $ref({
originWrongWords: [],
})
watch(() => props.words, (n: Word[]) => {
data.words = n
data.index = n.length ? 0 : -1
practiceStore.inputNumber = 0
practiceStore.wrongNumber = 0
practiceStore.repeatNumber = 0
practiceStore.total = n.length
practiceStore.wrongWords = []
practiceStore.startDate = Date.now()
})
let word = $computed(() => {
return data.words[data.index] ?? {
trans: [],
name: '',
usphone: '',
ukphone: '',
}
})
let input = $ref('')
let wrong = $ref('')
@@ -80,10 +60,32 @@ const [playAudio] = usePlayWordAudio()
const practiceStore = usePracticeStore()
watchEffect(() => {
data.words = props.words
data.index = props.words.length ? 0 : -1
practiceStore.inputNumber = 0
practiceStore.wrongNumber = 0
practiceStore.repeatNumber = 0
practiceStore.total = props.words.length
practiceStore.wrongWords = []
practiceStore.startDate = Date.now()
})
let word = $computed(() => {
return data.words[data.index] ?? {
trans: [],
name: '',
usphone: '',
ukphone: '',
}
})
let resetWord = $computed(() => {
return word.name.slice(input.length + wrong.length)
})
onMounted(() => {
emitter.on(EventKey.resetWord, () => {
input = ''

View File

@@ -145,6 +145,7 @@ header {
box-sizing: border-box;
transition: all .3s;
gap: 10rem;
opacity: 0;
.info {
font-size: 16rem;

View File

@@ -1,117 +1,159 @@
import {Sentence} from "@/types.ts";
import {DefaultArticleWord, Sentence, Word} from "@/types.ts";
import {cloneDeep} from "lodash";
interface KeyboardMap {
Period: string,
Comma: string,
Slash: string,
Exclamation: string,
Quote: string,
Period: string,
Comma: string,
Slash: string,
Exclamation: string,
Quote: string,
}
export const CnKeyboardMap: KeyboardMap = {
Period: '。',
Comma: '',
Slash: '',
Exclamation: '',
Quote: '”',
Period: '。',
Comma: '',
Slash: '',
Exclamation: '',
Quote: '”',
}
export const EnKeyboardMap: KeyboardMap = {
Period: '.',
Comma: ',',
Slash: '?',
Exclamation: '!',
Quote: '"',
Period: '.',
Comma: ',',
Slash: '?',
Exclamation: '!',
Quote: '"',
}
export function useSplitArticle(article: string, lang: string = 'en', keyboardMap: KeyboardMap = EnKeyboardMap): Sentence[][] {
let sections = []
let section = []
let sentence = {
sentence: '',
words: []
}
let sentences = []
let word = '';
// console.log(article,)
//加\n用于添加最后一段
article += '\n'
if (lang === 'en') {
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
}
// console.log('article', article)
article.split('').map(v => {
switch (v) {
case ' ':
sentence.words.push(word)
word = ''
break
case keyboardMap.Period:
case keyboardMap.Comma:
case keyboardMap.Slash:
case keyboardMap.Exclamation:
word += v
sentence.words.push(word)
sentence.words = sentence.words.filter(v => v)
sentence.sentence = sentence.words.join(' ')
sentences.push({
target: sentence.sentence,
trans: '',
location: `${sections.length}-${section.length}`
})
// sentence.words.push(word)
// sentence.words = sentence.words.filter(v => v)
// sentence.sentence = sentence.words.join(' ')
// sentence.sentence += v
// sentence.words.push(v)
section.push(sentence)
sentence = {
sentence: '',
words: []
}
word = ''
break
case keyboardMap.Quote:
let lastSentence = {
sentence: '',
words: []
}
if (section.length) {
lastSentence = section[section.length - 1]
} else {
let lastSection = sections[sections.length - 1]
lastSentence = lastSection[lastSection.length - 1]
}
if (lastSentence.sentence.includes(keyboardMap.Quote)) {
lastSentence.sentence += keyboardMap.Quote
lastSentence.words[lastSentence.words.length - 1] += keyboardMap.Quote
} else {
word += v
}
// console.log('lastSentence', lastSentence)
break
case '\n':
if (section.length) {
sections.push(section)
section = []
sentence = {
sentence: '',
words: []
}
word = ''
}
break
default:
word += v
break
let sections: Sentence[][] = []
let section: Sentence[] = []
let sentence: Sentence = {
sentence: '',
words: []
}
})
if (!sections.length && section.length) {
section.push(sentence)
sections.push(section)
}
let word = cloneDeep({...DefaultArticleWord, name: '', nextSpace: true});
//加\n用于添加最后一段
article += '\n'
if (lang === 'en') {
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
article = article.replaceAll(``, '"')
}
return sections
// console.log('article', article)
article.split('').map((v, i, arr) => {
switch (v) {
case ' ':
if (word.name) {
sentence.words.push(word)
word = cloneDeep(DefaultArticleWord)
}
break
case keyboardMap.Period:
case keyboardMap.Comma:
case keyboardMap.Slash:
case keyboardMap.Exclamation:
word.nextSpace = false
sentence.words.push(word)
sentence.words.push(cloneDeep({...DefaultArticleWord, name: v, nextSpace: true}))
// sentence.words = sentence.words.filter(v => v)
sentence.sentence = sentence.words.map(v => v.name).join(' ')
section.push({
sentence: '',
words: []
})
sentence = section[section.length - 1]
word = cloneDeep(DefaultArticleWord)
break
case keyboardMap.Quote:
let lastSentence = {
sentence: '',
words: []
}
let startSymbol = null
let indexs = {
a: -1,
b: -1,
c: -1
}
//TODO 可以优化成for+break
sections.toReversed().map((sectionItem, a) => {
sectionItem.toReversed().map((sentenceItem, b) => {
sentenceItem.words.toReversed().map((wordItem, c) => {
if (wordItem.symbolPosition !== '' && !startSymbol) {
startSymbol = wordItem.symbolPosition === 'end'
indexs = {a, b, c}
}
})
})
})
debugger
if (startSymbol || startSymbol === null) {
sentence.words.push(cloneDeep({
...DefaultArticleWord,
name: v,
nextSpace: false,
isSymbol: true,
symbolPosition: 'start'
}))
word = cloneDeep(DefaultArticleWord)
} else {
let addCurrent = false
sentence.words.toReversed().map((wordItem, c) => {
if (wordItem.symbolPosition === 'start' && !addCurrent) {
addCurrent = true
}
})
if (addCurrent) {
sentence.words.push(cloneDeep({
...DefaultArticleWord,
name: v,
nextSpace: true,
isSymbol: true,
symbolPosition: 'end'
}))
word = cloneDeep(DefaultArticleWord)
} else {
section[section.length - 2].words.push(cloneDeep({
...DefaultArticleWord,
name: v,
nextSpace: true,
isSymbol: true,
symbolPosition: 'end'
}))
section[section.length - 2].sentence = section[section.length - 2].words.map(v => v.name).join(' ')
}
}
sentence.sentence = sentence.words.map(v => v.name).join(' ')
break
case '\n':
section.pop()
if (i !== arr.length - 1) {
sections.push([])
section = sections[sections.length - 1]
section.push({
sentence: '',
words: []
})
sentence = section[section.length - 1]
word = cloneDeep(DefaultArticleWord)
}
break
default:
word.name += v
break
}
})
// if (!sections.length && section.length) {
// sections.push(section)
// }
return sections
}

View File

@@ -78,7 +78,7 @@ export const useBaseStore = defineStore('base', {
sideIsOpen: false,
isDictation: true,
setting: {
showToolbar: true,
showToolbar: false,
show: false,
value1: false,
value2: 50,

View File

@@ -1,15 +1,15 @@
export type Word = {
"name": string,
"usphone": string,
"ukphone": string,
"trans": string[]
"name": string,
"usphone": string,
"ukphone": string,
"trans": string[]
}
export const DefaultWord: Word = {
name: '',
usphone: '',
ukphone: '',
trans: []
name: '',
usphone: '',
ukphone: '',
trans: []
}
export const SaveKey = 'type-word-config'
@@ -23,163 +23,178 @@ export type LanguageCategoryType = 'en' | 'ja' | 'de' | 'code'
export type DictionaryResource = {
id: string
name: string
description: string
category: string
tags: string[]
url: string
length: number
language: LanguageType
languageCategory: LanguageCategoryType
//override default pronunciation when not undefined
defaultPronIndex?: number
id: string
name: string
description: string
category: string
tags: string[]
url: string
length: number
language: LanguageType
languageCategory: LanguageCategoryType
//override default pronunciation when not undefined
defaultPronIndex?: number
}
export type Dictionary = {
id: string
name: string
description: string
category: string
tags: string[]
url: string
length: number
language: LanguageType
languageCategory: LanguageCategoryType
// calculated in the store
chapterCount: number
//override default pronunciation when not undefined
defaultPronIndex?: number
id: string
name: string
description: string
category: string
tags: string[]
url: string
length: number
language: LanguageType
languageCategory: LanguageCategoryType
// calculated in the store
chapterCount: number
//override default pronunciation when not undefined
defaultPronIndex?: number
}
export type PronunciationConfig = {
name: string
pron: PronunciationType
name: string
pron: PronunciationType
}
export type LanguagePronunciationMapConfig = {
defaultPronIndex: number
pronunciation: PronunciationConfig[]
defaultPronIndex: number
pronunciation: PronunciationConfig[]
}
export type LanguagePronunciationMap = {
[key in LanguageType]: LanguagePronunciationMapConfig
[key in LanguageType]: LanguagePronunciationMapConfig
}
export type SoundResource = {
key: string
name: string
filename: string
key: string
name: string
filename: string
}
export interface DictJson {
description: string,
category: string,
tags: string[],
url: string,
length: number,
language: string,
languageCategory: string,
description: string,
category: string,
tags: string[],
url: string,
length: number,
language: string,
languageCategory: string,
}
export enum DictType {
newDict = 'newDict',
skipDict = 'skipDict',
wrongDict = 'wrongDict',
innerDict = 'innerDict',
customDict = 'customDict',
newDict = 'newDict',
skipDict = 'skipDict',
wrongDict = 'wrongDict',
innerDict = 'innerDict',
customDict = 'customDict',
}
export const DefaultArticleWord: ArticleWord = {
name: '',
usphone: '',
ukphone: '',
trans: [],
nextSpace: true,
isSymbol: false,
symbolPosition: ''
}
export interface ArticleWord extends Word {
nextSpace: boolean,
isSymbol: boolean,
symbolPosition: 'start' | 'end' | '',
}
export interface Sentence {
sentence: string,
words: string[]
sentence: string,
words: ArticleWord[]
}
export interface Article {
article: string,
articleTranslate: string,
newWords: Word[],
articleAllWords: string[],
sections: Sentence[][],
translate: {
sentence: string,
location: string
}[],
article: string,
articleTranslate: string,
newWords: Word[],
articleAllWords: string[],
sections: Sentence[][],
translate: {
sentence: string,
location: string
}[],
}
export interface Dict {
name: string,
sort: Sort,
type: DictType,
originWords: Word[],//原始单词
words: Word[],
chapterWordNumber: number,//章节单词数量
chapterWords: Word[][],
// articles: Article[],
chapterIndex: number,
chapterWordIndex: number,
statistics: Statistics[],
url: string,
name: string,
sort: Sort,
type: DictType,
originWords: Word[],//原始单词
words: Word[],
chapterWordNumber: number,//章节单词数量
chapterWords: Word[][],
// articles: Article[],
chapterIndex: number,
chapterWordIndex: number,
statistics: Statistics[],
url: string,
}
export interface Statistics {
startDate: number,//开始日期
endDate: number//结束日期
spend: number,//花费时间
wordNumber: number//单词数量
correctRate: number//正确率
wrongWordNumber: number//错误数
startDate: number,//开始日期
endDate: number//结束日期
spend: number,//花费时间
wordNumber: number//单词数量
correctRate: number//正确率
wrongWordNumber: number//错误数
}
export const DefaultStatistics: Statistics = {
startDate: Date.now(),
endDate: -1,
spend: -1,
wordNumber: -1,
correctRate: -1,
wrongWordNumber: -1,
startDate: Date.now(),
endDate: -1,
spend: -1,
wordNumber: -1,
correctRate: -1,
wrongWordNumber: -1,
}
export enum Sort {
normal = 0,
random = 1,
reverse = 2
normal = 0,
random = 1,
reverse = 2
}
export interface State {
newWordDict: Dict,
skipWordDict: Dict,
wrongWordDict: Dict,
dict: Dict,
oldDicts: Dict[],
current: {
dictType: DictType,
words: Word[],
index: number,
wrongWords: Word[],
originWrongWords: Word[],
repeatNumber: number,
statistics: Statistics
},
simpleWords: string[],
sideIsOpen: boolean,
isDictation: boolean,
theme: string,
setting: {
showToolbar: boolean,
show: boolean,
value1: boolean,
value2: number,
value3: number,
value4: boolean,
},
load:boolean
newWordDict: Dict,
skipWordDict: Dict,
wrongWordDict: Dict,
dict: Dict,
oldDicts: Dict[],
current: {
dictType: DictType,
words: Word[],
index: number,
wrongWords: Word[],
originWrongWords: Word[],
repeatNumber: number,
statistics: Statistics
},
simpleWords: string[],
sideIsOpen: boolean,
isDictation: boolean,
theme: string,
setting: {
showToolbar: boolean,
show: boolean,
value1: boolean,
value2: number,
value3: number,
value4: boolean,
},
load: boolean
}
export const ShortKeyMap = {
Show: 'Escape',
Ignore: 'Tab',
Remove: '`',
Collect: 'Enter',
Show: 'Escape',
Ignore: 'Tab',
Remove: '`',
Collect: 'Enter',
}