forked from KaotoIO/kaoto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vscode): Provide SettingsAdapter for VSCode
At the moment, the Kaoto settings are passed individually to VSCode, meaning that in case we want to add new settings, we need to pass individual parameters to the `KaotoEditorApp` class. This commit provides a SettingsAdapter so every new setting will be added directly to the adapter. relates: https://issues.redhat.com/browse/KTO-441 relates: KaotoIO#1221
- Loading branch information
Showing
11 changed files
with
130 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
/** Models components */ | ||
export * from './models'; | ||
/** | ||
* Models components | ||
* | ||
* This file shouldn't export anything other than models, for instance, no components, no hooks, etc. | ||
*/ | ||
export * from './models/settings'; |
This file was deleted.
Oops, something went wrong.
18 changes: 10 additions & 8 deletions
18
packages/ui/src/models/settings/default-settings-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { ROOT_PATH, setValue } from '../../utils'; | ||
import { AbstractSettingsAdapter } from './abstract-settings-adapter'; | ||
import { SettingsModel } from './settings.model'; | ||
import { AbstractSettingsAdapter, ISettingsModel, SettingsModel } from './settings.model'; | ||
|
||
export class DefaultSettingsAdapter extends AbstractSettingsAdapter { | ||
private readonly defaultSettings = new SettingsModel(); | ||
export class DefaultSettingsAdapter implements AbstractSettingsAdapter { | ||
private settings: ISettingsModel; | ||
|
||
constructor(settings?: Partial<ISettingsModel>) { | ||
this.settings = new SettingsModel(settings); | ||
} | ||
|
||
getSettings() { | ||
return this.defaultSettings; | ||
return this.settings; | ||
} | ||
|
||
saveSettings(settings: SettingsModel) { | ||
setValue(this.defaultSettings, ROOT_PATH, settings); | ||
saveSettings(settings: ISettingsModel) { | ||
Object.assign(this.settings, settings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './abstract-settings-adapter'; | ||
export * from './default-settings-adapter'; | ||
export * from './settings.model'; |
39 changes: 39 additions & 0 deletions
39
packages/ui/src/models/settings/localstorage-settings-adapter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { LocalStorageKeys } from '../local-storage-keys'; | ||
import { LocalStorageSettingsAdapter } from './localstorage-settings-adapter'; | ||
import { SettingsModel } from './settings.model'; | ||
|
||
describe('LocalStorageSettingsAdapter', () => { | ||
it('should create an instance with the default settings', () => { | ||
const adapter = new LocalStorageSettingsAdapter(); | ||
|
||
expect(adapter.getSettings()).toEqual(new SettingsModel()); | ||
}); | ||
|
||
it('should save and retrieve settings', () => { | ||
const adapter = new LocalStorageSettingsAdapter(); | ||
const newSettings: SettingsModel = { catalogUrl: 'http://example.com' }; | ||
|
||
adapter.saveSettings(newSettings); | ||
|
||
expect(adapter.getSettings()).toEqual(newSettings); | ||
}); | ||
|
||
it('should retrieve the saved settings from localStorage after creating a new instance', () => { | ||
const localStorageGetItemSpy = jest.spyOn(Storage.prototype, 'getItem'); | ||
|
||
new LocalStorageSettingsAdapter(); | ||
|
||
expect(localStorageGetItemSpy).toHaveBeenCalledWith(LocalStorageKeys.Settings); | ||
}); | ||
|
||
it('should save the settings to localStorage', () => { | ||
const localStorageSetItemSpy = jest.spyOn(Storage.prototype, 'setItem'); | ||
|
||
const adapter = new LocalStorageSettingsAdapter(); | ||
const newSettings: SettingsModel = { catalogUrl: 'http://example.com' }; | ||
|
||
adapter.saveSettings(newSettings); | ||
|
||
expect(localStorageSetItemSpy).toHaveBeenCalledWith(LocalStorageKeys.Settings, JSON.stringify(newSettings)); | ||
}); | ||
}); |
14 changes: 6 additions & 8 deletions
14
packages/ui/src/models/settings/localstorage-settings-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { LocalStorageKeys } from '../local-storage-keys'; | ||
import { AbstractSettingsAdapter } from './abstract-settings-adapter'; | ||
import { SettingsModel } from './settings.model'; | ||
import { AbstractSettingsAdapter, ISettingsModel, SettingsModel } from './settings.model'; | ||
|
||
export class LocalStorageSettingsAdapter extends AbstractSettingsAdapter { | ||
private readonly settings: SettingsModel; | ||
export class LocalStorageSettingsAdapter implements AbstractSettingsAdapter { | ||
private settings: ISettingsModel; | ||
|
||
constructor() { | ||
super(); | ||
|
||
const rawSettings = localStorage.getItem(LocalStorageKeys.Settings) ?? '{}'; | ||
const parsedSettings = JSON.parse(rawSettings); | ||
this.settings = new SettingsModel(parsedSettings); | ||
} | ||
|
||
getSettings(): SettingsModel { | ||
getSettings(): ISettingsModel { | ||
return this.settings; | ||
} | ||
|
||
saveSettings(settings: SettingsModel): void { | ||
saveSettings(settings: ISettingsModel): void { | ||
localStorage.setItem(LocalStorageKeys.Settings, JSON.stringify(settings)); | ||
this.settings = { ...settings }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
const DEFAULT_SETTINGS: SettingsModel = { | ||
catalogUrl: '', | ||
}; | ||
export interface ISettingsModel { | ||
catalogUrl: string; | ||
} | ||
|
||
export interface AbstractSettingsAdapter { | ||
getSettings(): ISettingsModel; | ||
saveSettings(settings: ISettingsModel): void; | ||
} | ||
|
||
export class SettingsModel { | ||
export class SettingsModel implements ISettingsModel { | ||
catalogUrl: string = ''; | ||
|
||
constructor(options: Partial<SettingsModel> = {}) { | ||
Object.assign(this, DEFAULT_SETTINGS, options); | ||
constructor(options: Partial<ISettingsModel> = {}) { | ||
Object.assign(this, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/ui/src/multiplying-architecture/KaotoEditorChannelApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { KogitoEditorChannelApi } from '@kie-tools-core/editor/dist/api'; | ||
import { ISettingsModel } from '../models/settings'; | ||
|
||
export interface KaotoEditorChannelApi extends KogitoEditorChannelApi { | ||
/** | ||
* @deprecated Use `getVSCodeKaotoSettings` instead | ||
* Returns the URL of the catalog. | ||
*/ | ||
getCatalogURL(): Promise<string | undefined>; | ||
|
||
/** | ||
* Returns the Kaoto VSCode settings defined. | ||
*/ | ||
getVSCodeKaotoSettings(): Promise<ISettingsModel>; | ||
} |
25 changes: 20 additions & 5 deletions
25
packages/ui/src/multiplying-architecture/KaotoEditorFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
import { KaotoEditorApp } from './KaotoEditorApp'; | ||
import { | ||
Editor, | ||
EditorFactory, | ||
EditorInitArgs, | ||
KogitoEditorEnvelopeContextType, | ||
} from '@kie-tools-core/editor/dist/api'; | ||
import { KaotoEditorChannelApi } from './KaotoEditorChannelApi'; | ||
import { DefaultSettingsAdapter } from '../models'; | ||
import { CatalogSchemaLoader, isDefined } from '../utils'; | ||
import { KaotoEditorApp } from './KaotoEditorApp'; | ||
import { KaotoEditorChannelApi } from './KaotoEditorChannelApi'; | ||
|
||
export class KaotoEditorFactory implements EditorFactory<Editor, KaotoEditorChannelApi> { | ||
public async createEditor( | ||
envelopeContext: KogitoEditorEnvelopeContextType<KaotoEditorChannelApi>, | ||
initArgs: EditorInitArgs, | ||
): Promise<Editor> { | ||
let catalogUrl = await envelopeContext.channelApi.requests.getCatalogURL(); | ||
const settings = await envelopeContext.channelApi.requests.getVSCodeKaotoSettings(); | ||
const settingsAdapter = new DefaultSettingsAdapter(settings); | ||
this.updateCatalogUrl(settingsAdapter, initArgs); | ||
|
||
return Promise.resolve(new KaotoEditorApp(envelopeContext, initArgs, settingsAdapter)); | ||
} | ||
|
||
/** | ||
* Updates the catalog URL in the settings if it is not defined, to include the embedded catalog. | ||
* It uses the resourcesPathPrefix from the initArgs to build the default catalog URL. | ||
* | ||
* @param settingsAdapter The settings adapter to update the catalog URL | ||
* @param initArgs The init args to get the resources path prefix | ||
*/ | ||
private updateCatalogUrl(settingsAdapter: DefaultSettingsAdapter, initArgs: EditorInitArgs) { | ||
let catalogUrl = settingsAdapter.getSettings().catalogUrl; | ||
|
||
if (!isDefined(catalogUrl) || catalogUrl === '') { | ||
catalogUrl = `${initArgs.resourcesPathPrefix}${CatalogSchemaLoader.DEFAULT_CATALOG_PATH.replace('.', '')}`; | ||
settingsAdapter.saveSettings({ ...settingsAdapter.getSettings(), catalogUrl }); | ||
} | ||
|
||
return Promise.resolve(new KaotoEditorApp(envelopeContext, initArgs, catalogUrl)); | ||
} | ||
} |