mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-06-29 05:33:14 +08:00
refactor: preload update
This commit is contained in:
parent
e8de8b6e06
commit
c2ce1cdeec
@ -56,6 +56,7 @@ class DB {
|
|||||||
}
|
}
|
||||||
updateDataBaseLocal(dataBase) {
|
updateDataBaseLocal(dataBase) {
|
||||||
// 更新文件数据
|
// 更新文件数据
|
||||||
|
console.log('updateDataBaseLocal')
|
||||||
fs.writeFileSync(this.path, JSON.stringify(dataBase || this.dataBase), (err) => {
|
fs.writeFileSync(this.path, JSON.stringify(dataBase || this.dataBase), (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
utools.showNotification('写入剪切板出错' + err)
|
utools.showNotification('写入剪切板出错' + err)
|
||||||
@ -63,7 +64,7 @@ class DB {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
setItem(cItem) {
|
addItem(cItem) {
|
||||||
this.dataBase.data.unshift(cItem)
|
this.dataBase.data.unshift(cItem)
|
||||||
this.updateDataBase()
|
this.updateDataBase()
|
||||||
this.updateDataBaseLocal()
|
this.updateDataBaseLocal()
|
||||||
@ -73,19 +74,44 @@ class DB {
|
|||||||
this.updateDataBase()
|
this.updateDataBase()
|
||||||
this.updateDataBaseLocal()
|
this.updateDataBaseLocal()
|
||||||
}
|
}
|
||||||
filterDataBase(key) {
|
filterDataBaseViaData(key) {
|
||||||
// 过滤展示数据
|
// 过滤展示数据
|
||||||
// TODO: 添加文件/目录名筛选
|
// TODO: 添加文件/目录名筛选
|
||||||
const filterValue = key.toLowerCase()
|
const filterValue = key.toLowerCase()
|
||||||
const textItems = this.dataBase.data.filter((item) => item.type === 'text')
|
const textItems = this.dataBase.data.filter((item) => item.type === 'text')
|
||||||
return textItems.filter((item) => item.data.toLowerCase().indexOf(filterValue) !== -1)
|
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
|
// inu1255: pbpaste & watchClipboard
|
||||||
function pbpaste() {
|
function pbpaste() {
|
||||||
// let file;
|
const files = utools.getCopyedFiles() // null | Array
|
||||||
// if (file) return {type: "file", data: file};
|
if (files) {
|
||||||
|
return {
|
||||||
|
type: 'file',
|
||||||
|
data: JSON.stringify(files)
|
||||||
|
}
|
||||||
|
}
|
||||||
const image = clipboard.readImage()
|
const image = clipboard.readImage()
|
||||||
if (!image.isEmpty())
|
if (!image.isEmpty())
|
||||||
return {
|
return {
|
||||||
@ -97,17 +123,18 @@ function pbpaste() {
|
|||||||
if (text.trim()) return { type: 'text', data: text }
|
if (text.trim()) return { type: 'text', data: text }
|
||||||
}
|
}
|
||||||
|
|
||||||
function watchClipboard(fn) {
|
function watchClipboard(db, fn) {
|
||||||
let prev = {}
|
let prev = db.dataBase.data[0] || {}
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
const item = pbpaste()
|
const item = pbpaste()
|
||||||
console.log(item)
|
item.id = crypto.createHash('md5').update(item.data).digest('hex')
|
||||||
// 比较前后两次剪切板内容的哈希值是否相同 如果相同则不更新
|
if (item && prev.id != item.id) {
|
||||||
item._id = crypto.createHash('md5').update(item).digest('hex')
|
// 剪切板元素 与最近一次复制内容不同
|
||||||
if (item && prev._id != item._id) {
|
|
||||||
// 剪切板有更新
|
|
||||||
prev = item
|
prev = item
|
||||||
fn(item)
|
fn(item)
|
||||||
|
} else {
|
||||||
|
// 剪切板元素 与上次复制内容相同
|
||||||
|
// 无更新
|
||||||
}
|
}
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
@ -122,21 +149,31 @@ function copy(item) {
|
|||||||
clipboard.writeImage(nImg)
|
clipboard.writeImage(nImg)
|
||||||
break
|
break
|
||||||
case 'file':
|
case 'file':
|
||||||
clipboard.writeText(item.data)
|
const paths = JSON.parse(item.data).map((file) => file.path)
|
||||||
|
utools.copyFile(paths)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
utools.outPlugin()
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = `${home}\\${dbName}`
|
const path = `${home}\\${dbName}`
|
||||||
const db = new DB(path)
|
const db = new DB(path)
|
||||||
db.init()
|
db.init()
|
||||||
|
|
||||||
watchClipboard((item) => {
|
watchClipboard(db, (item) => {
|
||||||
|
// 此函数不断执行
|
||||||
if (!item) return
|
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()
|
item.createTime = new Date().getTime()
|
||||||
db.setItem(item)
|
item.updateTime = new Date().getTime()
|
||||||
|
db.addItem(item)
|
||||||
})
|
})
|
||||||
|
|
||||||
window.db = db
|
window.db = db
|
||||||
window.copy = copy
|
window.copy = copy
|
||||||
|
window.openFile = utools.shellOpenPath
|
||||||
|
window.getIcon = utools.getFileIcon
|
||||||
|
Loading…
x
Reference in New Issue
Block a user