Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic keyboard shortcuts functionality #183

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/components/ViewerPage/common/OpGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,43 @@ const opLayout: OpName[][] = [
["augment", "augment", "diminish", "gyrate"],
]

const shortcutKeys: Record<string, OpName> = {
e: "expand",
a: "rectify",
s: "snub",
t: "truncate",
}

const opList = uniq(opLayout.flat())

const Shortcuts = function () {
const polyhedron = PolyhedronCtx.useState()
const applyOperation = useApplyOperation()
const { setOperation } = OperationCtx.useActions()

const selectOperation = (name: OpName) => {
const operation = operations[name]
if (!operation.canApplyTo(polyhedron)) return

if (operation.hasOptions(polyhedron)) {
setOperation(operation, polyhedron)
} else {
applyOperation(operation)
}
}

React.useEffect(() => {
document.onkeydown = (e) => {
if (shortcutKeys[e.key]) {
selectOperation(shortcutKeys[e.key])
}
e.preventDefault()
}
})

return null
}

interface Props {
name: OpName
disabled: boolean
Expand Down Expand Up @@ -67,10 +102,22 @@ const OpButton = memo(function ({ name, disabled }: Props) {
setOperation(operation, polyhedron)
}
}

const createTitle = () => {
if (!operation.canApplyTo(polyhedron) || disabled) return
const key = Object.keys(shortcutKeys).find((k) => shortcutKeys[k] === name)

if (key && shortcutKeys[key] === name) {
return `${name} (${key})`
}

return name
}
return (
<button
{...css()}
style={{ gridArea: name }}
title={createTitle()}
onClick={selectOperation}
disabled={!operation.canApplyTo(polyhedron) || disabled}
>
Expand Down Expand Up @@ -107,6 +154,7 @@ export default function OpGrid() {
})
return (
<div {...css()}>
<Shortcuts />
{opList.map((name) => (
<OpButton key={name} name={name} disabled={isTransitioning} />
))}
Expand Down