Add data import and export function
This commit is contained in:
@@ -4,4 +4,20 @@ export const SoundFileOptions = [
|
||||
{value: '机械键盘2', label: '机械键盘2'},
|
||||
{value: '老式机械键盘', label: '老式机械键盘'},
|
||||
{value: '笔记本键盘', label: '笔记本键盘'},
|
||||
]
|
||||
]
|
||||
|
||||
export const APP_NAME = 'Typing Word'
|
||||
|
||||
export const SAVE_DICT_KEY = {
|
||||
key: 'typing-word-dict',
|
||||
version: 3
|
||||
}
|
||||
export const SAVE_SETTING_KEY = {
|
||||
key: 'typing-word-setting',
|
||||
oldKey: 'typing-word-config',
|
||||
version: 8
|
||||
}
|
||||
export const EXPORT_DATA_KEY = {
|
||||
key: 'typing-word-export',
|
||||
version: 1
|
||||
}
|
||||
@@ -1,7 +1,124 @@
|
||||
import localforage from "localforage";
|
||||
import {SAVE_DICT_KEY, SAVE_SETTING_KEY} from "@/utils/const.ts";
|
||||
import {BaseState, DefaultBaseState} from "@/stores/base.ts";
|
||||
import {DefaultSettingState, SettingState} from "@/stores/setting.ts";
|
||||
import {cloneDeep} from "lodash-es";
|
||||
import {Dict, DictType} from "@/types.ts";
|
||||
|
||||
export function getRandom(a: number, b: number): number {
|
||||
return Math.random() * (b - a) + a;
|
||||
return Math.random() * (b - a) + a;
|
||||
}
|
||||
|
||||
export function no(){
|
||||
ElMessage.warning('未现实')
|
||||
export function no() {
|
||||
ElMessage.warning('未现实')
|
||||
}
|
||||
|
||||
export function checkAndUpgradeSaveDict(val: string) {
|
||||
// console.log(configStr)
|
||||
// console.log('s', new Blob([val]).size)
|
||||
// val = ''
|
||||
if (val) {
|
||||
try {
|
||||
let data
|
||||
if (typeof val === 'string') {
|
||||
data = JSON.parse(val)
|
||||
} else {
|
||||
data = val
|
||||
}
|
||||
let state: BaseState = data.val
|
||||
if (typeof state !== 'object') {
|
||||
return {}
|
||||
}
|
||||
if (!data.version) {
|
||||
return {}
|
||||
}
|
||||
let version = Number(data.version)
|
||||
// console.log('state', state)
|
||||
let defaultBaseState = DefaultBaseState()
|
||||
if (version === SAVE_DICT_KEY.version) {
|
||||
//防止人为删除数据,导致数据不完整报错
|
||||
for (const [key, value] of Object.entries(defaultBaseState)) {
|
||||
if (state[key] !== undefined) defaultBaseState[key] = state[key]
|
||||
}
|
||||
return defaultBaseState
|
||||
} else {
|
||||
//防止人为删除数据,导致数据不完整报错
|
||||
for (const [key, value] of Object.entries(defaultBaseState)) {
|
||||
if (state[key] !== undefined) defaultBaseState[key] = state[key]
|
||||
}
|
||||
return defaultBaseState
|
||||
}
|
||||
} catch (e) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
export function checkAndUpgradeSaveSetting(val: string) {
|
||||
// console.log(configStr)
|
||||
// console.log('s', new Blob([val]).size)
|
||||
// val = ''
|
||||
if (val) {
|
||||
try {
|
||||
let data
|
||||
if (typeof val === 'string') {
|
||||
data = JSON.parse(val)
|
||||
} else {
|
||||
data = val
|
||||
}
|
||||
let state: SettingState = data.val
|
||||
if (typeof state !== 'object') {
|
||||
return {}
|
||||
}
|
||||
if (!data.version) {
|
||||
return {}
|
||||
}
|
||||
let version = Number(data.version)
|
||||
|
||||
let defaultSettingState = DefaultSettingState()
|
||||
if (version === SAVE_SETTING_KEY.version) {
|
||||
//防止人为删除数据,导致数据不完整报错
|
||||
for (const [key, value] of Object.entries(defaultSettingState)) {
|
||||
if (state[key] !== undefined) defaultSettingState[key] = state[key]
|
||||
}
|
||||
return defaultSettingState
|
||||
} else {
|
||||
//为了保持永远是最新的快捷键选项列表,但保留住用户的自定义设置,去掉无效的快捷键选项
|
||||
//例: 2版本,可能有快捷键A。3版本没有了
|
||||
for (const [key, value] of Object.entries(defaultSettingState.shortcutKeyMap)) {
|
||||
if (state.shortcutKeyMap[key] !== undefined) defaultSettingState.shortcutKeyMap[key] = state.shortcutKeyMap[key]
|
||||
}
|
||||
delete state.shortcutKeyMap
|
||||
|
||||
for (const [key, value] of Object.entries(defaultSettingState)) {
|
||||
if (state[key] !== undefined) defaultSettingState[key] = state[key]
|
||||
}
|
||||
return defaultSettingState
|
||||
}
|
||||
} catch (e) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
//筛选未自定义的词典,未自定义的词典不需要保存单词,用的时候再下载
|
||||
export function shakeCommonDict(n: BaseState): BaseState {
|
||||
let data: BaseState = cloneDeep(n)
|
||||
data.myDictList.map((v: Dict) => {
|
||||
if (v.isCustom) {
|
||||
if (v.type === DictType.article) {
|
||||
v.articles.map(s => {
|
||||
delete s.sections
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (v.type === DictType.word) v.originWords = []
|
||||
if (v.type === DictType.article) v.articles = []
|
||||
v.words = []
|
||||
v.chapterWords = []
|
||||
}
|
||||
})
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user