Skip to content

Commit

Permalink
feat: add settings of modules
Browse files Browse the repository at this point in the history
  • Loading branch information
everright committed May 9, 2022
1 parent f7e1160 commit 39b051d
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 9 deletions.
18 changes: 15 additions & 3 deletions src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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';
import { AppViews } from './appView';
import { CoreViews } from './coreView';
import { processEnv } from '../../platform/node/constant';
import { proxyOpenExternal } from '../../shared/common/browserView';
import { deleteFile, readJson } from '../../shared/node/file';
import { STORAGE_TEMP as storageTemp } from '../../shared/common/constant';
import { UnitWorkerModule } from '../../workbench/node/unitWorker';
import Configuration from '../../platform/node/configuration/lib';
import { ConfigurationInterface } from 'src/platform/node/configuration';
let win: BrowserWindow = null;
export const subView = {
appView: null,
mainView: null,
};
const eoUpdater = new EoUpdater();
const eoUpdater = new EoUpdater()
const moduleManager: ModuleManagerInterface = ModuleManager();
const configuration: ConfigurationInterface = Configuration();
// Remote
const mainRemote = require('@electron/remote/main');
mainRemote.initialize();
Expand Down Expand Up @@ -215,6 +217,16 @@ try {
returnValue = moduleManager.getFeatures();
} else if (arg.action === 'getFeature') {
returnValue = moduleManager.getFeature(arg.data.featureKey);
} else if (arg.action === 'saveSettings') {
returnValue = configuration.saveSettings(arg.data.settings);
} else if (arg.action === 'saveModuleSettings') {
returnValue = configuration.saveModuleSettings(arg.data.moduleID, arg.data.settings);
} else if (arg.action === 'deleteModuleSettings') {
returnValue = configuration.deleteModuleSettings(arg.data.moduleID);
} else if (arg.action === 'getSettings') {
returnValue = configuration.getSettings();
} else if (arg.action === 'getModuleSettings') {
returnValue = configuration.getModuleSettings(arg.data.moduleID);
} else if (arg.action === 'getSidePosition') {
returnValue = subView.appView?.sidePosition;
} else if (arg.action === 'hook') {
Expand Down
2 changes: 1 addition & 1 deletion src/core/market/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"core.market.url": {
"type": "string",
"required": true,
"default": "",
"default": "http://127.0.0.1",
"label": "服务器地址",
"description": "插件安装来源的服务器地址"
},
Expand Down
20 changes: 20 additions & 0 deletions src/platform/electron-browser/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,23 @@ window.eo.storageRemote = (args) => {
shareObject.storageResult = null;
return output;
};

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

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

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

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

window.eo.getModuleSettings = (moduleID) => {
return ipcRenderer.sendSync('eo-sync', { action: 'getModuleSettings', data: { moduleID: moduleID } });
};
1 change: 1 addition & 0 deletions src/platform/node/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types';
100 changes: 100 additions & 0 deletions src/platform/node/configuration/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { DATA_DIR as dataDir } from '../../../../shared/common/constant';
import { ConfigurationInterface, ConfigurationValueInterface } from '../types';
import * as path from 'path';
import { fileExists, readJson, writeJson } from '../../../../shared/node/file';

export class Configuration implements ConfigurationInterface {

/**
* 配置文件地址
*/
private readonly configPath: string;

constructor() {
this.configPath = path.join(dataDir, 'config.json');
this.checkConfig();
}

/**
* 检查配置文件,不存在创建
*/
private checkConfig() {
if (!fileExists(this.configPath)) {
this.saveConfig({ settings: {} });
}
}

/**
* 读取配置文件
*/
private loadConfig(): ConfigurationValueInterface {
const data = readJson(this.configPath) || { settings: {} };
return data;
}

/**
* 保存配置文件
*/
private saveConfig(data: ConfigurationValueInterface): boolean {
return writeJson(this.configPath, data);
}

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

/**
* 保存模块配置
* @param moduleID
* @param settings
*/
saveModuleSettings(moduleID: string, settings: ConfigurationValueInterface): boolean {
let data = this.loadConfig();
if (!data.settings) {
data.settings = {};
}
data.settings[moduleID] = settings;
return this.saveConfig(data);
}

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

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

/**
* 获取模块配置
* @param moduleID
* @returns
*/
getModuleSettings(moduleID: string): ConfigurationValueInterface {
const settings = this.getSettings();
return settings[moduleID] || {};
}

}

export default () => new Configuration();
14 changes: 14 additions & 0 deletions src/platform/node/configuration/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 配置管理
*/
export interface ConfigurationInterface {
saveSettings: (settings: ConfigurationValueInterface) => boolean;
saveModuleSettings: (moduleID: string, settings: ConfigurationValueInterface) => boolean;
deleteModuleSettings: (moduleID: string) => boolean;
getSettings: () => ConfigurationValueInterface;
getModuleSettings: (moduleID: string) => ConfigurationValueInterface;
}

export interface ConfigurationValueInterface {
[propName: string]: any;
}
2 changes: 1 addition & 1 deletion src/platform/node/extension-manager/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ModuleHandler } from './handler';
import { CoreHandler } from './core';
import { ModuleHandlerResult, ModuleInfo, ModuleManagerInfo, ModuleManagerInterface, ModuleType } from '../types';
import * as path from 'path';
import { isNotEmpty } from '../../../..//shared/common/common';
import { isNotEmpty } from '../../../../shared/common/common';

