-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
531 lines (481 loc) · 14.6 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// main.ts
import {
App,
Notice,
Plugin,
PluginManifest,
TAbstractFile,
TFile,
TFolder,
WorkspaceLeaf,
} from "obsidian";
import {
DEFAULT_SETTINGS,
PluginDataJson,
langCodes,
} from "src/interfaces/GlobalSettings";
import {
loadTasksJsonFromDiskToSS,
onUnloadSave,
writeTasksFromSessionStorageToDisk,
} from "src/utils/tasksCache";
import { KanbanView } from "./src/views/KanbanView";
import { RealTimeScanning } from "src/utils/RealTimeScanning";
import { ScanningVault } from "src/utils/ScanningVault";
import { TaskBoardIcon } from "src/types/Icons";
import { TaskBoardSettingTab } from "./src/views/TaskBoardSettingTab";
import { VIEW_TYPE_TASKBOARD } from "src/types/GlobalVariables";
import { openAddNewTaskModal } from "src/services/OpenModals";
import { t } from "src/utils/lang/helper";
export default class TaskBoard extends Plugin {
app: App;
plugin: TaskBoard;
settings: PluginDataJson = DEFAULT_SETTINGS;
scanningVault: ScanningVault;
realTimeScanning: RealTimeScanning;
taskBoardFileStack: string[] = [];
editorModified: boolean;
currentModifiedFile: TFile | null;
IsTasksJsonChanged: boolean;
private _leafIsActive: boolean; // Private property to track leaf state
private ribbonIconEl: HTMLElement | null; // Store ribbonIconEl globally for reference
constructor(app: App, menifest: PluginManifest) {
super(app, menifest);
this.plugin = this;
this.app = this.plugin.app;
this.settings = DEFAULT_SETTINGS;
this.scanningVault = new ScanningVault(this.app, this.plugin);
this.realTimeScanning = new RealTimeScanning(this.app, this.plugin);
this.editorModified = false;
this.currentModifiedFile = null;
this.IsTasksJsonChanged = false;
this._leafIsActive = false;
this.ribbonIconEl = null;
}
async onload() {
console.log("TaskBoard : Loading plugin ...");
//Creates a Icon on Ribbon Bar
await this.getRibbonIcon();
// Loads settings data and creating the Settings Tab in main Setting
await this.loadSettings();
this.addSettingTab(new TaskBoardSettingTab(this.app, this));
this.getLanguage();
// Register events and commands only on Layout is ready
this.app.workspace.onLayoutReady(() => {
// Creating Few Events
this.registerEvents();
// Register few commands
this.registerCommands();
// For non-realtime scanning and scanning last modified files
this.createLocalStorageAndScanModifiedFiles();
// Run scanVaultForTasks if scanVaultAtStartup is true
this.scanVaultAtStartup();
// Load all the tasks from the tasks.json into sessionStorage and start Periodic scanning
this.loadTasksDataToSS();
// Register the Kanban view
this.registerTaskBoardView();
});
this.registerTaskBoardStatusBar();
}
onunload() {
console.log("TaskBoard : Unloading plugin...");
onUnloadSave(this.plugin);
// this.app.workspace.detachLeavesOfType(VIEW_TYPE_TASKBOARD);
}
async activateView(leafLayout: string) {
let leaf: WorkspaceLeaf | null = null;
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_TASKBOARD);
function isFromMainWindow(leaf: WorkspaceLeaf): boolean | undefined {
if (!leaf.view.containerEl.ownerDocument.defaultView) return;
return "Notice" in leaf.view.containerEl.ownerDocument.defaultView;
}
// Separate leaves into MainWindow and SeparateWindow categories
const mainWindowLeaf = leaves.find((leaf) => isFromMainWindow(leaf));
const separateWindowLeaf = leaves.find(
(leaf) => !isFromMainWindow(leaf)
);
if (leafLayout === "icon") {
// Focus on any existing leaf, prioritizing MainWindow
leaf =
mainWindowLeaf ||
separateWindowLeaf ||
this.app.workspace.getLeaf("tab");
} else if (leafLayout === "tab") {
// Check if a leaf exists in MainWindow
if (mainWindowLeaf) {
// Prevent duplicate in MainWindow
leaf = mainWindowLeaf;
} else {
// Allow opening a new leaf in MainWindow
leaf = this.app.workspace.getLeaf("tab");
}
} else if (leafLayout === "window") {
// Check if a leaf exists in SeparateWindow
if (separateWindowLeaf) {
// Prevent duplicate in SeparateWindow
leaf = separateWindowLeaf;
} else {
// Allow opening a new leaf in SeparateWindow
leaf = this.app.workspace.getLeaf("window");
}
} else {
// Default behavior: open in MainWindow
leaf = this.app.workspace.getLeaf("tab");
}
// Open or focus the leaf
if (leaf) {
this.leafIsActive = true;
await leaf.setViewState({
type: VIEW_TYPE_TASKBOARD,
active: true,
});
this.app.workspace.revealLeaf(leaf);
}
}
async getRibbonIcon() {
// Create a ribbon icon to open the Kanban board view
this.ribbonIconEl = this.addRibbonIcon(TaskBoardIcon, t(132), () => {
this.activateView("icon");
// this.app.workspace.ensureSideLeaf(VIEW_TYPE_TASKBOARD, "right", {
// active: true,
// reveal: true,
// });
});
}
get leafIsActive(): boolean {
return this._leafIsActive;
}
set leafIsActive(value: boolean) {
this._leafIsActive = value;
if (this._leafIsActive) {
this.ribbonIconEl?.addClass("task-board-ribbon-class");
} else {
this.ribbonIconEl?.removeClass("task-board-ribbon-class");
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
getLanguage() {
const obsidianLang = window.localStorage.getItem("language");
if (obsidianLang && obsidianLang in langCodes) {
localStorage.setItem("taskBoardLang", obsidianLang);
this.settings.data.globalSettings.lang = obsidianLang;
this.saveSettings();
} else {
localStorage.setItem(
"taskBoardLang",
// this.settings.data.globalSettings.lang
"en"
);
}
}
createLocalStorageAndScanModifiedFiles() {
// Following line will create a localStorage if the realTimeScanning setting is FALSE. And then it will scan the previous files which didnt got scanned, becaues the Obsidian was closed before that or crashed.
this.realTimeScanning.initializeStack(
this.settings.data.globalSettings.realTimeScanning
);
this.realTimeScanning.processStack();
}
scanVaultAtStartup() {
if (this.settings.data.globalSettings.scanVaultAtStartup) {
this.scanningVault.scanVaultForTasks();
}
}
loadTasksDataToSS() {
const _ = loadTasksJsonFromDiskToSS(this.plugin);
// And a setInteval is registered to start periodic saving.
}
registerTaskBoardView() {
this.registerView(
VIEW_TYPE_TASKBOARD,
(leaf) => new KanbanView(this, leaf)
);
}
registerTaskBoardStatusBar() {
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
// const statusBarItemEl = this.addStatusBarItem();
// statusBarItemEl.setText("Next task in # min");
}
registerCommands() {
this.addCommand({
id: "add-new-task",
name: t(131),
callback: () => {
const activeEditor = this.app.workspace.activeEditor?.editor;
const activeFile = this.app.workspace.getActiveFile();
if (activeEditor && activeFile) {
openAddNewTaskModal(this.app, this.plugin, activeFile);
} else {
new Notice(t(147));
}
},
});
this.addCommand({
id: "open-task-board",
name: t(132),
callback: () => {
this.activateView("tab");
},
});
this.addCommand({
id: "open-task-board-new-window",
name: t(133),
callback: () => {
this.activateView("window");
},
});
// // TODO : Remove this command before publishing, DEV commands
// this.addCommand({
// id: "4",
// name: "DEV : Save Data from sessionStorage to Disk",
// callback: () => {
// writeTasksJsonToDisk(this.plugin);
// },
// });
// this.addCommand({
// id: "5",
// name: "DEV : REFRESH_COLUMN",
// callback: () => {
// eventEmitter.emit("REFRESH_COLUMN");
// },
// });
}
registerEvents() {
// Start a timer to write tasks from sessionStorage to disk every 5 minutes
this.registerInterval(
window.setInterval(async () => {
await writeTasksFromSessionStorageToDisk(this.plugin);
}, 5 * 60 * 1000)
);
this.registerEvent(
this.app.vault.on("modify", (file: TAbstractFile) => {
this.editorModified = true;
if (file instanceof TFile) {
if (!file.path.endsWith(".excalidraw.md")) {
this.currentModifiedFile = file;
}
}
})
);
// Listen for editor-blur event and trigger scanning if the editor was modified
this.registerEvent(
this.app.workspace.on(
"active-leaf-change",
(leaf: WorkspaceLeaf | null) => {
this.onFileModifiedAndLostFocus();
}
)
);
this.registerDomEvent(window, "blur", () => {
this.onFileModifiedAndLostFocus();
});
// this.registerEvent(
// this.app.vault.on("create", (file) => {
// // NOT REQUIRED : This will be same as the modify functinality, since after adding the file, it will be modified, so i will catch that.
// })
// );
// this.registerEvent(
// this.app.vault.on("rename", (file) => {
// // console.log(
// // "TODO : A file has been renamed, immediately, change the corresponding data in Tasks.json file. That is find the old object under Pending and Completed part in tasks.json and either delete it or best way will be to replace the old name with new one."
// // );
// })
// );
// this.registerEvent(
// this.app.vault.on("delete", (file) => {
// // console.log(
// // "TODO : A file has been deleted, immediately remove the corresponding data in Tasks.json file."
// // );
// })
// );
const closeButton = document.querySelector<HTMLElement>(
".titlebar-button.mod-close"
);
if (closeButton) {
this.registerDomEvent(closeButton, "mouseenter", () => {
onUnloadSave(this.plugin);
});
}
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file, source, leaf) => {
if (source === "link-context-menu") return;
const fileIsFile = file instanceof TFile;
const fileIsFolder = file instanceof TFolder;
// const leafIsMarkdown = leaf?.view instanceof MarkdownView;
// const leafIsKanban = leaf?.view instanceof KanbanView;
// if (leafIsKanban || source === "pane-more-options") {
// console.log("MENU : If the fileIsFile ");
// menu.addItem((item) => {
// item.setTitle("Refresh Board")
// .setIcon(RefreshIcon)
// .setSection("pane")
// .onClick(() => {
// eventEmitter.emit("REFRESH_BOARD");
// });
// });
// }
if (fileIsFile) {
menu.addItem((item) => {
item.setTitle(t(134))
.setIcon(TaskBoardIcon)
.setSection("action")
.onClick(() => {
this.scanningVault.updateTasksFromFiles([file]);
this.scanningVault.saveTasksToFile();
});
});
if (
this.settings.data.globalSettings.scanFilters.files
.polarity === 2
) {
menu.addItem((item) => {
item.setTitle(t(135))
.setIcon(TaskBoardIcon)
.setSection("action")
.onClick(() => {
this.settings.data.globalSettings.scanFilters.files.values.push(
file.path
);
this.saveSettings();
});
});
}
if (
this.settings.data.globalSettings.scanFilters.files
.polarity === 1
) {
menu.addItem((item) => {
item.setTitle(t(136))
.setIcon(TaskBoardIcon)
.setSection("action")
.onClick(() => {
this.settings.data.globalSettings.scanFilters.files.values.push(
file.path
);
this.saveSettings();
});
});
}
// menu.addItem((item) => {
// item.setTitle("DEV : Save Changes") // Cant keep this option in the meny, only for dev
// .setIcon(TaskBoardIcon)
// .setSection("action")
// .onClick(() => {
// onUnloadSave(this.plugin);
// });
// });
}
if (fileIsFolder) {
// TODO : Implement this in future releases
// menu.addItem((item) => {
// item.setTitle("Update tasks from this folder")
// .setIcon(TaskBoardIcon)
// .setSection("action")
// .onClick(() => {
// });
// });
if (
this.settings.data.globalSettings.scanFilters.folders
.polarity === 2
) {
menu.addItem((item) => {
item.setTitle(t(137))
.setIcon(TaskBoardIcon)
.setSection("action")
.onClick(() => {
this.settings.data.globalSettings.scanFilters.folders.values.push(
file.path
);
this.saveSettings();
});
});
}
if (
this.settings.data.globalSettings.scanFilters.folders
.polarity === 1
) {
menu.addItem((item) => {
item.setTitle(t(138))
.setIcon(TaskBoardIcon)
.setSection("action")
.onClick(() => {
this.settings.data.globalSettings.scanFilters.folders.values.push(
file.path
);
this.saveSettings();
});
});
}
}
// if (
// !Platform.isMobile &&
// leafIsKanban &&
// leaf &&
// source === "sidebar-context-menu"
// ) {
// console.log("MENU : If the 'sidebar-context-menu'");
// menu.addItem((item) => {
// item.setTitle("Refresh Board")
// .setIcon(RefreshIcon)
// .setSection("action")
// .onClick(() => {
// eventEmitter.emit("REFRESH_BOARD");
// });
// })
// .addItem((item) => {
// item.setTitle("Open Board Settings")
// .setIcon(RefreshIcon)
// .setSection("action")
// .onClick(() => {
// // Need to find a way to open the Board Config Modal and then also to
// });
// })
// .addItem((item) => {
// item.setTitle("DEV : Save Changes") // Delete this item before release, only for dev
// .setIcon(RefreshIcon)
// .setSection("action")
// .onClick(() => {
// onUnloadSave(this.plugin);
// });
// });
// }
})
);
// this.registerEvent(
// this.app.workspace.on("editor-menu", (menu, editor, view) => {
// // const leafIsMarkdown = view instanceof MarkdownView;
// const leafIsKanban = view instanceof KanbanView;
// if (leafIsKanban) {
// console.log("MENU : If the fileIsFile ");
// // menu.addItem((item) => {
// // item.setTitle("Refresh Board")
// // .setIcon(RefreshIcon)
// // .setSection("pane")
// // .onClick(() => {
// // eventEmitter.emit("REFRESH_BOARD");
// // });
// // });
// }
// })
// );
}
async onFileModifiedAndLostFocus() {
if (this.editorModified && this.currentModifiedFile) {
await this.realTimeScanning.onFileChange(
this.currentModifiedFile,
this.settings.data.globalSettings.realTimeScanning,
this.settings.data.globalSettings.scanFilters
);
// Reset the editorModified flag after the scan.
this.editorModified = false;
}
}
}