Skip to content

Commit

Permalink
✨ Feature: automatically upload when clipboard changes
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #35
  • Loading branch information
Kuingsmile committed Apr 10, 2023
1 parent 7e9f730 commit 3c3e7cd
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"ali-oss": "^6.17.1",
"aws-sdk": "^2.1320.0",
"axios": "^1.3.4",
"clipboard-event": "^1.6.0",
"compare-versions": "^4.1.3",
"core-js": "^3.27.1",
"cos-nodejs-sdk-v5": "^2.11.19",
Expand Down
4 changes: 3 additions & 1 deletion public/i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ FIND_NEW_VERSION: Find New Version
NO_MORE_NOTICE: No More Notice
SHOW_DEVTOOLS: Show Devtools
CURRENT_PICBED: Current Picbed
START_WATCH_CLIPBOARD: Start Watch Clipboard
STOP_WATCH_CLIPBOARD: Stop Watch Clipboard

# ---renderer i18n begin---

Expand Down Expand Up @@ -192,7 +194,7 @@ SETTINGS_SYNC_DELETE_CLOUD: Sync delete from cloud storage of gallery
SETTINGS_ISHIDEDOCK: Hide Dock Icon
SETTINGS_ISHIDEDOCK_TIPS: Not support hide dock and tray at the same time
SETTINGS_ENCODE_OUTPUT_URL: Encode Output(or Copyed) URL

SETTINGS_WATCH_CLIPBOARD: Watch clipboard when software start
# shortcut-page

BUILTIN_CLIPBOARD_TIPS: Use builtin clipboard function to upload instead of using scripts
Expand Down
5 changes: 3 additions & 2 deletions public/i18n/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ FIND_NEW_VERSION: 发现新版本
NO_MORE_NOTICE: 以后不再提醒
SHOW_DEVTOOLS: 打开开发者工具
CURRENT_PICBED: 当前图床

START_WATCH_CLIPBOARD: 开始监听剪贴板
STOP_WATCH_CLIPBOARD: 停止监听剪贴板
# ---renderer i18n begin---

CHOOSE_YOUR_DEFAULT_PICBED: 选择 ${d} 作为你默认图床:
Expand Down Expand Up @@ -193,7 +194,7 @@ SETTINGS_SYNC_DELETE_CLOUD: 相册内删除时同步删除云端文件
SETTINGS_ISHIDEDOCK: 是否隐藏dock图标
SETTINGS_ISHIDEDOCK_TIPS: 不支持同时隐藏dock和托盘
SETTINGS_ENCODE_OUTPUT_URL: 输出(复制) URL 时进行转义

SETTINGS_WATCH_CLIPBOARD: 软件启动时自动监听剪贴板上传
# shortcut-page

SHORTCUT_NAME: 快捷键名称
Expand Down
5 changes: 3 additions & 2 deletions public/i18n/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ FIND_NEW_VERSION: 發現新版本
NO_MORE_NOTICE: 以後不再提醒
SHOW_DEVTOOLS: 開啟開發者工具
CURRENT_PICBED: 當前圖床

START_WATCH_CLIPBOARD: 開始監聽剪貼簿
STOP_WATCH_CLIPBOARD: 停止監聽剪貼簿
# ---renderer i18n begin---

CHOOSE_YOUR_DEFAULT_PICBED: 選擇 ${d} 作為你的預設圖床:
Expand Down Expand Up @@ -193,7 +194,7 @@ SETTINGS_SYNC_DELETE_CLOUD: 從相簿中刪除並同步從雲端刪除
SETTINGS_ISHIDEDOCK: 是否隱藏dock圖示
SETTINGS_ISHIDEDOCK_TIPS: 不支持同時隱藏dock和托盘
SETTINGS_ENCODE_OUTPUT_URL: 輸出(複製) URL 時進行轉義

SETTINGS_WATCH_CLIPBOARD: 軟體啟動時自動監聽剪貼簿上傳
# shortcut-page

SHORTCUT_NAME: 快捷鍵名稱
Expand Down
131 changes: 111 additions & 20 deletions src/main/apis/app/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,59 @@ import { ensureFilePath, handleCopyUrl } from '~/main/utils/common'
import { T } from '~/main/i18n'
import { isMacOSVersionGreaterThanOrEqualTo } from '~/main/utils/getMacOSVersion'
import { buildPicBedListMenu } from '~/main/events/remotes/menu'
import clipboardListener from 'clipboard-event'
import picgo from '../../core/picgo'
import { uploadClipboardFiles } from '../uploader/apis'
let contextMenu: Menu | null
let tray: Tray | null

