Skip to content

Commit

Permalink
SRS and completion from vim normal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Stvad committed Jun 25, 2024
1 parent 7456f3d commit 3ac6907
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/ts/core/features/vim-mode/commands/edit-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {nmap} from 'src/core/features/vim-mode/vim'
import {RoamBlock} from 'src/core/features/vim-mode/roam/roam-block'
import {SRSSignal, SRSSignals} from 'src/core/srs/scheduler'
import {AnkiScheduler} from 'src/core/srs/AnkiScheduler'
import {SM2Node} from 'src/core/srs/SM2Node'
import {RoamDb} from 'src/core/roam/roam-db'
import {getBlockUid} from 'src/core/roam/block'

const getBlockText = (uid: string): string => {
const block = RoamDb.getBlockByUid(uid)
return block[':block/string']
}

function selectedUid() {
const htmlId = RoamBlock.selected().id
return getBlockUid(htmlId)
}

const rescheduleSelectedNote = (signal: SRSSignal) => {
console.log('rescheduleSelectedNote', signal)
const uid = selectedUid()
const originalText = getBlockText(uid)
RoamDb.updateBlockText(uid, new AnkiScheduler().schedule(new SM2Node(originalText), signal).text)
}

const markDone = () => {
const uid = selectedUid()
const originalText = getBlockText(uid)
RoamDb.updateBlockText(uid, '{{[[DONE]]}} ' + originalText)
}

export const EditCommands = [
nmap('cmd+enter', 'Mark done', markDone),
...SRSSignals.map(it =>
nmap(`ctrl+shift+${it}`, `Reschedule Current Note (${SRSSignal[it]})`, () => rescheduleSelectedNote(it))
),
]
2 changes: 2 additions & 0 deletions src/ts/core/features/vim-mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {BlockManipulationCommands} from 'src/core/features/vim-mode/commands/blo
import {RoamBlock} from 'src/core/features/vim-mode/roam/roam-block'
import {HintCommands} from 'src/core/features/vim-mode/commands/hint-commands'
import {Browser} from 'src/core/common/browser'
import {EditCommands} from 'src/core/features/vim-mode/commands/edit-commands'

export const config: Feature = {
id: 'block_navigation_mode',
Expand All @@ -28,6 +29,7 @@ export const config: Feature = {
...VisualCommands,
...BlockManipulationCommands,
...HintCommands,
...EditCommands,
],
}

Expand Down
2 changes: 1 addition & 1 deletion src/ts/core/roam/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export const copyBlockReference = (htmlBlockId: string | undefined) => {
// An empirical observation:
const UID_LENGTH = 9
// Uid is the id Roam uses, blockId is the id of the html element
const getBlockUid = (htmlBlockId: string): string => htmlBlockId.substr(htmlBlockId?.length - UID_LENGTH)
export const getBlockUid = (htmlBlockId: string): string => htmlBlockId.substr(htmlBlockId?.length - UID_LENGTH)
5 changes: 5 additions & 0 deletions src/ts/core/roam/roam-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const RoamDb = {
return this.queryFirst('[:find ?e :in $ ?a :where [?e :block/uid ?a]]', uid)
},

updateBlockText(uid: string, newText: string) {
// @ts-ignore
runInPageContext((...args: any[]) => window.roamAlphaAPI.updateBlock(...args), {block: {uid, string: newText}})
},

getAllPages(): RoamPage[] {
return this.query(
'[:find ?uid ?title :where [?page :node/title ?title] [?page :block/uid ?uid]]'
Expand Down

0 comments on commit 3ac6907

Please sign in to comment.