fix: better protected notes handling and allow shift-clicking note up in the list

This commit is contained in:
Antonella Sgarlatta
2021-05-07 16:57:25 -03:00
parent b89cdde1fc
commit 6a6e1708d6
2 changed files with 60 additions and 31 deletions

View File

@@ -69,6 +69,42 @@ export class NotesState {
return Object.keys(this.selectedNotes).length; return Object.keys(this.selectedNotes).length;
} }
async selectNotesRange(selectedNote: SNNote): Promise<void> {
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<boolean>;
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<void> { async selectNote(uuid: UuidString): Promise<void> {
const note = this.application.findItem(uuid) as SNNote; const note = this.application.findItem(uuid) as SNNote;
if ( if (
@@ -78,27 +114,20 @@ export class NotesState {
) { ) {
if (this.selectedNotes[uuid]) { if (this.selectedNotes[uuid]) {
delete this.selectedNotes[uuid]; delete this.selectedNotes[uuid];
} else { } else if (await this.application.authorizeNoteAccess(note)) {
this.selectedNotes[uuid] = note; this.selectedNotes[uuid] = note;
this.lastSelectedNote = note; this.lastSelectedNote = note;
} }
} else if (this.io.activeModifiers.has(KeyboardModifier.Shift)) { } else if (this.io.activeModifiers.has(KeyboardModifier.Shift)) {
const notes = this.application.getDisplayableItems( await this.selectNotesRange(note);
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));
} else { } else {
this.selectedNotes = { if (await this.application.authorizeNoteAccess(note)) {
[uuid]: note, this.selectedNotes = {
}; [uuid]: note,
this.lastSelectedNote = note; };
await this.openEditor(uuid); await this.openEditor(uuid);
this.lastSelectedNote = note;
}
} }
} }
@@ -113,17 +142,15 @@ export class NotesState {
return; return;
} }
if (await this.application.authorizeNoteAccess(note)) { if (!this.activeEditor) {
if (!this.activeEditor) { this.application.editorGroup.createEditor(noteUuid);
this.application.editorGroup.createEditor(noteUuid); } else {
} else { this.activeEditor.setNote(note);
this.activeEditor.setNote(note); }
} await this.onActiveEditorChanged();
await this.onActiveEditorChanged();
if (note.waitingForKey) { if (note.waitingForKey) {
this.application.presentKeyRecoveryWizard(); this.application.presentKeyRecoveryWizard();
}
} }
} }

View File

@@ -298,10 +298,12 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
)); ));
} }
private openNotesContextMenu(e: MouseEvent, note: SNNote) { private async openNotesContextMenu(e: MouseEvent, note: SNNote) {
e.preventDefault(); e.preventDefault();
this.selectNote(note); if (!this.state.selectedNotes[note.uuid]) {
if (!note.protected) { await this.selectNote(note);
}
if (this.state.selectedNotes[note.uuid]) {
this.application.getAppState().notes.setContextMenuPosition({ this.application.getAppState().notes.setContextMenuPosition({
top: e.clientY, top: e.clientY,
left: e.clientX, left: e.clientX,
@@ -330,8 +332,8 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
} }
for (const note of this.state.renderedNotes) { for (const note of this.state.renderedNotes) {
if (!this.rightClickListeners.has(note.uuid)) { if (!this.rightClickListeners.has(note.uuid)) {
const listener = (e: MouseEvent): void => { const listener = async (e: MouseEvent): Promise<void> => {
return this.openNotesContextMenu(e, note); return await this.openNotesContextMenu(e, note);
}; };
document document
.getElementById(`note-${note.uuid}`) .getElementById(`note-${note.uuid}`)