export function setDockMenu () {
const isListeningClipboard = db.get('settings.isListeningClipboard') || false
const dockMenu = Menu.buildFromTemplate([
{
label: T('OPEN_MAIN_WINDOW'),
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
settingWindow!.focus()
if (windowManager.has(IWindowList.MINI_WINDOW)) {
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
}
}
},
{
label: T('START_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', true)
clipboardListener.startListening()
clipboardListener.on('change', () => {
picgo.log.info('clipboard changed')
uploadClipboardFiles()
})
setDockMenu()
},
enabled: !isListeningClipboard
},
{
label: T('STOP_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', false)
clipboardListener.stopListening()
setDockMenu()
},
enabled: isListeningClipboard
}
])
app.dock.setMenu(dockMenu)
}

export function createMenu () {
const submenu = buildPicBedListMenu()
const isListeningClipboard = db.get('settings.isListeningClipboard') || false
const appMenu = Menu.buildFromTemplate([
{
label: 'PicList',
submenu: [
{
label: T('ABOUT'),
click () {
dialog.showMessageBox({
title: 'PicList',
message: 'PicList',
detail: `Version: ${pkg.version}\nAuthor: Kuingsmile\nGithub: https://github.com/Kuingsmile/PicList`
})
}
},
{
label: T('OPEN_MAIN_WINDOW'),
click () {
Expand All @@ -49,6 +83,28 @@ export function createMenu () {
}
}
},
{
label: T('START_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', true)
clipboardListener.startListening()
clipboardListener.on('change', () => {
picgo.log.info('clipboard changed')
uploadClipboardFiles()
})
createMenu()
},
enabled: !isListeningClipboard
},
{
label: T('STOP_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', false)
clipboardListener.stopListening()
createMenu()
},
enabled: isListeningClipboard
},
{
label: T('RELOAD_APP'),
click () {
Expand Down Expand Up @@ -78,19 +134,10 @@ export function createMenu () {
}

export function createContextMenu () {
const isListeningClipboard = db.get('settings.isListeningClipboard') || false
if (process.platform === 'darwin' || process.platform === 'win32') {
const submenu = buildPicBedListMenu()
const template = [
{
label: T('ABOUT'),
click () {
dialog.showMessageBox({
title: 'PicList',
message: 'PicList',
detail: `Version: ${pkg.version}\nAuthor: Kuingsmile\nGithub: https://github.com/Kuingsmile/PicList`
})
}
},
{
label: T('OPEN_MAIN_WINDOW'),
click () {
Expand All @@ -108,6 +155,28 @@ export function createContextMenu () {
// @ts-ignore
submenu
},
{
label: T('START_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', true)
clipboardListener.startListening()
clipboardListener.on('change', () => {
picgo.log.info('clipboard changed')
uploadClipboardFiles()
})
createContextMenu()
},
enabled: !isListeningClipboard
},
{
label: T('STOP_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', false)
clipboardListener.stopListening()
createContextMenu()
},
enabled: isListeningClipboard
},
{
label: T('RELOAD_APP'),
click () {
Expand Down Expand Up @@ -200,6 +269,28 @@ export function createContextMenu () {
miniWindow.focus()
}
},
{
label: T('START_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', true)
clipboardListener.startListening()
clipboardListener.on('change', () => {
picgo.log.info('clipboard changed')
uploadClipboardFiles()
})
createContextMenu()
},
enabled: !isListeningClipboard
},
{
label: T('STOP_WATCH_CLIPBOARD'),
click () {
db.set('settings.isListeningClipboard', false)
clipboardListener.stopListening()
createContextMenu()
},
enabled: isListeningClipboard
},
{
label: T('ABOUT'),
click () {
Expand Down
2 changes: 1 addition & 1 deletion src/main/apis/app/uploader/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import picgo from '@core/picgo'
import GuiApi from '../../gui'

const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard')
const useBuiltinClipboard = db.get('settings.useBuiltinClipboard') === undefined ? true : !!db.get('settings.useBuiltinClipboard')
const win = windowManager.getAvailableWindow()
if (useBuiltinClipboard) {
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboard()
Expand Down
36 changes: 19 additions & 17 deletions src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
globalShortcut,
protocol,
Notification,
Menu,
dialog,
screen
} from 'electron'
Expand All @@ -26,7 +25,7 @@ import {
uploadClipboardFiles
} from 'apis/app/uploader/apis'
import {
createTray
createTray, setDockMenu
} from 'apis/app/system'
import server from '~/main/server/index'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
Expand All @@ -45,6 +44,10 @@ import UpDownTaskQueue from '../manage/datastore/upDownTaskQueue'
import { T } from '~/main/i18n'
import { UpdateInfo, autoUpdater } from 'electron-updater'
import updateChecker from '../utils/updateChecker'
import clipboardListener from 'clipboard-event'
import path from 'path'
import { CLIPBOARD_IMAGE_FOLDER } from '~/universal/utils/static'
import fs from 'fs-extra'
const isDevelopment = process.env.NODE_ENV !== 'production'

const handleStartUpFiles = (argv: string[], cwd: string) => {
Expand Down Expand Up @@ -138,21 +141,7 @@ class LifeCycle {
windowManager.create(IWindowList.TRAY_WINDOW)
windowManager.create(IWindowList.SETTING_WINDOW)
if (process.platform === 'darwin') {
app.dock.setMenu(
Menu.buildFromTemplate([
{
label: T('OPEN_MAIN_WINDOW'),
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
settingWindow!.focus()
if (windowManager.has(IWindowList.MINI_WINDOW)) {
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
}
}
}
])
)
setDockMenu()
}
const startMode = db.get('settings.startMode') || 'quiet'
if (startMode !== 'no-tray' && process.platform === 'darwin') {
Expand Down Expand Up @@ -213,6 +202,19 @@ class LifeCycle {
settingWindow.show()
settingWindow.focus()
}
const isAutoListenClipboard = db.get('settings.isAutoListenClipboard') || false
if (isAutoListenClipboard) {
db.set('settings.isListeningClipboard', true)
clipboardListener.startListening()
clipboardListener.on('change', () => {
picgo.log.info('clipboard changed')
uploadClipboardFiles()
})
} else {
db.set('settings.isListeningClipboard', false)
}
const clipboardDir = path.join(picgo.baseDir, CLIPBOARD_IMAGE_FOLDER)
fs.ensureDir(clipboardDir)
}
app.whenReady().then(readyFunction)
}
Expand Down
26 changes: 21 additions & 5 deletions src/renderer/pages/PicGoSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@
@change="handleEncodeOutputURL"
/>
</el-form-item>
<el-form-item
:label="$T('SETTINGS_WATCH_CLIPBOARD')"
>
<el-switch
v-model="form.isAutoListenClipboard"
:active-text="$T('SETTINGS_OPEN')"
:inactive-text="$T('SETTINGS_CLOSE')"
@change="handleIsAutoListenClipboard"
/>
</el-form-item>
<el-form-item
:style="{ marginRight: '-64px' }"
:label="$T('CHOOSE_SHOWED_PICBED')"
Expand Down Expand Up @@ -1034,7 +1044,7 @@ const customLinkRule = (rule: any, value: string, callback: (arg0?: Error) => vo
}
const $router = useRouter()
const form = reactive<ISettingForm>({
updateHelper: false,
updateHelper: true,
showPicBedList: [],
autoStart: false,
rename: false,
Expand All @@ -1044,14 +1054,15 @@ const form = reactive<ISettingForm>({
logLevel: ['all'],
autoCopyUrl: true,
checkBetaUpdate: true,
useBuiltinClipboard: false,
useBuiltinClipboard: true,
language: 'zh-CN',
logFileSizeLimit: 10,
deleteCloudFile: false,
isCustomMiniIcon: false,
customMiniIcon: '',
isHideDock: false,
encodeOutputURL: true
encodeOutputURL: true,
isAutoListenClipboard: false
})
const languageList = i18nManager.languageList.map(item => ({
Expand Down Expand Up @@ -1132,7 +1143,7 @@ async function initData () {
if (config !== undefined) {
const settings = config.settings || {}
const picBed = config.picBed
form.updateHelper = settings.showUpdateTip || false
form.updateHelper = settings.showUpdateTip === undefined ? true : settings.showUpdateTip
form.autoStart = settings.autoStart || false
form.rename = settings.rename || false
form.autoRename = settings.autoRename || false
Expand All @@ -1141,7 +1152,8 @@ async function initData () {
form.logLevel = initLogLevel(settings.logLevel || [])
form.autoCopyUrl = settings.autoCopy === undefined ? true : settings.autoCopy
form.checkBetaUpdate = settings.checkBetaUpdate === undefined ? true : settings.checkBetaUpdate
form.useBuiltinClipboard = settings.useBuiltinClipboard === undefined ? false : settings.useBuiltinClipboard
form.useBuiltinClipboard = settings.useBuiltinClipboard === undefined ? true : settings.useBuiltinClipboard
form.isAutoListenClipboard = settings.isAutoListenClipboard || false
form.language = settings.language ?? 'zh-CN'
form.encodeOutputURL = settings.encodeOutputURL === undefined ? true : settings.encodeOutputURL
form.deleteCloudFile = settings.deleteCloudFile || false
Expand Down Expand Up @@ -1277,6 +1289,10 @@ function useBuiltinClipboardChange (val: ICheckBoxValueType) {
saveConfig('settings.useBuiltinClipboard', val)
}
function handleIsAutoListenClipboard (val: ICheckBoxValueType) {
saveConfig('settings.isAutoListenClipboard', val)
}
function handleShowPicBedListChange (val: ICheckBoxValueType[]) {
const list = picBed.value.map(item => {
if (!val.includes(item.name)) {
Expand Down
Loading

0 comments on commit 3c3e7cd

Please sign in to comment.