Skip to content

Commit

Permalink
fix: better protected notes handling and allow shift-clicking note up…
Browse files Browse the repository at this point in the history
… in the list
  • Loading branch information
Antonella Sgarlatta committed May 7, 2021
1 parent b89cdde commit 6a6e170
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
79 changes: 53 additions & 26 deletions app/assets/javascripts/ui_models/app_state/notes_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,42 @@ export class NotesState {
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> {
const note = this.application.findItem(uuid) as SNNote;
if (
Expand All @@ -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;
}
}
}

Expand All @@ -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();
}
}

Expand Down
12 changes: 7 additions & 5 deletions app/assets/javascripts/views/notes/notes_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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,
Expand Down Expand Up @@ -330,8 +332,8 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
}
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<void> => {
return await this.openNotesContextMenu(e, note);
};
document
.getElementById(`note-${note.uuid}`)
Expand Down

0 comments on commit 6a6e170

Please sign in to comment.