export class ModuleManager implements ModuleManagerInterface {
/**
Expand Down
2 changes: 0 additions & 2 deletions src/workbench/browser/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { EnvState } from './shared/store/env.state';

// NG1 Upgrade
import { UpgradeModule } from '@angular/upgrade/static';
import { NzModalModule } from 'ng-zorro-antd/modal';

@NgModule({
declarations: [AppComponent],
Expand All @@ -25,7 +24,6 @@ import { NzModalModule } from 'ng-zorro-antd/modal';
AppRoutingModule,
HttpClientModule,
UpgradeModule,
NzModalModule,
NgxsModule.forRoot([EnvState]),
],
providers: [
Expand Down
1 change: 1 addition & 0 deletions src/workbench/browser/src/app/pages/pages.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<eo-navbar></eo-navbar>
<div class="home_container f_row">
<eo-setting></eo-setting>
<eo-sidebar></eo-sidebar>
<div class="home fg1">
<router-outlet></router-outlet>
Expand Down
4 changes: 2 additions & 2 deletions src/workbench/browser/src/app/pages/pages.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { PagesRoutingModule } from './pages-routing.module';

import { SettingModule } from './setting/setting.module'
import { PagesComponent } from './pages.component';
import { SharedModule } from '../shared/shared.module';

@NgModule({
declarations: [PagesComponent],
imports: [PagesRoutingModule, FormsModule, SharedModule],
imports: [PagesRoutingModule, FormsModule, SharedModule, SettingModule],
exports: []
})
export class PagesModule {}
24 changes: 24 additions & 0 deletions src/workbench/browser/src/app/pages/setting/setting.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<a class="sidebar_item sidebar_setting" title="设置" (click)="handleShowModal()" *ngIf="isVisible">
<i nz-icon nzType="setting"></i>
</a>

<nz-modal [(nzVisible)]="isVisible && isModal" nzWidth="70%" nzTitle="配置" (nzOnCancel)="handleCancel()">
<section *nzModalContent class="flex">
<form nz-form [nzLayout]="'vertical'" [formGroup]="validateForm" (ngSubmit)="handleSave()">
<nz-tabset [nzTabPosition]="position">
<nz-tab *ngFor="let module of modules" [nzTitle]="module.title">
<nz-form-item nz-col class="fg1" *ngFor="let field of module.fields">
<nz-form-label nzFor="{{ field.name}}">{{ field.label }} </nz-form-label>
<nz-form-control nzErrorTip="请输入 {{ field.label }}">
<input type="text" nz-input id="{{ field.name }}" formControlName="{{ field.name }}" [(ngModel)]="settings[module.key][field.key]" />
</nz-form-control>
</nz-form-item>
</nz-tab>
</nz-tabset>
</form>
</section>
<div *nzModalFooter class="footer">
<button nz-button nzType="primary" (click)="handleSave()">保存</button>
<button nz-button nzType="default" (click)="handleCancel()">取消</button>
</div>
</nz-modal>
16 changes: 16 additions & 0 deletions src/workbench/browser/src/app/pages/setting/setting.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.sidebar_setting {
font-size: 20px;
position: absolute;
padding: 15px;
bottom: 0;
}

.flex {
display: flex;
width: 100%;
height: 60vh;
}

.footer {
text-align: right;
}
101 changes: 101 additions & 0 deletions src/workbench/browser/src/app/pages/setting/setting.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Component, OnInit } from '@angular/core';
import { NzTabPosition } from 'ng-zorro-antd/tabs';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'eo-setting',
templateUrl: './setting.component.html',
styleUrls: ['./setting.component.scss'],
})
export class SettingComponent implements OnInit {
isVisible = false;
isModal = false;
position: NzTabPosition = 'left';
modules = [];
settings = {};
validateForm!: FormGroup;
constructor(
private fb: FormBuilder,
) {}

ngOnInit(): void {
this.init();
}

private init() {
if (window.eo && window.eo.getFeature) {
this.isVisible = true;
this.settings = window.eo.getSettings();
const featureList = window.eo.getFeature('configuration');
const controls = {};
featureList?.forEach((feature: object, key: string) => {
if (!feature['title'] || !feature['properties'] || typeof feature['properties'] !== 'object') {
return true;
}
if (!this.settings[key] || typeof this.settings[key] !== 'object') {
this.settings[key] = {};
}
const fields = [];
for (let field_key in feature['properties']) {
let field = feature['properties'][field_key];
// 加入允许的type限制
if (!field['type'] || !field['label']) {
continue;
}
if ('select' === field['type'] && !field['options']) {
continue;
}
const name = key + '_' + field_key;
field = Object.assign({
name: name,
key: field_key,
required: false,
default: '',
description: ''
}, field);
fields.push(field);
if (!this.settings[key][field_key]) {
this.settings[key][field_key] = field['default'];
}
// 可扩展加入更多默认校验
if (field.required) {
controls[name] = [null, [Validators.required]];
} else {
controls[name] = [null];
}
}
this.modules.push({
key: key,
title: feature['title'],
fields: fields,
});
});
this.validateForm = this.fb.group(controls);
}
}

handleShowModal() {
this.isModal = true;
}

handleSave(): void {
for (const i in this.validateForm.controls) {
if (this.validateForm.controls.hasOwnProperty(i)) {
this.validateForm.controls[i].markAsDirty();
this.validateForm.controls[i].updateValueAndValidity();
}
}
if (this.validateForm.status === 'INVALID') {
return;
}
// 加入根据返回显示提示消息
const saved = window.eo.saveSettings(this.settings);
if (saved) {
this.handleCancel();
}
}

handleCancel(): void {
this.isModal = false;
}
}
Loading

0 comments on commit 39b051d

Please sign in to comment.