-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
163 lines (125 loc) · 3.93 KB
/
Copy pathmain.ts
File metadata and controls
163 lines (125 loc) · 3.93 KB
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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
// Remember to rename these classes and interfaces!
interface noteAssistSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: noteAssistSettings = {
mySetting: 'default'
}
export default class noteAssist extends Plugin {
settings: noteAssistSettings;
wordSet: Set<string> = new Set();
async onload() {
this.loadWords();
await this.loadSettings();
this.registerEvent(
this.app.workspace.on("editor-change", () => {
console.log("Editor change detected!");
const currView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (currView) {
console.log("Active markdown view detected");
this.processText(currView.editor);
} else {
console.log("No active markdown view");
}
})
);
this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}
async loadWords() {
try {
const pluginPath = `${this.app.vault.configDir}/plugins/${this.manifest.id}/assets/words.txt`;
const wordsFile = await this.app.vault.adapter.read(pluginPath);
this.wordSet = new Set(wordsFile.split("\n").map(word => word.trim().toLowerCase()));
console.log("Loaded words:", [...this.wordSet].slice(0, 10));
} catch (error) {
console.error("Error loading words file:", error);
}
}
getPluginFileUrl(filePath: string): string {
return `plugin://${this.manifest.id}/${filePath}`;
}
validWord(word: string): Boolean {
return this.wordSet.has(word.toLowerCase());
}
processText(editor: Editor) {
let cursor = editor.getCursor();
let content = editor.getValue();
var pairs: { [key: string]: string } = {"-":"ion", "_": "ing"};
for (let i = 0; i < content.length; i++) {
if (Object.keys(pairs).includes(content[i])) {
let trigger = content[i];
let replacement = pairs[trigger];
let triggerPos = i;
console.log("Trigger ", trigger, " found at:", triggerPos);
let index = triggerPos - 1;
let word = "";
while (index >= 0 && (content[index] != " " && content[index] != "\n")) {
word = content[index] + word;
index--;
}
if (word.trim().length === 0) {
console.log("No preceding text, skipping replacement");
continue;
}
word = word.trim() + replacement;
console.log("Generated word:", word);
if (this.validWord(word)) {
console.log("Valid word detected:", word);
let start = editor.offsetToPos(triggerPos);
let end = editor.offsetToPos(triggerPos + trigger.length);
editor.replaceRange(replacement, start, end);
editor.setCursor({
line: cursor.line,
ch: Math.min(cursor.ch + (replacement.length - trigger.length), editor.getLine(cursor.line).length)
});
} else { console.log("Invalid word detected:", word); }
}
}
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const { contentEl } = this;
contentEl.setText('Woah!');
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: noteAssist;
constructor(app: App, plugin: noteAssist) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
}
}