Skip to content

Commit

Permalink
fix: multi-selection in note picker
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 13, 2024
1 parent 95a0f49 commit 6bfae73
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/elements/linkCreator/notePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class NotePicker extends PluginCEBase {

_cachedLibraryIDs: number[] = [];

_cachedSelectedNoteIDs: number[] = [];

_disableSelectionChange = false;

get content() {
return MozXULElement.parseXULToFragment(`
<linkset>
Expand Down Expand Up @@ -121,8 +125,8 @@ export class NotePicker extends PluginCEBase {
}

destroy(): void {
this.collectionsView.unregister();
if (this.itemsView) this.itemsView.unregister();
this.collectionsView?.unregister();
this.itemsView?.unregister();
unregisterPrefObserver(this._prefObserverID);
}

Expand Down Expand Up @@ -395,26 +399,58 @@ export class NotePicker extends PluginCEBase {
}

onItemSelected() {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "library";
const selectedIDs = this.itemsView.getSelectedItems(true) as number[];
// Compare the selected IDs with the cached IDs
// Since the library selection change can be triggered multiple times or with no change
if (arraysEqual(this._cachedLibraryIDs, selectedIDs)) {
return;
}
this.deselectOtherPanes();
this.dispatchSelectionChange();
}

onOpenedNoteSelected(selection: { selected: Set<number> }) {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "tabs";
this.deselectOtherPanes();
this.dispatchSelectionChange(selection);
}

onRecentNoteSelected(selection: { selected: Set<number> }) {
if (this._disableSelectionChange) {
return;
}
this.activeSelectionType = "recent";
this.deselectOtherPanes();
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() {
return ((getPref("linkCreator.recentNotes") as string) || "")
.split(",")
Expand All @@ -438,13 +474,23 @@ export class NotePicker extends PluginCEBase {
}

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(
new CustomEvent("selectionchange", {
detail: {
selectedNotes: this.getSelectedNotes(selection),
selectedNotes,
},
}),
);
return true;
}

getSelectedNotes(selection?: { selected: Set<number> }): Zotero.Item[] {
Expand Down

0 comments on commit 6bfae73

Please sign in to comment.