-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
182 lines (160 loc) · 4.77 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { EditorSelection, StateField } from "@codemirror/state";
import { BlockList } from "net";
import { App, Editor, editorEditorField, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TAbstractFile } from "obsidian";
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
databaseName: string;
databaseLocation: string;
lastWord: string;
dictionary: string[];
}
const DEFAULT_SETTINGS: MyPluginSettings = {
databaseName: "default",
databaseLocation: "/",
lastWord: "",
dictionary: [],
};
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: "database-autolinker",
name: "Check for functionality",
callback: () => {
this.checkDatabaseFunctionality();
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerDomEvent(document, "keypress", (evt: KeyboardEvent) => {
// only check database when a whole word is written
if (evt.code == "Space") {
if (this.checkForDatabase()) {
if (this.buildDictionary()) {
this.replaceByLinkToDictionaryEntry();
}
}
} else {
}
});
}
onunload() {}
checkDatabaseFunctionality() {
if (this.checkForDatabase) {
new Notice("Database is found");
} else {
new Notice("ERROR while findind database");
}
if (this.checkForDatabase()) {
new Notice("Entrys are found");
} else {
new Notice("ERROR while finding entrys");
}
}
checkForDatabase(): boolean {
const files = this.app.vault.getMarkdownFiles();
console.log("database name:", this.settings.databaseName);
for (let i = 0; i < files.length; i++) {
let fileNameAfterPath = files[i].path;
if (fileNameAfterPath.substring(fileNameAfterPath.lastIndexOf("/") + 1) == this.settings.databaseName + ".md") {
console.log("location of database:", files[i].path);
this.settings.databaseLocation = files[i].path;
return true;
}
}
return false;
}
async buildDictionary() {
const { vault } = this.app;
const databaseLocation = vault.getAbstractFileByPath(this.settings.databaseLocation);
vault
.cachedRead(databaseLocation)
.then((result) => {
let dic: string[];
dic = result.match(/#.*$/gm);
for (let i in dic) {
dic[i] = dic[i].substring(2);
}
if (dic.length > 0) {
// Dictionary has more than one entry
this.settings.dictionary = dic;
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
// const statusBarItemEl = this.addStatusBarItem();
// statusBarItemEl.setText(`${dic.length} dictionary entrys`);
return true;
} else {
// Dictionary is empty
return false;
}
})
.catch((err) => {
console.log(err);
});
}
replaceByLinkToDictionaryEntry() {
let pulledDictionary = this.settings.dictionary;
console.log("Dictionary:", pulledDictionary);
let spacePos = { line: -1, ch: -1 };
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const cursor = view.editor.getCursor();
spacePos.line = cursor.line;
spacePos.ch = cursor.ch;
view.editor.setSelection({ line: spacePos.line, ch: 0 }, spacePos);
let lWord: string;
lWord = view.editor.getSelection().split(" ").splice(-1)[0];
console.log("last word:", lWord, "cursor pos:", spacePos);
let test = pulledDictionary.findIndex((elem) => {
if (lWord.includes(elem)) {
return true;
}
});
if (test !== -1) {
// Word is in dictionary
view.editor.setSelection(
{
line: spacePos.line,
ch: spacePos.ch - lWord.length,
},
spacePos
);
view.editor.replaceSelection(`[[${this.settings.databaseName}#${pulledDictionary[test]}|${lWord}]]`);
} else {
console.log("no word to replace");
view.editor.setCursor(spacePos);
}
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Database Linker Settings" });
new Setting(containerEl)
.setName("Database Path")
.setDesc("Example: 'database - everything'")
.addText((text) =>
text
.setPlaceholder("Enter your database name")
.setValue(this.plugin.settings.databaseName)
.onChange(async (value) => {
console.log("Databasepath: " + value);
this.plugin.settings.databaseName = value;
await this.plugin.saveSettings();
})
);
}
}