Skip to content

Commit

Permalink
feat: visual indicator for selected tags and remove them when re-clic…
Browse files Browse the repository at this point in the history
…king
  • Loading branch information
Antonella Sgarlatta committed May 7, 2021
1 parent 660525d commit 437aa40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
8 changes: 6 additions & 2 deletions app/assets/javascripts/components/NotesOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ export const NotesOptions = observer(
className={`${buttonClass} py-2`}
onBlur={closeOnBlur}
onClick={() => {
appState.tags.addTagToSelectedNotes(tag);
appState.tags.isTagInSelectedNotes(tag)
? appState.tags.removeTagFromSelectedNotes(tag)
: appState.tags.addTagToSelectedNotes(tag);
}}
>
{tag.title}
<span className={appState.tags.isTagInSelectedNotes(tag) ? 'font-bold' : ''}>
{tag.title}
</span>
</button>
))}
</DisclosurePanel>
Expand Down
22 changes: 13 additions & 9 deletions app/assets/javascripts/ui_models/app_state/notes_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SNNote,
NoteMutator,
ContentType,
SNTag,
} from '@standardnotes/snjs';
import {
makeObservable,
Expand Down Expand Up @@ -76,27 +77,30 @@ export class NotesState {
const lastSelectedNoteIndex = notes.findIndex(
(note) => note.uuid == this.lastSelectedNote?.uuid
);
const selectedNoteIndex = notes.findIndex((note) => note.uuid == selectedNote.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);
notesToSelect = notes.slice(lastSelectedNoteIndex, selectedNoteIndex + 1);
} else {
notesToSelect = notes
.slice(selectedNoteIndex, lastSelectedNoteIndex + 1);
notesToSelect = notes.slice(selectedNoteIndex, lastSelectedNoteIndex + 1);
}

await Promise.all(
notesToSelect.map(async note => {
const requestAccess = note.protected && this.application.hasProtectionSources();
notesToSelect.map(async (note) => {
const requestAccess =
note.protected && this.application.hasProtectionSources();
if (requestAccess) {
if (!protectedNotesAccessRequest) {
protectedNotesAccessRequest = this.application.authorizeNoteAccess(note);
protectedNotesAccessRequest = this.application.authorizeNoteAccess(
note
);
}
}
if (!requestAccess || await protectedNotesAccessRequest) {
if (!requestAccess || (await protectedNotesAccessRequest)) {
this.selectedNotes[note.uuid] = note;
}
})
Expand Down
29 changes: 29 additions & 0 deletions app/assets/javascripts/ui_models/app_state/tags_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class TagsState {
tagsCount: computed,

addTagToSelectedNotes: action,
removeTagFromSelectedNotes: action,
isTagInSelectedNotes: action,
});

appEventListeners.push(
Expand Down Expand Up @@ -47,6 +49,33 @@ export class TagsState {
this.application.sync();
}

async removeTagFromSelectedNotes(tag: SNTag): Promise<void> {
const selectedNotes = Object.values(
this.application.getAppState().notes.selectedNotes
);
await Promise.all(
selectedNotes.map(
async (note) =>
await this.application.changeItem(tag.uuid, (mutator) => {
mutator.removeItemAsRelationship(note);
})
)
);
this.application.sync();
}

isTagInSelectedNotes(tag: SNTag): boolean {
const selectedNotes = Object.values(
this.application.getAppState().notes.selectedNotes
);
return selectedNotes.every((note) =>
this.application
.getAppState()
.getNoteTags(note)
.find((noteTag) => noteTag.uuid === tag.uuid)
);
}

get tagsCount(): number {
return this.tags.length;
}
Expand Down

0 comments on commit 437aa40

Please sign in to comment.