generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
133 lines (110 loc) · 2.74 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
import { Plugin, WorkspaceLeaf } from "obsidian";
import ukrainianStem from "stemmers/ukrainianStem";
import { SidebarView } from "components/sidebar";
import getBacklinks from "helpers/getBacklinks";
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: "default",
};
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
sidebarActive: boolean;
async getWordCaseBacklinks(): Promise<
() => {
[key: string]: [number, string, number, number][];
}
> {
const { vault } = this.app;
// Get current file name
const fileName = this.app.workspace.getActiveFile();
if (fileName?.basename === undefined) {
return () => {
return {};
};
}
const currentFileName: string[] = ukrainianStem(fileName.basename);
const fileContents: string[] = await Promise.all(
vault.getMarkdownFiles().map((file) => vault.cachedRead(file)),
);
return () => {
return getBacklinks(fileContents, currentFileName, ukrainianStem);
};
}
async activateView() {
this.sidebarActive = true;
this.app.workspace.detachLeavesOfType("word-case-backlink");
await this.app.workspace.getRightLeaf(false).setViewState({
type: "word-case-backlink",
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType("word-case-backlink")[0],
);
}
async updateView() {
if (!this.sidebarActive) return;
this.app.workspace
.getLeavesOfType("word-case-backlink")
.forEach((leaf) =>
leaf.setViewState({
type: "word-case-backlink",
state: {
updateName: true,
},
}),
);
}
async updateBacklinks() {
if (!this.sidebarActive) return;
const backlinks = await this.getWordCaseBacklinks();
this.app.workspace
.getLeavesOfType("word-case-backlink")
.forEach((leaf) =>
leaf.setViewState({
type: "word-case-backlink",
state: {
backlinks,
updateName: true,
},
}),
);
}
async onload() {
this.sidebarActive = false;
await this.loadSettings();
this.registerView(
"word-case-backlink",
(leaf: WorkspaceLeaf) => new SidebarView(leaf, this),
);
this.addRibbonIcon("dice", "Word Case Backlink", async () => {
await this.activateView();
await this.updateBacklinks();
});
if (this.app.workspace.getActiveFile()) {
await this.updateView();
}
this.registerEvent(
this.app.workspace.on("file-open", async () => {
await this.updateView();
}),
);
this.registerEvent(
this.app.vault.on("rename", async (file) => {
await this.updateView();
}),
);
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}