feat: navigate tags with arrow keys

This commit is contained in:
Antonella Sgarlatta
2021-06-02 20:18:59 -03:00
parent 434ea3258e
commit 672331faaa
2 changed files with 40 additions and 12 deletions

View File

@@ -36,6 +36,27 @@ export const NoteTag: FunctionalComponent<Props> = ({ appState, tag }) => {
} }
}; };
const onKeyUp = (event: KeyboardEvent) => {
let previousTagElement;
let nextTagElement;
switch (event.key) {
case "Backspace":
deleteTag();
break;
case "ArrowLeft":
previousTagElement = appState.activeNote.getPreviousTagElement(tag);
previousTagElement?.focus();
break;
case "ArrowRight":
nextTagElement = appState.activeNote.getNextTagElement(tag);
nextTagElement?.focus();
break;
default:
return;
}
};
return ( return (
<button <button
ref={(element) => { ref={(element) => {
@@ -46,11 +67,7 @@ export const NoteTag: FunctionalComponent<Props> = ({ appState, tag }) => {
className="sn-tag pl-1 pr-2 mr-2" className="sn-tag pl-1 pr-2 mr-2"
style={{ maxWidth: tagsContainerMaxWidth }} style={{ maxWidth: tagsContainerMaxWidth }}
onClick={onTagClick} onClick={onTagClick}
onKeyUp={(event) => { onKeyUp={onKeyUp}
if (event.key === 'Backspace') {
deleteTag();
}
}}
onFocus={onFocus} onFocus={onFocus}
onBlur={onBlur} onBlur={onBlur}
> >

View File

@@ -42,6 +42,7 @@ export class ActiveNoteState {
get activeNote(): SNNote | undefined { get activeNote(): SNNote | undefined {
return this.appState.notes.activeEditor?.note; return this.appState.notes.activeEditor?.note;
} }
setTagElement(tag: SNTag, element: HTMLButtonElement): void { setTagElement(tag: SNTag, element: HTMLButtonElement): void {
const tagIndex = this.getTagIndex(tag); const tagIndex = this.getTagIndex(tag);
if (tagIndex > -1) { if (tagIndex > -1) {
@@ -72,10 +73,23 @@ export class ActiveNoteState {
return this.tags.findIndex(t => t.uuid === tag.uuid); return this.tags.findIndex(t => t.uuid === tag.uuid);
} }
getPreviousTag(tag: SNTag): SNTag | undefined { getPreviousTagElement(tag: SNTag): HTMLButtonElement | undefined {
const previousTagIndex = this.getTagIndex(tag) - 1; const previousTagIndex = this.getTagIndex(tag) - 1;
if (previousTagIndex > -1 && this.tags.length > previousTagIndex) { if (previousTagIndex > -1 && this.tags.length > previousTagIndex) {
return this.tags[previousTagIndex]; const previousTag = this.tags[previousTagIndex];
if (previousTag) {
return this.getTagElement(previousTag);
}
}
}
getNextTagElement(tag: SNTag): HTMLButtonElement | undefined {
const nextTagIndex = this.getTagIndex(tag) + 1;
if (nextTagIndex > -1 && this.tags.length > nextTagIndex) {
const previousTag = this.tags[nextTagIndex];
if (previousTag) {
return this.getTagElement(previousTag);
}
} }
} }
@@ -117,15 +131,12 @@ export class ActiveNoteState {
async removeTagFromActiveNote(tag: SNTag): Promise<void> { async removeTagFromActiveNote(tag: SNTag): Promise<void> {
const { activeNote } = this; const { activeNote } = this;
if (activeNote) { if (activeNote) {
const previousTag = this.getPreviousTag(tag); const previousTagElement = this.getPreviousTagElement(tag);
await this.application.changeItem(tag.uuid, (mutator) => { await this.application.changeItem(tag.uuid, (mutator) => {
mutator.removeItemAsRelationship(activeNote); mutator.removeItemAsRelationship(activeNote);
}); });
this.application.sync(); this.application.sync();
if (previousTag) { previousTagElement?.focus();
const previousTagElement = this.getTagElement(previousTag);
previousTagElement?.focus();
}
this.reloadTags(); this.reloadTags();
} }
} }