diff --git a/src/ts/core/features/inc-dec-value.ts b/src/ts/core/features/inc-dec-value.ts index 0265afc..9bd653b 100644 --- a/src/ts/core/features/inc-dec-value.ts +++ b/src/ts/core/features/inc-dec-value.ts @@ -3,7 +3,7 @@ import {RoamDate} from '../roam/date' import {Roam} from '../roam/roam' import {RoamNode, Selection} from '../roam/roam-node' -const createModifier = (change: number) => (num: number) => num + change +export const createModifier = (change: number) => (num: number) => num + change export const config: Feature = { id: 'incDec', @@ -62,7 +62,7 @@ const nameInsideBrackets = (text: string, cursor: number): string => const nameIsDate = (pageName: string): boolean => pageName.match(RoamDate.regex) !== null -const modifyDate = (date: Date, modifier: (input: number) => number): Date => { +export const modifyDate = (date: Date, modifier: (input: number) => number): Date => { const newDate = new Date(date) newDate.setDate(modifier(date.getDate())) return newDate @@ -92,9 +92,11 @@ export const modify = (modifier: (input: number) => number) => { const numberStr = left + right const numberStartedAt = node.text.substring(0, cursor)?.match(/[0-9]*$/)?.index! - let number = modifier(parseInt(numberStr)) + const newNumber = modifier(parseInt(numberStr, 10)) newValue = - node.text.substring(0, numberStartedAt) + number + node.text.substring(numberStartedAt + numberStr.length) + node.text.substring(0, numberStartedAt) + + newNumber + + node.text.substring(numberStartedAt + numberStr.length) } else if (datesInContent && datesInContent.length === 1) { // e.g. Lor|em ipsum [[January 3rd, 2020]] 123 newValue = node.text.replace( diff --git a/src/ts/core/features/vim-mode/commands/edit-commands.ts b/src/ts/core/features/vim-mode/commands/edit-commands.ts index 0d0aa07..075c6db 100644 --- a/src/ts/core/features/vim-mode/commands/edit-commands.ts +++ b/src/ts/core/features/vim-mode/commands/edit-commands.ts @@ -5,6 +5,8 @@ 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' +import {RoamDate} from 'src/core/roam/date' +import {createModifier, modifyDate} from 'src/core/features/inc-dec-value' const getBlockText = (uid: string): string => { const block = RoamDb.getBlockByUid(uid) @@ -29,9 +31,27 @@ const markDone = () => { RoamDb.updateBlockText(uid, '{{[[DONE]]}} ' + originalText) } +const modifyBlockDate = (modifier: (input: number) => number) => { + const uid = selectedUid() + const originalText = getBlockText(uid) + + const datesInContent = originalText.match(RoamDate.regex) + if (!datesInContent || datesInContent.length !== 1) return + + RoamDb.updateBlockText( + uid, + originalText.replace( + datesInContent[0], + RoamDate.formatPage(modifyDate(RoamDate.parseFromReference(datesInContent[0]), modifier)) + ) + ) +} + export const EditCommands = [ nmap('cmd+enter', 'Mark done', markDone), ...SRSSignals.map(it => nmap(`ctrl+shift+${it}`, `Reschedule Current Note (${SRSSignal[it]})`, () => rescheduleSelectedNote(it)) ), + nmap('ctrl+alt+up', 'Increment Date', () => modifyBlockDate(createModifier(1))), + nmap('ctrl+alt+down', 'Decrement Date', () => modifyBlockDate(createModifier(-1))), ]