feat: 添加读取设置的逻辑

This commit is contained in:
ZiuChen 2022-09-18 11:54:14 +08:00
parent ef098863b0
commit 11c9124aaa
4 changed files with 61 additions and 5 deletions

9
src/data/operation.json Normal file
View File

@ -0,0 +1,9 @@
[
{ "id": "copy", "title": "复制", "icon": "📄" },
{ "id": "view", "title": "查看全部", "icon": "💬" },
{ "id": "open-folder", "title": "打开文件夹", "icon": "📁" },
{ "id": "collect", "title": "收藏", "icon": "⭐" },
{ "id": "remove", "title": "删除", "icon": "❌" },
{ "id": "word-break", "title": "分词", "icon": "💣" },
{ "id": "save-file", "title": "保存", "icon": "💾" }
]

22
src/data/setting.json Normal file
View File

@ -0,0 +1,22 @@
{
"database.path": "",
"database.maxsize": 800,
"database.maxage": 14,
"operation.shown": ["copy", "view", "collect", "remove"],
"operation.custom": [
{
"id": "custom.1663468392",
"title": "讯飞OCR识别",
"icon": "⚡",
"match": ["image"],
"command": "redirect:讯飞ocr"
},
{
"id": "custom.1663468466",
"title": "百度搜索",
"icon": "🔍",
"match": ["text"],
"command": "redirect:百度一下"
}
]
}

View File

@ -1,9 +1,12 @@
const { utools, existsSync, readFileSync, writeFileSync, mkdirSync, crypto, listener, clipboard } =
window.exports
import setting from './readSetting'
export default function initPlugin() {
const sep = utools.isWindows() ? '\\' : '/'
const DBPath = `${
const DBPath =
setting.database.path ||
`${
utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home')
}${sep}_utools_clipboard_manager_storage`
class DB {
@ -26,7 +29,7 @@ export default function initPlugin() {
this.dataBase = dataBase
// 将超过14天的数据删除 排除掉收藏
const now = new Date().getTime()
const deleteTime = now - '\u0031\u0034' * '\u0032\u0034' * 60 * 60 * 1000 // unicode
const deleteTime = now - setting.database.maxage * 24 * 60 * 60 * 1000 // unicode
this.dataBase.data = this.dataBase.data?.filter((item) => item.updateTime > deleteTime)
this.updateDataBaseLocal()
} catch (err) {
@ -59,7 +62,7 @@ export default function initPlugin() {
addItem(cItem) {
this.dataBase.data.unshift(cItem)
this.updateDataBase()
const exceedCount = this.dataBase.data.length - '\u0038\u0030\u0030'
const exceedCount = this.dataBase.data.length - setting.database.maxsize
if (exceedCount > 0) {
// 达到条数限制 删除超出部分
for (let i = 0; i < exceedCount; i++) {

22
src/global/readSetting.js Normal file
View File

@ -0,0 +1,22 @@
import defaultSetting from '../data/setting.json'
let setting = utools.dbStorage.getItem('setting')
if (!setting) {
// 将defaultSetting的key点语法转换为对象
setting = {}
for (const key in defaultSetting) {
const keys = key.split('.')
let obj = setting
for (let i = 0; i < keys.length; i++) {
if (i === keys.length - 1) {
obj[keys[i]] = defaultSetting[key]
} else {
if (!obj[keys[i]]) obj[keys[i]] = {}
obj = obj[keys[i]]
}
}
}
utools.dbStorage.setItem('setting', setting)
}
export default setting