diff --git a/src/zotero_kb/templates/index.html b/src/zotero_kb/templates/index.html
index ac59792..708d0cf 100644
--- a/src/zotero_kb/templates/index.html
+++ b/src/zotero_kb/templates/index.html
@@ -436,7 +436,6 @@
selectDescendantsButton: document.getElementById("modal-select-descendants-button"),
clearSelectionButton: document.getElementById("modal-clear-selection-button"),
importSelectedItemsButton: document.getElementById("modal-import-selected-items-button"),
- previewPopover: document.getElementById("item-preview-popover"),
currentProjectLabel: document.getElementById("current-project-label"),
cardList: document.getElementById("card-list"),
cardDetail: document.getElementById("card-detail"),
@@ -613,6 +612,24 @@
return lookup;
}
+ function normalizeSelectedCollectionKey() {
+ if (!state.collectionTree.length) {
+ state.selectedCollectionKey = null;
+ state.expandedCollectionKeys.clear();
+ persistImportSessionState();
+ return null;
+ }
+ const parentLookup = collectCollectionParents(state.collectionTree);
+ if (!state.selectedCollectionKey || !parentLookup.has(state.selectedCollectionKey)) {
+ state.selectedCollectionKey = state.collectionTree[0].collection_key;
+ }
+ state.expandedCollectionKeys = new Set(
+ Array.from(state.expandedCollectionKeys).filter((key) => parentLookup.has(key))
+ );
+ persistImportSessionState();
+ return state.selectedCollectionKey;
+ }
+
function ensureSelectedCollectionExpanded() {
if (!state.selectedCollectionKey) {
return;
@@ -733,9 +750,7 @@
setStatus(elements.collectionStatus, "正在加载 collection...");
const payload = await api("/api/zotero/collections/tree");
state.collectionTree = Array.isArray(payload.collections) ? payload.collections : [];
- if (!state.selectedCollectionKey && state.collectionTree.length) {
- state.selectedCollectionKey = state.collectionTree[0].collection_key;
- }
+ normalizeSelectedCollectionKey();
ensureSelectedCollectionExpanded();
persistImportSessionState();
renderCollectionTree();
@@ -750,6 +765,16 @@
async function loadCollectionItems(collectionKey) {
state.selectedCollectionKey = collectionKey;
+ if (normalizeSelectedCollectionKey() !== collectionKey) {
+ state.visibleCollectionItems = [];
+ renderCollectionTree();
+ renderCollectionItems();
+ setStatus(elements.collectionStatus, state.selectedCollectionKey ? "已切换到可用 collection。" : "");
+ if (state.selectedCollectionKey) {
+ await loadCollectionItems(state.selectedCollectionKey);
+ }
+ return;
+ }
ensureSelectedCollectionExpanded();
persistImportSessionState();
renderCollectionTree();
@@ -815,8 +840,6 @@
elements.importModal.hidden = false;
elements.importModal.setAttribute("aria-hidden", "false");
document.body.classList.toggle("modal-open", true);
- elements.previewPopover.hidden = true;
- elements.previewPopover.textContent = "";
updateImportActionState();
if (!state.collectionTree.length) {
try {
@@ -841,8 +864,6 @@
elements.importModal.hidden = true;
elements.importModal.setAttribute("aria-hidden", "true");
document.body.classList.toggle("modal-open", false);
- elements.previewPopover.hidden = true;
- elements.previewPopover.textContent = "";
}
async function loadProjects() {
diff --git a/tests/test_ui.py b/tests/test_ui.py
index a576122..eae301e 100644
--- a/tests/test_ui.py
+++ b/tests/test_ui.py
@@ -45,5 +45,5 @@ def test_index_contains_import_modal_controls(tmp_path) -> None:
assert "function openImportModal()" in html
assert "function closeImportModal()" in html
assert "function updateImportActionState()" in html
+ assert "function normalizeSelectedCollectionKey()" in html
assert 'document.body.classList.toggle("modal-open"' in html
- assert "${item.abstract" not in html