diff --git a/packages/schema/src/model.ts b/packages/schema/src/model.ts index 242b77efc..be5042a23 100644 --- a/packages/schema/src/model.ts +++ b/packages/schema/src/model.ts @@ -41,6 +41,18 @@ import { Modes } from './types'; const SETTINGS_ID = '@jupytergis/jupytergis-core:jupytergis-settings'; +const DEFAULT_SETTINGS: IJupyterGISSettings = { + proxyUrl: 'https://corsproxy.io', + leftPanelDisabled: false, + rightPanelDisabled: false, + layersDisabled: false, + stacBrowserDisabled: false, + filtersDisabled: false, + objectPropertiesDisabled: false, + annotationsDisabled: false, + identifyDisabled: false, +}; + export class JupyterGISModel implements IJupyterGISModel { constructor(options: JupyterGISModel.IOptions) { const { annotationModel, sharedModel, settingRegistry } = options; @@ -61,17 +73,7 @@ export class JupyterGISModel implements IJupyterGISModel { this._pathChanged = new Signal(this); this._settingsChanged = new Signal(this); - this._jgisSettings = { - proxyUrl: 'https://corsproxy.io', - leftPanelDisabled: false, - rightPanelDisabled: false, - layersDisabled: false, - stacBrowserDisabled: false, - filtersDisabled: false, - objectPropertiesDisabled: false, - annotationsDisabled: false, - identifyDisabled: false, - }; + this._jgisSettings = { ...DEFAULT_SETTINGS }; this.initSettings(); } @@ -92,17 +94,9 @@ export class JupyterGISModel implements IJupyterGISModel { this._updateLocalSettings(); const newSettings = this._jgisSettings; - const keys: (keyof IJupyterGISSettings)[] = [ - 'proxyUrl', - 'leftPanelDisabled', - 'rightPanelDisabled', - 'layersDisabled', - 'stacBrowserDisabled', - 'filtersDisabled', - 'objectPropertiesDisabled', - 'annotationsDisabled', - 'identifyDisabled', - ]; + const keys = Object.keys( + DEFAULT_SETTINGS, + ) as (keyof IJupyterGISSettings)[]; keys.forEach(key => { if (oldSettings[key] !== newSettings[key]) { @@ -112,17 +106,7 @@ export class JupyterGISModel implements IJupyterGISModel { }); } catch (error) { console.error(`Failed to load settings for ${SETTINGS_ID}:`, error); - this._jgisSettings = { - proxyUrl: 'https://corsproxy.io', - leftPanelDisabled: false, - rightPanelDisabled: false, - layersDisabled: false, - stacBrowserDisabled: false, - filtersDisabled: false, - objectPropertiesDisabled: false, - annotationsDisabled: false, - identifyDisabled: false, - }; + this._jgisSettings = { ...DEFAULT_SETTINGS }; } } } @@ -130,18 +114,15 @@ export class JupyterGISModel implements IJupyterGISModel { private _updateLocalSettings(): void { const composite = this._settings.composite; - this._jgisSettings = { - proxyUrl: (composite.proxyUrl as string) ?? 'https://corsproxy.io', - leftPanelDisabled: (composite.leftPanelDisabled as boolean) ?? false, - rightPanelDisabled: (composite.rightPanelDisabled as boolean) ?? false, - layersDisabled: (composite.layersDisabled as boolean) ?? false, - stacBrowserDisabled: (composite.stacBrowserDisabled as boolean) ?? false, - filtersDisabled: (composite.filtersDisabled as boolean) ?? false, - objectPropertiesDisabled: - (composite.objectPropertiesDisabled as boolean) ?? false, - annotationsDisabled: (composite.annotationsDisabled as boolean) ?? false, - identifyDisabled: (composite.identifyDisabled as boolean) ?? false, - }; + this._jgisSettings = Object.entries(DEFAULT_SETTINGS).reduce( + (acc, [key, defaultValue]) => { + const typedKey = key as keyof IJupyterGISSettings; + const compositeValue = composite[typedKey]; + (acc as any)[typedKey] = compositeValue ?? defaultValue; + return acc; + }, + {} as typeof DEFAULT_SETTINGS, + ); } get jgisSettings(): IJupyterGISSettings {