fix: 以递归方式检查剪贴板 调整频率 移除图片存储中多余字段

This commit is contained in:
ZiuChen 2022-08-27 14:27:49 +08:00
parent ab7189284d
commit 92666c6770

View File

@ -16,7 +16,7 @@ const isMacOs = utools.isMacOs()
const isWindows = utools.isWindows() const isWindows = utools.isWindows()
const DBPath = `${isMacOs ? userDataPath : homePath}${isWindows ? '\\' : '/'}${dbName}` const DBPath = `${isMacOs ? userDataPath : homePath}${isWindows ? '\\' : '/'}${dbName}`
let globalSameImage = false let globalImageOversize = false
class DB { class DB {
constructor(path) { constructor(path) {
@ -108,72 +108,66 @@ class DB {
} }
} }
const pbpaste = () => { const pbpaste = async () => {
// file return new Promise((res) => {
const files = utools.getCopyedFiles() // null | Array // file
if (files) { const files = utools.getCopyedFiles() // null | Array
return { if (files) {
type: 'file', res({
data: JSON.stringify(files) type: 'file',
data: JSON.stringify(files)
})
} }
} // text
// text const text = clipboard.readText()
const text = clipboard.readText() if (text.trim()) res({ type: 'text', data: text })
if (text.trim()) return { type: 'text', data: text } // image
// image
if (!globalSameImage) {
const image = clipboard.readImage() // 大图卡顿来源 const image = clipboard.readImage() // 大图卡顿来源
const data = image.toDataURL()
globalImageOversize = data.length > 4e5
if (!image.isEmpty()) { if (!image.isEmpty()) {
return { res({
type: 'image', type: 'image',
size: `${image.getSize().width}x${image.getSize().height}`, data: data
data: image.toDataURL() })
}
} }
} // else由于未返回值 控制台会报undefined 但不影响函数执行 })
} }
const watchClipboard = (db, fn) => { const sleep = async (timeout) => {
return new Promise((res) => {
setTimeout(() => {
res()
}, timeout)
})
}
const watchClipboard = async (db, fn) => {
let prev = db.dataBase.data[0] || {} let prev = db.dataBase.data[0] || {}
setInterval(() => { const callBack = async () =>
const item = pbpaste() pbpaste()
item.id = crypto.createHash('md5').update(item.data).digest('hex') .then((item) => {
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) prev = item
} else { fn(item)
// 剪切板元素 与上次复制内容相同 } else {
// 读一次prev 读一次item 若是相同的图片 那么之后都不再读了 // 剪切板元素 与上次复制内容相同
globalSameImage = prev.type === 'image' ? true : false }
} })
}, 100) .then(() => sleep(250))
.then(() => callBack())
callBack()
} }
const db = new DB(DBPath)
db.init()
watchClipboard(db, (item) => {
// 此函数不断执行
if (!item) return
if (db.updateItemViaId(item.id)) {
// 在库中 由 updateItemViaId 更新 updateTime
return
}
// 不在库中 由 addItem 添加
item.createTime = new Date().getTime()
item.updateTime = new Date().getTime()
db.addItem(item)
})
const copy = (item) => { const copy = (item) => {
switch (item.type) { switch (item.type) {
case 'text': case 'text':
clipboard.writeText(item.data) utools.copyText(item.data)
break break
case 'image': case 'image':
utools.copyImage(item.data) utools.copyImage(item.data)
globalSameImage = false // 复制图片 更新标志位
break break
case 'file': case 'file':
const paths = JSON.parse(item.data).map((file) => file.path) const paths = JSON.parse(item.data).map((file) => file.path)
@ -192,7 +186,26 @@ const focus = () => document.querySelector('.clip-search input')?.focus()
const toTop = () => (document.scrollingElement.scrollTop = 0) const toTop = () => (document.scrollingElement.scrollTop = 0)
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click() const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
const db = new DB(DBPath)
db.init()
watchClipboard(db, (item) => {
// 此函数不断执行
if (!item) return
if (db.updateItemViaId(item.id)) {
// 在库中 由 updateItemViaId 更新 updateTime
return
}
// 不在库中 由 addItem 添加
item.createTime = new Date().getTime()
item.updateTime = new Date().getTime()
db.addItem(item)
})
utools.onPluginEnter(() => { utools.onPluginEnter(() => {
if (globalImageOversize) {
utools.copyText('ImageOverSized')
globalImageOversize = false
}
document.querySelector('.clip-search input').select() // 进入插件将搜索框内容全选 document.querySelector('.clip-search input').select() // 进入插件将搜索框内容全选
focus() focus()
toTop() toTop()