From 54ee1309c0572bdaade8e628a10871ae29f02569 Mon Sep 17 00:00:00 2001 From: scarqin <1054139596@qq.com> Date: Wed, 8 Mar 2023 21:48:48 +0800 Subject: [PATCH] fix: enable disable extension error --- src/workbench/browser/src/app/app.module.ts | 7 +++-- .../feature-control.service.ts | 11 ++++++-- .../app/core/services/theme/theme.service.ts | 1 + .../chatgpt-robot/chatgpt-robot.component.ts | 8 ++---- .../workspace/project/api/api.component.ts | 1 + .../services/extensions/extension.service.ts | 28 +++++++++++-------- .../extensions/webExtension.service.ts | 16 +---------- 7 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/workbench/browser/src/app/app.module.ts b/src/workbench/browser/src/app/app.module.ts index e3d8dbfd4..96a121a84 100644 --- a/src/workbench/browser/src/app/app.module.ts +++ b/src/workbench/browser/src/app/app.module.ts @@ -89,8 +89,6 @@ export class AppModule { private notification: NotificationService ) { this.init(); - //! Feature show init before extensionService init - this.feature.init(); } async init() { //* Init language @@ -98,6 +96,9 @@ export class AppModule { this.lang.init(); } + //* Init feature before extension install + this.feature.init(); + //* Inject extension global data this.global.injectGlobalData(); //* Init local mock server @@ -108,10 +109,10 @@ export class AppModule { //* Init Extension await this.extensionService.init(); this.theme.queryExtensionThemes(); + //*Reset theme after theme/extension theme loading Promise.all([promiseSystem]).then(() => { this.theme.afterAllThemeLoad(); - this.theme.watchInstalledExtensionsChange(); }); //* Init notification diff --git a/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts b/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts index 41acd7e88..0549cab63 100644 --- a/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts +++ b/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts @@ -8,9 +8,10 @@ type configKey = keyof typeof featureJSON; }) export class FeatureControlService { config: { [key: configKey | string]: boolean }; - constructor(private message: MessageService) {} - init() { + constructor(private message: MessageService) { this.config = featureJSON; + } + init() { this.watchExtensionChange(); } watchExtensionChange() { @@ -18,7 +19,11 @@ export class FeatureControlService { if (inArg.type !== 'extensionsChange') return; const extension = inArg.data.extension; if (!extension?.features?.featureControl?.length) return; - switch (inArg.data.action) { + let aciton = inArg.data.action; + if (inArg.data.action === 'init') { + aciton = extension.enable ? 'enable' : 'disable'; + } + switch (aciton) { case 'install': case 'enable': { this.openFearure(extension?.features?.featureControl); diff --git a/src/workbench/browser/src/app/core/services/theme/theme.service.ts b/src/workbench/browser/src/app/core/services/theme/theme.service.ts index a248d7196..932dc7e44 100644 --- a/src/workbench/browser/src/app/core/services/theme/theme.service.ts +++ b/src/workbench/browser/src/app/core/services/theme/theme.service.ts @@ -31,6 +31,7 @@ export class ThemeService { ) { this.currentThemeID = this.setting.get('workbench.colorTheme') || this.defaultTheme; this.coreThemes = this.getCoreThemes(); + this.watchInstalledExtensionsChange(); } async initTheme() { await this.querySystemThemes(); diff --git a/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts b/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts index 63552bac5..19c5c593a 100644 --- a/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts +++ b/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts @@ -77,7 +77,7 @@ type messageItem = { export class ChatgptRobotComponent implements OnInit { title = $localize`ChatGPT Robot`; loading = false; - MAX_LIMIT = 10; + MAX_LIMIT = 5; nowUsage = StorageUtil.get('cr_usage'); initMessage = { date: new Date(), @@ -103,9 +103,7 @@ export class ChatgptRobotComponent implements OnInit { private store: StoreService ) {} ngOnInit() { - setTimeout(() => { - this.watchExtensionChange(); - }, 5000); + this.watchExtensionChange(); } login() { window.open(APP_CONFIG.GITHUB_REPO_URL, '_blank'); @@ -134,7 +132,7 @@ export class ChatgptRobotComponent implements OnInit { this.loading = false; if (!res?.result) { this.messages.push({ - text: `ChatGPT Error:${res?.msg}`, + text: `ChatGPT Error: ${res?.error || res?.msg || 'unknown error'}`, date: new Date(), reply: true, type: 'text', diff --git a/src/workbench/browser/src/app/pages/workspace/project/api/api.component.ts b/src/workbench/browser/src/app/pages/workspace/project/api/api.component.ts index 8f07ef037..943554112 100644 --- a/src/workbench/browser/src/app/pages/workspace/project/api/api.component.ts +++ b/src/workbench/browser/src/app/pages/workspace/project/api/api.component.ts @@ -108,6 +108,7 @@ export class ApiComponent implements OnInit, OnDestroy { } async initExtensionExtra() { this.rightExtras = []; + if (!this.router.url.includes('home/workspace/project/api/http/detail')) return; const apiPreviewTab = this.extensionService.getValidExtensionsByFature('apiPreviewTab'); apiPreviewTab?.forEach(async (value, key) => { const module = await this.extensionService.getExtensionPackage(key); diff --git a/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts b/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts index fcdbf72dc..825ec36b5 100644 --- a/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts +++ b/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts @@ -5,6 +5,7 @@ import { LanguageService } from 'eo/workbench/browser/src/app/core/services/lang import { DISABLE_EXTENSION_NAMES } from 'eo/workbench/browser/src/app/shared/constants/storageKeys'; import { FeatureInfo, ExtensionInfo, SidebarView } from 'eo/workbench/browser/src/app/shared/models/extension-manager'; import { MessageService } from 'eo/workbench/browser/src/app/shared/services/message'; +import StorageUtil from 'eo/workbench/browser/src/app/utils/storage/storage.utils'; import { APP_CONFIG } from 'eo/workbench/browser/src/environments/environment'; import { lastValueFrom, Subscription } from 'rxjs'; @@ -39,6 +40,7 @@ export class ExtensionService { this.updateInstalledInfo(this.getExtensions(), { action: 'init' }); + return; } //* Web Installl @@ -52,9 +54,12 @@ export class ExtensionService { const uniqueNames = [...Array.from(new Set(installedName)), ...this.webExtensionService.debugExtensionNames]; for (let i = 0; i < uniqueNames.length; i++) { const name = uniqueNames[i]; - await this.installExtension({ - name - }); + await this.installExtension( + { + name + }, + true + ); } } getExtension(name: string) { @@ -173,14 +178,17 @@ export class ExtensionService { * install extension by id * * @param id + * @param isInit first time install * @returns if install success */ - async installExtension({ name, version = 'latest' }): Promise { + async installExtension({ name, version = 'latest' }, isInit = false): Promise { const successCallback = () => { this.updateInstalledInfo(this.getExtensions(), { - action: 'install', - name + action: isInit ? 'init' : 'install', + name, + extension: this.installedList.find(val => val.name === name) }); + if (isInit) return; if (!this.isEnable(name)) { this.toggleEnableExtension(name, true); } @@ -254,7 +262,7 @@ export class ExtensionService { } private setDisabledExtension(arr: string[]) { - localStorage.setItem(DISABLE_EXTENSION_NAMES, JSON.stringify(arr)); + StorageUtil.set(DISABLE_EXTENSION_NAMES, arr); this.disabledExtensionNames = arr; } async getExtensionPackage(name: string): Promise { @@ -298,11 +306,7 @@ export class ExtensionService { return result; } private getDisableExtensionNames() { - try { - return JSON.parse(localStorage.getItem(DISABLE_EXTENSION_NAMES) || '[]'); - } catch (error) { - return []; - } + return StorageUtil.get(DISABLE_EXTENSION_NAMES) || []; } private async requestDetail(id) { const debugExtension = this.webExtensionService.debugExtensions.find(val => val.name === id); diff --git a/src/workbench/browser/src/app/shared/services/extensions/webExtension.service.ts b/src/workbench/browser/src/app/shared/services/extensions/webExtension.service.ts index 81d63db21..88b36cb13 100644 --- a/src/workbench/browser/src/app/shared/services/extensions/webExtension.service.ts +++ b/src/workbench/browser/src/app/shared/services/extensions/webExtension.service.ts @@ -1,7 +1,5 @@ import { Injectable } from '@angular/core'; -import { TranslateService } from 'eo/platform/common/i18n'; -import { DISABLE_EXTENSION_NAMES } from 'eo/workbench/browser/src/app/shared/constants/storageKeys'; -import { eoDeepCopy, JSONParse } from 'eo/workbench/browser/src/app/utils/index.utils'; +import { eoDeepCopy } from 'eo/workbench/browser/src/app/utils/index.utils'; import StorageUtil from 'eo/workbench/browser/src/app/utils/storage/storage.utils'; import { APP_CONFIG } from 'eo/workbench/browser/src/environments/environment'; @@ -141,18 +139,6 @@ export class WebExtensionService { return true; } - isEnable(name: string) { - return !this.getDisabledExtensionNames().includes(name); - } - - getDisabledExtensionNames() { - try { - return (this.disabledExtensionNames = JSON.parse(localStorage.getItem(DISABLE_EXTENSION_NAMES) || '[]')); - } catch (error) { - return []; - } - } - insertScript(scriptText) { const script = document.createElement('script'); script.type = 'text/javascript';