fix: 区分Mac平台 采用旧的剪贴板监听方式

This commit is contained in:
ZiuChen
2022-09-21 16:33:02 +08:00
parent d5332e69af
commit b4dc9f803b
7 changed files with 75 additions and 27 deletions

View File

@@ -1,5 +1,14 @@
const { utools, existsSync, readFileSync, writeFileSync, mkdirSync, crypto, listener, clipboard } =
window.exports
const {
utools,
existsSync,
readFileSync,
writeFileSync,
mkdirSync,
crypto,
listener,
clipboard,
time
} = window.exports
import setting from './readSetting'
export default function initPlugin() {
@@ -192,6 +201,19 @@ export default function initPlugin() {
const toTop = () => (document.scrollingElement.scrollTop = 0)
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
const handleClipboardChange = (item = pbpaste()) => {
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.updateTime = new Date().getTime()
db.addItem(item)
}
const registerClipEvent = (listener) => {
const exitHandler = () => {
utools.showNotification('剪贴板监听异常退出 请重启插件以开启监听')
@@ -205,30 +227,37 @@ export default function initPlugin() {
utools.outPlugin()
}
listener
.on('change', () => {
const item = pbpaste()
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.updateTime = new Date().getTime()
db.addItem(item)
})
.on('change', handleClipboardChange)
.on('close', exitHandler)
.on('exit', exitHandler)
.on('error', (error) => errorHandler(error))
}
// 首次启动插件 即开启监听
registerClipEvent(listener)
listener.startListening()
if (!utools.isMacOs()) {
// 首次启动插件 即开启监听
registerClipEvent(listener)
listener.startListening()
} else {
// macos 由于无法执行 clipboard-event-handler-mac 所以使用旧方法
let prev = db.dataBase.data[0] || {}
function loop() {
time.sleep(300).then(loop)
const item = pbpaste()
if (!item) return
item.id = crypto.createHash('md5').update(item.data).digest('hex')
if (item && prev.id != item.id) {
// 剪切板元素 与最近一次复制内容不同
prev = item
handleClipboardChange(item)
} else {
// 剪切板元素 与上次复制内容相同
}
}
loop()
}
utools.onPluginEnter(() => {
if (!listener.listening) {
if (!listener.listening && !utools.isMacOs()) {
// 进入插件后 如果监听已关闭 则重新开启监听
registerClipEvent(listener)
listener.startListening()
@@ -238,7 +267,7 @@ export default function initPlugin() {
})
utools.onPluginOut((processExit) => {
if (processExit) {
if (processExit && !utools.isMacOs()) {
utools.showNotification('剪贴板监听异常退出 请重启插件以开启监听')
listener.stopListening()
}

View File

@@ -226,10 +226,25 @@ onMounted(() => {
updateShowList(activeTab.value)
// 定期检查更新
window.listener.on('change', () => {
list.value = window.db.dataBase.data
updateShowList(activeTab.value)
})
if (!utools.isMacOs) {
window.listener.on('change', () => {
list.value = window.db.dataBase.data
updateShowList(activeTab.value)
})
} else {
// macos
let prev = {}
setInterval(() => {
const now = window.db.dataBase.data[0]
if (prev?.id === now?.id) {
} else {
// 有更新
list.value = window.db.dataBase.data
updateShowList(activeTab.value)
prev = now
}
}, 800)
}
// 监听搜索框
watch(filterText, (val) => updateShowList(activeTab.value))