Skip to content

Commit 89860b8

Browse files
committed
Utility changed to use app reference from QuickAdd. Added event action for startup macros.
obsidianmd/obsidian-releases#333 (comment)
1 parent e419724 commit 89860b8

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class QuickAdd extends Plugin {
7373

7474
this.addSettingTab(new QuickAddSettingsTab(this.app, this));
7575

76-
await new StartupMacroEngine(this.app, this.settings.macros).run();
76+
this.app.workspace.on('layout-ready', () => new StartupMacroEngine(this.app, this.settings.macros).run());
7777
this.addCommandsForChoices(this.settings.choices);
7878
}
7979

@@ -110,7 +110,7 @@ export default class QuickAdd extends Plugin {
110110
}
111111

112112
public removeCommandForChoice(choice: IChoice) {
113-
deleteObsidianCommand(`quickadd:choice:${choice.id}`);
113+
deleteObsidianCommand(this.app, `quickadd:choice:${choice.id}`);
114114
}
115115
}
116116

src/utility.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {App} from "obsidian";
2-
import {Notice} from "obsidian";
2+
import {MarkdownView, Notice} from "obsidian";
33
import {log} from "./logger/logManager";
44

55
export function getTemplater(app: App) {
@@ -27,23 +27,28 @@ export function getDate(input?: {format?: string, offset?: number|string}) {
2727

2828
export function appendToCurrentLine(toAppend: string, app: App) {
2929
try {
30-
// @ts-ignore
31-
const editor = app.workspace.activeLeaf.view.editor;
32-
const selected = editor.getSelection();
30+
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
31+
32+
if (!activeView) {
33+
log.logError(`unable to append '${toAppend}' to current line.`);
34+
return;
35+
}
36+
37+
const selected = activeView.editor.getSelection();
3338

34-
editor.replaceSelection(`${selected}${toAppend}`);
39+
activeView.editor.replaceSelection(`${selected}${toAppend}`);
3540
} catch {
3641
log.logError(`unable to append '${toAppend}' to current line.`);
3742
}
3843
}
3944

40-
export function findObsidianCommand(commandId: string) {
45+
export function findObsidianCommand(app: App, commandId: string) {
4146
// @ts-ignore
4247
return app.commands.findCommand(commandId);
4348
}
4449

45-
export function deleteObsidianCommand(commandId: string) {
46-
if (findObsidianCommand(commandId)) {
50+
export function deleteObsidianCommand(app: App, commandId: string) {
51+
if (findObsidianCommand(app, commandId)) {
4752
// @ts-ignore
4853
delete app.commands.commands[commandId];
4954
// @ts-ignore

0 commit comments

Comments
 (0)