generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helpers.ts
66 lines (58 loc) · 1.56 KB
/
Helpers.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
import {
App,
Notice,
PaneType,
SplitDirection,
TFile,
TFolder,
WorkspaceLeaf,
} from "obsidian";
import { suggest } from "./Suggester";
export class Helpers {
constructor(private app: App) {}
suggest<T>(
itemLabels: string[] | ((item: T) => string),
items: T[],
placeholder?: string,
limit?: number
) {
return suggest(this.app, itemLabels, items, placeholder || "", limit);
}
notify(message: string, time: number = 5000) {
new Notice(message, time);
}
getAllFolders() {
return this.app.vault
.getAllLoadedFiles()
.filter((f) => f instanceof TFolder)
.map((folder) => folder.path);
}
async openFile(
file: TFile,
params: {
paneType?: PaneType;
openInNewTab?: boolean;
direction?: SplitDirection;
mode?: "source" | "preview" | "default";
focus?: boolean;
} = {}
) {
let leaf: WorkspaceLeaf;
if (params.paneType === "split") {
leaf = this.app.workspace.getLeaf(
params.paneType,
params.direction
);
} else {
leaf = this.app.workspace.getLeaf(params.paneType);
}
if (params.mode) {
await leaf.openFile(file, { state: { mode: params.mode } });
} else {
await leaf.openFile(file);
}
if (params.focus) {
this.app.workspace.setActiveLeaf(leaf, { focus: true });
}
}
}