diff --git a/src/platform/electron-browser/preload.ts b/src/platform/electron-browser/preload.ts index 605e074ce..413ff8a1e 100644 --- a/src/platform/electron-browser/preload.ts +++ b/src/platform/electron-browser/preload.ts @@ -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) => { diff --git a/src/platform/node/configuration/lib/index.ts b/src/platform/node/configuration/lib/index.ts index 2eadfc326..1247a9707 100644 --- a/src/platform/node/configuration/lib/index.ts +++ b/src/platform/node/configuration/lib/index.ts @@ -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(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(); diff --git a/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.html b/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.html index 16bd96ced..716999f7b 100644 --- a/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.html +++ b/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.html @@ -29,7 +29,10 @@

Intro

- + + + +
diff --git a/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.ts b/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.ts index 4172dbe40..0bc7b1c9b 100644 --- a/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.ts +++ b/src/workbench/browser/src/app/pages/extension/detail/extension-detail.component.ts @@ -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) { diff --git a/src/workbench/browser/src/app/pages/extension/extension.module.ts b/src/workbench/browser/src/app/pages/extension/extension.module.ts index 8c68d1488..93f44de10 100644 --- a/src/workbench/browser/src/app/pages/extension/extension.module.ts +++ b/src/workbench/browser/src/app/pages/extension/extension.module.ts @@ -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], diff --git a/src/workbench/browser/src/app/shared/components/about/about.component.html b/src/workbench/browser/src/app/shared/components/about/about.component.html deleted file mode 100644 index 9bd75a7d6..000000000 --- a/src/workbench/browser/src/app/shared/components/about/about.component.html +++ /dev/null @@ -1,5 +0,0 @@ -
- - {{item.value}} - -
diff --git a/src/workbench/browser/src/app/shared/components/about/about.component.scss b/src/workbench/browser/src/app/shared/components/about/about.component.scss deleted file mode 100644 index 008b0f406..000000000 --- a/src/workbench/browser/src/app/shared/components/about/about.component.scss +++ /dev/null @@ -1,8 +0,0 @@ -.about ::ng-deep .ant-descriptions-item-label { - width: 84px; - position: relative; - &::after { - position: absolute; - right: 0; - } -} diff --git a/src/workbench/browser/src/app/shared/components/about/about.component.spec.ts b/src/workbench/browser/src/app/shared/components/about/about.component.spec.ts deleted file mode 100644 index 1c435a011..000000000 --- a/src/workbench/browser/src/app/shared/components/about/about.component.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { AboutComponent } from './about.component'; - -describe('AboutComponent', () => { - let component: AboutComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [AboutComponent], - }).compileComponents(); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(AboutComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/src/workbench/browser/src/app/shared/components/about/about.component.ts b/src/workbench/browser/src/app/shared/components/about/about.component.ts deleted file mode 100644 index 69963e549..000000000 --- a/src/workbench/browser/src/app/shared/components/about/about.component.ts +++ /dev/null @@ -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; - } -} diff --git a/src/workbench/browser/src/app/shared/components/setting/common/about.component.ts b/src/workbench/browser/src/app/shared/components/setting/common/about.component.ts index bd635a093..5b3f20c43 100644 --- a/src/workbench/browser/src/app/shared/components/setting/common/about.component.ts +++ b/src/workbench/browser/src/app/shared/components/setting/common/about.component.ts @@ -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; diff --git a/src/workbench/browser/src/app/shared/components/setting/common/data-storage.component.ts b/src/workbench/browser/src/app/shared/components/setting/common/data-storage.component.ts index 39ad3fd12..fac1d5ed7 100644 --- a/src/workbench/browser/src/app/shared/components/setting/common/data-storage.component.ts +++ b/src/workbench/browser/src/app/shared/components/setting/common/data-storage.component.ts @@ -9,9 +9,9 @@ import { NzMessageService } from 'ng-zorro-antd/message';
- - - + + +
@@ -22,17 +22,17 @@ import { NzMessageService } from 'ng-zorro-antd/message';

- + Host - + Security Token - + @@ -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) { diff --git a/src/workbench/browser/src/app/shared/components/setting/common/language-swtcher.componet.ts b/src/workbench/browser/src/app/shared/components/setting/common/language-swtcher.componet.ts index c41717188..0738a1c79 100644 --- a/src/workbench/browser/src/app/shared/components/setting/common/language-swtcher.componet.ts +++ b/src/workbench/browser/src/app/shared/components/setting/common/language-swtcher.componet.ts @@ -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: `
Language
- + 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 = 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); } } diff --git a/src/workbench/browser/src/app/shared/components/setting/setting.component.html b/src/workbench/browser/src/app/shared/components/setting/setting.component.html index 132496376..2910c42fe 100644 --- a/src/workbench/browser/src/app/shared/components/setting/setting.component.html +++ b/src/workbench/browser/src/app/shared/components/setting/setting.component.html @@ -44,8 +44,8 @@ - - + + diff --git a/src/workbench/browser/src/app/shared/components/setting/setting.component.ts b/src/workbench/browser/src/app/shared/components/setting/setting.component.ts index 4993564f6..ee7f06de8 100644 --- a/src/workbench/browser/src/app/shared/components/setting/setting.component.ts +++ b/src/workbench/browser/src/app/shared/components/setting/setting.component.ts @@ -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); } diff --git a/src/workbench/browser/src/app/shared/services/remote/remote.service.ts b/src/workbench/browser/src/app/shared/services/remote/remote.service.ts index 74a779e16..77b60a047 100644 --- a/src/workbench/browser/src/app/shared/services/remote/remote.service.ts +++ b/src/workbench/browser/src/app/shared/services/remote/remote.service.ts @@ -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 = new Subject(); /** 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 */ diff --git a/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts b/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts index 25f254b47..442fd93d6 100644 --- a/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts +++ b/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts @@ -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 = { 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'; diff --git a/src/workbench/browser/src/app/shared/services/storage/storage.service.ts b/src/workbench/browser/src/app/shared/services/storage/storage.service.ts index 902605d4f..596f9ccad 100644 --- a/src/workbench/browser/src/app/shared/services/storage/storage.service.ts +++ b/src/workbench/browser/src/app/shared/services/storage/storage.service.ts @@ -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)); + } }