fix: right-click should select note

This commit is contained in:
Antonella Sgarlatta
2021-05-07 12:50:28 -03:00
parent 181e1985fd
commit 8a9dc14d81

View File

@@ -78,6 +78,7 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
private searchKeyObserver: any private searchKeyObserver: any
private noteFlags: Partial<Record<UuidString, NoteFlag[]>> = {} private noteFlags: Partial<Record<UuidString, NoteFlag[]>> = {}
private removeObservers: Array<() => void> = []; private removeObservers: Array<() => void> = [];
private rightClickListeners: Map<UuidString, (e: MouseEvent) => void> = new Map();
/* @ngInject */ /* @ngInject */
constructor($timeout: ng.ITimeoutService,) { constructor($timeout: ng.ITimeoutService,) {
@@ -126,7 +127,7 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
deinit() { deinit() {
for (const remove of this.removeObservers) remove(); for (const remove of this.removeObservers) remove();
this.removeObservers.length = 0; this.removeObservers.length = 0;
this.removeAllContextMenuListeners(); this.removeRightClickListeners();
this.panelPuppet!.onReady = undefined; this.panelPuppet!.onReady = undefined;
this.panelPuppet = undefined; this.panelPuppet = undefined;
window.removeEventListener('resize', this.onWindowResize, true); window.removeEventListener('resize', this.onWindowResize, true);
@@ -297,8 +298,11 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
)); ));
} }
private openNotesContextMenu = (e: MouseEvent) => { private openNotesContextMenu(e: MouseEvent, note: SNNote) {
e.preventDefault(); e.preventDefault();
if (!this.state.selectedNotes[note.uuid]) {
this.selectNote(note);
}
this.application.getAppState().notes.setContextMenuPosition({ this.application.getAppState().notes.setContextMenuPosition({
top: e.clientY, top: e.clientY,
left: e.clientX, left: e.clientX,
@@ -306,38 +310,39 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
this.application.getAppState().notes.setContextMenuOpen(true); this.application.getAppState().notes.setContextMenuOpen(true);
} }
private removeAllContextMenuListeners = () => { private removeRightClickListeners() {
const { for (const [noteUuid, listener] of this.rightClickListeners.entries()) {
selectedNotes, document
selectedNotesCount, .getElementById(`note-${noteUuid}`)
} = this.application.getAppState().notes; ?.removeEventListener('contextmenu', listener);
if (selectedNotesCount > 0) {
Object.values(selectedNotes).forEach(({ uuid }) => {
document
.getElementById(`note-${uuid}`)
?.removeEventListener('contextmenu', this.openNotesContextMenu);
});
} }
}; this.rightClickListeners.clear();
}
private addContextMenuListeners = () => { private addRightClickListeners() {
const { for (const [noteUuid, listener] of this.rightClickListeners.entries()) {
selectedNotes, if (!this.state.renderedNotes.find(note => note.uuid === noteUuid)) {
selectedNotesCount,
} = this.application.getAppState().notes;
if (selectedNotesCount > 0) {
Object.values(selectedNotes).forEach(({ uuid }) => {
document document
.getElementById(`note-${uuid}`) .getElementById(`note-${noteUuid}`)
?.addEventListener('contextmenu', this.openNotesContextMenu); ?.removeEventListener('contextmenu', listener);
}); this.rightClickListeners.delete(noteUuid);
}
}
for (const note of this.state.renderedNotes) {
if (!this.rightClickListeners.has(note.uuid)) {
const listener = (e: MouseEvent): void => {
return this.openNotesContextMenu(e, note);
};
document
.getElementById(`note-${note.uuid}`)
?.addEventListener('contextmenu', listener);
this.rightClickListeners.set(note.uuid, listener);
}
} }
} }
async selectNote(note: SNNote): Promise<void> { async selectNote(note: SNNote): Promise<void> {
this.removeAllContextMenuListeners();
await this.appState.notes.selectNote(note.uuid); await this.appState.notes.selectNote(note.uuid);
this.addContextMenuListeners();
} }
async createNewNote() { async createNewNote() {
@@ -454,6 +459,7 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
renderedNotes, renderedNotes,
}); });
this.reloadPanelTitle(); this.reloadPanelTitle();
this.addRightClickListeners();
} }
private notesTagsList(notes: SNNote[]): string[] { private notesTagsList(notes: SNNote[]): string[] {