From fbe439dfc05e0b10adf455e22cbfd1eb1df3dae6 Mon Sep 17 00:00:00 2001 From: Greg Mooney Date: Wed, 8 Jan 2025 11:01:22 +0100 Subject: [PATCH] Refactor layer tree updates (#284) * Refactor layer moving * Implement suggestions --- .../base/src/panelview/components/layers.tsx | 3 +- packages/schema/src/model.ts | 35 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/base/src/panelview/components/layers.tsx b/packages/base/src/panelview/components/layers.tsx index a6c5f317..18525f9d 100644 --- a/packages/base/src/panelview/components/layers.tsx +++ b/packages/base/src/panelview/components/layers.tsx @@ -247,7 +247,8 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element { const groupState = await state.fetch(`jupytergis:${group.name}`); setOpen( - ((groupState as ReadonlyPartialJSONObject).expanded as boolean) ?? false + ((groupState as ReadonlyPartialJSONObject)?.expanded as boolean) ?? + false ); }; diff --git a/packages/schema/src/model.ts b/packages/schema/src/model.ts index 3964e8b7..41ef96ab 100644 --- a/packages/schema/src/model.ts +++ b/packages/schema/src/model.ts @@ -523,46 +523,41 @@ export class JupyterGISModel implements IJupyterGISModel { layerTree: IJGISLayerItem[], layerIdToRemove: string ) { - // Iterate over each item in the layerTree - for (let i = 0; i < layerTree.length; i++) { - const currentItem = layerTree[i]; - - // Check if the current item is a string and matches the target - if (typeof currentItem === 'string' && currentItem === layerIdToRemove) { - // Remove the item from the array - layerTree.splice(i, 1); - // Decrement i to ensure the next iteration processes the remaining items correctly - i--; - } else if (typeof currentItem !== 'string' && 'layers' in currentItem) { - // If the current item is a group, recursively call the function on its layers - this._removeLayerTreeLayer(currentItem.layers, layerIdToRemove); - } - } - + this._removeLayerTreeItem(layerTree, layerIdToRemove, true); this.sharedModel.layerTree = layerTree; } private _removeLayerTreeGroup( layerTree: IJGISLayerItem[], groupName: string + ) { + this._removeLayerTreeItem(layerTree, groupName, false); + this.sharedModel.layerTree = layerTree; + } + + private _removeLayerTreeItem( + layerTree: IJGISLayerItem[], + target: string, + isLayer: boolean ) { // Iterate over each item in the layerTree for (let i = 0; i < layerTree.length; i++) { const currentItem = layerTree[i]; + const matches = isLayer + ? typeof currentItem === 'string' && currentItem === target + : typeof currentItem !== 'string' && currentItem.name === target; // Check if the current item is a string and matches the target - if (typeof currentItem !== 'string' && currentItem.name === groupName) { + if (matches) { // Remove the item from the array layerTree.splice(i, 1); // Decrement i to ensure the next iteration processes the remaining items correctly i--; } else if (typeof currentItem !== 'string' && 'layers' in currentItem) { // If the current item is a group, recursively call the function on its layers - this._removeLayerTreeGroup(currentItem.layers, groupName); + this._removeLayerTreeItem(currentItem.layers, target, isLayer); } } - - this.sharedModel.layerTree = layerTree; } renameLayerGroup(groupName: string, newName: string): void {