refactor: preload update

This commit is contained in:
ZiuChen 2022-08-14 16:35:41 +08:00
parent e8de8b6e06
commit c2ce1cdeec

View File

@ -56,6 +56,7 @@ class DB {
}
updateDataBaseLocal(dataBase) {
// 更新文件数据
console.log('updateDataBaseLocal')
fs.writeFileSync(this.path, JSON.stringify(dataBase || this.dataBase), (err) => {
if (err) {
utools.showNotification('写入剪切板出错' + err)
@ -63,7 +64,7 @@ class DB {
}
})
}
setItem(cItem) {
addItem(cItem) {
this.dataBase.data.unshift(cItem)
this.updateDataBase()
this.updateDataBaseLocal()
@ -73,19 +74,44 @@ class DB {
this.updateDataBase()
this.updateDataBaseLocal()
}
filterDataBase(key) {
filterDataBaseViaData(key) {
// 过滤展示数据
// TODO: 添加文件/目录名筛选
const filterValue = key.toLowerCase()
const textItems = this.dataBase.data.filter((item) => item.type === 'text')
return textItems.filter((item) => item.data.toLowerCase().indexOf(filterValue) !== -1)
}
filterDataBaseViaId(id) {
return this.dataBase.data.filter((item) => item.id === id)
}
updateItemViaId(id) {
for (const item of this.dataBase.data) {
console.log(item.id, id)
if (item.id === id) {
item.updateTime = new Date().getTime()
this.sortDataBaseViaTime()
return true
}
}
return false
}
sortDataBaseViaTime() {
this.dataBase.data = this.dataBase.data.sort((a, b) => {
return b.updateTime - a.updateTime
})
this.updateDataBaseLocal()
}
}
// inu1255: pbpaste & watchClipboard
function pbpaste() {
// let file;
// if (file) return {type: "file", data: file};
const files = utools.getCopyedFiles() // null | Array
if (files) {
return {
type: 'file',
data: JSON.stringify(files)
}
}
const image = clipboard.readImage()
if (!image.isEmpty())
return {
@ -97,17 +123,18 @@ function pbpaste() {
if (text.trim()) return { type: 'text', data: text }
}
function watchClipboard(fn) {
let prev = {}
function watchClipboard(db, fn) {
let prev = db.dataBase.data[0] || {}
setInterval(() => {
const item = pbpaste()
console.log(item)
// 比较前后两次剪切板内容的哈希值是否相同 如果相同则不更新
item._id = crypto.createHash('md5').update(item).digest('hex')
if (item && prev._id != item._id) {
// 剪切板有更新
item.id = crypto.createHash('md5').update(item.data).digest('hex')
if (item && prev.id != item.id) {
// 剪切板元素 与最近一次复制内容不同
prev = item
fn(item)
} else {
// 剪切板元素 与上次复制内容相同
// 无更新
}
}, 500)
}
@ -122,21 +149,31 @@ function copy(item) {
clipboard.writeImage(nImg)
break
case 'file':
clipboard.writeText(item.data)
const paths = JSON.parse(item.data).map((file) => file.path)
utools.copyFile(paths)
break
}
utools.outPlugin()
}
const path = `${home}\\${dbName}`
const db = new DB(path)
db.init()
watchClipboard((item) => {
watchClipboard(db, (item) => {
// 此函数不断执行
if (!item) return
item.id = crypto.createHash('md5').update(item.data).digest('hex')
if (db.updateItemViaId(item.id)) {
// 在库中 由 updateItemViaId 更新 updateTime
return
}
// 不在库中 由 addItem 添加
item.createTime = new Date().getTime()
db.setItem(item)
item.updateTime = new Date().getTime()
db.addItem(item)
})
window.db = db
window.copy = copy
window.openFile = utools.shellOpenPath
window.getIcon = utools.getFileIcon