Skip to content

Commit

Permalink
feat: navigate tags with arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonella Sgarlatta committed Jun 2, 2021
1 parent 434ea32 commit 672331f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
27 changes: 22 additions & 5 deletions app/assets/javascripts/components/NoteTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<button
ref={(element) => {
Expand All @@ -46,11 +67,7 @@ export const NoteTag: FunctionalComponent<Props> = ({ appState, tag }) => {
className="sn-tag pl-1 pr-2 mr-2"
style={{ maxWidth: tagsContainerMaxWidth }}
onClick={onTagClick}
onKeyUp={(event) => {
if (event.key === 'Backspace') {
deleteTag();
}
}}
onKeyUp={onKeyUp}
onFocus={onFocus}
onBlur={onBlur}
>
Expand Down
25 changes: 18 additions & 7 deletions app/assets/javascripts/ui_models/app_state/active_note_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class ActiveNoteState {
get activeNote(): SNNote | undefined {
return this.appState.notes.activeEditor?.note;
}

setTagElement(tag: SNTag, element: HTMLButtonElement): void {
const tagIndex = this.getTagIndex(tag);
if (tagIndex > -1) {
Expand Down Expand Up @@ -72,10 +73,23 @@ export class ActiveNoteState {
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;
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);
}
}
}

Expand Down Expand Up @@ -117,15 +131,12 @@ export class ActiveNoteState {
async removeTagFromActiveNote(tag: SNTag): Promise<void> {
const { activeNote } = this;
if (activeNote) {
const previousTag = this.getPreviousTag(tag);
const previousTagElement = this.getPreviousTagElement(tag);
await this.application.changeItem(tag.uuid, (mutator) => {
mutator.removeItemAsRelationship(activeNote);
});
this.application.sync();
if (previousTag) {
const previousTagElement = this.getTagElement(previousTag);
previousTagElement?.focus();
}
previousTagElement?.focus();
this.reloadTags();
}
}
Expand Down

0 comments on commit 672331f

Please sign in to comment.