fix: multi-selection in note picker

This commit is contained in:
windingwind 2024-12-13 13:17:52 +01:00
parent 95a0f4916d
commit 6bfae7376c

View File

@ -36,6 +36,10 @@ export class NotePicker extends PluginCEBase {
_cachedLibraryIDs: number[] = []; _cachedLibraryIDs: number[] = [];
_cachedSelectedNoteIDs: number[] = [];
_disableSelectionChange = false;
get content() { get content() {
return MozXULElement.parseXULToFragment(` return MozXULElement.parseXULToFragment(`
<linkset> <linkset>
@ -121,8 +125,8 @@ export class NotePicker extends PluginCEBase {
} }
destroy(): void { destroy(): void {
this.collectionsView.unregister(); this.collectionsView?.unregister();
if (this.itemsView) this.itemsView.unregister(); this.itemsView?.unregister();
unregisterPrefObserver(this._prefObserverID); unregisterPrefObserver(this._prefObserverID);
} }
@ -395,6 +399,9 @@ export class NotePicker extends PluginCEBase {
} }
onItemSelected() { onItemSelected() {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "library"; this.activeSelectionType = "library";
const selectedIDs = this.itemsView.getSelectedItems(true) as number[]; const selectedIDs = this.itemsView.getSelectedItems(true) as number[];
// Compare the selected IDs with the cached IDs // Compare the selected IDs with the cached IDs
@ -402,19 +409,48 @@ export class NotePicker extends PluginCEBase {
if (arraysEqual(this._cachedLibraryIDs, selectedIDs)) { if (arraysEqual(this._cachedLibraryIDs, selectedIDs)) {
return; return;
} }
this.deselectOtherPanes();
this.dispatchSelectionChange(); this.dispatchSelectionChange();
} }
onOpenedNoteSelected(selection: { selected: Set<number> }) { onOpenedNoteSelected(selection: { selected: Set<number> }) {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "tabs"; this.activeSelectionType = "tabs";
this.deselectOtherPanes();
this.dispatchSelectionChange(selection); this.dispatchSelectionChange(selection);
} }
onRecentNoteSelected(selection: { selected: Set<number> }) { onRecentNoteSelected(selection: { selected: Set<number> }) {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "recent"; this.activeSelectionType = "recent";
this.deselectOtherPanes();
this.dispatchSelectionChange(selection); this.dispatchSelectionChange(selection);
} }
deselectItemsPane() {
this.itemsView?.selection?.clearSelection();
}
deselectOpenedNotePane() {
this.openedNotesView?.treeInstance?.selection?.clearSelection();
}
deselectRecentNotePane() {
this.recentNotesView?.treeInstance?.selection?.clearSelection();
}
deselectOtherPanes() {
this._disableSelectionChange = true;
if (this.activeSelectionType !== "library") this.deselectItemsPane();
if (this.activeSelectionType !== "tabs") this.deselectOpenedNotePane();
if (this.activeSelectionType !== "recent") this.deselectRecentNotePane();
this._disableSelectionChange = false;
}
getRecentNotes() { getRecentNotes() {
return ((getPref("linkCreator.recentNotes") as string) || "") return ((getPref("linkCreator.recentNotes") as string) || "")
.split(",") .split(",")
@ -438,13 +474,23 @@ export class NotePicker extends PluginCEBase {
} }
dispatchSelectionChange(selection?: { selected: Set<number> }) { dispatchSelectionChange(selection?: { selected: Set<number> }) {
if (this._disableSelectionChange) {
return false;
}
const selectedNotes = this.getSelectedNotes(selection);
const selectedNoteIDs = selectedNotes.map((n) => n.id);
if (arraysEqual(this._cachedSelectedNoteIDs, selectedNoteIDs)) {
return false;
}
this._cachedSelectedNoteIDs = selectedNoteIDs;
this.dispatchEvent( this.dispatchEvent(
new CustomEvent("selectionchange", { new CustomEvent("selectionchange", {
detail: { detail: {
selectedNotes: this.getSelectedNotes(selection), selectedNotes,
}, },
}), }),
); );
return true;
} }
getSelectedNotes(selection?: { selected: Set<number> }): Zotero.Item[] { getSelectedNotes(selection?: { selected: Set<number> }): Zotero.Item[] {