Skip to content

Commit febaa98

Browse files
committed
Refactor item selection
1 parent 94ba529 commit febaa98

File tree

5 files changed

+279
-174
lines changed

5 files changed

+279
-174
lines changed

packages/base/src/commands/index.ts

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,7 @@ export function addCommands(
508508
label: trans.__('Rename Layer'),
509509
execute: async () => {
510510
const model = tracker.currentWidget?.model;
511-
await Private.renameSelectedItem(model, 'layer', (layerId, newName) => {
512-
const layer = model?.getLayer(layerId);
513-
if (layer) {
514-
layer.name = newName;
515-
model?.sharedModel.updateLayer(layerId, layer);
516-
}
517-
});
511+
await Private.renameSelectedItem(model, 'layer');
518512
},
519513
});
520514

@@ -532,9 +526,7 @@ export function addCommands(
532526
label: trans.__('Rename Group'),
533527
execute: async () => {
534528
const model = tracker.currentWidget?.model;
535-
await Private.renameSelectedItem(model, 'group', (groupName, newName) => {
536-
model?.renameLayerGroup(groupName, newName);
537-
});
529+
await Private.renameSelectedItem(model, 'group');
538530
},
539531
});
540532

@@ -635,13 +627,7 @@ export function addCommands(
635627
label: trans.__('Rename Source'),
636628
execute: async () => {
637629
const model = tracker.currentWidget?.model;
638-
await Private.renameSelectedItem(model, 'source', (sourceId, newName) => {
639-
const source = model?.getSource(sourceId);
640-
if (source) {
641-
source.name = newName;
642-
model?.sharedModel.updateSource(sourceId, source);
643-
}
644-
});
630+
await Private.renameSelectedItem(model, 'source');
645631
},
646632
});
647633

@@ -1146,39 +1132,6 @@ namespace Private {
11461132
};
11471133
}
11481134

1149-
export async function getUserInputForRename(
1150-
text: HTMLElement,
1151-
input: HTMLInputElement,
1152-
original: string,
1153-
): Promise<string> {
1154-
const parent = text.parentElement as HTMLElement;
1155-
parent.replaceChild(input, text);
1156-
input.value = original;
1157-
input.select();
1158-
input.focus();
1159-
1160-
return new Promise<string>(resolve => {
1161-
input.addEventListener('blur', () => {
1162-
parent.replaceChild(text, input);
1163-
resolve(input.value);
1164-
});
1165-
1166-
input.addEventListener('keydown', (event: KeyboardEvent) => {
1167-
if (event.key === 'Enter') {
1168-
event.stopPropagation();
1169-
event.preventDefault();
1170-
input.blur();
1171-
} else if (event.key === 'Escape') {
1172-
event.stopPropagation();
1173-
event.preventDefault();
1174-
input.value = original;
1175-
input.blur();
1176-
text.focus();
1177-
}
1178-
});
1179-
});
1180-
}
1181-
11821135
export function removeSelectedItems(
11831136
model: IJupyterGISModel | undefined,
11841137
itemTypeToRemove: SelectionType,
@@ -1201,11 +1154,10 @@ namespace Private {
12011154
export async function renameSelectedItem(
12021155
model: IJupyterGISModel | undefined,
12031156
itemType: SelectionType,
1204-
callback: (itemId: string, newName: string) => void,
12051157
) {
12061158
const selectedItems = model?.localState?.selected.value;
12071159

1208-
if (!selectedItems) {
1160+
if (!selectedItems || !model) {
12091161
console.error(`No ${itemType} selected`);
12101162
return;
12111163
}
@@ -1224,34 +1176,8 @@ namespace Private {
12241176
return;
12251177
}
12261178

1227-
const nodeId = selectedItems[itemId].selectedNodeId;
1228-
if (!nodeId) {
1229-
return;
1230-
}
1231-
1232-
const node = document.getElementById(nodeId);
1233-
if (!node) {
1234-
console.warn(`Node with ID ${nodeId} not found`);
1235-
return;
1236-
}
1237-
1238-
const edit = document.createElement('input');
1239-
edit.classList.add('jp-gis-left-panel-input');
1240-
const originalName = node.innerText;
1241-
const newName = await Private.getUserInputForRename(
1242-
node,
1243-
edit,
1244-
originalName,
1245-
);
1246-
1247-
if (!newName) {
1248-
console.warn('New name cannot be empty');
1249-
return;
1250-
}
1251-
1252-
if (newName !== originalName) {
1253-
callback(itemId, newName);
1254-
}
1179+
// Set editing state - component will show inline input
1180+
model.setEditingItem(itemType, itemId);
12551181
}
12561182

12571183
export function executeConsole(tracker: JupyterGISTracker): void {

0 commit comments

Comments
 (0)