Skip to content

Commit 3ac6907

Browse files
committed
SRS and completion from vim normal mode
1 parent 7456f3d commit 3ac6907

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {nmap} from 'src/core/features/vim-mode/vim'
2+
import {RoamBlock} from 'src/core/features/vim-mode/roam/roam-block'
3+
import {SRSSignal, SRSSignals} from 'src/core/srs/scheduler'
4+
import {AnkiScheduler} from 'src/core/srs/AnkiScheduler'
5+
import {SM2Node} from 'src/core/srs/SM2Node'
6+
import {RoamDb} from 'src/core/roam/roam-db'
7+
import {getBlockUid} from 'src/core/roam/block'
8+
9+
const getBlockText = (uid: string): string => {
10+
const block = RoamDb.getBlockByUid(uid)
11+
return block[':block/string']
12+
}
13+
14+
function selectedUid() {
15+
const htmlId = RoamBlock.selected().id
16+
return getBlockUid(htmlId)
17+
}
18+
19+
const rescheduleSelectedNote = (signal: SRSSignal) => {
20+
console.log('rescheduleSelectedNote', signal)
21+
const uid = selectedUid()
22+
const originalText = getBlockText(uid)
23+
RoamDb.updateBlockText(uid, new AnkiScheduler().schedule(new SM2Node(originalText), signal).text)
24+
}
25+
26+
const markDone = () => {
27+
const uid = selectedUid()
28+
const originalText = getBlockText(uid)
29+
RoamDb.updateBlockText(uid, '{{[[DONE]]}} ' + originalText)
30+
}
31+
32+
export const EditCommands = [
33+
nmap('cmd+enter', 'Mark done', markDone),
34+
...SRSSignals.map(it =>
35+
nmap(`ctrl+shift+${it}`, `Reschedule Current Note (${SRSSignal[it]})`, () => rescheduleSelectedNote(it))
36+
),
37+
]

src/ts/core/features/vim-mode/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {BlockManipulationCommands} from 'src/core/features/vim-mode/commands/blo
1111
import {RoamBlock} from 'src/core/features/vim-mode/roam/roam-block'
1212
import {HintCommands} from 'src/core/features/vim-mode/commands/hint-commands'
1313
import {Browser} from 'src/core/common/browser'
14+
import {EditCommands} from 'src/core/features/vim-mode/commands/edit-commands'
1415

1516
export const config: Feature = {
1617
id: 'block_navigation_mode',
@@ -28,6 +29,7 @@ export const config: Feature = {
2829
...VisualCommands,
2930
...BlockManipulationCommands,
3031
...HintCommands,
32+
...EditCommands,
3133
],
3234
}
3335

src/ts/core/roam/block.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export const copyBlockReference = (htmlBlockId: string | undefined) => {
1717
// An empirical observation:
1818
const UID_LENGTH = 9
1919
// Uid is the id Roam uses, blockId is the id of the html element
20-
const getBlockUid = (htmlBlockId: string): string => htmlBlockId.substr(htmlBlockId?.length - UID_LENGTH)
20+
export const getBlockUid = (htmlBlockId: string): string => htmlBlockId.substr(htmlBlockId?.length - UID_LENGTH)

src/ts/core/roam/roam-db.ts

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export const RoamDb = {
3434
return this.queryFirst('[:find ?e :in $ ?a :where [?e :block/uid ?a]]', uid)
3535
},
3636

37+
updateBlockText(uid: string, newText: string) {
38+
// @ts-ignore
39+
runInPageContext((...args: any[]) => window.roamAlphaAPI.updateBlock(...args), {block: {uid, string: newText}})
40+
},
41+
3742
getAllPages(): RoamPage[] {
3843
return this.query(
3944
'[:find ?uid ?title :where [?page :node/title ?title] [?page :block/uid ?uid]]'

0 commit comments

Comments
 (0)