mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-06-07 13:54:05 +08:00
refactor: 将copy
paste
createFile
移入utils
This commit is contained in:
parent
faf2c8b722
commit
a1cc65a362
@ -1,4 +1,5 @@
|
|||||||
const { existsSync, readFileSync, writeFileSync, mkdirSync, watch } = require('fs')
|
const { existsSync, readFileSync, writeFileSync, mkdirSync, watch } = require('fs')
|
||||||
|
const { sep } = require('path')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
const listener = require('./listener')
|
const listener = require('./listener')
|
||||||
const { clipboard } = require('electron')
|
const { clipboard } = require('electron')
|
||||||
@ -11,6 +12,7 @@ window.exports = {
|
|||||||
writeFileSync,
|
writeFileSync,
|
||||||
mkdirSync,
|
mkdirSync,
|
||||||
watch,
|
watch,
|
||||||
|
sep,
|
||||||
crypto,
|
crypto,
|
||||||
listener,
|
listener,
|
||||||
clipboard,
|
clipboard,
|
||||||
|
@ -3,18 +3,16 @@ const {
|
|||||||
existsSync,
|
existsSync,
|
||||||
readFileSync,
|
readFileSync,
|
||||||
writeFileSync,
|
writeFileSync,
|
||||||
mkdirSync,
|
|
||||||
watch,
|
watch,
|
||||||
crypto,
|
crypto,
|
||||||
listener,
|
listener,
|
||||||
clipboard,
|
clipboard,
|
||||||
time,
|
time
|
||||||
Buffer
|
|
||||||
} = window.exports
|
} = window.exports
|
||||||
|
import { copy, paste, createFile } from '../utils'
|
||||||
import setting from './readSetting'
|
import setting from './readSetting'
|
||||||
|
|
||||||
export default function initPlugin() {
|
export default function initPlugin() {
|
||||||
const SEP = utools.isWindows() ? '\\' : '/'
|
|
||||||
class DB {
|
class DB {
|
||||||
constructor(path) {
|
constructor(path) {
|
||||||
const d = new Date()
|
const d = new Date()
|
||||||
@ -45,6 +43,7 @@ export default function initPlugin() {
|
|||||||
(item) => item.updateTime > deleteTime || item.collect
|
(item) => item.updateTime > deleteTime || item.collect
|
||||||
)
|
)
|
||||||
this.updateDataBaseLocal()
|
this.updateDataBaseLocal()
|
||||||
|
this.watchDataBaseUpdate()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
utools.showNotification('读取剪切板出错: ' + err)
|
utools.showNotification('读取剪切板出错: ' + err)
|
||||||
return
|
return
|
||||||
@ -64,6 +63,7 @@ export default function initPlugin() {
|
|||||||
try {
|
try {
|
||||||
const dataBase = JSON.parse(data)
|
const dataBase = JSON.parse(data)
|
||||||
this.dataBase = dataBase
|
this.dataBase = dataBase
|
||||||
|
window.db.dataBase = dataBase // 更新内存中数据
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
utools.showNotification('读取剪切板出错: ' + err)
|
utools.showNotification('读取剪切板出错: ' + err)
|
||||||
return
|
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)
|
const db = new DB(setting.database.path)
|
||||||
db.init()
|
db.init()
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import defaultSetting from '../data/setting.json'
|
import defaultSetting from '../data/setting.json'
|
||||||
import { pointToObj } from '../utils'
|
import { pointToObj } from '../utils'
|
||||||
|
|
||||||
const SEP = utools.isWindows() ? '\\' : '/'
|
const defaultPath = `${utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home')}${
|
||||||
const defaultPath = `${
|
window.exports.sep
|
||||||
utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home')
|
}_utools_clipboard_manager_storage`
|
||||||
}${SEP}_utools_clipboard_manager_storage`
|
|
||||||
|
|
||||||
export default function restoreSetting() {
|
export default function restoreSetting() {
|
||||||
// 将defaultSetting的key点语法转换为对象
|
// 将defaultSetting的key点语法转换为对象
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
const {
|
||||||
|
utools,
|
||||||
|
existsSync,
|
||||||
|
readFileSync,
|
||||||
|
writeFileSync,
|
||||||
|
mkdirSync,
|
||||||
|
watch,
|
||||||
|
sep,
|
||||||
|
crypto,
|
||||||
|
listener,
|
||||||
|
clipboard,
|
||||||
|
time,
|
||||||
|
Buffer
|
||||||
|
} = window.exports
|
||||||
|
|
||||||
const dateFormat = (timeStamp) => {
|
const dateFormat = (timeStamp) => {
|
||||||
const startTime = new Date(timeStamp) // 开始时间
|
const startTime = new Date(timeStamp) // 开始时间
|
||||||
const endTime = new Date() // 结束时间
|
const endTime = new Date() // 结束时间
|
||||||
@ -36,4 +51,49 @@ const pointToObj = (objWithPointKey) => {
|
|||||||
return rtnObj
|
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 }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user