Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
feat: export config.json & import config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
SheltonZhu committed Jun 5, 2021
1 parent be3d9c2 commit 59df312
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function updateAppConfig(
forceAppendArray = false
) {
const changedKeys = getUpdatedKeys(currentConfig, targetConfig);
logger.debug('changedKeys: ', changedKeys.length);
// 只有有数据变更才更新配置
if (changedKeys.length) {
const oldConfig = clone(currentConfig, true);
Expand Down
1 change: 0 additions & 1 deletion src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ ipcMain
});
})
.on(events.EVENT_APP_CLIPBOARD_DATA_EDIT, async (e, params) => {
logger.info(params);
await db.clipboardCard.updateById(params._id, params);
})
.on(events.EVENT_APP_CLIPBOARD_PASTE, async (e, params) => {
Expand Down
46 changes: 46 additions & 0 deletions src/main/tray-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { app, shell, dialog } from 'electron';
import bootstrapPromise, { appConfigPath } from './bootstrap';
import logger, { logPath } from './logger';
import { meta } from '../shared/env';
import { chooseSavePath, chooseFile } from '../shared/dialog';
import { writeJson, readJson } from 'fs-extra';
import { currentConfig, updateAppConfig } from './data';

import {
showWindow as showClipboard,
Expand All @@ -13,6 +16,49 @@ import {
openDevtool as sod,
} from './window-settings';
import { notificationIcon } from '../shared/icon';
import { showNotification } from './notification';

// 导出配置文件
export function exportConfigToFile() {
chooseSavePath('选择导出的目录', {}, 'config.json').then((filePath) => {
logger.debug('[exportConfigToFile]: filePath:', filePath);
if (filePath) {
writeJson(filePath, currentConfig, { spaces: '\t' });
showNotification(`点击打开: ${filePath}`, '导出配置成功!', () => {
shell.openPath(filePath).catch((e) => {
logger.error(`[tray]: 打开配置失败: ${e}`);
});
});
}
});
}

// 导入配置文件
export function importConfigFromFile() {
chooseFile(
'选择config.json',
[{ name: 'Json', extensions: ['json'] }],
'config.json'
).then((filePath) => {
logger.debug('[importConfigFromFile]: filePath:', filePath);
if (filePath) {
readJson(filePath)
.then((fileConfig) => {
logger.debug('fileConfig:', fileConfig);
updateAppConfig(fileConfig, false);
showNotification(`导入配置: ${filePath}`, '导入配置成功!');
})
.catch((e) => {
logger.error(e);
showNotification(e.toString(), '导入配置失败!', () => {
shell.openPath(filePath).catch((e) => {
logger.error(`[tray]: 打开配置失败: ${e}`);
});
});
});
}
});
}

// 打开窗口
export function showClipboardWindow() {
Expand Down
6 changes: 5 additions & 1 deletion src/main/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function generateMenus() {
{ label: '设置', click: handler.showSettingsWindow },
{
label: '配置',
submenu: [{ label: '打开配置文件', click: handler.openConfigFile }],
submenu: [
{ label: '打开配置文件', click: handler.openConfigFile },
{ label: '导出配置文件', click: handler.exportConfigToFile },
{ label: '导入配置文件', click: handler.importConfigFromFile },
],
},
{
label: '帮助',
Expand Down
1 change: 0 additions & 1 deletion src/main/window-clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export function getBounds() {
* 隐藏主视图
*/
export function hideWindow(isPaste) {
console.log(isPaste);
if (isWin && activeWin && isPaste) {
activeWin.bringToTop();
activeWin = null;
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ ipcRenderer
})
.on(events.EVENT_RX_SYNC_MAIN, (e, appConfig) => {
// 同步数据
store.dispatch('changeConfig', [appConfig]).then();
store
.dispatch('changeConfig', appConfig)
.then()
.catch((e) => {
console.log(e);
});
})
.on(events.EVENT_APP_CHANGE_BIND, (e, shortcut) => {
changeBind(shortcut.funcName, shortcut.oldKey, shortcut.newKey);
Expand Down
46 changes: 46 additions & 0 deletions src/shared/dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { isWin } from './env';
import { dialog as _dialog, remote } from 'electron';
import logger from '../main/logger';

const dialog = _dialog || remote.dialog;

async function choose(
title,
filters,
isFile = true,
isSave = false,
defaultPath
) {
const pathObject = await dialog[isSave ? 'showSaveDialog' : 'showOpenDialog'](
{
title,
defaultPath,
properties: [isFile ? 'openFile' : 'openDirectory'],
filters,
buttonLabel: isSave ? '导出配置' : '导入配置',
}
);
logger.debug('pathObject:', pathObject);

if (isSave && !pathObject.canceled) {
return pathObject.filePath;
} else if (
pathObject.filePaths &&
pathObject.filePaths.length &&
!pathObject.canceled
) {
if (isWin) {
return pathObject.filePaths[0].replace(/\\/g, '\\\\');
}
return pathObject.filePaths[0];
}
return null;
}

export function chooseFile(title, filters, defaultPath) {
return choose(title, filters, true, false, defaultPath);
}

export function chooseSavePath(title, filters, defaultPath) {
return choose(title, filters, true, true, defaultPath);
}

0 comments on commit 59df312

Please sign in to comment.