mirror of
https://gitee.com/eolink_admin/postcat.git
synced 2024-12-03 04:07:54 +08:00
feat: settings logic
This commit is contained in:
parent
f9c5e21288
commit
1093438402
@ -125,8 +125,9 @@ window.eo.storageRemote = (args) => {
|
||||
return output;
|
||||
};
|
||||
|
||||
window.eo.saveSettings = ({ settings, nestedSettings }) => {
|
||||
return ipcRenderer.sendSync('eo-sync', { action: 'saveSettings', data: { settings, nestedSettings } });
|
||||
window.eo.saveSettings = (settings) => {
|
||||
console.log('window.eo.saveSettings', settings);
|
||||
return ipcRenderer.sendSync('eo-sync', { action: 'saveSettings', data: { settings } });
|
||||
};
|
||||
|
||||
window.eo.saveModuleSettings = (moduleID, settings) => {
|
||||
|
@ -41,10 +41,10 @@ export class Configuration implements ConfigurationInterface {
|
||||
/**
|
||||
* 保存全局配置
|
||||
*/
|
||||
saveSettings({ settings = {}, nestedSettings = {} }): boolean {
|
||||
saveSettings({ settings = {} }): boolean {
|
||||
console.log('settings', settings);
|
||||
let data = this.loadConfig();
|
||||
data.settings = settings;
|
||||
data.nestedSettings = nestedSettings;
|
||||
return this.saveConfig(data);
|
||||
}
|
||||
|
||||
@ -56,11 +56,7 @@ export class Configuration implements ConfigurationInterface {
|
||||
saveModuleSettings(moduleID: string, settings: ConfigurationValueInterface): boolean {
|
||||
let data = this.loadConfig();
|
||||
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);
|
||||
}
|
||||
|
||||
@ -93,13 +89,38 @@ export class Configuration implements ConfigurationInterface {
|
||||
* @returns
|
||||
*/
|
||||
getModuleSettings<T = any>(section?: string): T {
|
||||
const localSettings = this.getSettings();
|
||||
localSettings.nestedSettings ??= {};
|
||||
if (section) {
|
||||
return section.split('.')?.reduce((p, k) => p?.[k], localSettings.nestedSettings);
|
||||
return this.getConfiguration(section);
|
||||
}
|
||||
return localSettings.nestedSettings;
|
||||
|
||||
/**
|
||||
* 根据key路径获取对应的配置的值
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
getConfiguration = (keyPath: string) => {
|
||||
const localSettings = this.getSettings()?.settings || {};
|
||||
|
||||
if (Reflect.has(localSettings, keyPath)) {
|
||||
return Reflect.get(localSettings, keyPath);
|
||||
}
|
||||
|
||||
const keys = Object.keys(localSettings);
|
||||
const filterKeys = keys.filter((n) => n.startsWith(keyPath));
|
||||
if (filterKeys.length) {
|
||||
return filterKeys.reduce((pb, ck) => {
|
||||
const keyArr = ck.replace(`${keyPath}.`, '').split('.');
|
||||
const targetKey = keyArr.pop();
|
||||
const target = keyArr.reduce((p, v) => {
|
||||
p[v] ??= {};
|
||||
return p[v];
|
||||
}, pb);
|
||||
target[targetKey] = localSettings[ck];
|
||||
return pb;
|
||||
}, {});
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
|
||||
export default () => new Configuration();
|
||||
|
@ -29,7 +29,10 @@
|
||||
<h2 class="text-lg font-bold">Intro</h2>
|
||||
<!-- <nz-divider></nz-divider> -->
|
||||
<div class="h-full overflow-auto markdown-desc">
|
||||
<eo-shadow-dom [text]="extensionDetail?.introduction" [options]="{ html: true }"> </eo-shadow-dom>
|
||||
<nz-skeleton [nzLoading]="introLoading" [nzActive]="true">
|
||||
<eo-shadow-dom [text]="extensionDetail?.introduction" [options]="{ html: true }">
|
||||
</eo-shadow-dom>
|
||||
</nz-skeleton>
|
||||
</div>
|
||||
</div>
|
||||
<div class="w-[1px] bg-[#f2f2f2] mx-[10px]"></div>
|
||||
|
@ -11,6 +11,7 @@ import { ExtensionService } from '../extension.service';
|
||||
})
|
||||
export class ExtensionDetailComponent implements OnInit {
|
||||
isOperating = false;
|
||||
introLoading = false;
|
||||
extensionDetail: EoExtensionInfo;
|
||||
resourceInfo = [
|
||||
{
|
||||
@ -61,11 +62,15 @@ export class ExtensionDetailComponent implements OnInit {
|
||||
|
||||
async fetchReadme() {
|
||||
try {
|
||||
this.introLoading = true;
|
||||
const htmlText = await (await fetch(`https://www.npmjs.com/package/${this.extensionDetail.name}`)).text();
|
||||
const domParser = new DOMParser();
|
||||
const html = domParser.parseFromString(htmlText, 'text/html');
|
||||
this.extensionDetail.introduction = html.querySelector('#readme').innerHTML;
|
||||
} catch (error) {}
|
||||
} catch (error) {
|
||||
} finally {
|
||||
this.introLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private findLinkInSingleAssets(assets, item) {
|
||||
|
@ -17,6 +17,7 @@ import { NzTagModule } from 'ng-zorro-antd/tag';
|
||||
import { NzDividerModule } from 'ng-zorro-antd/divider';
|
||||
import { NzTreeModule } from 'ng-zorro-antd/tree';
|
||||
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
|
||||
import { NzSkeletonModule } from 'ng-zorro-antd/skeleton';
|
||||
import { SharedModule } from 'eo/workbench/browser/src/app/shared/shared.module';
|
||||
|
||||
@NgModule({
|
||||
@ -34,6 +35,7 @@ import { SharedModule } from 'eo/workbench/browser/src/app/shared/shared.module'
|
||||
NzDividerModule,
|
||||
NzTreeModule,
|
||||
NzDropDownModule,
|
||||
NzSkeletonModule,
|
||||
],
|
||||
providers: [ExtensionService],
|
||||
declarations: [ExtensionComponent, ExtensionListComponent, ExtensionDetailComponent],
|
||||
|
@ -1,5 +0,0 @@
|
||||
<div class="about">
|
||||
<nz-descriptions nzTitle="关于" [nzColumn]="1">
|
||||
<nz-descriptions-item *ngFor="let item of list" [nzTitle]="item.label">{{item.value}}</nz-descriptions-item>
|
||||
</nz-descriptions>
|
||||
</div>
|
@ -1,8 +0,0 @@
|
||||
.about ::ng-deep .ant-descriptions-item-label {
|
||||
width: 84px;
|
||||
position: relative;
|
||||
&::after {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AboutComponent } from './about.component';
|
||||
|
||||
describe('AboutComponent', () => {
|
||||
let component: AboutComponent;
|
||||
let fixture: ComponentFixture<AboutComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [AboutComponent],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(AboutComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
@ -1,104 +0,0 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { ElectronService } from '../../../core/services';
|
||||
import pkg from '../../../../../../../../package.json';
|
||||
|
||||
const dependencies = {
|
||||
...pkg.dependencies,
|
||||
...pkg.devDependencies,
|
||||
} as const;
|
||||
|
||||
type DescriptionsItem = {
|
||||
readonly id: string;
|
||||
readonly label: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
const descriptions: DescriptionsItem[] = [
|
||||
{
|
||||
id: 'version',
|
||||
label: '当前版本号',
|
||||
value: pkg.version,
|
||||
},
|
||||
{
|
||||
id: 'publishTime',
|
||||
label: '发布日期',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'homeDir',
|
||||
label: '安装目录',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'electron',
|
||||
label: 'Electron',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'chrome',
|
||||
label: 'Chromium',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'node',
|
||||
label: 'Node.js',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'v8',
|
||||
label: 'V8',
|
||||
value: '',
|
||||
},
|
||||
{
|
||||
id: 'os',
|
||||
label: 'OS',
|
||||
value: '',
|
||||
},
|
||||
];
|
||||
|
||||
@Component({
|
||||
selector: 'eo-about',
|
||||
templateUrl: './about.component.html',
|
||||
styleUrls: ['./about.component.scss'],
|
||||
})
|
||||
export class AboutComponent implements OnInit {
|
||||
list = descriptions;
|
||||
|
||||
constructor(private electron: ElectronService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
fetch('https://api.github.com/repos/eolinker/eoapi/releases')
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
const publishTime = data.find((n) => n.tag_name.slice(1) === pkg.version)?.published_at;
|
||||
const publishObj = this.list.find((n) => n.id === 'publishTime');
|
||||
if (publishTime) {
|
||||
publishObj.value = new Intl.DateTimeFormat('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
weekday: 'long',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
hour12: false,
|
||||
})
|
||||
.format(new Date(publishTime))
|
||||
.replace(/星期[^]?/, '');
|
||||
} else {
|
||||
publishObj.value = `当前版本(v${pkg.version})尚未发布`;
|
||||
}
|
||||
});
|
||||
const systemInfo = this.getSystemInfo();
|
||||
this.list.forEach((item) => {
|
||||
if (item.id in systemInfo) {
|
||||
item.value = systemInfo[item.id];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getSystemInfo() {
|
||||
const systemInfo = this.electron.ipcRenderer.sendSync('get-system-info');
|
||||
return systemInfo;
|
||||
}
|
||||
}
|
@ -20,6 +20,9 @@ const descriptions: DescriptionsItem[] = [
|
||||
// label: '发布日期',
|
||||
// value: '',
|
||||
// },
|
||||
];
|
||||
|
||||
const electronDetails: DescriptionsItem[] = [
|
||||
{
|
||||
id: 'homeDir',
|
||||
label: 'Install Location',
|
||||
@ -51,7 +54,6 @@ const descriptions: DescriptionsItem[] = [
|
||||
value: '',
|
||||
},
|
||||
];
|
||||
|
||||
@Component({
|
||||
selector: 'eo-about',
|
||||
template: `
|
||||
@ -65,7 +67,7 @@ const descriptions: DescriptionsItem[] = [
|
||||
styles: [
|
||||
`
|
||||
.about ::ng-deep .ant-descriptions-item-label {
|
||||
width: 110px;
|
||||
width: 112px;
|
||||
position: relative;
|
||||
padding-right: 16px;
|
||||
justify-content: flex-end;
|
||||
@ -83,6 +85,7 @@ export class AboutComponent implements OnInit {
|
||||
constructor(private electron: ElectronService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.appendDetailWithElectron();
|
||||
// fetch('https://api.github.com/repos/eolinker/eoapi/releases')
|
||||
// .then((response) => response.json())
|
||||
// .then((data) => {
|
||||
@ -113,6 +116,15 @@ export class AboutComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
appendDetailWithElectron() {
|
||||
if (!this.electron.isElectron) {
|
||||
return;
|
||||
}
|
||||
|
||||
// this.list = [...this.list, ...electronDetails];
|
||||
this.list.push(...electronDetails);
|
||||
}
|
||||
|
||||
getSystemInfo() {
|
||||
const systemInfo = this.electron.ipcRenderer.sendSync('get-system-info');
|
||||
return systemInfo;
|
||||
|
@ -9,9 +9,9 @@ import { NzMessageService } from 'ng-zorro-antd/message';
|
||||
<form nz-form nzLayout="vertical" [formGroup]="validateForm" (ngSubmit)="submitForm()">
|
||||
<nz-form-item>
|
||||
<nz-form-control>
|
||||
<nz-select formControlName="dataStorage" nzPlaceHolder="Data Storage">
|
||||
<nz-option nzValue="remote-server" nzLabel="Remote Server"></nz-option>
|
||||
<nz-option nzValue="localhost" nzLabel="Localhost"></nz-option>
|
||||
<nz-select formControlName="eoapi-common.dataStorage" nzPlaceHolder="Data Storage">
|
||||
<nz-option nzValue="http" nzLabel="Remote Server"></nz-option>
|
||||
<nz-option nzValue="local" nzLabel="Localhost"></nz-option>
|
||||
</nz-select>
|
||||
</nz-form-control>
|
||||
<div class="text-[12px] mt-[8px] text-gray-400">
|
||||
@ -22,17 +22,17 @@ import { NzMessageService } from 'ng-zorro-antd/message';
|
||||
</p>
|
||||
</div>
|
||||
</nz-form-item>
|
||||
<ng-container *ngIf="validateForm.value.dataStorage === 'remote-server'">
|
||||
<ng-container *ngIf="validateForm.value['eoapi-common.dataStorage'] === 'http'">
|
||||
<nz-form-item>
|
||||
<nz-form-label>Host</nz-form-label>
|
||||
<nz-form-control nzErrorTip="Please input your Host">
|
||||
<input nz-input formControlName="remoteServer.url" placeholder="your host" />
|
||||
<input nz-input formControlName="eoapi-common.remoteServer.url" placeholder="your host" />
|
||||
</nz-form-control>
|
||||
</nz-form-item>
|
||||
<nz-form-item>
|
||||
<nz-form-label>Security Token</nz-form-label>
|
||||
<nz-form-control nzErrorTip="Please input your Security Token">
|
||||
<input nz-input formControlName="remoteServer.token" placeholder="your security token" />
|
||||
<input nz-input formControlName="eoapi-common.remoteServer.token" placeholder="your security token" />
|
||||
</nz-form-control>
|
||||
</nz-form-item>
|
||||
</ng-container>
|
||||
@ -61,9 +61,15 @@ export class DataStorageComponent implements OnInit, OnChanges {
|
||||
|
||||
ngOnInit(): void {
|
||||
this.validateForm = this.fb.group({
|
||||
dataStorage: this.model.dataStorage ?? 'remote-server',
|
||||
'remoteServer.url': [this.model['remoteServer.url'], [Validators.required]],
|
||||
'remoteServer.token': [this.model['remoteServer.token'], [Validators.required]],
|
||||
'eoapi-common.dataStorage': this.model['eoapi-common.dataStorage'] ?? 'local',
|
||||
'eoapi-common.remoteServer.url': [
|
||||
this.model['eoapi-common.remoteServer.url'] || 'http://localhost:3000',
|
||||
[Validators.required],
|
||||
],
|
||||
'eoapi-common.remoteServer.token': [
|
||||
this.model['eoapi-common.remoteServer.token'] || '1ab2c3d4e5f61ab2c3d4e5f6',
|
||||
[Validators.required],
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
@ -79,12 +85,12 @@ export class DataStorageComponent implements OnInit, OnChanges {
|
||||
* 测试远程服务器地址是否可用
|
||||
*/
|
||||
async pingRmoteServerUrl() {
|
||||
const dataStorage = this.validateForm.value.dataStorage;
|
||||
const remoteUrl = this.validateForm.value['remoteServer.url'];
|
||||
const token = this.validateForm.value['remoteServer.token'];
|
||||
const dataStorage = this.validateForm.value['eoapi-common.dataStorage'];
|
||||
const remoteUrl = this.validateForm.value['eoapi-common.remoteServer.url'];
|
||||
const token = this.validateForm.value['eoapi-common.remoteServer.token'];
|
||||
|
||||
if (dataStorage !== 'remote-server') {
|
||||
return Promise.reject(false);
|
||||
if (dataStorage !== 'http') {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
try {
|
||||
@ -100,11 +106,11 @@ export class DataStorageComponent implements OnInit, OnChanges {
|
||||
throw result;
|
||||
}
|
||||
// await result.json();
|
||||
this.message.create('success', '远程服务器地址设置成功!');
|
||||
// this.message.create('success', 'Remote server address set successfully!');
|
||||
return Promise.resolve(true);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
this.message.create('error', '远程服务器连接失败!');
|
||||
this.message.create('error', 'Remote server connection failed!');
|
||||
return Promise.reject(false);
|
||||
}
|
||||
}
|
||||
@ -115,10 +121,13 @@ export class DataStorageComponent implements OnInit, OnChanges {
|
||||
this.loading = true;
|
||||
const result = await this.pingRmoteServerUrl().finally(() => (this.loading = false));
|
||||
if (Object.is(result, true)) {
|
||||
this.message.success('远程数据源连接成功');
|
||||
this.message.success('The remote data source connection is successful!');
|
||||
}
|
||||
this.model = this.validateForm.value;
|
||||
this.modelChange.emit(this.validateForm.value);
|
||||
this.model = {
|
||||
...this.model,
|
||||
...this.validateForm.value,
|
||||
};
|
||||
this.modelChange.emit(this.model);
|
||||
} else {
|
||||
Object.values(this.validateForm.controls).forEach((control) => {
|
||||
if (control.invalid) {
|
||||
|
@ -1,10 +1,15 @@
|
||||
import { Component, EventEmitter, Input, Output } from '@angular/core';
|
||||
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'eo-language-switcher',
|
||||
template: `
|
||||
<div class="font-bold text-lg mb-2">Language</div>
|
||||
<nz-select nzPlaceHolder="Language" (ngModelChange)="(handleChange)" [nzCustomTemplate]="defaultTemplate">
|
||||
<nz-select
|
||||
nzPlaceHolder="Language"
|
||||
[ngModel]="model['eoapi-language']"
|
||||
(ngModelChange)="handleChange($event)"
|
||||
[nzCustomTemplate]="defaultTemplate"
|
||||
>
|
||||
<nz-option nzCustomContent nzValue="en" nzLabel="English">
|
||||
<iconpark-icon name="language"></iconpark-icon>
|
||||
English
|
||||
@ -28,13 +33,14 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
|
||||
],
|
||||
})
|
||||
export class LanguageSwticherComponent {
|
||||
@Input() model: object;
|
||||
@Input() model: object = {};
|
||||
@Output() modelChange: EventEmitter<any> = new EventEmitter();
|
||||
|
||||
constructor() {}
|
||||
|
||||
handleChange(data) {
|
||||
this.model = data;
|
||||
this.modelChange.emit(data);
|
||||
console.log('data', data);
|
||||
this.model['eoapi-language'] = data;
|
||||
this.modelChange.emit(this.model);
|
||||
}
|
||||
}
|
||||
|
@ -44,8 +44,8 @@
|
||||
</nz-tree-view>
|
||||
</div>
|
||||
<nz-divider nzType="vertical" class="divider"></nz-divider>
|
||||
<eo-data-storage *ngIf="selected === 'eoapi-common'" [(model)]="settings['eoapi-common']"></eo-data-storage>
|
||||
<eo-language-switcher *ngIf="selected === 'eoapi-language'" [(model)]="settings['eoapi-language']">
|
||||
<eo-data-storage *ngIf="selected === 'eoapi-common'" [(model)]="settings"></eo-data-storage>
|
||||
<eo-language-switcher *ngIf="selected === 'eoapi-language'" [(model)]="settings">
|
||||
</eo-language-switcher>
|
||||
<form *ngIf="currentConfiguration.length" nz-form [nzLayout]="'vertical'" [formGroup]="validateForm"
|
||||
(ngSubmit)="handleSave()" class="form">
|
||||
|
@ -37,6 +37,9 @@ export class SettingComponent implements OnInit {
|
||||
this.init();
|
||||
this.remoteServerUrl = this.settings['eoapi-common.remoteServer.url'];
|
||||
this.remoteServerToken = this.settings['eoapi-common.remoteServer.token'];
|
||||
this.oldDataStorage = this.settings['eoapi-common.dataStorage'];
|
||||
} else {
|
||||
// this.handleSave();
|
||||
}
|
||||
}
|
||||
get isShowModal() {
|
||||
@ -80,12 +83,7 @@ export class SettingComponent implements OnInit {
|
||||
$isShowModal = false;
|
||||
/** current active configure */
|
||||
/** all configure */
|
||||
settings = {
|
||||
'eoapi-common': {},
|
||||
'eoapi-theme': {},
|
||||
'eoapi-features': {},
|
||||
'eoapi-about': {},
|
||||
};
|
||||
settings = {};
|
||||
treeNodes = [
|
||||
{
|
||||
name: 'Data Storage',
|
||||
@ -105,14 +103,13 @@ export class SettingComponent implements OnInit {
|
||||
},
|
||||
];
|
||||
/** local configure */
|
||||
localSettings = { settings: {}, nestedSettings: {} };
|
||||
/** nested settings */
|
||||
nestedSettings = {};
|
||||
localSettings = {};
|
||||
validateForm!: FormGroup;
|
||||
/** remote server url */
|
||||
remoteServerUrl = '';
|
||||
/** remote server token */
|
||||
remoteServerToken = '';
|
||||
oldDataStorage = '';
|
||||
|
||||
get selected() {
|
||||
return this.selectListSelection.selected.at(0)?.moduleID;
|
||||
@ -170,7 +167,7 @@ export class SettingComponent implements OnInit {
|
||||
// 平级配置对象
|
||||
Object.keys(properties).forEach((fieldKey) => {
|
||||
const props = properties[fieldKey];
|
||||
this.settings[fieldKey] = this.localSettings?.settings?.[fieldKey] ?? props.default;
|
||||
this.settings[fieldKey] = this.localSettings?.[fieldKey] ?? props.default;
|
||||
// 可扩展加入更多默认校验
|
||||
if (props.required) {
|
||||
controls[fieldKey] = [null, [Validators.required]];
|
||||
@ -205,7 +202,7 @@ export class SettingComponent implements OnInit {
|
||||
* @returns
|
||||
*/
|
||||
getConfiguration(key: string) {
|
||||
return key.split('.').reduce((p, k) => p[k], this.nestedSettings);
|
||||
// return key.split('.').reduce((p, k) => p?.[k], this.nestedSettings);
|
||||
}
|
||||
/**
|
||||
* 获取模块的标题
|
||||
@ -233,9 +230,11 @@ export class SettingComponent implements OnInit {
|
||||
// return;
|
||||
// }
|
||||
// ! this.isVisible = true;
|
||||
this.nestedSettings = {};
|
||||
// 获取本地设置
|
||||
this.localSettings = window.eo?.getSettings?.() || JSON.stringify(localStorage.getItem('localSettings') || '{}');
|
||||
this.settings = this.localSettings = JSON.parse(localStorage.getItem('localSettings') || '{}');
|
||||
// @ts-ignore
|
||||
window.getConfiguration = this.remoteService.getConfiguration;
|
||||
console.log('localSettings', this.localSettings);
|
||||
// const featureList = window.eo.getFeature('configuration');
|
||||
const modules = window.eo?.getModules() || new Map([]);
|
||||
// const extensitonConfigurations = [...modules.values()].filter((n) => n.contributes?.configuration);
|
||||
@ -309,7 +308,6 @@ export class SettingComponent implements OnInit {
|
||||
this.validateForm.valueChanges.subscribe(debounce(this.handleSave.bind(this), 300));
|
||||
// 默认选中第一项
|
||||
this.selectModule(this.treeControl.dataNodes.at(0));
|
||||
this.handleSave();
|
||||
}
|
||||
|
||||
handleShowModal() {
|
||||
@ -326,31 +324,34 @@ export class SettingComponent implements OnInit {
|
||||
// if (this.validateForm.status === 'INVALID') {
|
||||
// return;
|
||||
// }
|
||||
const data = { settings: this.settings, nestedSettings: this.nestedSettings };
|
||||
// 加入根据返回显示提示消息
|
||||
const saved = window.eo?.saveSettings
|
||||
? window.eo.saveSettings(data)
|
||||
: localStorage.setItem('localSettings', JSON.stringify(data));
|
||||
if (saved) {
|
||||
// this.handleCancel();
|
||||
}
|
||||
console.log('localSettings', data);
|
||||
localStorage.setItem('localSettings', JSON.stringify(this.settings));
|
||||
window.eo?.saveSettings?.({ ...this.settings });
|
||||
}
|
||||
|
||||
async handleCancel() {
|
||||
try {
|
||||
const isUpdateRemoteInfo =
|
||||
this.remoteServerUrl !== this.settings['eoapi-common.remoteServer.url'] ||
|
||||
this.remoteServerToken !== this.settings['eoapi-common.remoteServer.token'];
|
||||
if (isUpdateRemoteInfo && this.isRemote) {
|
||||
this.message.success('你已修改远程服务相关信息,页面即将刷新');
|
||||
this.remoteServerToken !== this.settings['eoapi-common.remoteServer.token'] ||
|
||||
this.oldDataStorage !== this.settings['eoapi-common.dataStorage'];
|
||||
console.log(
|
||||
'isUpdateRemoteInfo',
|
||||
isUpdateRemoteInfo,
|
||||
this.settings,
|
||||
this.remoteServerUrl,
|
||||
this.remoteServerToken
|
||||
);
|
||||
if (isUpdateRemoteInfo) {
|
||||
this.message.success('你已修改数据源相关信息,页面将在2秒后刷新...');
|
||||
setTimeout(() => {
|
||||
this.remoteService.switchToHttp();
|
||||
this.remoteService.switchDataSource();
|
||||
this.remoteService.refreshComponent();
|
||||
}, 2000);
|
||||
}
|
||||
} catch (error) {
|
||||
} finally {
|
||||
this.handleSave();
|
||||
|
||||
this.isShowModal = false;
|
||||
this.isShowModalChange.emit(false);
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Subject, takeUntil } from 'rxjs';
|
||||
import {
|
||||
DataSourceType,
|
||||
DATA_SOURCE_TYPE_KEY,
|
||||
StorageService,
|
||||
} from 'eo/workbench/browser/src/app/shared/services/storage/storage.service';
|
||||
import { DataSourceType, StorageService } from 'eo/workbench/browser/src/app/shared/services/storage/storage.service';
|
||||
import { MessageService } from 'eo/workbench/browser/src/app/shared/services/message/message.service';
|
||||
import { Message } from 'eo/workbench/browser/src/app/shared/services/message/message.model';
|
||||
import { NzMessageService } from 'ng-zorro-antd/message';
|
||||
@ -25,7 +21,7 @@ export const IS_SHOW_DATA_SOURCE_TIP = 'IS_SHOW_DATA_SOURCE_TIP';
|
||||
export class RemoteService {
|
||||
private destroy$: Subject<void> = new Subject<void>();
|
||||
/** data source type @type { DataSourceType } */
|
||||
dataSourceType: DataSourceType = (localStorage.getItem(DATA_SOURCE_TYPE_KEY) as DataSourceType) || 'local';
|
||||
dataSourceType: DataSourceType = this.getSettings()?.['eoapi-common.dataStorage'] ?? 'local';
|
||||
get isElectron() {
|
||||
return this.electronService.isElectron;
|
||||
}
|
||||
@ -121,6 +117,44 @@ export class RemoteService {
|
||||
this.storageService.toggleDataSource({ dataSourceType: 'http' });
|
||||
}
|
||||
|
||||
getSettings() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem('localSettings') || '{}');
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key路径获取对应的配置的值
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
getConfiguration = (keyPath: string) => {
|
||||
const localSettings = this.getSettings();
|
||||
|
||||
if (Reflect.has(localSettings, keyPath)) {
|
||||
return Reflect.get(localSettings, keyPath);
|
||||
}
|
||||
|
||||
const keys = Object.keys(localSettings);
|
||||
const filterKeys = keys.filter((n) => n.startsWith(keyPath));
|
||||
if (filterKeys.length) {
|
||||
return filterKeys.reduce((pb, ck) => {
|
||||
const keyArr = ck.replace(`${keyPath}.`, '').split('.');
|
||||
const targetKey = keyArr.pop();
|
||||
const target = keyArr.reduce((p, v) => {
|
||||
p[v] ??= {};
|
||||
return p[v];
|
||||
}, pb);
|
||||
target[targetKey] = localSettings[ck];
|
||||
return pb;
|
||||
}, {});
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* switch data
|
||||
*/
|
||||
|
@ -1,9 +1,6 @@
|
||||
import Dexie, { Table } from 'dexie';
|
||||
import { messageService } from 'eo/workbench/browser/src/app/shared/services/message/message.service';
|
||||
import {
|
||||
DataSourceType,
|
||||
DATA_SOURCE_TYPE_KEY,
|
||||
} from 'eo/workbench/browser/src/app/shared/services/storage/storage.service';
|
||||
import { DataSourceType } from 'eo/workbench/browser/src/app/shared/services/storage/storage.service';
|
||||
import { tree2obj } from 'eo/workbench/browser/src/app/utils/tree/tree.utils';
|
||||
import { Observable } from 'rxjs';
|
||||
import {
|
||||
@ -26,8 +23,16 @@ export type ResultType<T = any> = {
|
||||
|
||||
let isFirstLoad = true;
|
||||
|
||||
const getSettings = () => {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem('localSettings') || '{}');
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
const getApiUrl = (apiData: ApiData) => {
|
||||
const dataSourceType: DataSourceType = (localStorage.getItem(DATA_SOURCE_TYPE_KEY) as DataSourceType) || 'local';
|
||||
const dataSourceType: DataSourceType = getSettings()?.['eoapi-common.dataStorage'] ?? 'local';
|
||||
|
||||
/** Is it a remote data source */
|
||||
const isRemote = dataSourceType === 'http';
|
||||
|
@ -5,7 +5,6 @@ import { HttpStorage } from './http/lib';
|
||||
import { MessageService } from '../../../shared/services/message';
|
||||
|
||||
export type DataSourceType = 'local' | 'http';
|
||||
export const DATA_SOURCE_TYPE_KEY = 'DATA_SOURCE_TYPE_KEY';
|
||||
/** is show local data source tips */
|
||||
export const IS_SHOW_REMOTE_SERVER_NOTIFICATION = 'IS_SHOW_REMOTE_SERVER_NOTIFICATION';
|
||||
|
||||
@ -16,7 +15,7 @@ export const IS_SHOW_REMOTE_SERVER_NOTIFICATION = 'IS_SHOW_REMOTE_SERVER_NOTIFIC
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class StorageService {
|
||||
private instance;
|
||||
private dataSourceType: DataSourceType = (localStorage.getItem(DATA_SOURCE_TYPE_KEY) as DataSourceType) || 'local';
|
||||
private dataSourceType: DataSourceType = this.getSettings()?.['eoapi-common.dataStorage'] ?? 'local';
|
||||
constructor(
|
||||
private injector: Injector,
|
||||
private messageService: MessageService,
|
||||
@ -64,7 +63,7 @@ export class StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem(DATA_SOURCE_TYPE_KEY, type);
|
||||
this.setDataStorage(type);
|
||||
this.messageService.send({
|
||||
type: 'onDataSourceChange',
|
||||
data: { ...options, dataSourceType: this.dataSourceType },
|
||||
@ -75,4 +74,16 @@ export class StorageService {
|
||||
this.dataSourceType = dataSourceType ?? (this.dataSourceType === 'http' ? 'local' : 'http');
|
||||
this.setStorage(this.dataSourceType, options);
|
||||
};
|
||||
getSettings() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem('localSettings') || '{}');
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
setDataStorage(value) {
|
||||
const settings = this.getSettings();
|
||||
settings['eoapi-common.dataStorage'] = value;
|
||||
localStorage.setItem('localSettings', JSON.stringify(settings));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user