-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathinc-dec-value.ts
108 lines (92 loc) · 4.35 KB
/
inc-dec-value.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {Feature, Shortcut} from '../settings'
import {RoamDate} from '../roam/date'
import {Roam} from '../roam/roam'
import {RoamNode, Selection} from '../roam/roam-node'
export const createModifier = (change: number) => (num: number) => num + change
export const config: Feature = {
id: 'incDec',
name: 'Increase / Decrease value or date',
settings: [
{
type: 'shortcut',
id: 'incShortcut',
label: 'Increase date or number by 1',
initValue: 'Ctrl+Alt+ArrowUp',
onPress: () => modify(createModifier(1)),
} as Shortcut,
{
type: 'shortcut',
id: 'decShortcut',
label: 'Decrease date or number by 1',
initValue: 'Ctrl+Alt+ArrowDown',
onPress: () => modify(createModifier(-1)),
} as Shortcut,
{
type: 'shortcut',
id: 'incWeekShortcut',
label: 'Increase date or number by 7',
initValue: 'Ctrl+Alt+PageUp',
onPress: () => modify(createModifier(7)),
} as Shortcut,
{
type: 'shortcut',
id: 'decWeekShortcut',
label: 'Decrease date or number by 7',
initValue: 'Ctrl+Alt+PageDown',
onPress: () => modify(createModifier(-7)),
} as Shortcut,
],
}
const openBracketsLeftIndex = (text: string, cursor: number): number => text.substring(0, cursor).lastIndexOf('[[')
const closingBracketsLeftIndex = (text: string, cursor: number): number => text.substring(0, cursor).lastIndexOf(']]')
const closingBracketsRightIndex = (text: string, cursor: number): number =>
cursor + text.substring(cursor).indexOf(']]')
const cursorPlacedBetweenBrackets = (text: string, cursor: number): boolean =>
openBracketsLeftIndex(text, cursor) < closingBracketsRightIndex(text, cursor) &&
closingBracketsLeftIndex(text, cursor) < openBracketsLeftIndex(text, cursor)
const cursorPlacedOnNumber = (text: any, cursor: number): boolean =>
text.substring(0, cursor).match(/[0-9]*$/)[0] + text.substring(cursor).match(/^[0-9]*/)[0] !== ''
const cursorPlacedOnDate = (text: string, cursor: number): boolean =>
cursorPlacedBetweenBrackets(text, cursor) && nameIsDate(nameInsideBrackets(text, cursor))
const nameInsideBrackets = (text: string, cursor: number): string =>
text.substring(text.substring(0, cursor).lastIndexOf('[['), cursor + text.substring(cursor).indexOf(']]') + 2)
const nameIsDate = (pageName: string): boolean => pageName.match(RoamDate.regex) !== null
export const modifyDate = (date: Date, modifier: (input: number) => number): Date => {
const newDate = new Date(date)
newDate.setDate(modifier(date.getDate()))
return newDate
}
export const modify = (modifier: (input: number) => number) => {
const node = Roam.getActiveRoamNode()
if (!node) return
const cursor = node.selection.start
const datesInContent = node.text.match(RoamDate.regex)
let newValue = node.text
if (cursorPlacedOnDate(node.text, cursor)) {
// e.g. Lorem ipsum [[Janu|ary 3rd, 2020]] 123
newValue =
node.text.substring(0, openBracketsLeftIndex(node.text, cursor)) +
RoamDate.formatPage(
modifyDate(RoamDate.parseFromReference(nameInsideBrackets(node.text, cursor)), modifier)
) +
node.text.substring(closingBracketsRightIndex(node.text, cursor) + 2)
} else if (cursorPlacedOnNumber(node.text, cursor)) {
// e.g. Lorem ipsum [[January 3rd, 2020]] 12|3
const left = node.text.substring(0, cursor)?.match(/[0-9]*$/)![0]
const right = node.text.substring(cursor)?.match(/^[0-9]*/)![0]
const numberStr = left + right
const numberStartedAt = node.text.substring(0, cursor)?.match(/[0-9]*$/)?.index!
const newNumber = modifier(parseInt(numberStr, 10))
newValue =
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(
datesInContent[0],
RoamDate.formatPage(modifyDate(RoamDate.parseFromReference(datesInContent[0]), modifier))
)
}
Roam.save(new RoamNode(newValue, new Selection(cursor, cursor)))
}