Skip to content

Commit

Permalink
fix: enable disable extension error
Browse files Browse the repository at this point in the history
  • Loading branch information
scarqin committed Mar 8, 2023
1 parent e095a18 commit 54ee130
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
7 changes: 4 additions & 3 deletions src/workbench/browser/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ export class AppModule {
private notification: NotificationService
) {
this.init();
//! Feature show init before extensionService init
this.feature.init();
}
async init() {
//* Init language
if (APP_CONFIG.production) {
this.lang.init();
}

//* Init feature before extension install
this.feature.init();

//* Inject extension global data
this.global.injectGlobalData();
//* Init local mock server
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ 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() {
this.message.get().subscribe((inArg: Message) => {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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');
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -39,6 +40,7 @@ export class ExtensionService {
this.updateInstalledInfo(this.getExtensions(), {
action: 'init'
});
return;
}

//* Web Installl
Expand All @@ -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) {
Expand Down Expand Up @@ -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<boolean> {
async installExtension({ name, version = 'latest' }, isInit = false): Promise<boolean> {
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);
}
Expand Down Expand Up @@ -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<any> {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 54ee130

Please sign in to comment.