-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcommands.ts
184 lines (165 loc) · 5 KB
/
commands.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { Moment } from "moment";
import { Command, MarkdownView, Notice, TFile } from "obsidian";
import {
createDailyNote,
createMonthlyNote,
createQuarterlyNote,
createWeeklyNote,
createYearlyNote,
getAllDailyNotes,
getAllMonthlyNotes,
getAllQuarterlyNotes,
getAllWeeklyNotes,
getAllYearlyNotes,
getDailyNote,
getDateFromFile,
getMonthlyNote,
getQuarterlyNote,
getWeeklyNote,
getYearlyNote,
} from "obsidian-daily-notes-interface";
import type { IPeriodicity } from "src/settings";
import { orderedValues } from "src/utils";
interface IPeriodConfig {
unitOfTime: "day" | "week" | "month" | "quarter" | "year";
relativeUnit: string;
createNote: (date: Moment) => Promise<TFile>;
getNote: (date: Moment, allFiles: Record<string, TFile>) => TFile;
getAllNotes: () => Record<string, TFile>;
}
export const periodConfigs: Record<IPeriodicity, IPeriodConfig> = {
daily: {
unitOfTime: "day",
relativeUnit: "today",
createNote: createDailyNote,
getNote: getDailyNote,
getAllNotes: getAllDailyNotes,
},
weekly: {
unitOfTime: "week",
relativeUnit: "this week",
createNote: createWeeklyNote,
getNote: getWeeklyNote,
getAllNotes: getAllWeeklyNotes,
},
monthly: {
unitOfTime: "month",
relativeUnit: "this month",
createNote: createMonthlyNote,
getNote: getMonthlyNote,
getAllNotes: getAllMonthlyNotes,
},
quarterly: {
unitOfTime: "quarter",
relativeUnit: "this quarter",
createNote: createQuarterlyNote,
getNote: getQuarterlyNote,
getAllNotes: getAllQuarterlyNotes,
},
yearly: {
unitOfTime: "year",
relativeUnit: "this year",
createNote: createYearlyNote,
getNote: getYearlyNote,
getAllNotes: getAllYearlyNotes,
},
};
export async function openPeriodicNote(
periodicity: IPeriodicity,
date: Moment,
inNewSplit: boolean
): Promise<void> {
const config = periodConfigs[periodicity];
const startOfPeriod = date.clone().startOf(config.unitOfTime);
let allNotes: Record<string, TFile>;
try {
allNotes = config.getAllNotes();
} catch (err) {
console.error(`failed to find your ${periodicity} notes folder`, err);
new Notice(`Failed to find your ${periodicity} notes folder`);
return;
}
let periodicNote = config.getNote(startOfPeriod, allNotes);
if (!periodicNote) {
periodicNote = await config.createNote(startOfPeriod);
}
await openFile(periodicNote, inNewSplit);
}
function getActiveFile(): TFile | null {
const { workspace } = window.app;
const activeView = workspace.getActiveViewOfType(MarkdownView);
return activeView?.file;
}
async function openFile(file: TFile, inNewSplit: boolean): Promise<void> {
const { workspace } = window.app;
const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
await leaf.openFile(file, { active: true });
}
async function openNextNote(periodicity: IPeriodicity): Promise<void> {
const config = periodConfigs[periodicity];
const activeFile = getActiveFile();
try {
const allNotes = orderedValues(config.getAllNotes());
const activeNoteIndex = allNotes.findIndex((file) => file === activeFile);
const nextNote = allNotes[activeNoteIndex + 1];
if (nextNote) {
await openFile(nextNote, false);
}
} catch (err) {
console.error(`failed to find your ${periodicity} notes folder`, err);
new Notice(`Failed to find your ${periodicity} notes folder`);
}
}
async function openPrevNote(periodicity: IPeriodicity): Promise<void> {
const config = periodConfigs[periodicity];
const activeFile = getActiveFile();
try {
const allNotes = orderedValues(config.getAllNotes());
const activeNoteIndex = allNotes.findIndex((file) => file === activeFile);
const prevNote = allNotes[activeNoteIndex - 1];
if (prevNote) {
await openFile(prevNote, false);
}
} catch (err) {
console.error(`failed to find your ${periodicity} notes folder`, err);
new Notice(`Failed to find your ${periodicity} notes folder`);
}
}
export function getCommands(periodicity: IPeriodicity): Command[] {
const config = periodConfigs[periodicity];
return [
{
id: `open-${periodicity}-note`,
name: `Open ${periodicity} note`,
callback: () => openPeriodicNote(periodicity, window.moment(), false),
},
{
id: `next-${periodicity}-note`,
name: `Open next ${periodicity} note`,
checkCallback: (checking: boolean) => {
if (checking) {
const activeFile = getActiveFile();
return !!(
activeFile && getDateFromFile(activeFile, config.unitOfTime)
);
}
openNextNote(periodicity);
},
},
{
id: `prev-${periodicity}-note`,
name: `Open previous ${periodicity} note`,
checkCallback: (checking: boolean) => {
if (checking) {
const activeFile = getActiveFile();
return !!(
activeFile && getDateFromFile(activeFile, config.unitOfTime)
);
}
openPrevNote(periodicity);
},
},
];
}