Skip to content

Commit

Permalink
Make each sidebar page it's own "panel", and provide shortcuts for
Browse files Browse the repository at this point in the history
navigating between sidebar panels
  • Loading branch information
tntmarket committed Oct 11, 2020
1 parent 6f9d22c commit dadaa19
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/ts/core/features/vim-mode/commands/panel-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const closeSidebarPage = () => {

export const PanelCommands = [
// Need to wrap in function to preserve the `this` reference inside of RoamPanel
nmap('h', 'Select Panel Left', () => RoamPanel.previousPanel().select()),
nmap('l', 'Select Panel Right', () => RoamPanel.nextPanel().select()),
nmap('h', 'Select Main Panel', () => RoamPanel.mainPanel().select()),
nmap('l', 'Select Prior Sidebar Panel', () => RoamPanel.previousSidebarPanel().select()),
nmap('ctrl+k', 'Select Previous Panel', () => RoamPanel.previousPanel().select()),
nmap('ctrl+j', 'Select Next Panel', () => RoamPanel.nextPanel().select()),
map('ctrl+w', 'Close Page in Side Bar', closeSidebarPage),
]
2 changes: 1 addition & 1 deletion src/ts/core/features/vim-mode/roam/roam-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const RoamEvent = {
// Triggers when the sidebar is shown or hidden
onSidebarToggle(handler: (isSideBarShowing: boolean) => void): DisconnectFn {
return onSelectorChange(Selectors.sidebar, () => {
const isSidebarShowing = !!document.querySelector(Selectors.sidebarContent)
const isSidebarShowing = !!document.querySelector(Selectors.sidebar)
handler(isSidebarShowing)
})
},
Expand Down
20 changes: 17 additions & 3 deletions src/ts/core/features/vim-mode/roam/roam-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ type BlockNavigationState = {
panelOrder: PanelId[]
panels: Map<PanelId, RoamPanel>
focusedPanel: PanelIndex
lastFocusedSidebarPanel: PanelIndex
previouslySelectedBlocks: Set<BlockId>
}

const state: BlockNavigationState = {
panelOrder: [],
panels: new Map(),
focusedPanel: 0,
lastFocusedSidebarPanel: 1,
previouslySelectedBlocks: new Set(),
}

/**
Expand All @@ -26,7 +30,7 @@ type PanelIndex = number
type PanelElement = HTMLElement

const PANEL_CSS_CLASS = 'roam-toolkit--panel'
const PANEL_SELECTOR = `.${PANEL_CSS_CLASS}, ${Selectors.sidebarContent}`
const PANEL_SELECTOR = `.${PANEL_CSS_CLASS}, ${Selectors.sidebarPage}`

/**
* A "Panel" is a viewport that contains blocks. For now, there is just
Expand Down Expand Up @@ -121,8 +125,13 @@ export class RoamPanel {
}

select() {
if (state.focusedPanel > 0) {
state.lastFocusedSidebarPanel = state.focusedPanel
}
state.focusedPanel = state.panelOrder.indexOf(this.element)
this.element.scrollIntoView({behavior: 'smooth'})
this.element.scrollIntoView({
inline: 'end',
})
}

static selected(): RoamPanel {
Expand All @@ -144,6 +153,10 @@ export class RoamPanel {
return RoamPanel.at(0)
}

static previousSidebarPanel(): RoamPanel {
return RoamPanel.at(state.lastFocusedSidebarPanel)
}

static previousPanel(): RoamPanel {
return RoamPanel.at(state.focusedPanel - 1)
}
Expand All @@ -161,7 +174,8 @@ export class RoamPanel {
private static get(panelId: PanelId): RoamPanel {
// lazily create one if doesn't already exist
if (!state.panels.has(panelId)) {
state.panels.set(panelId, new RoamPanel(panelId))
const panel = new RoamPanel(panelId)
state.panels.set(panelId, panel)
}
return assumeExists(state.panels.get(panelId))
}
Expand Down
4 changes: 2 additions & 2 deletions src/ts/core/roam/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const Selectors = {
mainContent: '.roam-article',
mainPanel: '.roam-body-main',

sidebarContent: '#roam-right-sidebar-content',
sidebarPage: '#right-sidebar > div',
sidebarContent: '.sidebar-content',
sidebarPage: '.sidebar-content > div',
sidebar: '#right-sidebar',

leftPanel: '.roam-sidebar-container',
Expand Down

0 comments on commit dadaa19

Please sign in to comment.