Skip to content

Commit c275987

Browse files
committed
Fixes for 2.0-beta.2
- properly unload UI and eventlisteners when plugin gets unloaded - Fix styling when theme background color is white - fix for Month metadata not refreshing when switching months
1 parent e86d31a commit c275987

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Locale, Moment } from "moment";
2-
import type { App, TFile } from "obsidian";
2+
import type { Plugin, TFile } from "obsidian";
33
import type { IGranularity } from "obsidian-daily-notes-interface";
44
import { SvelteComponentTyped } from "svelte";
55

@@ -78,7 +78,7 @@ export interface IDayMetadata
7878
IEvaluatedMetadata {}
7979

8080
export class Calendar extends SvelteComponentTyped<{
81-
app: App;
81+
plugin: Plugin;
8282
showWeekNums: boolean;
8383
localeData?: Locale;
8484
eventHandlers: CallableFunction[];

src/components/Calendar.svelte

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<svelte:options immutable />
22

33
<script lang="ts">
4-
import { App, debounce } from "obsidian";
4+
import { Plugin, debounce } from "obsidian";
55
import type { Locale, Moment } from "moment";
66
import { setContext } from "svelte";
77
import { writable } from "svelte/store";
@@ -22,7 +22,7 @@
2222
export let eventHandlers: CallableFunction[];
2323
2424
// External sources (All optional)
25-
export let app: App;
25+
export let plugin: Plugin;
2626
export let sources: ICalendarSource[] = [];
2727
export let getSourceSettings: (sourceId: string) => ISourceSettings;
2828
export let selectedId: string;
@@ -31,7 +31,7 @@
3131
export let today: Moment = window.moment();
3232
export let displayedMonth: Moment = today;
3333
34-
setContext(IS_MOBILE, (window.app as any).isMobile);
34+
setContext(IS_MOBILE, (plugin.app as any).isMobile);
3535
3636
let displayedMonthStore = writable<Moment>(displayedMonth);
3737
setContext(DISPLAYED_MONTH, displayedMonthStore);
@@ -47,7 +47,7 @@
4747
$: month = getMonth($displayedMonthStore, localeData);
4848
$: daysOfWeek = getDaysOfWeek(today, localeData);
4949
50-
const fileCache = new PeriodicNotesCache(app, sources);
50+
const fileCache = new PeriodicNotesCache(plugin, sources);
5151
5252
function openPopover() {
5353
showPopover = true;

src/components/Month.svelte

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@
4444
const dispatch = createEventDispatcher();
4545
4646
let file: TFile | null;
47-
fileCache.store.subscribe(() => {
47+
function getMetadata() {
4848
file = fileCache.getFile($displayedMonth, "month");
4949
metadata = fileCache.getEvaluatedMetadata(
5050
"month",
5151
$displayedMonth,
5252
getSourceSettings
5353
);
54-
});
54+
}
55+
fileCache.store.subscribe(getMetadata);
56+
displayedMonth.subscribe(getMetadata);
5557
5658
function handleHover(event: PointerEvent, meta: IDayMetadata) {
5759
if (!appHasMonthlyNotesPluginLoaded()) {

src/components/popover/Box.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
background-color: var(--background-primary);
6262
border-radius: 4px;
6363
box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.25);
64-
color: white;
64+
color: var(--text-normal);
6565
display: flex;
6666
flex-direction: column;
6767
padding: 24px;

src/fileStore.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { App, TAbstractFile, TFile } from "obsidian";
21
import type { Moment } from "moment";
3-
import { get, Writable, writable } from "svelte/store";
2+
import { App, Plugin, TAbstractFile, TFile } from "obsidian";
43
import {
54
getAllDailyNotes,
65
getAllMonthlyNotes,
@@ -10,6 +9,7 @@ import {
109
getDateUID,
1110
IGranularity,
1211
} from "obsidian-daily-notes-interface";
12+
import { get, Writable, writable } from "svelte/store";
1313

1414
import type { ICalendarSource, IDayMetadata, ISourceSettings } from "./types";
1515

@@ -46,24 +46,28 @@ export default class PeriodicNotesCache {
4646
public store: Writable<Record<PeriodicNoteID, TFile>>;
4747
private sources: ICalendarSource[];
4848

49-
constructor(app: App, sources: ICalendarSource[]) {
50-
this.app = app;
49+
constructor(plugin: Plugin, sources: ICalendarSource[]) {
50+
this.app = plugin.app;
5151
this.sources = sources;
5252
this.store = writable<Record<PeriodicNoteID, TFile>>({});
5353

54-
// TODO register this to plugin
55-
app.workspace.onLayoutReady(() => {
56-
app.vault.on("create", this.onFileCreated.bind(this));
57-
app.vault.on("delete", this.onFileDeleted.bind(this));
58-
app.vault.on("rename", this.onFileRenamed.bind(this));
59-
app.vault.on("modify", this.onFileModified.bind(this));
54+
plugin.app.workspace.onLayoutReady(() => {
55+
const { vault } = this.app;
56+
plugin.registerEvent(vault.on("create", this.onFileCreated, this));
57+
plugin.registerEvent(vault.on("delete", this.onFileDeleted, this));
58+
plugin.registerEvent(vault.on("rename", this.onFileRenamed, this));
59+
plugin.registerEvent(vault.on("modify", this.onFileModified, this));
6060
this.initialize();
6161
});
6262

6363
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64-
const workspace = app.workspace as any;
65-
workspace.on("periodic-notes:settings-updated", this.initialize, this);
66-
workspace.on("calendar:metadata-updated", this.initialize, this);
64+
const workspace = this.app.workspace as any;
65+
plugin.registerEvent(
66+
workspace.on("periodic-notes:settings-updated", this.initialize, this)
67+
);
68+
plugin.registerEvent(
69+
workspace.on("calendar:metadata-updated", this.initialize, this)
70+
);
6771
}
6872

6973
public onFileCreated(file: TAbstractFile): void {

0 commit comments

Comments
 (0)