Skip to content

Commit

Permalink
add keyboard driven selection to the pageCitationmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
blackforestboi committed Feb 25, 2024
1 parent 8f31d4d commit 3447d3c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
29 changes: 29 additions & 0 deletions src/citations/PageCitations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ export default class PageCitations extends React.PureComponent<Props, State> {
})
}

componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown)
}

componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown)
}

handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'ArrowRight') {
event.preventDefault()
event.stopPropagation()
this.setState({ optionSelected: 'ShareViaLink' })
} else if (event.key === 'ArrowLeft') {
event.preventDefault()
event.stopPropagation()
this.setState({ optionSelected: 'CopyToClipboard' })
} else if (event.key === 'Tab') {
event.preventDefault()
event.stopPropagation()
this.setState((prevState) => ({
optionSelected:
prevState.optionSelected === 'CopyToClipboard'
? 'ShareViaLink'
: 'CopyToClipboard',
}))
}
}

private renderPreview = async (
template: Template,
templateType: 'originalPage' | 'examplePage',
Expand Down
58 changes: 53 additions & 5 deletions src/custom-lists/ui/space-links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,41 @@ export interface Props {

interface State {
showCopyMessageIdx: number | null
selectedLink: number
}

export default class SpaceLinks extends React.PureComponent<Props> {
state: State = { showCopyMessageIdx: null }
state: State = { showCopyMessageIdx: null, selectedLink: 0 }

componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown, true)
}

componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown, true)
}

handleKeyDown = (event: KeyboardEvent) => {
const maxIndex = this.props.inviteLinks.length - 1
if (event.key === 'ArrowDown') {
event.preventDefault()
event.stopPropagation()
const nextIndex = this.state.selectedLink + 1
this.setState({
selectedLink: nextIndex > maxIndex ? maxIndex : nextIndex,
})
} else if (event.key === 'ArrowUp') {
event.preventDefault()
event.stopPropagation()
const prevIndex = this.state.selectedLink - 1
this.setState({ selectedLink: prevIndex < 0 ? 0 : prevIndex })
} else if (event.key === 'Enter') {
const { link } = this.props.inviteLinks[this.state.selectedLink]
this.showCopyMessage(this.state.selectedLink)
this.props.copyLink(link)
this.trackClick('reader')
}
}

private showCopyMessage(idx: number) {
this.setState({ showCopyMessageIdx: idx }, () =>
Expand Down Expand Up @@ -91,6 +122,19 @@ export default class SpaceLinks extends React.PureComponent<Props> {
this.props.copyLink(link)
await this.trackClick('reader')
})}
selected={
this.state.selectedLink === linkIdx
}
onMouseEnter={() =>
this.setState({
selectedLink: linkIdx,
})
}
onMouseLeave={() =>
this.setState({
selectedLink: null,
})
}
>
<Link>
{this.state.showCopyMessageIdx ===
Expand Down Expand Up @@ -217,7 +261,9 @@ const LinkAndRoleBox = styled.div<{
`

const LinkBox = styled.div`
const LinkBox = styled.div<{
selected: boolean
}>`
width: fill-available;
display: flex;
font-size: 14px;
Expand All @@ -232,9 +278,11 @@ const LinkBox = styled.div`
align-items: center;
border-radius: 5px;
&:hover {
outline: 1px solid ${(props) => props.theme.colors.greyScale3};
}
${(props) =>
props.selected &&
css`
background-color: ${(props) => props.theme.colors.greyScale2}90;
`}
`

const Link = styled.span`
Expand Down
3 changes: 2 additions & 1 deletion src/dashboard-refactor/header/search-bar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export default class SearchBar extends PureComponent<SearchBarProps> {
onKeyDown={(e) => {
if (
e.key !== 'ArrowDown' &&
e.key !== 'ArrowUp'
e.key !== 'ArrowUp' &&
e.key !== 'Escape'
) {
e.stopPropagation()
}
Expand Down
11 changes: 4 additions & 7 deletions src/dashboard-refactor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export class DashboardContainer extends StatefulUIElement<
this.props.inPageMode &&
!this.state.isNoteSidebarShown
) {
event.stopPropagation()
event.preventDefault()
this.props.closeInPageMode()
}

Expand All @@ -197,9 +199,9 @@ export class DashboardContainer extends StatefulUIElement<
this.processEvent('changeFocusItem', {
direction: event.key === 'ArrowUp' ? 'up' : 'down',
})
event.stopPropagation()
event.preventDefault()
}
event.stopPropagation()
event.preventDefault()
}
}

Expand Down Expand Up @@ -1651,11 +1653,6 @@ export class DashboardContainer extends StatefulUIElement<
isPeeking: true,
})
}}
onMouseLeave={() => {
this.processEvent('setSidebarPeeking', {
isPeeking: true,
})
}}
onDragEnter={() => {
this.processEvent('setSidebarPeeking', {
isPeeking: true,
Expand Down

0 comments on commit 3447d3c

Please sign in to comment.