-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2983 from quantified-uncertainty/select-items
Select items and keyboard nav
- Loading branch information
Showing
20 changed files
with
604 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@quri/squiggle-lang": patch | ||
"@quri/squiggle-components": patch | ||
--- | ||
|
||
Adds simple keyboard navigation for Viewer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
packages/components/src/components/SquiggleViewer/SqViewNode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import { SqValue, SqValuePath } from "@quri/squiggle-lang"; | ||
|
||
import { getChildrenValues, TraverseCalculatorEdge } from "./utils.js"; | ||
|
||
//We might want to bring this into the SquiggleLang library. The ``traverseCalculatorEdge`` part is awkward though. | ||
class SqValueNode { | ||
constructor( | ||
public root: SqValue, | ||
public path: SqValuePath, | ||
public traverseCalculatorEdge: TraverseCalculatorEdge | ||
) { | ||
this.isEqual = this.isEqual.bind(this); | ||
} | ||
|
||
uid() { | ||
return this.path.uid(); | ||
} | ||
|
||
isEqual(other: SqValueNode): boolean { | ||
return this.uid() === other.uid(); | ||
} | ||
|
||
sqValue(): SqValue | undefined { | ||
return this.root.getSubvalueByPath(this.path, this.traverseCalculatorEdge); | ||
} | ||
|
||
parent() { | ||
const parentPath = this.path.parent(); | ||
return parentPath | ||
? new SqValueNode(this.root, parentPath, this.traverseCalculatorEdge) | ||
: undefined; | ||
} | ||
|
||
children() { | ||
const value = this.sqValue(); | ||
if (!value) { | ||
return []; | ||
} | ||
return getChildrenValues(value) | ||
.map((childValue) => { | ||
const path = childValue.context?.path; | ||
return path | ||
? new SqValueNode(this.root, path, this.traverseCalculatorEdge) | ||
: undefined; | ||
}) | ||
.filter((a): a is NonNullable<typeof a> => a !== undefined); | ||
} | ||
|
||
lastChild(): SqValueNode | undefined { | ||
return this.children().at(-1); | ||
} | ||
|
||
siblings() { | ||
return this.parent()?.children() ?? []; | ||
} | ||
|
||
prevSibling() { | ||
const index = this.getParentIndex(); | ||
const isRootOrError = index === -1; | ||
const isFirstSibling = index === 0; | ||
if (isRootOrError || isFirstSibling) { | ||
return undefined; | ||
} | ||
return this.siblings()[index - 1]; | ||
} | ||
|
||
nextSibling() { | ||
const index = this.getParentIndex(); | ||
const isRootOrError = index === -1; | ||
const isLastSibling = index === this.siblings().length - 1; | ||
if (isRootOrError || isLastSibling) { | ||
return undefined; | ||
} | ||
return this.siblings()[index + 1]; | ||
} | ||
|
||
getParentIndex() { | ||
return this.siblings().findIndex(this.isEqual); | ||
} | ||
} | ||
|
||
type GetIsCollapsed = (path: SqValuePath) => boolean; | ||
type Params = { getIsCollapsed: GetIsCollapsed }; | ||
|
||
//This is split from SqValueNode because it handles more specialized logic for viewing open/closed nodes in the Viewer. It works for lists of nodes - we'll need new logic for tabular data. | ||
export class SqListViewNode { | ||
constructor( | ||
public node: SqValueNode, | ||
public params: Params | ||
) { | ||
this.make = this.make.bind(this); | ||
} | ||
|
||
static make( | ||
root: SqValue, | ||
path: SqValuePath, | ||
traverseCalculatorEdge: TraverseCalculatorEdge, | ||
getIsCollapsed: GetIsCollapsed | ||
) { | ||
const node = new SqValueNode(root, path, traverseCalculatorEdge); | ||
return new SqListViewNode(node, { getIsCollapsed }); | ||
} | ||
|
||
make(node: SqValueNode) { | ||
return new SqListViewNode(node, this.params); | ||
} | ||
|
||
// A helper function to make a node or undefined | ||
makeU(node: SqValueNode | undefined) { | ||
return node ? new SqListViewNode(node, this.params) : undefined; | ||
} | ||
|
||
value(): SqValue | undefined { | ||
return this.node.sqValue(); | ||
} | ||
isRoot() { | ||
return this.node.path.isRoot(); | ||
} | ||
parent() { | ||
return this.makeU(this.node.parent()); | ||
} | ||
children() { | ||
return this.node.children().map(this.make); | ||
} | ||
lastChild() { | ||
return this.makeU(this.node.lastChild()); | ||
} | ||
siblings() { | ||
return this.node.siblings().map(this.make); | ||
} | ||
prevSibling() { | ||
return this.makeU(this.node.prevSibling()); | ||
} | ||
nextSibling() { | ||
return this.makeU(this.node.nextSibling()); | ||
} | ||
private isCollapsed() { | ||
return this.params.getIsCollapsed(this.node.path); | ||
} | ||
|
||
private hasVisibleChildren() { | ||
return !this.isCollapsed() && this.children().length > 0; | ||
} | ||
|
||
private lastVisibleSubChild(): SqListViewNode | undefined { | ||
if (this.hasVisibleChildren()) { | ||
const lastChild = this.lastChild(); | ||
return lastChild?.lastVisibleSubChild() || lastChild; | ||
} else { | ||
return this; | ||
} | ||
} | ||
|
||
private nextAvailableSibling(): SqListViewNode | undefined { | ||
return this.nextSibling() || this.parent()?.nextAvailableSibling(); | ||
} | ||
|
||
next(): SqListViewNode | undefined { | ||
return this.hasVisibleChildren() | ||
? this.children()[0] | ||
: this.nextAvailableSibling(); | ||
} | ||
|
||
prev(): SqListViewNode | undefined { | ||
const prevSibling = this.prevSibling(); | ||
return prevSibling ? prevSibling.lastVisibleSubChild() : this.parent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
e6dde34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
squiggle-website – ./packages/website
squiggle-website-git-main-quantified-uncertainty.vercel.app
www.squiggle-language.com
squiggle-website-quantified-uncertainty.vercel.app
squiggle-language.com
preview.squiggle-language.com
e6dde34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
quri-hub – ./packages/hub
www.squigglehub.org
squiggle-hub.com
www.squiggle-hub.org
quri-hub-git-main-quantified-uncertainty.vercel.app
quri-hub-quantified-uncertainty.vercel.app
squiggle-hub.org
squigglehub.com
www.squigglehub.com
www.squiggle-hub.com
squigglehub.org