Skip to content

Commit

Permalink
Restore commands to select first and last blocks. Fix bug where the p…
Browse files Browse the repository at this point in the history
…revious block was being scrolled into view, rather than the just selected block
  • Loading branch information
tntmarket committed Jun 29, 2020
1 parent 1543927 commit 3dbe0ea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/ts/core/features/vim-mode/commands/navigation-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const selectBlockUp = (mode: Mode) => jumpBlocksInFocusedPanel(mode, -1)

const selectBlockDown = (mode: Mode) => jumpBlocksInFocusedPanel(mode, 1)

const selectFirstBlock = () => {
const panel = RoamPanel.selected()
panel.selectBlock(panel.firstBlock().id)
}

const selectLastBlock = () => {
const panel = RoamPanel.selected()
panel.selectBlock(panel.lastBlock().id)
}

const selectManyBlocksUp = (mode: Mode) => jumpBlocksInFocusedPanel(mode, -8)

const selectManyBlocksDown = (mode: Mode) => jumpBlocksInFocusedPanel(mode, 8)
Expand All @@ -16,6 +26,8 @@ const scrollDown = () => RoamPanel.selected().scrollAndReselectBlockToStayVisibl
export const NavigationCommands = [
nvmap('k', 'Select Block Up', selectBlockUp),
nvmap('j', 'Select Block Down', selectBlockDown),
nmap('g g', 'Select First Block', selectFirstBlock),
nmap('G', 'Select Last Block', selectLastBlock),
nmap('ctrl+u', 'Select Many Blocks Up', selectManyBlocksUp),
nmap('ctrl+d', 'Select Many Blocks Down', selectManyBlocksDown),
nvmap('ctrl+y', 'Scroll Up', scrollUp),
Expand Down
22 changes: 12 additions & 10 deletions src/ts/core/features/vim-mode/roam/roam-panel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {findLast} from 'lodash'
import {findLast, last} from 'lodash'

import {Selectors} from 'src/core/roam/selectors'
import {assumeExists} from 'src/core/common/assert'
import {RoamBlock, BlockElement, BlockId} from 'src/core/features/vim-mode/roam/roam-block'
import {BlockElement, BlockId, RoamBlock} from 'src/core/features/vim-mode/roam/roam-block'
import {relativeItem} from 'src/core/common/array'

type BlockNavigationState = {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class RoamPanel {
if (!blockId || !document.getElementById(blockId)) {
// Fallback to selecting the first block,
// if blockId is not initialized yet, or the block no longer exists
const firstBlockId = this.firstBlockId()
const firstBlockId = this.firstBlock().id
this.selectBlock(firstBlockId)
return firstBlockId
}
Expand All @@ -66,12 +66,12 @@ export class RoamPanel {

selectBlock(blockId: string) {
state.panels.set(this.panelId, blockId)
this.scrollUntilBlockIsVisible(this.selectedBlock().element)
}

selectRelativeBlock(blocksToJump: number) {
const block = this.selectedBlock().element
this.selectBlock(this.relativeBlockId(block.id, blocksToJump))
this.scrollUntilBlockIsVisible(block)
}

scrollUntilBlockIsVisible(block: BlockElement) {
Expand All @@ -87,8 +87,12 @@ export class RoamPanel {
}
}

firstBlockId(): BlockId {
return assumeExists(this.element.querySelector(Selectors.block)).id
firstBlock(): BlockElement {
return assumeExists(this.element.querySelector(Selectors.block) as BlockElement)
}

lastBlock(): BlockElement {
return assumeExists(last(this.blocks()) as BlockElement)
}

static get(panelId: PanelId): RoamPanel {
Expand Down Expand Up @@ -125,13 +129,11 @@ export class RoamPanel {
}

private firstVisibleBlock(): BlockElement {
const blocks = this.blocks()
return assumeExists(blocks.find(blockIsVisible), 'Could not find any visible block')
return assumeExists(this.blocks().find(blockIsVisible), 'Could not find any visible block')
}

private lastVisibleBlock() {
const blocks = this.blocks()
return assumeExists(findLast(blocks, blockIsVisible), 'Could not find any visible block')
return assumeExists(findLast(this.blocks(), blockIsVisible), 'Could not find any visible block')
}
}

Expand Down

0 comments on commit 3dbe0ea

Please sign in to comment.