diff --git a/public/preload.js b/public/preload.js index 11f1464..917914b 100644 --- a/public/preload.js +++ b/public/preload.js @@ -1,4 +1,5 @@ const { existsSync, readFileSync, writeFileSync, mkdirSync, watch } = require('fs') +const { sep } = require('path') const crypto = require('crypto') const listener = require('./listener') const { clipboard } = require('electron') @@ -11,6 +12,7 @@ window.exports = { writeFileSync, mkdirSync, watch, + sep, crypto, listener, clipboard, diff --git a/src/global/initPlugin.js b/src/global/initPlugin.js index 99b4d76..34cb748 100644 --- a/src/global/initPlugin.js +++ b/src/global/initPlugin.js @@ -3,18 +3,16 @@ const { existsSync, readFileSync, writeFileSync, - mkdirSync, watch, crypto, listener, clipboard, - time, - Buffer + time } = window.exports +import { copy, paste, createFile } from '../utils' import setting from './readSetting' export default function initPlugin() { - const SEP = utools.isWindows() ? '\\' : '/' class DB { constructor(path) { const d = new Date() @@ -45,6 +43,7 @@ export default function initPlugin() { (item) => item.updateTime > deleteTime || item.collect ) this.updateDataBaseLocal() + this.watchDataBaseUpdate() } catch (err) { utools.showNotification('读取剪切板出错: ' + err) return @@ -64,6 +63,7 @@ export default function initPlugin() { try { const dataBase = JSON.parse(data) this.dataBase = dataBase + window.db.dataBase = dataBase // 更新内存中数据 } catch (err) { utools.showNotification('读取剪切板出错: ' + err) return @@ -158,51 +158,6 @@ export default function initPlugin() { } } - const copy = (item, isHideMainWindow = true) => { - switch (item.type) { - case 'text': - utools.copyText(item.data) - break - case 'image': - utools.copyImage(item.data) - break - case 'file': - const paths = JSON.parse(item.data).map((file) => file.path) - utools.copyFile(paths) - break - } - isHideMainWindow && utools.hideMainWindow() - } - - const paste = () => { - if (utools.isMacOs()) utools.simulateKeyboardTap('v', 'command') - else utools.simulateKeyboardTap('v', 'ctrl') - } - - const createFile = (item) => { - const tempPath = utools.getPath('temp') - const folderPath = tempPath + SEP + 'utools-clipboard-manager' - if (!existsSync(folderPath)) { - try { - mkdirSync(folderPath) - } catch (err) { - utools.showNotification('创建临时文件夹出错: ' + err) - } - } - const { type } = item - if (type === 'image') { - const base64Data = item.data.replace(/^data:image\/\w+;base64,/, '') // remove the prefix - const buffer = Buffer.from(base64Data, 'base64') // to Buffer - const filePath = folderPath + SEP + new Date().valueOf() + '.png' - writeFileSync(filePath, buffer) - return filePath - } else if (type === 'text') { - const filePath = folderPath + SEP + new Date().valueOf() + '.txt' - writeFileSync(filePath, item.data) - return filePath - } - } - const db = new DB(setting.database.path) db.init() diff --git a/src/global/restoreSetting.js b/src/global/restoreSetting.js index 38e9320..cd1c74f 100644 --- a/src/global/restoreSetting.js +++ b/src/global/restoreSetting.js @@ -1,10 +1,9 @@ import defaultSetting from '../data/setting.json' import { pointToObj } from '../utils' -const SEP = utools.isWindows() ? '\\' : '/' -const defaultPath = `${ - utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home') -}${SEP}_utools_clipboard_manager_storage` +const defaultPath = `${utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home')}${ + window.exports.sep +}_utools_clipboard_manager_storage` export default function restoreSetting() { // 将defaultSetting的key点语法转换为对象 diff --git a/src/utils/index.js b/src/utils/index.js index 4c9d3ec..3d995ed 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,3 +1,18 @@ +const { + utools, + existsSync, + readFileSync, + writeFileSync, + mkdirSync, + watch, + sep, + crypto, + listener, + clipboard, + time, + Buffer +} = window.exports + const dateFormat = (timeStamp) => { const startTime = new Date(timeStamp) // 开始时间 const endTime = new Date() // 结束时间 @@ -36,4 +51,49 @@ const pointToObj = (objWithPointKey) => { return rtnObj } -export { dateFormat, pointToObj } +const copy = (item, isHideMainWindow = true) => { + switch (item.type) { + case 'text': + utools.copyText(item.data) + break + case 'image': + utools.copyImage(item.data) + break + case 'file': + const paths = JSON.parse(item.data).map((file) => file.path) + utools.copyFile(paths) + break + } + isHideMainWindow && utools.hideMainWindow() +} + +const paste = () => { + if (utools.isMacOs()) utools.simulateKeyboardTap('v', 'command') + else utools.simulateKeyboardTap('v', 'ctrl') +} + +const createFile = (item) => { + const tempPath = utools.getPath('temp') + const folderPath = tempPath + sep + 'utools-clipboard-manager' + if (!existsSync(folderPath)) { + try { + mkdirSync(folderPath) + } catch (err) { + utools.showNotification('创建临时文件夹出错: ' + err) + } + } + const { type } = item + if (type === 'image') { + const base64Data = item.data.replace(/^data:image\/\w+;base64,/, '') // remove the prefix + const buffer = Buffer.from(base64Data, 'base64') // to Buffer + const filePath = folderPath + sep + new Date().valueOf() + '.png' + writeFileSync(filePath, buffer) + return filePath + } else if (type === 'text') { + const filePath = folderPath + sep + new Date().valueOf() + '.txt' + writeFileSync(filePath, item.data) + return filePath + } +} + +export { dateFormat, pointToObj, copy, paste, createFile }