Skip to content

Commit

Permalink
feat: settings logic
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jun 28, 2022
1 parent f9c5e21 commit 1093438
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 226 deletions.
5 changes: 3 additions & 2 deletions src/platform/electron-browser/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
45 changes: 33 additions & 12 deletions src/platform/node/configuration/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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 localSettings.nestedSettings;
return this.getConfiguration(section);
}

/**
* 根据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();
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExtensionService } from '../extension.service';
})
export class ExtensionDetailComponent implements OnInit {
isOperating = false;
introLoading = false;
extensionDetail: EoExtensionInfo;
resourceInfo = [
{
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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],
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const descriptions: DescriptionsItem[] = [
// label: '发布日期',
// value: '',
// },
];

const electronDetails: DescriptionsItem[] = [
{
id: 'homeDir',
label: 'Install Location',
Expand Down Expand Up @@ -51,7 +54,6 @@ const descriptions: DescriptionsItem[] = [
value: '',
},
];

@Component({
selector: 'eo-about',
template: `
Expand All @@ -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;
Expand All @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 1093438

Please sign in to comment.