refactor: 将copy paste createFile移入utils

This commit is contained in:
ZiuChen 2022-10-10 19:59:20 +08:00
parent faf2c8b722
commit a1cc65a362
4 changed files with 70 additions and 54 deletions

View File

@ -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,

View File

@ -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()

View File

@ -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点语法转换为对象

View File

@ -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 }