diff --git a/app/assets/javascripts/ui_models/app_state/notes_state.ts b/app/assets/javascripts/ui_models/app_state/notes_state.ts index e3324b86c..f09bd189b 100644 --- a/app/assets/javascripts/ui_models/app_state/notes_state.ts +++ b/app/assets/javascripts/ui_models/app_state/notes_state.ts @@ -69,6 +69,42 @@ export class NotesState { return Object.keys(this.selectedNotes).length; } + async selectNotesRange(selectedNote: SNNote): Promise { + const notes = this.application.getDisplayableItems( + ContentType.Note + ) as SNNote[]; + const lastSelectedNoteIndex = notes.findIndex( + (note) => note.uuid == this.lastSelectedNote?.uuid + ); + const selectedNoteIndex = notes.findIndex((note) => note.uuid == selectedNote.uuid); + let protectedNotesAccessRequest: Promise; + let notesToSelect = []; + + if (selectedNoteIndex > lastSelectedNoteIndex) { + notesToSelect = notes + .slice(lastSelectedNoteIndex, selectedNoteIndex + 1); + } else { + notesToSelect = notes + .slice(selectedNoteIndex, lastSelectedNoteIndex + 1); + } + + await Promise.all( + notesToSelect.map(async note => { + const requestAccess = note.protected && this.application.hasProtectionSources(); + if (requestAccess) { + if (!protectedNotesAccessRequest) { + protectedNotesAccessRequest = this.application.authorizeNoteAccess(note); + } + } + if (!requestAccess || await protectedNotesAccessRequest) { + this.selectedNotes[note.uuid] = note; + } + }) + ); + + this.lastSelectedNote = selectedNote; + } + async selectNote(uuid: UuidString): Promise { const note = this.application.findItem(uuid) as SNNote; if ( @@ -78,27 +114,20 @@ export class NotesState { ) { if (this.selectedNotes[uuid]) { delete this.selectedNotes[uuid]; - } else { + } else if (await this.application.authorizeNoteAccess(note)) { this.selectedNotes[uuid] = note; this.lastSelectedNote = note; } } else if (this.io.activeModifiers.has(KeyboardModifier.Shift)) { - const notes = this.application.getDisplayableItems( - ContentType.Note - ) as SNNote[]; - const lastSelectedNoteIndex = notes.findIndex( - (note) => note.uuid == this.lastSelectedNote?.uuid - ); - const selectedNoteIndex = notes.findIndex((note) => note.uuid == uuid); - notes - .slice(lastSelectedNoteIndex, selectedNoteIndex + 1) - .forEach((note) => (this.selectedNotes[note.uuid] = note)); + await this.selectNotesRange(note); } else { - this.selectedNotes = { - [uuid]: note, - }; - this.lastSelectedNote = note; - await this.openEditor(uuid); + if (await this.application.authorizeNoteAccess(note)) { + this.selectedNotes = { + [uuid]: note, + }; + await this.openEditor(uuid); + this.lastSelectedNote = note; + } } } @@ -113,17 +142,15 @@ export class NotesState { return; } - if (await this.application.authorizeNoteAccess(note)) { - if (!this.activeEditor) { - this.application.editorGroup.createEditor(noteUuid); - } else { - this.activeEditor.setNote(note); - } - await this.onActiveEditorChanged(); + if (!this.activeEditor) { + this.application.editorGroup.createEditor(noteUuid); + } else { + this.activeEditor.setNote(note); + } + await this.onActiveEditorChanged(); - if (note.waitingForKey) { - this.application.presentKeyRecoveryWizard(); - } + if (note.waitingForKey) { + this.application.presentKeyRecoveryWizard(); } } diff --git a/app/assets/javascripts/views/notes/notes_view.ts b/app/assets/javascripts/views/notes/notes_view.ts index 32ca1f0c2..df8d7072c 100644 --- a/app/assets/javascripts/views/notes/notes_view.ts +++ b/app/assets/javascripts/views/notes/notes_view.ts @@ -298,10 +298,12 @@ class NotesViewCtrl extends PureViewCtrl { )); } - private openNotesContextMenu(e: MouseEvent, note: SNNote) { + private async openNotesContextMenu(e: MouseEvent, note: SNNote) { e.preventDefault(); - this.selectNote(note); - if (!note.protected) { + if (!this.state.selectedNotes[note.uuid]) { + await this.selectNote(note); + } + if (this.state.selectedNotes[note.uuid]) { this.application.getAppState().notes.setContextMenuPosition({ top: e.clientY, left: e.clientX, @@ -330,8 +332,8 @@ class NotesViewCtrl extends PureViewCtrl { } for (const note of this.state.renderedNotes) { if (!this.rightClickListeners.has(note.uuid)) { - const listener = (e: MouseEvent): void => { - return this.openNotesContextMenu(e, note); + const listener = async (e: MouseEvent): Promise => { + return await this.openNotesContextMenu(e, note); }; document .getElementById(`note-${note.uuid}`)