Skip to content

Commit

Permalink
move focusOnPath to ItemStore
Browse files Browse the repository at this point in the history
  • Loading branch information
berekuk committed Jan 25, 2024
1 parent a425662 commit da41d3e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export class ItemStore {
scrollViewerToPath(path: SqValuePath) {
this.handles[path.uid()]?.scrollIntoView();
}

focusOnPath(path: SqValuePath) {
this.handles[path.uid()]?.focusOnHeader();
}
}

type ViewerContextShape = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { SqValuePath } from "@quri/squiggle-lang";

import { ItemStore } from "../ViewerProvider.js";

export const focusSqValueHeader = (path: SqValuePath, itemStore: ItemStore) => {
itemStore.handles[path.uid()]?.focusOnHeader();
};

// Returns boolean to indicate if the key was handled. The caller might want to do something else if it wasn't.
export function keyboardEventHandler(
handlers: Partial<Record<string, () => void>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SqValuePath } from "@quri/squiggle-lang";

import { useViewerContext } from "../ViewerProvider.js";
import { focusSqValueHeader, keyboardEventHandler } from "./utils.js";
import { keyboardEventHandler } from "./utils.js";

export function useZoomedInSqValueKeyEvent(selected: SqValuePath) {
const {
Expand All @@ -15,7 +15,7 @@ export function useZoomedInSqValueKeyEvent(selected: SqValuePath) {

// This timeout is a hack to make sure the header is zoomedIn after the reset
setTimeout(() => {
focusSqValueHeader(selected, itemStore);
itemStore.focusOnPath(selected);
}, 1);
}

Expand Down Expand Up @@ -45,7 +45,7 @@ export function useZoomedInSqValueKeyEvent(selected: SqValuePath) {
ArrowRight: () => {
const newItem = findNode(selected)?.children()[0];
if (newItem) {
focusSqValueHeader(newItem.node.path, itemStore);
itemStore.focusOnPath(newItem.node.path);
}
},
Enter: resetToRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SqValuePath } from "@quri/squiggle-lang";

import { toggleCollapsed, useViewerContext } from "../ViewerProvider.js";
import { focusSqValueHeader, keyboardEventHandler } from "./utils.js";
import { keyboardEventHandler } from "./utils.js";

export function useZoomedOutSqValueKeyEvent(selected: SqValuePath) {
const {
Expand All @@ -14,17 +14,15 @@ export function useZoomedOutSqValueKeyEvent(selected: SqValuePath) {
return keyboardEventHandler({
ArrowDown: () => {
const newPath = findNode(selected)?.next()?.node.path;
newPath && focusSqValueHeader(newPath, itemStore);
newPath && itemStore.focusOnPath(newPath);
},
ArrowUp: () => {
const newPath = findNode(selected)?.prev()?.node.path;
newPath && focusSqValueHeader(newPath, itemStore);
newPath && itemStore.focusOnPath(newPath);
},
ArrowLeft: () => {
const newItem = findNode(selected)?.parent();
newItem &&
!newItem.isRoot() &&
focusSqValueHeader(newItem.node.path, itemStore);
newItem && !newItem.isRoot() && itemStore.focusOnPath(newItem.node.path);
},
ArrowRight: () => {
const newItem = findNode(selected)?.children().at(0);
Expand All @@ -34,10 +32,10 @@ export function useZoomedOutSqValueKeyEvent(selected: SqValuePath) {
if (isCollapsed) {
toggleCollapsed(itemStore, selected);
setTimeout(() => {
focusSqValueHeader(newItem.node.path, itemStore);
itemStore.focusOnPath(newItem.node.path);
}, 1);
} else {
focusSqValueHeader(newItem.node.path, itemStore);
itemStore.focusOnPath(newItem.node.path);
}
}
},
Expand Down

0 comments on commit da41d3e

Please sign in to comment.