Skip to content

Commit

Permalink
feat(UI): global settings (#25)
Browse files Browse the repository at this point in the history
* feat(UI): global settings

* update code

* remove redundant code

* feat: support scrolll when content overflow

* feat: auto save to local

* pref: update css style

* test

* fix: sync configuration when show setting modal

* fix: window is not define

* fix: key undefind

* feat: debug push extension

* fix: auto save

* feat: add selectTheme

* fix: setting icon css style

Co-authored-by: 夜鹰 <17kungfuboy@gmail.com>
  • Loading branch information
buqiyuan and kungfuboy authored May 18, 2022
1 parent 8109b86 commit 039c1a6
Show file tree
Hide file tree
Showing 26 changed files with 762 additions and 223 deletions.
6 changes: 3 additions & 3 deletions src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app, BrowserWindow, ipcMain, screen } from 'electron';
import { EoUpdater } from './updater';
import * as path from 'path';
import * as os from 'os'
import * as os from 'os';
import ModuleManager from '../../platform/node/extension-manager/lib/manager';
import { ModuleInfo, ModuleManagerInterface } from '../../platform/node/extension-manager';
import { StorageHandleStatus, StorageProcessType } from '../../platform/browser/IndexedDB';
Expand All @@ -18,7 +18,7 @@ export const subView = {
appView: null,
mainView: null,
};
const eoUpdater = new EoUpdater()
const eoUpdater = new EoUpdater();
const moduleManager: ModuleManagerInterface = ModuleManager();
const configuration: ConfigurationInterface = Configuration();
// Remote
Expand Down Expand Up @@ -219,7 +219,7 @@ try {
} else if (arg.action === 'getFeature') {
returnValue = moduleManager.getFeature(arg.data.featureKey);
} else if (arg.action === 'saveSettings') {
returnValue = configuration.saveSettings(arg.data.settings);
returnValue = configuration.saveSettings(arg.data);
} else if (arg.action === 'saveModuleSettings') {
returnValue = configuration.saveModuleSettings(arg.data.moduleID, arg.data.settings);
} else if (arg.action === 'deleteModuleSettings') {
Expand Down
2 changes: 2 additions & 0 deletions src/app/electron-main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const appVersion = require('../../../package.json').version;
export class EoUpdater {
constructor() {
this.watchLog();
// 是否自动更新
// autoUpdater.autoDownload = window.eo.getModuleSettings('common.app.autoUpdate') !== false;
if (appVersion.includes('beta')) autoUpdater.channel = 'beta';
console.log('appVersion', appVersion, autoUpdater.channel);
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/electron-browser/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ window.eo.storageRemote = (args) => {
return output;
};

window.eo.saveSettings = (settings) => {
return ipcRenderer.sendSync('eo-sync', { action: 'saveSettings', data: { settings: settings } });
window.eo.saveSettings = ({ settings, nestedSettings }) => {
return ipcRenderer.sendSync('eo-sync', { action: 'saveSettings', data: { settings, nestedSettings } });
};

window.eo.saveModuleSettings = (moduleID, settings) => {
Expand Down
51 changes: 28 additions & 23 deletions src/platform/node/configuration/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as path from 'path';
import { fileExists, readJson, writeJson } from '../../../../shared/node/file';

export class Configuration implements ConfigurationInterface {

/**
* 配置文件地址
*/
Expand Down Expand Up @@ -36,65 +35,71 @@ export class Configuration implements ConfigurationInterface {
* 保存配置文件
*/
private saveConfig(data: ConfigurationValueInterface): boolean {
return writeJson(this.configPath, data);
return writeJson(this.configPath, data, true);
}

/**
* 保存全局配置
*/
saveSettings(settings: ConfigurationValueInterface): boolean {
saveSettings({ settings = {}, nestedSettings = {} }): boolean {
let data = this.loadConfig();
data.settings = settings;
return this.saveConfig(data);
data.nestedSettings = nestedSettings;
return this.saveConfig(data);
}

/**
* 保存模块配置
* @param moduleID
* @param settings
* @param moduleID
* @param settings
*/
saveModuleSettings(moduleID: string, settings: ConfigurationValueInterface): boolean {
let data = this.loadConfig();
if (!data.settings) {
data.settings = {};
}
data.settings ??= {};
data.nestedSettings ??= {};
data.settings[moduleID] = settings;
const propArr = moduleID.split('.');
const target = propArr.slice(0, -1).reduce((p, k) => p[k], data.nestedSettings);
target[propArr.at(-1)] = settings;
return this.saveConfig(data);
}

/**
* 删除模块配置
* @param moduleID
* @returns
* @param moduleID
* @returns
*/
deleteModuleSettings(moduleID: string): boolean {
let data = this.loadConfig();
if (data.settings && data.settings[moduleID]) {
delete(data.settings[moduleID]);
delete data.settings[moduleID];
return this.saveConfig(data);
}
return false;
}

/**
* 获取全局配置
* 获取全局配置
* @returns
*/
getSettings(): ConfigurationValueInterface {
const data = this.loadConfig();
return data.settings;
return data;
}

/**
* 获取模块配置
* @param moduleID
* @returns
* 获取模块配置, 以小数点分割的属性链,如:common.app.update
* @param section
* @returns
*/
getModuleSettings(moduleID: string): ConfigurationValueInterface {
const settings = this.getSettings();
return settings[moduleID] || {};
}

getModuleSettings(section?: string): ConfigurationValueInterface {
const localSettings = this.getSettings();
localSettings.nestedSettings ??= {};
if (section) {
return section.split('.').reduce((p, k) => p[k], localSettings.nestedSettings);
}
return localSettings.nestedSettings;
}
}

export default () => new Configuration();
12 changes: 6 additions & 6 deletions src/platform/node/extension-manager/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ModuleManager implements ModuleManagerInterface {

/**
* 获取所有功能点列表
* @returns
* @returns
*/
getFeatures(): Map<string, Map<string, object>> {
return this.features;
Expand All @@ -134,11 +134,11 @@ export class ModuleManager implements ModuleManagerInterface {
}
return this.modules.get(moduleID);
}

/**
* 获取某个功能点的集合
* @param featureKey
* @returns
* @param featureKey
* @returns
*/
getFeature(featureKey: string): Map<string, object> {
return this.features.get(featureKey);
Expand Down Expand Up @@ -181,7 +181,7 @@ export class ModuleManager implements ModuleManagerInterface {

/**
* 清除功能点集合中的模块功能点
* @param moduleInfo
* @param moduleInfo
*/
private deleteFeatures(moduleInfo: ModuleInfo) {
if (moduleInfo.features && typeof moduleInfo.features === 'object' && isNotEmpty(moduleInfo.features)) {
Expand All @@ -200,7 +200,7 @@ export class ModuleManager implements ModuleManagerInterface {
private setup(moduleInfo: ModuleInfo) {
if (isNotEmpty(moduleInfo.moduleID)) {
this.set(moduleInfo);
}
}
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/platform/node/extension-manager/types/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ModuleHandlerResult } from './handler';
export enum ModuleType {
system = 'system',
app = 'app',
feature = 'feature'
feature = 'feature',
}

/**
Expand Down Expand Up @@ -58,11 +58,20 @@ export interface ModuleInfo {
sidePosition?: SidePosition;
// 配置项
configuration?: ModuleConfiguration;
/** 贡献点 */
contributes: ModuleContributes;
// 功能点配置
features?: {
[index: string]: object
[index: string]: object;
};
}
/**
* 贡献点
*/

export type ModuleContributes = {
configuration: ModuleConfiguration;
};

/**
* 模块配置项接口
Expand All @@ -71,7 +80,7 @@ export interface ModuleConfiguration {
title: string;
properties: {
[index: string]: ModuleConfigurationField;
}
};
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/shared/node/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import * as path from 'path';
* @param file
* @param data
*/
export const writeJson = (file: string, data: object): boolean => {
return writeFile(file, JSON.stringify(data));
export const writeJson = (file: string, data: object, formatted = false): boolean => {
return writeFile(file, formatted ? JSON.stringify(data, null, 2) : JSON.stringify(data));
};

/**
* Read json file, then return object.
* @param file
*/
export const readJson = (file: string): (object | null) => {
export const readJson = (file: string): object | null => {
const data: string = readFile(file);
if ('' === data) {
return null;
Expand All @@ -37,7 +37,7 @@ export const readFile = (file: string): string => {
/**
* Delete file.
* @param file string
* @returns
* @returns
*/
export const deleteFile = (file: string): boolean => {
try {
Expand All @@ -46,7 +46,7 @@ export const deleteFile = (file: string): boolean => {
} catch (e) {
return false;
}
}
};

/**
* Write data into file.
Expand Down
92 changes: 46 additions & 46 deletions src/workbench/browser/src/app/core/services/theme/theme.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,52 @@ export const THEMES = [
key: '森林',
value: 'classic_forest',
},
{
key: '日出',
value: 'classic_sunrise',
},
{
key: '玩具',
value: 'classic_toy',
},
],
},
{
title: '简洁',
lists: [
{
key: '森林',
value: 'clean_forest',
},
{
key: '日出',
value: 'clean_sunrise',
},
{
key: '云',
value: 'clean_cloud',
},
],
},
{
title: '深色',
lists: [
{
key: '夜晚',
value: 'night_black',
},
{
key: '森林',
value: 'night_forest',
},
{
key: '命令行',
value: 'night_cmd',
},
{
key: '日落',
value: 'night_dusk',
},
// {
// key: '日出',
// value: 'classic_sunrise',
// },
// {
// key: '玩具',
// value: 'classic_toy',
// },
],
},
// {
// title: '简洁',
// lists: [
// {
// key: '森林',
// value: 'clean_forest',
// },
// {
// key: '日出',
// value: 'clean_sunrise',
// },
// {
// key: '云',
// value: 'clean_cloud',
// },
// ],
// },
// {
// title: '深色',
// lists: [
// {
// key: '夜晚',
// value: 'night_black',
// },
// {
// key: '森林',
// value: 'night_forest',
// },
// {
// key: '命令行',
// value: 'night_cmd',
// },
// {
// key: '日落',
// value: 'night_dusk',
// },
// ],
// },
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 注册配置项
*/
class ConfigurationRegistry {
constructor() {}
}

export const configurationRegistry = new ConfigurationRegistry();
Loading

0 comments on commit 039c1a6

Please sign in to comment.