-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
342 lines (297 loc) · 8.48 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import {
App,
ItemView,
Notice,
Plugin,
PluginSettingTab,
Setting,
TAbstractFile,
TFile,
getIcon,
} from "obsidian";
const AUTO_UPDATE_DAILY_NOTE = "autoUpdateDailyNote";
const DAYS = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
interface CanvasView extends ItemView {
canvas: Canvas;
}
interface Canvas {
cardMenuEl: HTMLElement;
nodes: CanvasNode[];
removeNode(node: CanvasNode): void;
requestSave(): void;
createFileNode(options: any): CanvasNode;
deselectAll(): void;
addNode(node: CanvasNode): void;
}
interface CanvasNode {
unknownData: UnknownData;
nodeEl: HTMLElement;
file: TFile;
x: number;
y: number;
width: number;
height: number;
}
interface UnknownData {
nodeType: string;
}
interface CanvasDailyNotePluginSettings {
createIfNotExists: boolean;
skipMonday: boolean;
skipTuesday: boolean;
skipWednesday: boolean;
skipThursday: boolean;
skipFriday: boolean;
skipSaturday: boolean;
skipSunday: boolean;
}
const DEFAULT_SETTINGS: CanvasDailyNotePluginSettings = {
createIfNotExists: false,
skipMonday: false,
skipTuesday: false,
skipWednesday: false,
skipThursday: false,
skipFriday: false,
skipSaturday: false,
skipSunday: false,
};
interface DailyNotePluginOptions {
folder: string;
}
interface DailyNotePlugin {
getDailyNote(): TFile;
options: DailyNotePluginOptions;
}
/**
* This allows a "live-reload" of Obsidian when developing the plugin.
* Any changes to the code will force reload Obsidian.
*/
if (process.env.NODE_ENV === "development") {
new EventSource("http://127.0.0.1:8000/esbuild").addEventListener(
"change",
() => location.reload()
);
}
export default class CanvasDailyNotePlugin extends Plugin {
settings: CanvasDailyNotePluginSettings;
dailyNotePlugin: DailyNotePlugin;
async onload() {
await this.loadSettings();
// Get an instance of the daily notes plugin so we can interact with it
this.dailyNotePlugin = (this.app as any).internalPlugins.getPluginById(
"daily-notes"
)?.instance;
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new CanvasDailyNotePluginSettingTab(this.app, this));
// Hook into the file open event
this.registerEvent(
this.app.workspace.on("file-open", this.handleFileOpen.bind(this))
);
}
/**
* When a file is opened, we check if the file is a canvas. If it is, we'll hook into it.
*/
async handleFileOpen() {
const canvasView = this.app.workspace.getActiveViewOfType(
ItemView
) as CanvasView;
// Only need to run this code if we're looking at a canvas
if (canvasView?.getViewType() !== "canvas") {
return;
}
const canvas = canvasView?.canvas;
this.createButton(canvas);
this.processCanvasNodes(canvas);
}
/**
* Add a new button to the card UI at the bottom. Clicking the button will attempt to add a daily note to the canvas.
* @param canvas
*/
createButton(canvas: Canvas) {
const cardMenuEl = canvas.cardMenuEl;
// Only create the canvas button if it doesn't already exist
if (!cardMenuEl.querySelector(".canvas-button-adddailynote")) {
const button = cardMenuEl.createEl("div", {
attr: {
class: "canvas-card-menu-button canvas-button-adddailynote",
},
});
const icon = getIcon("calendar") as Node;
button.appendChild(icon).addEventListener("click", async () => {
let dailyFile = this.getExistingDailyFile();
if (!dailyFile && !this.settings.createIfNotExists) {
new Notice(
"Daily note currently does not exist and plugin settings are set to not create it."
);
return;
}
// Don't create note on days that are configured to be skipped
const dayOfTheWeek = DAYS[new Date().getDay()];
// @ts-ignore
if (!dailyFile && this.settings[`skip${dayOfTheWeek}`]) {
new Notice(
`Daily note currently does not exist and plugin settings are set to not create it on ${dayOfTheWeek}.`
);
return;
}
// This will either get the existing note or create a new one. Either way, returns the file.
dailyFile = await this.dailyNotePlugin.getDailyNote();
if (dailyFile instanceof TFile) {
this.addDailyNote(canvas, dailyFile);
}
});
}
}
/**
* This services two purposes
* 1. Adding a styling class to the daily note nodes
* 2. Updating any out of date daily note nodes with today's note
* @param canvas
*/
processCanvasNodes(canvas: Canvas) {
let dailyFile = this.getExistingDailyFile();
canvas.nodes.forEach(async (node) => {
if (node.unknownData.nodeType !== AUTO_UPDATE_DAILY_NOTE) {
return;
}
// Add class to each found auto daily note
node.nodeEl.addClass("canvas-node-dailynote");
// If the note is out of date, replace it with a new daily note node in the same x/y with the same width/height
if (node?.file?.path !== dailyFile?.path || !node.file) {
if (!dailyFile && !this.settings.createIfNotExists) {
return;
}
const dayOfTheWeek = DAYS[new Date().getDay()];
// @ts-ignore
if (!dailyFile && this.settings[`skip${dayOfTheWeek}`]) {
return;
}
canvas.removeNode(node);
canvas.requestSave();
dailyFile = await this.dailyNotePlugin.getDailyNote();
if (dailyFile instanceof TFile) {
this.addDailyNote(canvas, dailyFile, {
x: node.x,
y: node.y,
width: node.width,
height: node.height,
});
}
}
});
}
/**
* Gets the existing daily note based on the daily notes plugin settings or returns null if it does not exist.
*/
getExistingDailyFile(): TFile | TAbstractFile | null | undefined {
const dailyFolder = this.dailyNotePlugin.options.folder;
const expectedNotePath = `${dailyFolder.replace(
/^\/|\\/,
""
)}/${new Date().getFullYear()}-${String(
new Date().getMonth() + 1
).padStart(2, "0")}-${String(new Date().getDate()).padStart(
2,
"0"
)}.md`;
let dailyFile = this.app.vault.getAbstractFileByPath(expectedNotePath);
return dailyFile;
}
/**
* Adds the Daily Note node to the canvas. Stores a special "nodeType" property so we can identify it later.
* @param canvas
* @param dailyFile
* @param options
*/
addDailyNote(canvas: Canvas, dailyFile: TFile, options: any = {}) {
const dailyFileNode = canvas.createFileNode({
pos: {
x: options.x || 0,
y: options.y || 0,
height: options.height || 500,
width: options.width || 500,
},
size: {
x: options.x || 0,
y: options.y || 0,
height: options.height || 500,
width: options.width || 500,
},
file: dailyFile,
path: this.dailyNotePlugin.options.folder,
focus: false,
save: true,
});
dailyFileNode.unknownData.nodeType = AUTO_UPDATE_DAILY_NOTE;
canvas.deselectAll();
canvas.addNode(dailyFileNode);
canvas.requestSave();
}
onunload() {}
/**
* Load data from disk, stored in data.json in plugin folder
*/
async loadSettings() {
const data = (await this.loadData()) || {};
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
}
/**
* Save data to disk, stored in data.json in plugin folder
*/
async saveSettings() {
await this.saveData(this.settings);
}
}
class CanvasDailyNotePluginSettingTab extends PluginSettingTab {
plugin: CanvasDailyNotePlugin;
constructor(app: App, plugin: CanvasDailyNotePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Automatically create daily note")
.setDesc(
`Should the plugin attempt to create the daily note if it does not exist?`
)
.addToggle((component) => {
component.setValue(this.plugin.settings.createIfNotExists);
component.onChange((value) => {
this.plugin.settings.createIfNotExists = value;
this.plugin.saveSettings();
});
});
containerEl.createEl("hr");
containerEl.createEl("h1", { text: "Skip days" });
containerEl.createEl("p", {
attr: {
style: "display: block; margin-bottom: 10px",
},
text: "If there are certain days of the week you wish to skip creating a new note for, you can configure that here. The plugin will not attempt to automatically create new notes on those days.",
});
DAYS.forEach((day) => {
new Setting(containerEl)
.setName(day)
.setDesc(`Skip automatically creating notes on ${day}?`)
.addToggle((component) => {
// @ts-ignore
component.setValue(this.plugin.settings[`skip${day}`]);
component.onChange((value) => {
// @ts-ignore
this.plugin.settings[`skip${day}`] = value;
this.plugin.saveSettings();
});
});
});
}
}