Skip to content

Commit c3cdc4e

Browse files
committed
Merge branch 'roam-js' into roam-js-vim-colemak
2 parents ac8c76b + 9d02098 commit c3cdc4e

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

src/ts/core/features/inc-dec-value.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {RoamDate} from '../roam/date'
33
import {Roam} from '../roam/roam'
44
import {RoamNode, Selection} from '../roam/roam-node'
55

6-
const createModifier = (change: number) => (num: number) => num + change
6+
export const createModifier = (change: number) => (num: number) => num + change
77

88
export const config: Feature = {
99
id: 'incDec',
@@ -62,7 +62,7 @@ const nameInsideBrackets = (text: string, cursor: number): string =>
6262

6363
const nameIsDate = (pageName: string): boolean => pageName.match(RoamDate.regex) !== null
6464

65-
const modifyDate = (date: Date, modifier: (input: number) => number): Date => {
65+
export const modifyDate = (date: Date, modifier: (input: number) => number): Date => {
6666
const newDate = new Date(date)
6767
newDate.setDate(modifier(date.getDate()))
6868
return newDate
@@ -92,9 +92,11 @@ export const modify = (modifier: (input: number) => number) => {
9292
const numberStr = left + right
9393
const numberStartedAt = node.text.substring(0, cursor)?.match(/[0-9]*$/)?.index!
9494

95-
let number = modifier(parseInt(numberStr))
95+
const newNumber = modifier(parseInt(numberStr, 10))
9696
newValue =
97-
node.text.substring(0, numberStartedAt) + number + node.text.substring(numberStartedAt + numberStr.length)
97+
node.text.substring(0, numberStartedAt) +
98+
newNumber +
99+
node.text.substring(numberStartedAt + numberStr.length)
98100
} else if (datesInContent && datesInContent.length === 1) {
99101
// e.g. Lor|em ipsum [[January 3rd, 2020]] 123
100102
newValue = node.text.replace(
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
import {RoamDate} from 'src/core/roam/date'
9+
import {createModifier, modifyDate} from 'src/core/features/inc-dec-value'
10+
11+
const getBlockText = (uid: string): string => {
12+
const block = RoamDb.getBlockByUid(uid)
13+
return block[':block/string']
14+
}
15+
16+
function selectedUid() {
17+
const htmlId = RoamBlock.selected().id
18+
return getBlockUid(htmlId)
19+
}
20+
21+
const rescheduleSelectedNote = (signal: SRSSignal) => {
22+
console.log('rescheduleSelectedNote', signal)
23+
const uid = selectedUid()
24+
const originalText = getBlockText(uid)
25+
RoamDb.updateBlockText(uid, new AnkiScheduler().schedule(new SM2Node(originalText), signal).text)
26+
}
27+
28+
const toggleDone = () => {
29+
const uid = selectedUid()
30+
const originalText = getBlockText(uid)
31+
let newText = originalText
32+
if (originalText.startsWith('{{[[DONE]]}} ')) {
33+
newText = originalText.replace('{{[[DONE]]}} ', '')
34+
} else if (originalText.startsWith('{{[[TODO]]}} ')) {
35+
newText = originalText.replace('{{[[TODO]]}} ', '{{[[DONE]]}} ')
36+
} else {
37+
newText = '{{[[DONE]]}} ' + originalText
38+
}
39+
40+
RoamDb.updateBlockText(uid, newText)
41+
}
42+
43+
const modifyBlockDate = (modifier: (input: number) => number) => {
44+
const uid = selectedUid()
45+
const originalText = getBlockText(uid)
46+
47+
const datesInContent = originalText.match(RoamDate.regex)
48+
if (!datesInContent || datesInContent.length !== 1) return
49+
50+
RoamDb.updateBlockText(
51+
uid,
52+
originalText.replace(
53+
datesInContent[0],
54+
RoamDate.formatPage(modifyDate(RoamDate.parseFromReference(datesInContent[0]), modifier))
55+
)
56+
)
57+
}
58+
59+
export const EditCommands = [
60+
nmap('cmd+enter', 'Toggle done', toggleDone),
61+
...SRSSignals.map(it =>
62+
nmap(`ctrl+shift+${it}`, `Reschedule Current Note (${SRSSignal[it]})`, () => rescheduleSelectedNote(it))
63+
),
64+
nmap('ctrl+alt+up', 'Increment Date', () => modifyBlockDate(createModifier(1))),
65+
nmap('ctrl+alt+down', 'Decrement Date', () => modifyBlockDate(createModifier(-1))),
66+
]

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 0 deletions
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)