Skip to content

Commit e6fcf88

Browse files
committed
fix: Templater Commands not running on automatic Daily Note Creation on Obsidian 1.11.0+
refs: #1689
1 parent 80a4b3d commit e6fcf88

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/handlers/EventHandler.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,51 @@ import {
55
EventRef,
66
Menu,
77
MenuItem,
8+
moment,
9+
normalizePath,
810
TAbstractFile,
911
TFile,
1012
TFolder,
1113
} from "obsidian";
14+
import { get_active_file } from "utils/Utils";
1215

1316
export default class EventHandler {
1417
private trigger_on_file_creation_event: EventRef | undefined;
1518

1619
constructor(
1720
private plugin: TemplaterPlugin,
1821
private templater: Templater,
19-
private settings: Settings
22+
private settings: Settings,
2023
) {}
2124

2225
setup(): void {
23-
if (Array.isArray(this.plugin.app.workspace.onLayoutReadyCallbacks)) {
24-
// Use onLayoutReadyCallbacks instead of onLayoutReady
25-
// to ensure that the event is registered before core plugin events (e.g. daily notes autorun)
26-
this.plugin.app.workspace.onLayoutReadyCallbacks.unshift({
27-
pluginId: this.plugin.manifest.id,
28-
callback: () => {
29-
this.update_trigger_file_on_creation();
30-
},
31-
});
32-
} else {
33-
// Fallback to onLayoutReady if onLayoutReadyCallbacks is not available
34-
this.plugin.app.workspace.onLayoutReady(() => {
35-
this.update_trigger_file_on_creation();
36-
});
37-
}
26+
this.plugin.app.workspace.onLayoutReady(() => {
27+
if (this.settings.trigger_on_file_creation) {
28+
const open_behavior =
29+
this.plugin.app.vault.getConfig("openBehavior");
30+
if (open_behavior === "daily") {
31+
const daily_notes_plugin =
32+
this.plugin.app.internalPlugins.getEnabledPluginById(
33+
"daily-notes",
34+
);
35+
if (daily_notes_plugin) {
36+
const { folder, format } = daily_notes_plugin.options;
37+
const daily_note_path = normalizePath(
38+
`${folder}/${moment().format(format)}.md`,
39+
);
40+
const active_file = get_active_file(this.plugin.app);
41+
if (active_file?.path === daily_note_path) {
42+
Templater.on_file_creation(
43+
this.templater,
44+
this.plugin.app,
45+
active_file,
46+
);
47+
}
48+
}
49+
}
50+
}
51+
this.update_trigger_file_on_creation();
52+
});
3853
this.update_syntax_highlighting();
3954
this.update_file_menu();
4055
}
@@ -60,14 +75,14 @@ export default class EventHandler {
6075
Templater.on_file_creation(
6176
this.templater,
6277
this.plugin.app,
63-
file
64-
)
78+
file,
79+
),
6580
);
6681
this.plugin.registerEvent(this.trigger_on_file_creation_event);
6782
} else {
6883
if (this.trigger_on_file_creation_event) {
6984
this.plugin.app.vault.offref(
70-
this.trigger_on_file_creation_event
85+
this.trigger_on_file_creation_event,
7186
);
7287
this.trigger_on_file_creation_event = undefined;
7388
}
@@ -85,13 +100,13 @@ export default class EventHandler {
85100
.setIcon("templater-icon")
86101
.onClick(() => {
87102
this.plugin.fuzzy_suggester.create_new_note_from_template(
88-
file
103+
file,
89104
);
90105
});
91106
});
92107
}
93-
}
94-
)
108+
},
109+
),
95110
);
96111
}
97112
}

0 commit comments

Comments
 (0)