feat: visual indicator for selected tags and remove them when re-clicking

This commit is contained in:
Antonella Sgarlatta
2021-05-07 18:24:28 -03:00
parent 660525dc18
commit 437aa40d7a
3 changed files with 48 additions and 11 deletions

View File

@@ -128,10 +128,14 @@ export const NotesOptions = observer(
className={`${buttonClass} py-2`} className={`${buttonClass} py-2`}
onBlur={closeOnBlur} onBlur={closeOnBlur}
onClick={() => { 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> </button>
))} ))}
</DisclosurePanel> </DisclosurePanel>

View File

@@ -6,6 +6,7 @@ import {
SNNote, SNNote,
NoteMutator, NoteMutator,
ContentType, ContentType,
SNTag,
} from '@standardnotes/snjs'; } from '@standardnotes/snjs';
import { import {
makeObservable, makeObservable,
@@ -76,27 +77,30 @@ export class NotesState {
const lastSelectedNoteIndex = notes.findIndex( const lastSelectedNoteIndex = notes.findIndex(
(note) => note.uuid == this.lastSelectedNote?.uuid (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 protectedNotesAccessRequest: Promise<boolean>;
let notesToSelect = []; let notesToSelect = [];
if (selectedNoteIndex > lastSelectedNoteIndex) { if (selectedNoteIndex > lastSelectedNoteIndex) {
notesToSelect = notes notesToSelect = notes.slice(lastSelectedNoteIndex, selectedNoteIndex + 1);
.slice(lastSelectedNoteIndex, selectedNoteIndex + 1);
} else { } else {
notesToSelect = notes notesToSelect = notes.slice(selectedNoteIndex, lastSelectedNoteIndex + 1);
.slice(selectedNoteIndex, lastSelectedNoteIndex + 1);
} }
await Promise.all( await Promise.all(
notesToSelect.map(async note => { notesToSelect.map(async (note) => {
const requestAccess = note.protected && this.application.hasProtectionSources(); const requestAccess =
note.protected && this.application.hasProtectionSources();
if (requestAccess) { if (requestAccess) {
if (!protectedNotesAccessRequest) { 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; this.selectedNotes[note.uuid] = note;
} }
}) })

View File

@@ -17,6 +17,8 @@ export class TagsState {
tagsCount: computed, tagsCount: computed,
addTagToSelectedNotes: action, addTagToSelectedNotes: action,
removeTagFromSelectedNotes: action,
isTagInSelectedNotes: action,
}); });
appEventListeners.push( appEventListeners.push(
@@ -47,6 +49,33 @@ export class TagsState {
this.application.sync(); 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 { get tagsCount(): number {
return this.tags.length; return this.tags.length;
} }