Skip to content

Commit

Permalink
fix: base keyboard modifier state on latest event
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Jan 12, 2022
1 parent cc97991 commit 8f4f188
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
40 changes: 25 additions & 15 deletions app/assets/javascripts/services/ioService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,38 @@ export class IOService {
this.activeModifiers.delete(modifier);
};

handleKeyDown = (event: KeyboardEvent): void => {
for (const modifier of this.modifiersForEvent(event)) {
this.addActiveModifier(modifier);
}
this.notifyObserver(event, KeyboardKeyEvent.Down);
public handleComponentKeyDown = (
modifier: KeyboardModifier | undefined
): void => {
this.addActiveModifier(modifier);
};

handleComponentKeyDown = (modifier: KeyboardModifier | undefined): void => {
this.addActiveModifier(modifier);
public handleComponentKeyUp = (
modifier: KeyboardModifier | undefined
): void => {
this.removeActiveModifier(modifier);
};

handleKeyUp = (event: KeyboardEvent): void => {
for (const modifier of this.modifiersForEvent(event)) {
this.removeActiveModifier(modifier);
}
this.notifyObserver(event, KeyboardKeyEvent.Up);
private handleKeyDown = (event: KeyboardEvent): void => {
this.updateAllModifiersFromEvent(event);
this.notifyObserver(event, KeyboardKeyEvent.Down);
};

handleComponentKeyUp = (modifier: KeyboardModifier | undefined): void => {
this.removeActiveModifier(modifier);
private handleKeyUp = (event: KeyboardEvent): void => {
this.updateAllModifiersFromEvent(event);
this.notifyObserver(event, KeyboardKeyEvent.Up);
};

private updateAllModifiersFromEvent(event: KeyboardEvent): void {
for (const modifier of Object.values(KeyboardModifier)) {
if (event.getModifierState(modifier)) {
this.addActiveModifier(modifier);
} else {
this.removeActiveModifier(modifier);
}
}
}

handleWindowBlur = (): void => {
for (const modifier of this.activeModifiers) {
this.activeModifiers.delete(modifier);
Expand Down Expand Up @@ -136,7 +146,7 @@ export class IOService {
return eventModifiers;
}

eventMatchesKeyAndModifiers(
private eventMatchesKeyAndModifiers(
event: KeyboardEvent,
key: KeyboardKey | string,
modifiers: KeyboardModifier[] = []
Expand Down
59 changes: 30 additions & 29 deletions app/assets/javascripts/ui_models/app_state/notes_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,43 +114,44 @@ export class NotesState {

async selectNote(uuid: UuidString, userTriggered?: boolean): Promise<void> {
const note = this.application.findItem(uuid) as SNNote;
if (!note) {
return;
}

const hasMeta = this.io.activeModifiers.has(KeyboardModifier.Meta);
const hasCtrl = this.io.activeModifiers.has(KeyboardModifier.Ctrl);
const hasShift = this.io.activeModifiers.has(KeyboardModifier.Shift);

if (note) {
if (userTriggered && (hasMeta || hasCtrl)) {
if (this.selectedNotes[uuid]) {
delete this.selectedNotes[uuid];
} else if (await this.application.authorizeNoteAccess(note)) {
runInAction(() => {
this.selectedNotes[uuid] = note;
this.lastSelectedNote = note;
});
}
} else if (userTriggered && hasShift) {
await this.selectNotesRange(note);
} else {
const shouldSelectNote =
this.selectedNotesCount > 1 || !this.selectedNotes[uuid];
if (
shouldSelectNote &&
(await this.application.authorizeNoteAccess(note))
) {
runInAction(() => {
this.selectedNotes = {
[note.uuid]: note,
};
this.lastSelectedNote = note;
});
}
if (userTriggered && (hasMeta || hasCtrl)) {
if (this.selectedNotes[uuid]) {
delete this.selectedNotes[uuid];
} else if (await this.application.authorizeNoteAccess(note)) {
runInAction(() => {
this.selectedNotes[uuid] = note;
this.lastSelectedNote = note;
});
}

if (this.selectedNotesCount === 1) {
await this.openNote(Object.keys(this.selectedNotes)[0]);
} else if (userTriggered && hasShift) {
await this.selectNotesRange(note);
} else {
const shouldSelectNote =
this.selectedNotesCount > 1 || !this.selectedNotes[uuid];
if (
shouldSelectNote &&
(await this.application.authorizeNoteAccess(note))
) {
runInAction(() => {
this.selectedNotes = {
[note.uuid]: note,
};
this.lastSelectedNote = note;
});
}
}

if (this.selectedNotesCount === 1) {
await this.openNote(Object.keys(this.selectedNotes)[0]);
}
}

private async openNote(noteUuid: string): Promise<void> {
Expand Down

0 comments on commit 8f4f188

Please sign in to comment.